Skip to content

Commit

Permalink
fix: Moved some dictionaries to __init__ methods.
Browse files Browse the repository at this point in the history
`colors` in `Argparse.ColorizingHelpFormatter` class.
`_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class.
`sign_colors` in `PPrint.PrettyPrinter` class.
`colors` in `TreePrint.TreePrint.Node` class.
  • Loading branch information
MPCodeWriter21 committed Jun 27, 2023
1 parent 137582f commit e9d7ceb
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 126 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Help this project by [Donation](DONATE.md)
Changes
-----------

### 2.5.3

Moved some dictionaries to `__init__` methods.
`colors` in `Argparse.ColorizingHelpFormatter` class.
`_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class.
`sign_colors` in `PPrint.PrettyPrinter` class.
`colors` in `TreePrint.TreePrint.Node` class.

### 2.5.2

Improved type-hintings.
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ python setup.py install
Changes
-------

### 2.5.2
### 2.5.3

Improved type-hintings.
Moved some dictionaries to `__init__` methods.
`colors` in `Argparse.ColorizingHelpFormatter` class.
`_level_name` in `Formatters._Formatter` class and `level_colors` in `Formatters.ColorizingFormatter` class.
`sign_colors` in `PPrint.PrettyPrinter` class.
`colors` in `TreePrint.TreePrint.Node` class.

[Full CHANGELOG](https://github.com/MPCodeWriter21/log21/blob/master/CHANGELOG.md)

Expand Down
28 changes: 15 additions & 13 deletions log21/Argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@


class ColorizingHelpFormatter(_argparse.HelpFormatter):
colors = {
'usage': 'Cyan',
'brackets': 'LightRed',
'switches': 'LightCyan',
'values': 'Green',
'colons': 'LightRed',
'commas': 'LightRed',
'section headers': 'LightGreen',
'help': 'LightWhite',
'choices': 'LightGreen'
}

def __init__(self, prog, indent_increment=2, max_help_position=24, width=None, colors: _Mapping[str, str] = None):
def __init__(self, prog, indent_increment=2, max_help_position=24, width=None,
colors: _Optional[_Mapping[str, str]] = None):
super().__init__(prog, indent_increment, max_help_position, width)

self.colors = {
'usage': 'Cyan',
'brackets': 'LightRed',
'switches': 'LightCyan',
'values': 'Green',
'colons': 'LightRed',
'commas': 'LightRed',
'section headers': 'LightGreen',
'help': 'LightWhite',
'choices': 'LightGreen'
}

if colors:
for key, value in colors.items():
if key in self.colors:
Expand Down
40 changes: 20 additions & 20 deletions log21/Formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@


class _Formatter(__Formatter):
_level_names: _Dict[int, str] = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARNING: 'WARNING',
ERROR: 'ERROR',
CRITICAL: 'CRITICAL',
PRINT: 'PRINT',
INPUT: 'INPUT'
}

def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, style: str = '%',
level_names: _Optional[_Mapping[int, str]] = None):
"""
Expand Down Expand Up @@ -49,6 +39,17 @@ def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, s
>>>
"""
super().__init__(fmt=fmt, datefmt=datefmt, style=style)

self._level_names: _Dict[int, str] = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARNING: 'WARNING',
ERROR: 'ERROR',
CRITICAL: 'CRITICAL',
PRINT: 'PRINT',
INPUT: 'INPUT'
}

if level_names:
for level, name in level_names.items():
self.level_names[level] = name
Expand Down Expand Up @@ -89,16 +90,6 @@ def format(self, record) -> str:


class ColorizingFormatter(_Formatter):
# Default color values
level_colors: _Dict[int, _Tuple[str, ...]] = {
DEBUG: ('lightblue',),
INFO: ('green',),
WARNING: ('lightyellow',),
ERROR: ('light red',),
CRITICAL: ('background red', 'white'),
PRINT: ('Cyan',),
INPUT: ('Magenta',)
}
time_color: _Tuple[str, ...] = ('lightblue',)
name_color = pathname_color = filename_color = module_color = func_name_color = thread_name_color = \
message_color = tuple()
Expand All @@ -112,6 +103,15 @@ def __init__(self, fmt: _Optional[str] = None, datefmt: _Optional[str] = None, s
thread_name_color: _Optional[_Tuple[str, ...]] = None,
message_color: _Optional[_Tuple[str, ...]] = None):
super().__init__(fmt=fmt, datefmt=datefmt, style=style, level_names=level_names)
self.level_colors: _Dict[int, _Tuple[str, ...]] = {
DEBUG: ('lightblue',),
INFO: ('green',),
WARNING: ('lightyellow',),
ERROR: ('light red',),
CRITICAL: ('background red', 'white'),
PRINT: ('Cyan',),
INPUT: ('Magenta',)
}
# Checks and sets colors
if level_colors:
if not isinstance(level_colors, _Mapping):
Expand Down
Loading

0 comments on commit e9d7ceb

Please sign in to comment.