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

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}")

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.2.tar.gz (7.9 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.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ilibrary-0.0.2.tar.gz
  • Upload date:
  • Size: 7.9 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.2.tar.gz
Algorithm Hash digest
SHA256 2ab690d504e4176d77918997bdf47a553aff8bbd66360755b614a63681ec08d8
MD5 e3a16f31e4c092c36e799b1522b791b8
BLAKE2b-256 d3e40d9cfb41f5ca2f0534ceb6a1bfd1c4dafae9c850b872f1957cc973d6b9da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ilibrary-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1697f7ae8a35fe777930a9d4e7188c6cde97a0677c1a1a318df00fcd5cb6e7a0
MD5 de3229497f210c2767c13ba0a3da77a1
BLAKE2b-256 808905e75fbaf330364aff5ae20e33712db145df62e29d2003faf59b6bd85c10

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