Skip to content

Commit

Permalink
Start migrating logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed May 6, 2024
1 parent e44b22b commit d011c48
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
30 changes: 29 additions & 1 deletion qgis_plugin_manager/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
__email__ = '[email protected]'

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
Expand All @@ -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",
)
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 13 additions & 10 deletions qgis_plugin_manager/local_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__email__ = '[email protected]'

import configparser
import logging
import os
import shutil
import stat
Expand All @@ -22,6 +23,8 @@
to_bool,
)

logger = logging.getLogger(__name__)


class LocalDirectory:

Expand All @@ -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}",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}. "
Expand Down
3 changes: 2 additions & 1 deletion qgis_plugin_manager/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__email__ = '[email protected]'

import base64
import logging
import os
import re
import shutil
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d011c48

Please sign in to comment.