ftp_download.prefs

  1import asyncio
  2import logging
  3from os.path import join, expanduser, exists, split
  4from os import makedirs
  5
  6
  7class GlobalConfigDefaults:
  8    def __init__(self):
  9        self.verbose: bool = True
 10        """Should event messages be printed do `stdout`? This does not affect logging""" # noqa
 11
 12        self.timeout: int = 10
 13        """Time to wait for request responses in seconds (unused yet)"""
 14
 15        self.retry: int = 5
 16        """Maximum amount of attempts for every request (unused yet)"""
 17
 18        self.raise_if_invalid: bool = True
 19        """Shoud stop if an invalid path is provided? If `False` it will just print a message""" # noqa
 20
 21        self.use_async: bool = False
 22        """Should send asynchronous download requests? If `True` your program might run faster, due to not waiting for downloads to finish. Might be incompatible with Jupyter Notebooks and some implementations of GUI applications that runs on event loops.""" # noqa
 23
 24        self.download_folder: str = join(
 25            expanduser("~"), "Downloads", "ftp_download"
 26        )
 27        """Standard destination path for all downloads"""
 28
 29    def __repr__(self):
 30        return (
 31            "FTP_DOWLOAD SETTINGS: \n" +
 32            f"verbose = {self.verbose}; \n" +
 33            f"raise_if_invalid = {self.raise_if_invalid};  \n" +
 34            f"use_async = {self.use_async}; \n" +
 35            f"download_folder = {self.download_folder}"
 36        )
 37
 38
 39Conf = GlobalConfigDefaults()
 40"""Stores global configuration parameters, change it's parameter values to set your preferences""" # noqa
 41
 42LOG_FILE = join(Conf.download_folder, ".logs")
 43"""Full path to the logfile"""
 44
 45LOG_FMT = "[%(asctime)s] - %(levelname)s::%(name)s - %(message)s"
 46"""Format string used by the logger"""
 47
 48LOG_LVL = logging.DEBUG
 49"""`int` specifying the log level for the `logging` library"""
 50
 51
 52class LocalLogger():
 53    def __init__(self):
 54        self.logger = logging.getLogger(__name__)
 55        self.logger.setLevel(logging.DEBUG)
 56        self.handler = logging.FileHandler(
 57            filename=LOG_FILE
 58        )
 59        self.formatter = logging.Formatter(
 60            fmt=LOG_FMT
 61        )
 62
 63        self.handler.setFormatter(self.formatter)
 64        self.handler.setLevel(logging.DEBUG)
 65        self.logger.addHandler(self.handler)
 66        self.logger.propagate = False
 67
 68    def debug(self, message):
 69        self.logger.debug(msg=message)
 70
 71    def info(self, message):
 72        self.logger.info(msg=message)
 73
 74    def warn(self, message):
 75        self.logger.warn(msg=message)
 76
 77    def error(self, message):
 78        self.logger.error(msg=message)
 79
 80    def critical(self, message):
 81        self.logger.critical(msg=message)
 82
 83
 84def set_log_configs():
 85    global log
 86
 87    makedirs(split(LOG_FILE)[0], exist_ok=True)
 88    if not exists(LOG_FILE):
 89        f = open(LOG_FILE, "x")
 90        f.close()
 91
 92    for handler in logging.root.handlers[:]:
 93        logging.root.removeHandler(handler)
 94
 95    logging.basicConfig(
 96        format=LOG_FMT,
 97        level=LOG_LVL,
 98        handlers=[logging.FileHandler(filename=LOG_FILE)]
 99    )
100
101    log = logging.getLogger()
102    log.propagate = False
103
104
105set_log_configs()
class GlobalConfigDefaults:
 8class GlobalConfigDefaults:
 9    def __init__(self):
