Skip to content

Commit 509902c

Browse files
committed
dev: refactor default columns configuration into a method
This way we can re-run it on demand.
1 parent acc17ed commit 509902c

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ API Reference
7878
.. autoclass:: ConsoleRenderer
7979
:members: get_default_level_styles, get_default_column_styles, exception_formatter, sort_keys
8080

81-
.. autoclass:: Styles
81+
.. autoclass:: ColumnStyles
8282

8383
.. autofunction:: get_active_console_renderer
8484
.. autoexception:: NoConsoleRendererConfiguredError

src/structlog/dev.py

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ def _pad(s: str, length: int) -> str:
111111

112112

113113
@dataclass(frozen=True)
114-
class Styles:
114+
class ColumnStyles:
115115
"""
116-
Style settings for console rendering.
116+
Column styles settings for console rendering.
117117
118118
These are console ANSI codes that are printed before the respective fields.
119119
This allows for a certain amount of customization if you don't want to
@@ -140,7 +140,7 @@ class Styles:
140140
kv_value: str
141141

142142

143-
_colorful_styles = Styles(
143+
_colorful_styles = ColumnStyles(
144144
reset=RESET_ALL,
145145
bright=BRIGHT,
146146
level_critical=RED,
@@ -156,7 +156,7 @@ class Styles:
156156
kv_value=MAGENTA,
157157
)
158158

159-
_plain_styles = Styles(
159+
_plain_styles = ColumnStyles(
160160
reset="",
161161
bright="",
162162
level_critical="",
@@ -651,22 +651,90 @@ def add_meaningless_arg(arg: str) -> None:
651651

652652
styles = self.get_default_column_styles(colors, force_colors)
653653

654-
self._styles = styles
654+
self._repr_native_str = repr_native_str
655655

656-
level_to_color = (
657-
self.get_default_level_styles(colors)
656+
self._configure_columns(
657+
styles=styles,
658+
level_styles=self.get_default_level_styles(colors)
658659
if level_styles is None
659-
else level_styles
660-
).copy()
660+
else level_styles,
661+
pad_level=pad_level,
662+
timestamp_key=timestamp_key,
663+
event_key=event_key,
664+
pad_event=pad_event,
665+
)
666+
667+
@classmethod
668+
def get_default_column_styles(
669+
cls, colors: bool, force_colors: bool = False
670+
) -> ColumnStyles:
671+
"""
672+
Configure and return the appropriate styles class for console output.
673+
674+
This method handles the setup of colorful or plain styles, including
675+
proper colorama initialization on Windows systems when colors are
676+
enabled.
677+
678+
Args:
679+
colors: Whether to use colorful output styles.
680+
681+
force_colors:
682+
Force colorful output even in non-interactive environments.
683+
Only relevant on Windows with colorama.
684+
685+
Returns:
686+
The configured styles.
687+
688+
Raises:
689+
SystemError:
690+
On Windows when colors=True but colorama is not installed.
691+
692+
.. versionadded:: 25.5.0
693+
"""
694+
if not colors:
695+
return _plain_styles
696+
697+
if _IS_WINDOWS: # pragma: no cover
698+
# On Windows, we can't do colorful output without colorama.
699+
if colorama is None:
700+
raise SystemError(
701+
_MISSING.format(
702+
who=cls.__name__ + " with `colors=True`",
703+
package="colorama",
704+
)
705+
)
706+
# Colorama must be init'd on Windows, but must NOT be
707+
# init'd on other OSes, because it can break colors.
708+
if force_colors:
709+
colorama.deinit()
710+
colorama.init(strip=False)
711+
else:
712+
colorama.init()
713+
714+
return _colorful_styles
715+
716+
def _configure_columns(
717+
self,
718+
*,
719+
styles: ColumnStyles,
720+
level_styles: dict[str, str],
721+
pad_level: bool,
722+
timestamp_key: str,
723+
event_key: str,
724+
pad_event: int,
725+
) -> None:
726+
"""
727+
Re-configures columns according to the given styles and parameters.
728+
"""
729+
self._styles = styles
730+
level_to_color = level_styles.copy()
661731

662732
for key in level_to_color:
663733
level_to_color[key] += styles.bright
664734
self._longest_level = len(
665735
max(level_to_color.keys(), key=lambda e: len(e))
666736
)
667737

668-
self._repr_native_str = repr_native_str
669-
670738
self._default_column_formatter = KeyValueColumnFormatter(
671739
styles.kv_key,
672740
styles.kv_value,
@@ -716,55 +784,6 @@ def add_meaningless_arg(arg: str) -> None:
716784
Column("logger_name", logger_name_formatter),
717785
]
718786

719-
@classmethod
720-
def get_default_column_styles(
721-
cls, colors: bool, force_colors: bool = False
722-
) -> Styles:
723-
"""
724-
Configure and return the appropriate styles class for console output.
725-
726-
This method handles the setup of colorful or plain styles, including
727-
proper colorama initialization on Windows systems when colors are
728-
enabled.
729-
730-
Args:
731-
colors: Whether to use colorful output styles.
732-
733-
force_colors:
734-
Force colorful output even in non-interactive environments.
735-
Only relevant on Windows with colorama.
736-
737-
Returns:
738-
The configured styles.
739-
740-
Raises:
741-
SystemError:
742-
On Windows when colors=True but colorama is not installed.
743-
744-
.. versionadded:: 25.5.0
745-
"""
746-
if not colors:
747-
return _plain_styles
748-
749-
if _IS_WINDOWS: # pragma: no cover
750-
# On Windows, we can't do colorful output without colorama.
751-
if colorama is None:
752-
raise SystemError(
753-
_MISSING.format(
754-
who=cls.__name__ + " with `colors=True`",
755-
package="colorama",
756-
)
757-
)
758-
# Colorama must be init'd on Windows, but must NOT be
759-
# init'd on other OSes, because it can break colors.
760-
if force_colors:
761-
colorama.deinit()
762-
colorama.init(strip=False)
763-
else:
764-
colorama.init()
765-
766-
return _colorful_styles
767-
768787
def _repr(self, val: Any) -> str:
769788
"""
770789
Determine representation of *val* depending on its type &
@@ -837,7 +856,7 @@ def get_default_level_styles(colors: bool = True) -> dict[str, str]:
837856
Whether to use colorful styles. This must match the *colors*
838857
parameter to `ConsoleRenderer`. Default: `True`.
839858
"""
840-
styles: Styles
859+
styles: ColumnStyles
841860
styles = _colorful_styles if colors else _plain_styles
842861
return {
843862
"critical": styles.level_critical,

0 commit comments

Comments
 (0)