From 91ea1b8e9c4bebfc08066e3ade3a2cb1b030e913 Mon Sep 17 00:00:00 2001 From: JGStew Date: Thu, 29 Aug 2024 15:42:32 -0400 Subject: [PATCH] fix path for log file --- examples/export_all_sites.py | 46 ++++++++++++++++++++++++++++++++-- src/besapi/plugin_utilities.py | 16 ++++++------ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/examples/export_all_sites.py b/examples/export_all_sites.py index cd84b89..2008202 100644 --- a/examples/export_all_sites.py +++ b/examples/export_all_sites.py @@ -19,6 +19,7 @@ import logging import logging.handlers import os +import ntpath import platform import shutil import sys @@ -32,6 +33,45 @@ invoke_folder = None +def get_invoke_folder(verbose=0): + """Get the folder the script was invoked from""" + # 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 get_invoke_file_name(verbose=0): + """Get the filename the script was invoked from""" + # 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_file_path = sys.executable + else: + if verbose: + print("running in a normal Python process") + invoke_file_path = __file__ + + if verbose: + print(f"invoke_file_path = {invoke_file_path}") + + # get just the file name, return without file extension: + return os.path.splitext(ntpath.basename(invoke_file_path))[0] + + def main(): """Execution starts here""" print("main() start") @@ -56,9 +96,11 @@ def main(): verbose = args.verbose # get folder the script was invoked from: - invoke_folder = besapi.plugin_utilities.get_invoke_folder() + invoke_folder = get_invoke_folder() + + log_file_path = invoke_folder + get_invoke_file_name() + ".log" - besapi.plugin_utilities.setup_plugin_logging() + besapi.plugin_utilities.setup_plugin_logging(log_file_path) logging.info("----- Starting New Session ------") logging.debug("invoke folder: %s", invoke_folder) diff --git a/src/besapi/plugin_utilities.py b/src/besapi/plugin_utilities.py index 6190e71..5c8d030 100644 --- a/src/besapi/plugin_utilities.py +++ b/src/besapi/plugin_utilities.py @@ -86,27 +86,25 @@ def setup_plugin_argparse(plugin_args_required=False): return arg_parser -def setup_plugin_logging(log_file_name="", verbose=0, console=True): +def setup_plugin_logging(log_file_path="", verbose=0, console=True): """setup logging for plugin use""" - # get folder the script was invoked from: - invoke_folder = get_invoke_folder(verbose) - if not log_file_name or log_file_name == "": - log_file_name = get_invoke_file_name() + ".log" + if not log_file_path or log_file_path == "": + log_file_path = os.path.join( + get_invoke_folder(verbose), get_invoke_file_name() + ".log" + ) # set different log levels: log_level = logging.WARNING if verbose: log_level = logging.INFO + print("INFO: Log File Path: %s", log_file_path) if verbose > 1: log_level = logging.DEBUG - # get path to put log file in: - log_filename = os.path.join(invoke_folder, log_file_name) - handlers = [ logging.handlers.RotatingFileHandler( - log_filename, maxBytes=5 * 1024 * 1024, backupCount=1 + log_file_path, maxBytes=5 * 1024 * 1024, backupCount=1 ) ]