Skip to main content

iLibrary - Tools for IBM i

Project description

iLibrary

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

Modules required


  • json
  • os
  • paramiko
  • pyodbc
  • time
  • zipfile

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('ALBEER1')  
            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.1.tar.gz (8.2 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.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ilibrary-0.0.1.tar.gz
  • Upload date:
  • Size: 8.2 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.1.tar.gz
Algorithm Hash digest
SHA256 8f2c38a512e36a600b32aff9c45d8e80223c94abace6abb390c50f22258a21b7
MD5 fba329fbf3359462f95199a4479bc43a
BLAKE2b-256 c0ed0983d2bc4767f08de955f31aa0009b9830211eceec8e861c3ca81277ec14

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ilibrary-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 46883c232582aae6b4d625c1ececf4be45a00c7080b90eb80337a3b129554891
MD5 d1e6e8118a1fc98473dd738d399e98d8
BLAKE2b-256 aaf60c4f0c57e5125fc1b1ccf19af58eaf8bacdf399c13edde4af9b9185df7a0

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