Skip to content

Commit

Permalink
Replace Printer through Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvogel committed Feb 12, 2025
1 parent 40b0a3e commit d146bbc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 216 deletions.
25 changes: 12 additions & 13 deletions src/pyProfileMgr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

import sys
import argparse
import logging
from colorama import just_fix_windows_console

# Import command modules
from pyProfileMgr import cmd_profile

from pyProfileMgr.printer import Printer
from pyProfileMgr.ret import Ret
from pyProfileMgr.version import __version__, __author__, __email__, __repository__, __license__

Expand All @@ -49,6 +49,8 @@
# Variables
################################################################################

LOG: logging.Logger = logging.getLogger(__name__)

# Add command modules here
_CMD_MODULES = [
cmd_profile,
Expand All @@ -72,7 +74,7 @@

def add_parser() -> argparse.ArgumentParser:
""" Adds the parser for command line arguments and
sets the execute function of each
sets the execute function of each
cmd module as callback for the subparser command.
Returns the parser after all the modules have been registered
and added their subparsers.
Expand Down Expand Up @@ -111,31 +113,28 @@ def main() -> Ret.CODE:
int: System exit status.
"""
ret_status = Ret.CODE.RET_OK
printer = Printer()
args = None

# Older windows consoles doesn't support ANSI color codes by default.
# Enable the Windows built-in ANSI support.
just_fix_windows_console()

# Get parser
# Create the main parser and add the subparsers.
parser = add_parser()

# Parse command line arguments.
# If error occurs, exits the program from this point with code 2.
# Parse the command line arguments.
args = parser.parse_args()

if args is None:
ret_status = Ret.CODE.RET_ERROR_ARGPARSE
parser.print_help()
else:
# In verbose mode print all program arguments
# If the verbose flag is set, change the default logging level.
if args.verbose:
printer.set_verbose()
print("Program arguments: ")

logging.basicConfig(level=logging.INFO)
LOG.info("Program arguments: ")
for arg in vars(args):
print(f"* {arg} = {vars(args)[arg]}")
print("\n")
LOG.info("* %s = %s", arg, vars(args)[arg])

# Call command function and return exit status
ret_status = args.func(args)
Expand All @@ -145,10 +144,10 @@ def main() -> Ret.CODE:

return ret_status


################################################################################
# Main
################################################################################


if __name__ == "__main__":
sys.exit(main())
11 changes: 7 additions & 4 deletions src/pyProfileMgr/cmd_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@
################################################################################
# Imports
################################################################################

import argparse
import logging

from pyProfileMgr.profile_mgr import ProfileMgr

from pyProfileMgr.printer import Printer, PrintType
from pyProfileMgr.ret import Ret


################################################################################
# Variables
################################################################################

LOG = Printer()
LOG: logging.Logger = logging.getLogger(__name__)


################################################################################
# Classes
Expand Down Expand Up @@ -314,10 +317,10 @@ def _add_profile(args) -> Ret.CODE:

if args.server is None:
ret_status = Ret.CODE.RET_ERROR_MISSING_SERVER_URL
LOG.print_error(PrintType.ERROR, ret_status)
LOG.error("%s", Ret.MSG[ret_status])
elif args.token is None and (args.user is None or args.password is None):
ret_status = Ret.CODE.RET_ERROR_MISSING_USER_INFORMATION
LOG.print_error(PrintType.ERROR, ret_status)
LOG.error("%s", Ret.MSG[ret_status])
print("Profiles can only be created using login credentials." +
"Please provide a token using the --token option or --user/--password.")
else:
Expand Down
26 changes: 16 additions & 10 deletions src/pyProfileMgr/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@
################################################################################
# Imports
################################################################################

import os
import ctypes
import logging

from pyProfileMgr.ret import Ret, Warnings
from pyProfileMgr.printer import Printer, PrintType


################################################################################
# Variables
################################################################################

LOG: logging.Logger = logging.getLogger(__name__)

FILE_ATTRIBUTE_HIDDEN = 0x02
printer = Printer()


################################################################################
# Classes
################################################################################
Expand All @@ -61,13 +67,13 @@ def __init__(self):
self._content = None

def process_file_argument(self, default_name: str, file_arg: str) -> Ret.CODE:
""" Get the filename. Handle possible extension errors
""" Get the filename. Handle possible extension errors
with the filename provided via the -file option.
If a path to a file was supplied, the path will be kept.
The returned filename will be without extension.
Args:
issue_key (str): The current issue key.
issue_key (str): The current issue key.
arg_file (str): The -file option string provided via the console.
Returns:
Expand All @@ -90,8 +96,8 @@ def process_file_argument(self, default_name: str, file_arg: str) -> Ret.CODE:
f'{default_name}.json'))

elif ext != '.json':
printer.print_error(PrintType.WARNING,
Warnings.CODE.WARNING_UNKNOWN_FILE_EXTENSION)
LOG.warning(
"%s", Warnings.MSG[Warnings.CODE.WARNING_UNKNOWN_FILE_EXTENSION])

path, ext = os.path.splitext(self.get_path())

Expand Down Expand Up @@ -178,7 +184,7 @@ def read_file(self) -> Ret.CODE:
return ret_status

def get_file(self) -> object:
""" Return the file object in
""" Return the file object in
this instance.
Returns:
Expand All @@ -187,7 +193,7 @@ def get_file(self) -> object:
return self._file

def get_file_extension(self) -> str:
""" Return the file extension
""" Return the file extension
of the file as a string.
Returns:
Expand Down Expand Up @@ -249,7 +255,7 @@ def open_file(self, file_mode: str) -> Ret.CODE:
and save the file obj.
Args:
file_mode (str): For reading files 'r' or for writing files 'w'.
file_mode (str): For reading files 'r' or for writing files 'w'.
Returns:
Ret.CODE: Returns Ret.CODE.RET_OK if successful or else the corresponding error code.
Expand All @@ -276,7 +282,7 @@ def hide_file(self) -> None:
FILE_ATTRIBUTE_HIDDEN)

def close_file(self) -> None:
""" Close the file in the class instance.
""" Close the file in the class instance.
"""
if self._file is not None:
if not self._file.closed:
Expand Down
58 changes: 0 additions & 58 deletions src/pyProfileMgr/print_type.py

This file was deleted.

114 changes: 0 additions & 114 deletions src/pyProfileMgr/printer.py

This file was deleted.

Loading

0 comments on commit d146bbc

Please sign in to comment.