Skip to content

Commit

Permalink
Improved formatting and method/class docstrings.
Browse files Browse the repository at this point in the history
- Applied strict typing to method/class params lacking typing.

Signed-off-by: schlopp96 <[email protected]>
  • Loading branch information
schlopp96 committed Jun 24, 2022
1 parent ef90f7f commit 368c1b1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
21 changes: 12 additions & 9 deletions PyFiTransfer/appevents/CLI_loop.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
from msvcrt import getch

from PyFiTransfer.appevents.events import Events, exprogram, logger
from PyFiTransfer.appevents.events import events, exit_program, logger


def CLI_loop() -> None:
"""CLI program event loop.
---
:return: run CLI program.
:rtype: None
"""
origin: str = Events._get_src_dir()

origin: str = events._get_src_dir()
logger.info(f'Got containing directory:\n>> "{origin}"\n')

targetDir: str = Events._get_dest_dir()
targetDir: str = events._get_dest_dir()
logger.info(f'Got target destination:\n>> "{targetDir}"\n')

fileExt: str = Events._get_ext()
fileExt: str = events._get_ext()
logger.info(
f'Got extension of files to be transferred:\n>> ".{fileExt}"\n')

if Events._verify_dir(origin) and Events._verify_dir(targetDir):
if events._verify_dir(origin) and events._verify_dir(targetDir):
logger.info("Starting file transfer...\n")

if not Events.transfer(origin, targetDir, fileExt):
if not events.transfer(origin, targetDir, fileExt):
print('Press any key to exit...')
getch()
return exprogram.error("File transfer failed!")
return exit_program.error("File transfer failed!")
print('Press any key to exit...')
getch()
return exprogram.success()
return exprogram.error(f'No files found with extension: "{fileExt}"')
return exit_program.success()
return exit_program.error(f'No files found with extension: "{fileExt}"')
14 changes: 11 additions & 3 deletions PyFiTransfer/appevents/GUI_loop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from PyFiTransfer.appevents.events import Events, exprogram, logger
from PyFiTransfer.appevents.events import events, exit_program, logger
from PyFiTransfer.appgui.gui import sg, window


def GUI_loop() -> None:
"""GUI program event loop.
---
:return: run GUI program.
:rtype: None
"""

while True:
event, vals = window.read()

