From d011c480eed5dcba0f96d6ac612511b01de084a7 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Wed, 2 Nov 2022 14:47:47 +0100 Subject: [PATCH] Start migrating logging --- qgis_plugin_manager/__main__.py | 30 +++++++++++++++++++++++++- qgis_plugin_manager/local_directory.py | 23 +++++++++++--------- qgis_plugin_manager/remote.py | 3 ++- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/qgis_plugin_manager/__main__.py b/qgis_plugin_manager/__main__.py index ca3e5ff..43ee988 100755 --- a/qgis_plugin_manager/__main__.py +++ b/qgis_plugin_manager/__main__.py @@ -5,11 +5,12 @@ __email__ = 'info@3liz.org' import argparse +import logging import os from pathlib import Path -from qgis_plugin_manager.__about__ import __version__ +from qgis_plugin_manager.__about__ import __title_clean__, __version__ from qgis_plugin_manager.definitions import Level from qgis_plugin_manager.local_directory import LocalDirectory from qgis_plugin_manager.remote import Remote @@ -23,6 +24,16 @@ def main() -> int: ) parser.add_argument("-v", "--version", action="version", version=__version__) + parser.add_argument( + "-v", + "-v", + "--verbose", + action="count", + default=1, + dest="verbosity", + help="Verbosity level: None = WARNING, -v = INFO, -vv = DEBUG", + ) + subparsers = parser.add_subparsers( title="commands", description="qgis-plugin-manager command", dest="command", ) @@ -73,6 +84,23 @@ def main() -> int: args = parser.parse_args() + # set log level depending on verbosity argument + # by default 1 => + args.verbosity = 40 - (10 * args.verbosity) if args.verbosity > 0 else 0 + logging.basicConfig( + level=args.verbosity, + # format="%(asctime)s||%(levelname)s||%(module)s||%(message)s", + format='%(levelname)s:%(message)s', + # datefmt="%Y-%m-%d %H:%M:%S", + ) + + console = logging.StreamHandler() + console.setLevel(args.verbosity) + + # add the handler to the root logger + logger = logging.getLogger(__title_clean__) + logger.debug(f"Log level set: {logging}") + # if no command is passed, print the help and exit if not args.command: parser.print_help() diff --git a/qgis_plugin_manager/local_directory.py b/qgis_plugin_manager/local_directory.py index f3157d9..f7239e1 100644 --- a/qgis_plugin_manager/local_directory.py +++ b/qgis_plugin_manager/local_directory.py @@ -3,6 +3,7 @@ __email__ = 'info@3liz.org' import configparser +import logging import os import shutil import stat @@ -22,6 +23,8 @@ to_bool, ) +logger = logging.getLogger(__name__) + class LocalDirectory: @@ -45,14 +48,14 @@ def init(self) -> bool: """ Init this qgis-plugin-manager by creating the default sources.list.""" source_file = sources_file(self.folder) if source_file.exists(): - print(f"{Level.Alert}{source_file.absolute()} is already existing. Quit{Level.End}") + logger.warning(f"{Level.Alert}{source_file.absolute()} is already existing. Quit{Level.End}") return False if self.qgis_version: version = "[VERSION]" - print("Init https://plugins.qgis.org") + logger.info("Init https://plugins.qgis.org") else: - print( + logger.warning( f"{Level.Alert}" f"QGIS version is unknown, creating with a default {DEFAULT_QGIS_VERSION}" f"{Level.End}", @@ -182,21 +185,21 @@ def remove(self, plugin_name: str) -> bool: try: shutil.rmtree(plugin_path) except Exception as e: - print(f"{Level.Critical}Plugin {plugin_name} could not be removed : {e!s}") + logger.error(f"{Level.Critical}Plugin {plugin_name} could not be removed : {e!s}") if not Path(self.folder.joinpath(plugin_folder)).exists(): - print(f"{Level.Success}Plugin {plugin_name} removed") + logger.info(f"{Level.Success}Plugin {plugin_name} removed") restart_qgis_server() return True else: - print( + logger.error( f"{Level.Alert}" f"Plugin {plugin_name} using folder {plugin_folder} could not be removed " f"for unknown reason" f"{Level.End}", ) break - print(f"{Level.Alert}Plugin name '{plugin_name}' not found{Level.End}") + logger.error(f"{Level.Alert}Plugin name '{plugin_name}' not found{Level.End}") all_names = list(set(all_names)) similarity = similar_names(plugin_name.lower(), all_names) @@ -321,13 +324,13 @@ def print_table(self, current_directory: bool = True): if len(data): print(pretty_table(data, headers)) else: - print( - f"{Level.Alert}No plugin found in the current directory {self.folder.absolute()}{Level.End}", + logger.error( + f"{Level.Alert}No plugin found in the current directory {self.folder.absolute()}{Level.End}" ) if len(list_of_owners) > 1: list_of_owners = [f"'{i}'" for i in list_of_owners] - print( + logger.error( f"{Level.Alert}" f"Different rights have been detected : {','.join(list_of_owners)}" f"{Level.End}. " diff --git a/qgis_plugin_manager/remote.py b/qgis_plugin_manager/remote.py index afd2f9d..58583dd 100644 --- a/qgis_plugin_manager/remote.py +++ b/qgis_plugin_manager/remote.py @@ -3,6 +3,7 @@ __email__ = 'info@3liz.org' import base64 +import logging import os import re import shutil @@ -43,7 +44,7 @@ def remote_is_ready(self) -> bool: source_list = sources_file(self.folder) if not source_list.exists(): if not self.setting_error: - print(f"{Level.Critical}The {source_list.absolute()} file does not exist{Level.End}") + logger.error(f"{Level.Critical}The {source_list.absolute()} file does not exist{Level.End}") print("Use the 'init' command to create the file") self.setting_error = True return False