10        self.verbose: bool = True
11        """Should event messages be printed do `stdout`? This does not affect logging""" # noqa
12
13        self.timeout: int = 10
14        """Time to wait for request responses in seconds (unused yet)"""
15
16        self.retry: int = 5
17        """Maximum amount of attempts for every request (unused yet)"""
18
19        self.raise_if_invalid: bool = True
20        """Shoud stop if an invalid path is provided? If `False` it will just print a message""" # noqa
21
22        self.use_async: bool = False
23        """Should send asynchronous download requests? If `True` your program might run faster, due to not waiting for downloads to finish. Might be incompatible with Jupyter Notebooks and some implementations of GUI applications that runs on event loops.""" # noqa
24
25        self.download_folder: str = join(
26            expanduser("~"), "Downloads", "ftp_download"
27        )
28        """Standard destination path for all downloads"""
29
30    def __repr__(self):
31        return (
32            "FTP_DOWLOAD SETTINGS: \n" +
33            f"verbose = {self.verbose}; \n" +
34            f"raise_if_invalid = {self.raise_if_invalid};  \n" +
35            f"use_async = {self.use_async}; \n" +
36            f"download_folder = {self.download_folder}"
37        )
verbose: bool

Should event messages be printed do stdout? This does not affect logging

timeout: int

Time to wait for request responses in seconds (unused yet)

retry: int

Maximum amount of attempts for every request (unused yet)

raise_if_invalid: bool

Shoud stop if an invalid path is provided? If False it will just print a message

use_async: bool

Should send asynchronous download requests? If True your program might run faster, due to not waiting for downloads to finish. Might be incompatible with Jupyter Notebooks and some implementations of GUI applications that runs on event loops.

download_folder: str

Standard destination path for all downloads

Conf = FTP_DOWLOAD SETTINGS: verbose = True; raise_if_invalid = True; use_async = False; download_folder = /home/user/Downloads/ftp_download

Stores global configuration parameters, change it's parameter values to set your preferences

LOG_FILE = '/home/user/Downloads/ftp_download/.logs'

Full path to the logfile

LOG_FMT = '[%(asctime)s] - %(levelname)s::%(name)s - %(message)s'

Format string used by the logger

LOG_LVL = 10

int specifying the log level for the logging library

class LocalLogger:
53class LocalLogger():
54    def __init__(self):
55        self.logger = logging.getLogger(__name__)
56        self.logger.setLevel(logging.DEBUG)
57        self.handler = logging.FileHandler(
58            filename=LOG_FILE
59        )
60        self.formatter = logging.Formatter(
61            fmt=LOG_FMT
62        )
63
64        self.handler.setFormatter(self.formatter)
65        self.handler.setLevel(logging.DEBUG)
66        self.logger.addHandler(self.handler)
67        self.logger.propagate = False
68
69    def debug(self, message):
70        self.logger.debug(msg=message)
71
72    def info(self, message):
73        self.logger.info(msg=message)
74
75    def warn(self, message):
76        self.logger.warn(msg=message)
77
78    def error(self, message):
79        self.logger.error(msg=message)
80
81    def critical(self, message):
82        self.logger.critical(msg=message)
logger
handler
formatter
def debug(self, message):
69    def debug(self, message):
70        self.logger.debug(msg=message)
def info(self, message):
72    def info(self, message):
73        self.logger.info(msg=message)
def warn(self, message):
75    def warn(self, message):
76        self.logger.warn(msg=message)
def error(self, message):
78    def error(self, message):
79        self.logger.error(msg=message)
def critical(self, message):
81    def critical(self, message):
82        self.logger.critical(msg=message)
def set_log_configs():
 85def set_log_configs():
 86    global log
 87
 88    makedirs(split(LOG_FILE)[0], exist_ok=True)
 89    if not exists(LOG_FILE):
 90        f = open(LOG_FILE, "x")
 91        f.close()
 92
 93    for handler in logging.root.handlers[:]:
 94        logging.root.removeHandler(handler)
 95
 96    logging.basicConfig(
 97        format=LOG_FMT,
 98        level=LOG_LVL,
 99        handlers=[logging.FileHandler(filename=LOG_FILE)]
100    )
101
102    log = logging.getLogger()
103    log.propagate = False