From 0169e8ed273e8a5949b62d6009fb71e5fd3c302e Mon Sep 17 00:00:00 2001 From: JGStew Date: Tue, 27 Aug 2024 14:43:58 -0400 Subject: [PATCH] update export_all_sites to use new plugin_utilities to simplify --- examples/export_all_sites.py | 124 ++++------------------------------- 1 file changed, 11 insertions(+), 113 deletions(-) diff --git a/examples/export_all_sites.py b/examples/export_all_sites.py index 9e37b1c..cd84b89 100644 --- a/examples/export_all_sites.py +++ b/examples/export_all_sites.py @@ -24,6 +24,7 @@ import sys import besapi +import besapi.plugin_utilities __version__ = "0.0.1" verbose = 0 @@ -31,64 +32,15 @@ invoke_folder = None -def get_invoke_folder(): - """Get the folder the script was invoked from - - References: - - https://github.com/jgstew/tools/blob/master/Python/locate_self.py - """ - # using logging here won't actually log it to the file: - - if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): - if verbose: - print("running in a PyInstaller bundle") - invoke_folder = os.path.abspath(os.path.dirname(sys.executable)) - else: - if verbose: - print("running in a normal Python process") - invoke_folder = os.path.abspath(os.path.dirname(__file__)) - - if verbose: - print(f"invoke_folder = {invoke_folder}") - - return invoke_folder - - -def test_file_exists(path): - """return true if file exists""" - - if not (os.path.isfile(path) and os.access(path, os.R_OK)): - path = os.path.join(invoke_folder, path) - - logging.info("testing if exists: `%s`", path) - - if os.path.isfile(path) and os.access(path, os.R_OK) and os.access(path, os.W_OK): - return path - - return False - - def main(): """Execution starts here""" print("main() start") - parser = argparse.ArgumentParser( - description="Provde command line arguments for REST URL, username, and password" - ) - parser.add_argument( - "-v", - "--verbose", - help="Set verbose output", - required=False, - action="count", - default=0, - ) - parser.add_argument( - "-besserver", "--besserver", help="Specify the BES URL", required=False - ) - parser.add_argument("-r", "--rest-url", help="Specify the REST URL", required=False) - parser.add_argument("-u", "--user", help="Specify the username", required=False) - parser.add_argument("-p", "--password", help="Specify the password", required=False) + print("NOTE: this script requires besapi v3.3.3+") + + parser = besapi.plugin_utilities.setup_plugin_argparse() + + # add additonal arg specific to this script: parser.add_argument( "-d", "--delete", @@ -104,71 +56,17 @@ def main(): verbose = args.verbose # get folder the script was invoked from: - invoke_folder = get_invoke_folder() - - # set different log levels: - log_level = logging.INFO - if verbose: - log_level = logging.INFO - if verbose > 1: - log_level = logging.DEBUG - - # get path to put log file in: - log_filename = os.path.join(invoke_folder, "export_all_sites.log") - - print(f"Log File Path: {log_filename}") - - handlers = [ - logging.handlers.RotatingFileHandler( - log_filename, maxBytes=5 * 1024 * 1024, backupCount=1 - ) - ] - - # log output to console: - handlers.append(logging.StreamHandler()) - - # setup logging: - logging.basicConfig( - encoding="utf-8", - level=log_level, - format="%(asctime)s %(levelname)s:%(message)s", - handlers=handlers, - ) + invoke_folder = besapi.plugin_utilities.get_invoke_folder() + + besapi.plugin_utilities.setup_plugin_logging() + logging.info("----- Starting New Session ------") logging.debug("invoke folder: %s", invoke_folder) logging.debug("Python version: %s", platform.sys.version) logging.debug("BESAPI Module version: %s", besapi.besapi.__version__) logging.debug("this plugin's version: %s", __version__) - password = args.password - - if not password: - logging.warning("Password was not provided, provide REST API password.") - print("Password was not provided, provide REST API password.") - password = getpass.getpass() - - # process args, setup connection: - rest_url = args.rest_url - - # normalize url to https://HostOrIP:52311 - if rest_url and rest_url.endswith("/api"): - rest_url = rest_url.replace("/api", "") - - try: - bes_conn = besapi.besapi.BESConnection(args.user, password, rest_url) - # bes_conn.login() - except ( - AttributeError, - ConnectionRefusedError, - besapi.besapi.requests.exceptions.ConnectionError, - ): - try: - # print(args.besserver) - bes_conn = besapi.besapi.BESConnection(args.user, password, args.besserver) - # handle case where args.besserver is None - # AttributeError: 'NoneType' object has no attribute 'startswith' - except AttributeError: - bes_conn = besapi.besapi.get_bes_conn_using_config_file() + bes_conn = besapi.plugin_utilities.get_besapi_connection(args) export_folder = os.path.join(invoke_folder, "export")