|
| 1 | +import datetime |
| 2 | +import importlib.resources |
| 3 | +import logging |
| 4 | +import logging.config |
| 5 | +import os |
| 6 | +import platform |
| 7 | +import string |
| 8 | +import sys |
| 9 | +import time |
| 10 | +import warnings |
| 11 | +from typing import Sequence |
| 12 | +from . import __version__ |
| 13 | + |
| 14 | +import colorlog |
| 15 | +import colorlog.escape_codes |
| 16 | + |
| 17 | +__all__: Sequence[str] = ('start_logging', 'print_banner') |
| 18 | + |
| 19 | + |
| 20 | +day_prefixes: dict[int, str] = { |
| 21 | + 1: 'st', |
| 22 | + 2: 'nd', |
| 23 | + 3: 'rd', |
| 24 | + 4: 'th', |
| 25 | + 5: 'th', |
| 26 | + 6: 'th', |
| 27 | + 7: 'th', |
| 28 | + 8: 'th', |
| 29 | + 9: 'th', |
| 30 | + 0: 'th', |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +def start_logging(flavor: None | int | str | dict, debug: bool = False): |
| 35 | + if len(logging.root.handlers) != 0: |
| 36 | + return # the user is most likely using logging.basicConfig, or is being spearheaded by something else. |
| 37 | + |
| 38 | + if flavor is None: |
| 39 | + flavor = logging.DEBUG if debug else logging.INFO |
| 40 | + |
| 41 | + if isinstance(flavor, dict): |
| 42 | + logging.config.dictConfig(flavor) |
| 43 | + |
| 44 | + if flavor.get('handler'): |
| 45 | + return |
| 46 | + |
| 47 | + flavor = None |
| 48 | + |
| 49 | + # things that will never be logged. |
| 50 | + logging.logThreads = None |
| 51 | + logging.logProcesses = None |
| 52 | + |
| 53 | + colorlog.basicConfig( |
| 54 | + level=flavor, |
| 55 | + format='%(log_color)s%(bold)s%(levelname)-1.1s%(thin)s %(asctime)23.23s %(bold)s%(name)s: ' |
| 56 | + '%(thin)s%(message)s%(reset)s', |
| 57 | + stream=sys.stderr, |
| 58 | + log_colors={ |
| 59 | + 'DEBUG': 'cyan', |
| 60 | + 'INFO': 'green', |
| 61 | + 'WARNING': 'yellow', |
| 62 | + 'ERROR': 'red', |
| 63 | + 'CRITICAL': 'red, bg_white', |
| 64 | + }, |
| 65 | + ) |
| 66 | + warnings.simplefilter('always', DeprecationWarning) |
| 67 | + logging.captureWarnings(True) |
| 68 | + |
| 69 | + |
| 70 | +def get_day_prefix(num: int) -> str: |
| 71 | + n = str(num) |
| 72 | + return day_prefixes[int(n[len(n) - 1])] |
| 73 | + |
| 74 | + |
| 75 | +def print_banner( |
| 76 | + bot_name: str = 'A bot', |
| 77 | + module: str | None = 'pycord', |
| 78 | +): |
| 79 | + banners = importlib.resources.files(module) |
| 80 | + |
| 81 | + for trav in banners.iterdir(): |
| 82 | + if trav.name == 'banner.txt': |
| 83 | + banner = trav.read_text() |
| 84 | + elif trav.name == 'ibanner.txt': |
| 85 | + info_banner = trav.read_text() |
| 86 | + |
| 87 | + today = datetime.date.today() |
| 88 | + |
| 89 | + args = { |
| 90 | + # the # prefix only works on Windows, and the - prefix only works on linux/unix systems |
| 91 | + 'current_time': today.strftime(f'%B the %#d{get_day_prefix(today.day)} of %Y') |
| 92 | + if os.name == 'nt' |
| 93 | + else today.strftime(f'%B the %-d{get_day_prefix(today.day)} of %Y'), |
| 94 | + 'py_version': platform.python_version(), |
| 95 | + 'botname': bot_name, |
| 96 | + 'version': __version__ |
| 97 | + } |
| 98 | + args |= colorlog.escape_codes.escape_codes |
| 99 | + |
| 100 | + sys.stdout.write(string.Template(banner).safe_substitute(args)) |
| 101 | + sys.stdout.write(string.Template(info_banner).safe_substitute(args)) |
| 102 | + sys.stdout.flush() |
| 103 | + time.sleep(0.162) # sleep for a bit to prevent overfill. |
0 commit comments