From 87bb6302ff174c71c22f0a563d62297179167511 Mon Sep 17 00:00:00 2001 From: Federico Luzzi Date: Mon, 22 Apr 2024 18:17:11 -0300 Subject: [PATCH] Update hooks functionality --- README.md | 107 ++++++++++++++++++++++++++++++++---- connpy/__init__.py | 104 ++++++++++++++++++++++++++++++++--- connpy/_version.py | 2 +- connpy/ai.py | 10 ++++ connpy/api.py | 6 +- connpy/configfile.py | 18 +++++- connpy/connapp.py | 48 ++++++++-------- connpy/core.py | 17 +++++- connpy/core_plugins/sync.py | 103 ++++++++-------------------------- connpy/hooks.py | 59 +++++++++++++++++--- 10 files changed, 338 insertions(+), 136 deletions(-) diff --git a/README.md b/README.md index f89f7e8..a116247 100644 --- a/README.md +++ b/README.md @@ -111,17 +111,104 @@ options: - Pass statements ### Specific Class Requirements -- The plugin script must define at least two specific classes: +- The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture: 1. **Class `Parser`**: - - Must contain only one method: `__init__`. - - The `__init__` method must initialize at least two attributes: - - `self.parser`: An instance of `argparse.ArgumentParser`. - - `self.description`: A string containing the description of the parser. + - **Purpose**: Handles parsing of command-line arguments. + - **Requirements**: + - Must contain only one method: `__init__`. + - The `__init__` method must initialize at least two attributes: + - `self.parser`: An instance of `argparse.ArgumentParser`. + - `self.description`: A string containing the description of the parser. 2. **Class `Entrypoint`**: - - Must have an `__init__` method that accepts exactly three parameters besides `self`: - - `args`: Arguments passed to the plugin. - - The parser instance (typically `self.parser` from the `Parser` class). - - The Connapp instance to interact with the Connpy app. + - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application. + - **Requirements**: + - Must have an `__init__` method that accepts exactly three parameters besides `self`: + - `args`: Arguments passed to the plugin. + - The parser instance (typically `self.parser` from the `Parser` class). + - The Connapp instance to interact with the Connpy app. + 3. **Class `Preload`**: + - **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic. + - **Requirements**: + - Contains at least an `__init__` method that accepts parameter connapp besides `self`. + +### Class Dependencies and Combinations +- **Dependencies**: + - `Parser` and `Entrypoint` are interdependent and must both be present if one is included. + - `Preload` is independent and may exist alone or alongside the other classes. +- **Valid Combinations**: + - `Parser` and `Entrypoint` together. + - `Preload` alone. + - All three classes (`Parser`, `Entrypoint`, `Preload`). + +### Preload Modifications and Hooks + +In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs. + +#### Modifying Classes with `modify` +The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions. + +- **Usage**: Modify a class to include additional configurations or changes +- **Modify Method Signature**: + - `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance. +- **Modification Method Signature**: + - **Arguments**: + - `cls`: This function accepts a single argument, the class instance, which it then modifies. + - **Modifiable Classes**: + - `connapp.config` + - `connapp.node` + - `connapp.nodes` + - `connapp.ai` + - ```python + def modify_config(cls): + # Example modification: adding a new attribute or modifying an existing one + cls.new_attribute = 'New Value' + + class Preload: + def __init__(self, connapp): + # Applying modification to the config class instance + connapp.config.modify(modify_config) + ``` + +#### Implementing Hooks with `register_pre_hook` and `register_post_hook` +These methods allow you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities. + + - **Usage**: Register hooks to methods to execute additional logic before or after the main method execution. +- **Registration Methods Signature**: + - `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments. + - `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs. +- **Method Signatures for Pre-Hooks** + - `pre_hook_method(*args, **kwargs)` + - **Arguments**: + - `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method. + - **Return**: + - Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received. +- **Method Signatures for Post-Hooks**: + - `post_hook_method(*args, **kwargs)` + - **Arguments**: + - `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method. + - `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller. + - **Return**: + - Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result. + - ```python + def pre_processing_hook(*args, **kwargs): + print("Pre-processing logic here") + # Modify arguments or perform any checks + return args, kwargs # Return modified or unmodified args and kwargs + + def post_processing_hook(*args, **kwargs): + print("Post-processing logic here") + # Modify the result or perform any final logging or cleanup + return kwargs["result"] # Return the modified or unmodified result + + class Preload: + def __init__(self, connapp): + # Registering a pre-hook + connapp.ai.some_method.register_pre_hook(pre_processing_hook) + + # Registering a post-hook + connapp.node.another_method.register_post_hook(post_processing_hook) + ``` + ### Executable Block - The plugin script can include an executable block: @@ -131,7 +218,7 @@ options: ### Script Verification - The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards. - Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system. -- + ### Example Script For a practical example of how to write a compatible plugin script, please refer to the following example: diff --git a/connpy/__init__.py b/connpy/__init__.py index 13a0a46..2794fa3 100644 --- a/connpy/__init__.py +++ b/connpy/__init__.py @@ -91,17 +91,103 @@ options: - Pass statements ### Specific Class Requirements -- The plugin script must define at least two specific classes: +- The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture: 1. **Class `Parser`**: - - Must contain only one method: `__init__`. - - The `__init__` method must initialize at least two attributes: - - `self.parser`: An instance of `argparse.ArgumentParser`. - - `self.description`: A string containing the description of the parser. + - **Purpose**: Handles parsing of command-line arguments. + - **Requirements**: + - Must contain only one method: `__init__`. + - The `__init__` method must initialize at least two attributes: + - `self.parser`: An instance of `argparse.ArgumentParser`. + - `self.description`: A string containing the description of the parser. 2. **Class `Entrypoint`**: - - Must have an `__init__` method that accepts exactly three parameters besides `self`: - - `args`: Arguments passed to the plugin. - - The parser instance (typically `self.parser` from the `Parser` class). - - The Connapp instance to interact with the Connpy app. + - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application. + - **Requirements**: + - Must have an `__init__` method that accepts exactly three parameters besides `self`: + - `args`: Arguments passed to the plugin. + - The parser instance (typically `self.parser` from the `Parser` class). + - The Connapp instance to interact with the Connpy app. + 3. **Class `Preload`**: + - **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic. + - **Requirements**: + - Contains at least an `__init__` method that accepts parameter connapp besides `self`. + +### Class Dependencies and Combinations +- **Dependencies**: + - `Parser` and `Entrypoint` are interdependent and must both be present if one is included. + - `Preload` is independent and may exist alone or alongside the other classes. +- **Valid Combinations**: + - `Parser` and `Entrypoint` together. + - `Preload` alone. + - All three classes (`Parser`, `Entrypoint`, `Preload`). + +### Preload Modifications and Hooks + +In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs. + +#### Modifying Classes with `modify` +The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions. + +- **Usage**: Modify a class to include additional configurations or changes +- **Modify Method Signature**: + - `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance. +- **Modification Method Signature**: + - **Arguments**: + - `cls`: This function accepts a single argument, the class instance, which it then modifies. + - **Modifiable Classes**: + - `connapp.config` + - `connapp.node` + - `connapp.nodes` + - `connapp.ai` + - ```python + def modify_config(cls): + # Example modification: adding a new attribute or modifying an existing one + cls.new_attribute = 'New Value' + + class Preload: + def __init__(self, connapp): + # Applying modification to the config class instance + connapp.config.modify(modify_config) + ``` + +#### Implementing Hooks with `register_pre_hook` and `register_post_hook` +These methods allow you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities. + + - **Usage**: Register hooks to methods to execute additional logic before or after the main method execution. +- **Registration Methods Signature**: + - `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments. + - `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs. +- **Method Signatures for Pre-Hooks** + - `pre_hook_method(*args, **kwargs)` + - **Arguments**: + - `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method. + - **Return**: + - Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received. +- **Method Signatures for Post-Hooks**: + - `post_hook_method(*args, **kwargs)` + - **Arguments**: + - `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method. + - `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller. + - **Return**: + - Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result. + - ```python + def pre_processing_hook(*args, **kwargs): + print("Pre-processing logic here") + # Modify arguments or perform any checks + return args, kwargs # Return modified or unmodified args and kwargs + + def post_processing_hook(*args, **kwargs): + print("Post-processing logic here") + # Modify the result or perform any final logging or cleanup + return kwargs["result"] # Return the modified or unmodified result + + class Preload: + def __init__(self, connapp): + # Registering a pre-hook + connapp.ai.some_method.register_pre_hook(pre_processing_hook) + + # Registering a post-hook + connapp.node.another_method.register_post_hook(post_processing_hook) + ``` ### Executable Block - The plugin script can include an executable block: diff --git a/connpy/_version.py b/connpy/_version.py index 2879e2e..93666ae 100644 --- a/connpy/_version.py +++ b/connpy/_version.py @@ -1,2 +1,2 @@ -__version__ = "4.0.0b2" +__version__ = "4.0.0b5" diff --git a/connpy/ai.py b/connpy/ai.py index c981532..5ca55d3 100755 --- a/connpy/ai.py +++ b/connpy/ai.py @@ -6,7 +6,9 @@ import ast from textwrap import dedent from .core import nodes from copy import deepcopy +from .hooks import ClassHook,MethodHook +@ClassHook class ai: ''' This class generates a ai object. Containts all the information and methods to make requests to openAI chatGPT to run actions on the application. @@ -175,6 +177,7 @@ Categorize the user's request based on the operation they want to perform on the self.__prompt["confirmation_function"]["parameters"]["properties"]["response"]["type"] = "string" self.__prompt["confirmation_function"]["parameters"]["required"] = ["result"] + @MethodHook def process_string(self, s): if s.startswith('[') and s.endswith(']') and not (s.startswith("['") and s.endswith("']")) and not (s.startswith('["') and s.endswith('"]')): # Extract the content inside square brackets and split by comma @@ -185,6 +188,7 @@ Categorize the user's request based on the operation they want to perform on the s = '[' + new_content + ']' return s + @MethodHook def _retry_function(self, function, max_retries, backoff_num, *args): #Retry openai requests retries = 0 @@ -201,6 +205,7 @@ Categorize the user's request based on the operation they want to perform on the myfunction = False return myfunction + @MethodHook def _clean_command_response(self, raw_response, node_list): #Parse response for command request to openAI GPT. info_dict = {} @@ -218,6 +223,7 @@ Categorize the user's request based on the operation they want to perform on the info_dict["variables"][key] = newvalue return info_dict + @MethodHook def _get_commands(self, user_input, nodes): #Send the request for commands for each device to openAI GPT. output_list = [] @@ -257,6 +263,7 @@ Categorize the user's request based on the operation they want to perform on the output["response"] = self._clean_command_response(json_result, node_list) return output + @MethodHook def _get_filter(self, user_input, chat_history = None): #Send the request to identify the filter and other attributes from the user input to GPT. message = [] @@ -298,6 +305,7 @@ Categorize the user's request based on the operation they want to perform on the output["chat_history"] = chat_history return output + @MethodHook def _get_confirmation(self, user_input): #Send the request to identify if user is confirming or denying the task message = [] @@ -322,6 +330,7 @@ Categorize the user's request based on the operation they want to perform on the output["result"] = json_result["response"] return output + @MethodHook def confirm(self, user_input, max_retries=3, backoff_num=1): ''' Send the user input to openAI GPT and verify if response is afirmative or negative. @@ -347,6 +356,7 @@ Categorize the user's request based on the operation they want to perform on the output = f"{self.model} api is not responding right now, please try again later." return output + @MethodHook def ask(self, user_input, dryrun = False, chat_history = None, max_retries=3, backoff_num=1): ''' Send the user input to openAI GPT and parse the response to run an action in the application. diff --git a/connpy/api.py b/connpy/api.py index 9bcffbb..9ae7928 100755 --- a/connpy/api.py +++ b/connpy/api.py @@ -1,5 +1,5 @@ from flask import Flask, request, jsonify -from connpy import configfile, node, nodes +from connpy import configfile, node, nodes, hooks from connpy.ai import ai as myai from waitress import serve import os @@ -126,6 +126,7 @@ def run_commands(): return({"DataError": error}) return output +@hooks.MethodHook def stop_api(): # Read the process ID (pid) from the file try: @@ -152,14 +153,17 @@ def stop_api(): print(f"Server with process ID {pid} stopped.") return port +@hooks.MethodHook def debug_api(port=8048): app.custom_config = configfile() app.run(debug=True, port=port) +@hooks.MethodHook def start_server(port=8048): app.custom_config = configfile() serve(app, host='0.0.0.0', port=port) +@hooks.MethodHook def start_api(port=8048): if os.path.exists(PID_FILE1) or os.path.exists(PID_FILE2): print("Connpy server is already running.") diff --git a/connpy/configfile.py b/connpy/configfile.py index dac2078..76556ac 100755 --- a/connpy/configfile.py +++ b/connpy/configfile.py @@ -6,12 +6,13 @@ import re from Crypto.PublicKey import RSA from pathlib import Path from copy import deepcopy -from .hooks import ConfigHook +from .hooks import MethodHook, ClassHook #functions and classes +@ClassHook class configfile: ''' This class generates a configfile object. Containts a dictionary storing, config, nodes and profiles, normaly used by connection manager. @@ -107,7 +108,7 @@ class configfile: jsonconf.close() return jsondata - @ConfigHook + @MethodHook def _saveconfig(self, conf): #Save config file newconfig = {"config":{}, "connections": {}, "profiles": {}} @@ -131,6 +132,7 @@ class configfile: os.chmod(keyfile, 0o600) return key + @MethodHook def _explode_unique(self, unique): #Divide unique name into folder, subfolder and id uniques = unique.split("@") @@ -151,6 +153,7 @@ class configfile: return False return result + @MethodHook def getitem(self, unique, keys = None): ''' Get an node or a group of nodes from configfile which can be passed to node/nodes class @@ -206,6 +209,7 @@ class configfile: newnode.pop("type") return newnode + @MethodHook def getitems(self, uniques): ''' Get a group of nodes from configfile which can be passed to node/nodes class @@ -247,6 +251,7 @@ class configfile: return nodes + @MethodHook def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ): #Add connection from config if folder == '': @@ -257,6 +262,7 @@ class configfile: self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type} + @MethodHook def _connections_del(self,*, id, folder='', subfolder=''): #Delete connection from config if folder == '': @@ -266,6 +272,7 @@ class configfile: elif folder != '' and subfolder != '': del self.connections[folder][subfolder][id] + @MethodHook def _folder_add(self,*, folder, subfolder = ''): #Add Folder from config if subfolder == '': @@ -275,6 +282,7 @@ class configfile: if subfolder not in self.connections[folder]: self.connections[folder][subfolder] = {"type": "subfolder"} + @MethodHook def _folder_del(self,*, folder, subfolder=''): #Delete folder from config if subfolder == '': @@ -283,15 +291,18 @@ class configfile: del self.connections[folder][subfolder] + @MethodHook def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ): #Add profile from config self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost} + @MethodHook def _profiles_del(self,*, id ): #Delete profile from config del self.profiles[id] + @MethodHook def _getallnodes(self, filter = None): #get all nodes on configfile nodes = [] @@ -314,6 +325,7 @@ class configfile: raise ValueError("filter must be a string or a list of strings") return nodes + @MethodHook def _getallnodesfull(self, filter = None, extract = True): #get all nodes on configfile with all their attributes. nodes = {} @@ -353,6 +365,7 @@ class configfile: return nodes + @MethodHook def _getallfolders(self): #get all folders on configfile folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"] @@ -363,6 +376,7 @@ class configfile: folders.extend(subfolders) return folders + @MethodHook def _profileused(self, profile): #Check if profile is used before deleting it nodes = [] diff --git a/connpy/connapp.py b/connpy/connapp.py index 31cc8e0..f462ba6 100755 --- a/connpy/connapp.py +++ b/connpy/connapp.py @@ -45,9 +45,13 @@ class connapp: ''' self.node = node - self.connnodes = nodes + self.nodes = nodes + self.start_api = start_api + self.stop_api = stop_api + self.debug_api = debug_api + self.ai = ai self.config = config - self.nodes = self.config._getallnodes() + self.nodes_list = self.config._getallnodes() self.folders = self.config._getallfolders() self.profiles = list(self.config.profiles.keys()) self.case = self.config.config["case"] @@ -211,16 +215,16 @@ class connapp: def _connect(self, args): if args.data == None: - matches = self.nodes + matches = self.nodes_list if len(matches) == 0: print("There are no nodes created") print("try: conn --help") exit(9) else: if args.data.startswith("@"): - matches = list(filter(lambda k: args.data in k, self.nodes)) + matches = list(filter(lambda k: args.data in k, self.nodes_list)) else: - matches = list(filter(lambda k: k.startswith(args.data), self.nodes)) + matches = list(filter(lambda k: k.startswith(args.data), self.nodes_list)) if len(matches) == 0: print("{} not found".format(args.data)) exit(2) @@ -275,10 +279,10 @@ class connapp: elif args.data.startswith("@"): type = "folder" matches = list(filter(lambda k: k == args.data, self.folders)) - reversematches = list(filter(lambda k: "@" + k == args.data, self.nodes)) + reversematches = list(filter(lambda k: "@" + k == args.data, self.nodes_list)) else: type = "node" - matches = list(filter(lambda k: k == args.data, self.nodes)) + matches = list(filter(lambda k: k == args.data, self.nodes_list)) reversematches = list(filter(lambda k: k == "@" + args.data, self.folders)) if len(matches) > 0: print("{} already exist".format(matches[0])) @@ -326,7 +330,7 @@ class connapp: if args.data == None: print("Missing argument node") exit(3) - matches = list(filter(lambda k: k == args.data, self.nodes)) + matches = list(filter(lambda k: k == args.data, self.nodes_list)) if len(matches) == 0: print("{} not found".format(args.data)) exit(2) @@ -511,8 +515,8 @@ class connapp: if not self.case: args.data[0] = args.data[0].lower() args.data[1] = args.data[1].lower() - source = list(filter(lambda k: k == args.data[0], self.nodes)) - dest = list(filter(lambda k: k == args.data[1], self.nodes)) + source = list(filter(lambda k: k == args.data[0], self.nodes_list)) + dest = list(filter(lambda k: k == args.data[1], self.nodes_list)) if len(source) != 1: print("{} not found".format(args.data[0])) exit(2) @@ -550,7 +554,7 @@ class connapp: count = 0 for n in ids: unique = n + newnodes["location"] - matches = list(filter(lambda k: k == unique, self.nodes)) + matches = list(filter(lambda k: k == unique, self.nodes_list)) reversematches = list(filter(lambda k: k == "@" + unique, self.folders)) if len(matches) > 0: print("Node {} already exist, ignoring it".format(unique)) @@ -577,7 +581,7 @@ class connapp: newnode["password"] = newnodes["password"] count +=1 self.config._connections_add(**newnode) - self.nodes = self.config._getallnodes() + self.nodes_list = self.config._getallnodes() if count > 0: self.config._saveconfig(self.config.file) print("Succesfully added {} nodes".format(count)) @@ -829,7 +833,7 @@ class connapp: arguments["org"] = args.org[0] if args.api_key: arguments["api_key"] = args.api_key[0] - self.myai = ai(self.config, **arguments) + self.myai = self.ai(self.config, **arguments) if args.ask: input = " ".join(args.ask) request = self.myai.ask(input, dryrun = True) @@ -920,7 +924,7 @@ class connapp: if not confirmation: response = "Request cancelled" else: - nodes = self.connnodes(self.config.getitems(response["nodes"]), config = self.config) + nodes = self.nodes(self.config.getitems(response["nodes"]), config = self.config) if response["action"] == "run": output = nodes.run(**response["args"]) response = "" @@ -936,17 +940,17 @@ class connapp: def _func_api(self, args): if args.command == "stop" or args.command == "restart": - args.data = stop_api() + args.data = self.stop_api() if args.command == "start" or args.command == "restart": if args.data: - start_api(args.data) + self.start_api(args.data) else: - start_api() + self.start_api() if args.command == "debug": if args.data: - debug_api(args.data) + self.debug_api(args.data) else: - debug_api() + self.debug_api() return def _node_run(self, args): @@ -997,7 +1001,7 @@ class connapp: if len(nodes) == 0: print("{} don't match any node".format(nodelist)) exit(2) - nodes = self.connnodes(self.config.getitems(nodes), config = self.config) + nodes = self.nodes(self.config.getitems(nodes), config = self.config) stdout = False if output is None: pass @@ -1158,14 +1162,14 @@ class connapp: if current[1:] not in self.profiles: raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current)) elif current != "": - if current not in self.nodes : + if current not in self.nodes_list : raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current)) return True def _profile_jumphost_validation(self, answers, current): #Validation for Jumphost in inquirer when managing profiles if current != "": - if current not in self.nodes : + if current not in self.nodes_list : raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current)) return True diff --git a/connpy/core.py b/connpy/core.py index 62b77e6..24dbd3b 100755 --- a/connpy/core.py +++ b/connpy/core.py @@ -12,10 +12,11 @@ import sys import threading from pathlib import Path from copy import deepcopy +from .hooks import ClassHook, MethodHook import io #functions and classes - +@ClassHook class node: ''' This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet. @@ -139,6 +140,7 @@ class node: else: self.jumphost = "" + @MethodHook def _passtx(self, passwords, *, keyfile=None): # decrypts passwords, used by other methdos. dpass = [] @@ -161,6 +163,7 @@ class node: + @MethodHook def _logfile(self, logfile = None): # translate logs variables and generate logs path. if logfile == None: @@ -176,6 +179,7 @@ class node: logfile = re.sub(r'\$\{date (.*)}',now.strftime(dateconf.group(1)), logfile) return logfile + @MethodHook def _logclean(self, logfile, var = False): #Remove special ascii characters and other stuff from logfile. if var == False: @@ -202,6 +206,7 @@ class node: else: return t + @MethodHook def _savelog(self): '''Save the log buffer to the file at regular intervals if there are changes.''' t = threading.current_thread() @@ -217,11 +222,13 @@ class node: prev_size = current_size # Update the previous size sleep(5) + @MethodHook def _filter(self, a): #Set time for last input when using interact self.lastinput = time() return a + @MethodHook def _keepalive(self): #Send keepalive ctrl+e when idletime passed without new inputs on interact self.lastinput = time() @@ -233,6 +240,7 @@ class node: sleep(1) + @MethodHook def interact(self, debug = False): ''' Allow user to interact with the node directly, mostly used by connection manager. @@ -274,6 +282,7 @@ class node: print(connect) exit(1) + @MethodHook def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10): ''' Run a command or list of commands on the node and return the output. @@ -362,6 +371,7 @@ class node: f.close() return connect + @MethodHook def test(self, commands, expected, vars = None,*, prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10): ''' Run a command or list of commands on the node, then check if expected value appears on the output after the last command. @@ -457,6 +467,7 @@ class node: self.status = 1 return connect + @MethodHook def _connect(self, debug = False, timeout = 10, max_attempts = 3): # Method to connect to the node, it parse all the information, create the ssh/telnet command and login to the node. if self.protocol in ["ssh", "sftp"]: @@ -557,6 +568,7 @@ class node: self.child = child return True +@ClassHook class nodes: ''' This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously. @@ -608,12 +620,14 @@ class nodes: setattr(self,n,this) + @MethodHook def _splitlist(self, lst, n): #split a list in lists of n members. for i in range(0, len(lst), n): yield lst[i:i + n] + @MethodHook def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None): ''' Run a command or list of commands on all the nodes in nodelist. @@ -698,6 +712,7 @@ class nodes: self.status = status return output + @MethodHook def test(self, commands, expected, vars = None,*, prompt = None, parallel = 10, timeout = None): ''' Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command. diff --git a/connpy/core_plugins/sync.py b/connpy/core_plugins/sync.py index 060ff9d..c5c6535 100755 --- a/connpy/core_plugins/sync.py +++ b/connpy/core_plugins/sync.py @@ -6,6 +6,7 @@ import zipfile import tempfile import io import yaml +import threading from google.oauth2.credentials import Credentials from google.auth.transport.requests import Request from googleapiclient.discovery import build @@ -23,8 +24,9 @@ class sync: self.file = connapp.config.file self.key = connapp.config.key self.google_client = f"{os.path.dirname(os.path.abspath(__file__))}/sync_client" + self.connapp = connapp try: - self.sync = connapp.config.config["sync"] + self.sync = self.connapp.config.config["sync"] except: self.sync = False @@ -293,18 +295,29 @@ class sync: print(f"Backup from Google Drive restored successfully: {selected_file['name']}") return 0 - # @staticmethod - def config_listener_post(self, file, result): + def config_listener_post(self, args, kwargs): if self.sync: if self.check_login_status() == True: - if not result: + if not kwargs["result"]: self.compress_and_upload() - return result + return kwargs["result"] + + def config_listener_pre(self, *args, **kwargs): + try: + self.sync = self.connapp.config.config["sync"] + except: + self.sync = False + return args, kwargs + + def start_post_thread(self, *args, **kwargs): + post_thread = threading.Thread(target=self.config_listener_post, args=(args,kwargs)) + post_thread.start() class Preload: def __init__(self, connapp): syncapp = sync(connapp) - connapp.config._saveconfig.register_post_hook(syncapp.config_listener_post) + connapp.config._saveconfig.register_post_hook(syncapp.start_post_thread) + connapp.config._saveconfig.register_pre_hook(syncapp.config_listener_pre) class Parser: def __init__(self): @@ -324,8 +337,6 @@ class Parser: class Entrypoint: def __init__(self, args, parser, connapp): syncapp = sync(connapp) - # print(args) - # print(syncapp.__dict__) if args.command == 'login': syncapp.login() elif args.command == "status": @@ -342,81 +353,11 @@ class Entrypoint: syncapp.restore_last_config(args.id) elif args.command == "logout": syncapp.logout() - # if args.command == 'netmask': - # if args.file: - # for line in args.file: - # line = line.strip() - # if line: - # print(NetmaskTools.process_input(args.conversion, line.strip())) - # else: - # input_str = ' '.join(args.input) - # print(NetmaskTools.process_input(args.conversion, input_str)) - # elif args.command == 'summarize': - # with args.file as file: - # subnets = [line.strip() for line in file if line.strip()] - # summarized = Sumarize.summarize_subnets(subnets, args.mode) - # if isinstance(summarized, list): - # for subnet in summarized: - # print(subnet) - # else: - # print(summarized) - # elif args.command == 'password': - # if connapp: - # for passwd in Password.get_passwords(args, connapp): - # print(passwd) - # elif args.command == 'connect': - # Connect.connect_command(args) - # else: - # parser.print_help() - def _connpy_completion(wordsnumber, words, info = None): if wordsnumber == 3: - result = ["--help", "netmask", "summarize", "password", "connect"] + result = ["--help", "login", "status", "start", "stop", "list", "once", "restore", "logout"] #NETMASK_completion - if wordsnumber == 4 and words[1] == "netmask": - result = ['cidr_to_netmask', 'cidr_to_wildcard', - 'netmask_to_cidr', 'wildcard_to_cidr', - 'netmask_to_wildcard', 'wildcard_to_netmask', 'cidr_to_range', "--file", "--help"] - elif wordsnumber == 6 and words[1] == "netmask" and words[2] in ["-f", "--file"]: - result = ['cidr_to_netmask', 'cidr_to_wildcard', - 'netmask_to_cidr', 'wildcard_to_cidr', - 'netmask_to_wildcard', 'wildcard_to_netmask'] - elif wordsnumber == 5 and words[1] == "netmask" and words[2] in ["-f", "--file"]: - result = _getcwd(words, words[2]) - elif wordsnumber == 6 and words[1] == "netmask" and words[3] in ["-f", "--file"]: - result = _getcwd(words, words[2]) - #SUMMARIZE_completion - elif wordsnumber == 4 and words[1] == "summarize": - result = _getcwd(words, words[1]) - result.extend(["--mode", "--help"]) - elif wordsnumber == 5 and words[1] == "summarize": - if words[2] == "--mode": - result = ["strict", "inclusive"] - else: - result = ["--mode"] - elif wordsnumber == 6 and words[1] == "summarize": - if words[3] == "--mode": - result = ["strict", "inclusive"] - elif words[3] in ["strict", "inclusive"]: - result = _getcwd(words, words[3]) - #PASSWORD_completion - elif wordsnumber == 4 and words[1] == "password": - result = info["nodes"] - result.extend(info["profiles"]) - result.extend(["--help", "--profile"]) - elif wordsnumber == 5 and words[1] == "password": - if words[2] == "--profile": - result = info["profiles"] - else: - result = ["--profile"] - #CONNECT_completion - elif wordsnumber == 4 and words[1] == "connect": - result = ["start", "stop", "restart", "--help"] - + if wordsnumber == 4 and words[1] == "restore": + result = ["--help", "--id"] return result - -if __name__ == "__main__": - parser = Parser() - args = parser.parser.parse_args() - Entrypoint(args, parser.parser, None) diff --git a/connpy/hooks.py b/connpy/hooks.py index 35526e0..056edee 100755 --- a/connpy/hooks.py +++ b/connpy/hooks.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 #Imports -from functools import wraps +from functools import wraps, partial #functions and classes -class ConfigHook: - """Decorator class to enable Config save hooking""" +class MethodHook: + """Decorator class to enable Methods hooking""" def __init__(self, func): self.func = func @@ -19,11 +19,10 @@ class ConfigHook: try: args, kwargs = hook(*args, **kwargs) except Exception as e: - print(f"ConfigHook Pre-hook raised an exception: {e}") + print(f"{self.func.__name__} Pre-hook {hook.__name__} raised an exception: {e}") try: - # Execute original function - result = self.func(self.instance, *args, **kwargs) + result = self.func(*args, **kwargs) finally: # Execute post-hooks after the original function @@ -31,13 +30,22 @@ class ConfigHook: try: result = hook(*args, **kwargs, result=result) # Pass result to hooks except Exception as e: - print(f"ConfigHook Post-hook raised an exception: {e}") + print(f"{self.func.__name__} Post-hook {hook.__name__} raised an exception: {e}") return result def __get__(self, instance, owner): - self.instance = instance - return self + if not instance: + return self + else: + return self.make_callable(instance) + + def make_callable(self, instance): + # Returns a callable that also allows access to hook registration methods + callable_instance = partial(self.__call__, instance) + callable_instance.register_pre_hook = self.register_pre_hook + callable_instance.register_post_hook = self.register_post_hook + return callable_instance def register_pre_hook(self, hook): """Register a function to be called before the original function""" @@ -46,3 +54,36 @@ class ConfigHook: def register_post_hook(self, hook): """Register a function to be called after the original function""" self.post_hooks.append(hook) + +class ClassHook: + """Decorator class to enable Class Modifying""" + def __init__(self, cls): + self.cls = cls + # Initialize deferred class hooks if they don't already exist + if not hasattr(cls, 'deferred_class_hooks'): + cls.deferred_class_hooks = [] + + def __call__(self, *args, **kwargs): + instance = self.cls(*args, **kwargs) + # Attach instance-specific modify method + instance.modify = self.make_instance_modify(instance) + # Apply any deferred modifications + for hook in self.cls.deferred_class_hooks: + hook(instance) + return instance + + def __getattr__(self, name): + """Delegate attribute access to the class.""" + return getattr(self.cls, name) + + def modify(self, modification_func): + """Queue a modification to be applied to all future instances of the class.""" + self.cls.deferred_class_hooks.append(modification_func) + + def make_instance_modify(self, instance): + """Create a modify function that is bound to a specific instance.""" + def modify_instance(modification_func): + """Modify this specific instance.""" + modification_func(instance) + return modify_instance +