From b306f643e87b95708a09bd97b3b3df5d20a59dca Mon Sep 17 00:00:00 2001 From: Andreas Legner Date: Wed, 15 Apr 2026 06:40:32 +0200 Subject: [PATCH 1/3] Write better Examples --- Examples/example_library.py | 124 +++++++++++++++++++++++ Examples/example_user.py | 49 +++++++-- app/iLibrary/src/iLibrary/Usr/sendMSG.py | 2 +- test.py | 21 ---- 4 files changed, 168 insertions(+), 28 deletions(-) create mode 100644 Examples/example_library.py diff --git a/Examples/example_library.py b/Examples/example_library.py new file mode 100644 index 0000000..67cbf7f --- /dev/null +++ b/Examples/example_library.py @@ -0,0 +1,124 @@ +import json +from os.path import join +import os +from dotenv import load_dotenv +from iLibrary import Library + +#load ENV file and get the Connection Settings +dotenv_path = join('..', '.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") + + + + +# ---------------------------------------------------- +# make a Savefile from a library +# ---------------------------------------------------- +def getSaveFile(): + + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + + PORT = 22 #for the Pub400.com use the Port 2222 + LIBRARY = '' + SAVE_FILE_NAME = 'FOO' + LOCAL_PATH = 'YOUR_LOCAL_PATH' + DESCRIPTION = 'Saved from iLibrary' + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to save the Library + # The result is returned as a JSON string + raw_result = l.saveLibrary( + library=LIBRARY, + saveFileName=SAVE_FILE_NAME, + description=DESCRIPTION, + localPath=LOCAL_PATH, + port=PORT + ) + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + + + +# ---------------------------------------------------- +# Get library information about all libraries from +# IBM i Server using iLibrary +# ---------------------------------------------------- +def getAllLibraries(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to get all libraries on the system + # The result is returned as a JSON string + raw_result = l.getAllLibraries() + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + + + + +# ---------------------------------------------------- +# Get single library information from IBM i Server +# using iLibrary +# ---------------------------------------------------- +def getSingleLibraryInfo(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + LIBRARY = '' + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to show singe information about the Library + # The result is returned as a JSON string + raw_result = l.getLibraryInfo( + library=LIBRARY + ) + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + +if __name__ == '__main__': + getSaveFile() + getAllLibraries() + getSingleLibraryInfo() \ No newline at end of file diff --git a/Examples/example_user.py b/Examples/example_user.py index c2a6334..d1de08f 100644 --- a/Examples/example_user.py +++ b/Examples/example_user.py @@ -12,14 +12,12 @@ DB_PASSWORD = os.environ.get("DB_PASSWORD") DB_SYSTEM = os.environ.get("DB_SYSTEM") -if __name__ == "__main__": - # ---------------------------------------------------- # Get all Users information from IBM i Server using # iLibrary # ---------------------------------------------------- - +def getAllUsers(): # Flag to enable/disable Mapepire connection mode USE_MAPEPIRE = False @@ -43,11 +41,13 @@ # Print the error message for debugging print(e) + + # ---------------------------------------------------- # Get single Users information from IBM i Server # using iLibrary # ---------------------------------------------------- - +def getSingleUser(): # Flag to enable/disable Mapepire connection mode USE_MAPEPIRE = False @@ -77,6 +77,43 @@ # Output the error message for debugging purposes print(e) +# ---------------------------------------------------- +# Send a message to a IBM i User using iLibrary +# ---------------------------------------------------- +def sendMessage(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + # Username to search for in the IBM i system + USERNAME_TO_SEARCH = '' + MESSAGE_TO_SEND = 'From iLibrary' + + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures proper connection handling (open/close) + with User(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as u: + + # Retrieve detailed information for a single user + # - username: the user profile to look up + # Returns data as a JSON string + raw_result = u.send_message_to_user( + username=USERNAME_TO_SEARCH, + message=MESSAGE_TO_SEND, + ) + + # Convert the JSON string into a Python object (dict or list) + data = json.loads(raw_result) -#TODO --------------------------------------------- -#TODO Write the SendMessage Function to here \ No newline at end of file + # Pretty-print the result for better readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during execution + except Exception as e: + # Output the error message for debugging purposes + print(e) + + +if __name__ == "__main__": + getAllUsers() + getSingleUser() + sendMessage() \ No newline at end of file diff --git a/app/iLibrary/src/iLibrary/Usr/sendMSG.py b/app/iLibrary/src/iLibrary/Usr/sendMSG.py index ce44b6f..bf55ad7 100644 --- a/app/iLibrary/src/iLibrary/Usr/sendMSG.py +++ b/app/iLibrary/src/iLibrary/Usr/sendMSG.py @@ -22,7 +22,7 @@ def send_message_to_user( # msgtype: str = None, # rpymsgq: str = None, ccsid: int = None - ): + ) -> dict[str, str]: """ Sends a message to a specified user on the system. This method interacts with the system to send a message by executing an SQL query. It validates the required diff --git a/test.py b/test.py index 4e550b7..fc87a82 100644 --- a/test.py +++ b/test.py @@ -15,24 +15,3 @@ if __name__ == "__main__": - try: - #try to get a connection to the Servcer - with IFS(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=False) as i: - - path = '/home/ALBEER/' - raw_result = i.readIFS(path_to_read=path, subtrees=False) - data = json.loads(raw_result) - print(json.dumps(data, indent=2)) - except Exception as e: - print(f"An error occurred: {e}") - - # - # try: - # with User(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=False) as i: - # path = '/a' - # data = i.getSingleUserInformation('Test') - # # print(data) - # - # print(data) - # except Exception as e: - # print(f"An error occurred: {e}") \ No newline at end of file From e9086e517c963458f7c49d03d4ff615777d8c69c Mon Sep 17 00:00:00 2001 From: Andreas Legner Date: Wed, 15 Apr 2026 06:43:39 +0200 Subject: [PATCH 2/3] Fixes --- test.py => dev_test.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test.py => dev_test.py (100%) diff --git a/test.py b/dev_test.py similarity index 100% rename from test.py rename to dev_test.py From 0baab20b7c7d7b3047837f7753cb4dc5c02485a3 Mon Sep 17 00:00:00 2001 From: Andreas Legner Date: Wed, 15 Apr 2026 06:45:04 +0200 Subject: [PATCH 3/3] Fixes --- dev_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dev_test.py b/dev_test.py index fc87a82..00b3de5 100644 --- a/dev_test.py +++ b/dev_test.py @@ -15,3 +15,4 @@ if __name__ == "__main__": + print('Nothing to do') \ No newline at end of file