Skip to main content

iLibrary - Tools for IBM i library

Project description

iLibrary PyPI Downloads

This class provides methods to connect to the system via pyodbc for SQL and paramiko for SFTP transfers.

Modules required


  • json
  • os
  • paramiko
  • pyodbc

Class Library


A class to manage libraries and files on an IBM i system. It provides methods to connect to the system via pyodbc for SQL and paramiko for SFTP transfers.

Methods


enter(self) -> 'Library'


Establishes the database connection when entering a with block.

exit(self, exc_type, exc_val, exc_tb)


Closes the database connection when exiting a with block. This method is called automatically, even if an error occurred.

init(self, db_user: str, db_password: str, db_host: str, db_driver: str)


Initializes the class attributes for a database connection. The actual connection is established in the __enter__ method.

  • Args:

    • db_user (str): The user ID for the database connection.
    • db_password (str): The password for the database user.
    • db_host (str): The system/host name for the database connection.
    • db_driver (str): The ODBC driver to be used.

getInfoForLibrary()


Retrieves information about a specific library.

Args:

  • library (str): The name of the library to retrieve information about.

  • wantJson (bool, optional): If set to True, the function returns a JSON-formatted string. If False, it returns a Python object. Defaults to True.
    Returns:

  • str: A JSON string if wantJson is True.

  • obj: A Python object if wantJson is False.

Example:

from os.path import join, dirname  
import os  
from dotenv import load_dotenv  
import iLibrary  
  
#load ENV file and get the Connection Settings  
dotenv_path = join(dirname(__file__), '.env')  
load_dotenv(dotenv_path)  
DB_DRIVER = os.environ.get("DB_DRIVER")  
DB_USER = os.environ.get("DB_USER")  
DB_PASSWORD = os.environ.get("DB_PASSWORD")  
DB_SYSTEM = os.environ.get("DB_SYSTEM")  
  
  
if __name__ == "__main__":  
    try:  
        with iLibrary.Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER) as lib:  
            #try to get the SAVF File from the IBM i Server  
            result = lib.getInfoForLibrary('LIB_NAME')  
            print(f"Query result: {result}")  
  
    except Exception as e:  
        print(f"An error occurred in the main block: {e}")

removeFile(self)


Remove a saved file from the library on the AS400. Args

  • library: The name of the library where the save file will be created.
  • saveFileName: The name of the save file to be created. Returns
  • Boolean: True if the file was removed successfully, False otherwise.
if __name__ == "__main__":  
    try:  
        with iLibrary.Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER) as lib:  
            result = lib.removeSaveFile(library='LIB_NAME', saveFileName='SAVE_FILE_NAME') 
            print(f"Query result: {result}")  
  
    except Exception as e:  
        print(f"An error occurred in the main block: {e}")

iclose(self)


A helper method to close the connection, also useful for manual closure.

saveLibrary()


Saves a complete library from the IBM i to a save file.

This method creates a save file on the IBM i and then uses the SAVLIB (Save Library) CL command to save the specified library's contents into it. Optionally, it can download the resulting save file to the local machine as a ZIP file.

Args:

  • library (str): The name of the library to be saved.

  • saveFileName (str): The name of the save file that will be created to hold the library.

  • description (str, optional): A text description for the save file. Defaults to None.

  • localPath (str, optional): The local file path where the downloaded save file will be stored. Required if getZip is True. Defaults to None.

  • remPath (str, optional): The remote file path on the IBM i's IFS where the save file will be temporarily stored before downloading. Required if getZip is True. Defaults to None.

  • getZip (bool, optional): If True, the save file will be downloaded to the local machine and then deleted from the remote IFS. Defaults to False.

  • port (int, optional): The port for the SSH connection. Defaults to 22.

Returns:

  • bool: True if the library was saved successfully (and downloaded if requested), False otherwise.
    Example
from os.path import join, dirname  
import os  
from dotenv import load_dotenv  
import iLibrary  
  
#load ENV file and get the Connection Settings  
dotenv_path = join(dirname(__file__), '.env')  
load_dotenv(dotenv_path)  
DB_DRIVER = os.environ.get("DB_DRIVER")  
DB_USER = os.environ.get("DB_USER")  
DB_PASSWORD = os.environ.get("DB_PASSWORD")  
DB_SYSTEM = os.environ.get("DB_SYSTEM")  
  
  
if __name__ == "__main__":  
    try:  
        with iLibrary.Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER) as lib:  
            #try to get the SAVF File from the IBM i Server  
            result = lib.saveLibrary(library='YOUR_LIB',   saveFileName='SAVEFILE_NAME', getZip=True, localPath=join(dirname(__file__)), remPath='/home/USERNAME/')  
            print(f"Query result: {result}")  
  
    except Exception as e:  
        print(f"An error occurred in the main block: {e}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ilibrary-0.0.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ilibrary-0.0.4-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file ilibrary-0.0.4.tar.gz.

File metadata

  • Download URL: ilibrary-0.0.4.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for ilibrary-0.0.4.tar.gz
Algorithm Hash digest
SHA256 4249071cc55c59312bbf57caccfdb240ffbf1be663db8beecfd6f5a583eb69be
MD5 a3d85103f9de6248cdb1db6c091c8c71
BLAKE2b-256 1a621b038403721a6309bb122667ea2c738fccd5e7237094fa519afb4b852b9d

See more details on using hashes here.

File details

Details for the file ilibrary-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ilibrary-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for ilibrary-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f3dbe7131a896db62922918fd3a3f51ea587282ca3145c5d477e246cb6fee5da
MD5 17502fc4df45dd0502566e700fa99913
BLAKE2b-256 34f7d0a35337f7ae94a3cdd2ddb68c96b1730985c8807fd951190f1914fa5e2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page