Expand All @@ -21,10 +29,10 @@ def GUI_loop() -> None:
if len(vals['-FileExtensionInput-']) < 1:
sg.Popup('Make sure all fields are filled out!')
continue
Events.transfer(vals['-SourceFolderInput-'],
events.transfer(vals['-SourceFolderInput-'],
vals['-TargetFolderInput-'],
vals['-FileExtensionInput-'])

window.close()

return exprogram.success()
return exit_program.success()
42 changes: 27 additions & 15 deletions PyFiTransfer/appevents/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from os import scandir as lsContents
from os.path import basename as base
from shutil import move
from typing import NoReturn

from genericpath import isdir
from PyFiTransfer.applogger.logger import _LogGenerator
Expand All @@ -20,19 +21,19 @@ class FileTransfer:
- Contains the following class methods:
- :meth:`__get_src_dir(self) -> str`
- :func:`__get_src_dir(self) -> str`
- Get starting location of files to be transferred.
- :meth:`__get_dest_dir(self) -> str`
- :func:`__get_dest_dir(self) -> str`
- Get target destination for file transfer.
- :meth:`__get_ext(self) -> str`
- :func:`__get_ext(self) -> str`
- Get extension of files to transfer.
- :meth:`__verify_dir(self, filepath: PathLike | str) -> bool`
- :func:`__verify_dir(self, filepath: PathLike | str) -> bool`
- Verify if given filepath is a directory.
- :meth:`transfer(self, src_dir: str | os.PathLike, target_dir: str | os.PathLike, file_ext: str | os.PathLike) -> bool`
- :func:`transfer(self, src_dir: str | os.PathLike, target_dir: str | os.PathLike, file_ext: str | os.PathLike) -> bool`
- Transfer files from source directory to target directory.
"""

Expand Down Expand Up @@ -194,30 +195,39 @@ class Exit:
- Contains the following class methods:
- :meth:`success(self) -> None`
- :func:`success(self) -> None`
- Exit program with success.
- :meth:`error(self) -> None`
- :func:`error(self) -> None`
- Exit program with error.
"""

def __init__(self):
"""Initialize exit instance."""
def __init__(self) -> None:
"""Initialize exit instance.
---
:return: :class:`Exit` class instance.
:rtype: None
"""

self.exit_code = 0

def success(self) -> None:
def success(self) -> NoReturn | None:
"""Exit program with success.
---
:return: exit program with success.
:rtype: None
"""

logger.info(f"Operation Successful!\n\n>> Exiting Program...{BORDER}")

self.exit_code = 0
exit(self.exit_code)
return exit(self.exit_code)

def error(self, msg: str) -> None:
def error(self, msg: str) -> NoReturn | None:
"""Exit program with error.
---
Expand All @@ -227,10 +237,12 @@ def error(self, msg: str) -> None:
:return: exit program with error.
:rtype: None
"""

self.exit_code = 1

logger.error(f'{msg}\n>> Exiting Program...{BORDER}')
exit(self.exit_code)
return exit(self.exit_code)


Events = FileTransfer()
exprogram = Exit()
events = FileTransfer()
exit_program = Exit()
45 changes: 28 additions & 17 deletions PyFiTransfer/applogger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,46 @@ class _LogGenerator():
def __init__(self,
name: str = __name__,
filename: str = __name__,
logfmt: str = '[%(asctime)s - %(levelname)s] : %(message)s',
msgfmt: str = '[%(asctime)s - %(levelname)s] : %(message)s',
datefmt: str = "%Y-%m-%d %H:%M:%S",
level=DEBUG):
level: int = DEBUG):
"""Initialize logger instance.
- For the :int:`level` parameter, the level of logging can be any of the following:
- For the :param:`level` parameter, the level of logging can be any of the following:
- CRITICAL = 50
- FATAL = CRITICAL
- ERROR = 40
- WARNING = 30
- WARN = WARNING
- INFO = 20
- DEBUG = 10
- DEBUG = 10 (default)
- NOTSET = 0
- Example of a log entry with default parameters:
>>> [2020-01-01 00:00:00 - DEBUG] : This is a debug message.
---
:param name: assign specific name to logger, defaults to `__name__`.
:type name: :class:`str`, optional
:param logfmt: Initialize the formatter either with the specified format string, or a default as described above, defaults to '[%(asctime)s - %(levelname)s] : %(message)s'
:type logfmt: :class:`str`, optional
:param datefmt: set date formatting, defaults to '%Y-%m-%d %H:%M:%S'
:param msgfmt: initialize log entry formatter either with a specified custom formatting, or the default formatting as described above, defaults to `'[%(asctime)s - %(levelname)s] : %(message)s'`
:type msgfmt: :class:`str`, optional
:param datefmt: set date formatting, defaults to `'%Y-%m-%d %H:%M:%S'`
:type datefmt: :class:`str`, optional
:param level: Set the logging level of this logger. Level must be an int or a str, defaults to `DEBUG`.
:param level: Set the logging level of this logger. Level must be an int or a str, defaults to `DEBUG` (10).
:type level: :class:`int`, optional
"""
self.name = name
self.filename = filename
self.logger = logging.getLogger(self.name)

self.name: str = name
self.filename: str = filename
log_file: str = f'./logs/{self.filename}.log'
self.logfmt = logfmt
self.datefmt = datefmt
self.level = level
self.formatter = logging.Formatter(logfmt, datefmt=datefmt)
self.fhandler = logging.FileHandler(log_file)
self.msgfmt: str = msgfmt
self.datefmt: str = datefmt
self.level: int = level
self.logger: logging.Logger = logging.getLogger(self.name)
self.formatter: logging.Formatter = logging.Formatter(msgfmt, datefmt)
self.fhandler: logging.FileHandler = logging.FileHandler(log_file)
self.logger.addHandler(self.fhandler)
self.fhandler.setFormatter(self.formatter)
self.logger.setLevel(level)
Expand All @@ -68,6 +73,7 @@ def debug(self, msg: str) -> None:
:return: create log entry with given context.
:rtype: None
"""

return self.logger.debug(msg)

def info(self, msg: str) -> None:
Expand All @@ -80,6 +86,7 @@ def info(self, msg: str) -> None:
:return: create log entry with given context.
:rtype: None
"""

return self.logger.info(msg)

def warning(self, msg: str) -> None:
Expand All @@ -92,6 +99,7 @@ def warning(self, msg: str) -> None:
:return: create log entry with given context.
:rtype: None
"""

return self.logger.warning(msg)

def error(self, msg: str) -> None:
Expand All @@ -104,6 +112,7 @@ def error(self, msg: str) -> None:
:return: create log entry with given context.
:rtype: None
"""

return self.logger.error(msg)

def exception(self, msg: str, exc_info=True) -> None:
Expand All @@ -118,6 +127,7 @@ def exception(self, msg: str, exc_info=True) -> None:
:return: create log entry with given context and include exception info.
:rtype: None
"""

return self.logger.error(msg, exc_info=exc_info)

def critical(self, msg: str) -> None:
Expand All @@ -130,4 +140,5 @@ def critical(self, msg: str) -> None:
:return: create log entry with given context.
:rtype: None
"""
return self.logger.critical(msg)

return self.logger.critical(msg)

0 comments on commit 368c1b1

Please sign in to comment.