Skip to content

Commit dcb1c09

Browse files
committed
Add plain mode for logging
1 parent 7108c04 commit dcb1c09

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

patchwork/app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,22 @@ def sigint_handler(signum, frame):
144144
@click.option("patched_api_key", "--patched_api_key", help="API key to use with the patched.codes service.")
145145
@click.option("disable_telemetry", "--disable_telemetry", is_flag=True, help="Disable telemetry.", default=False)
146146
@click.option("debug", "--debug", is_flag=True, help="Enable debug mode.", default=False)
147+
@click.option("plain", "--plain", is_flag=True, help="Enable plain mode (no panel).", default=False)
147148
def cli(
148149
log: str,
149150
patchflow: str,
150151
opts: list[str],
151152
config: str | None,
152153
output: str | None,
154+
plain: bool,
153155
data_format: str,
154156
patched_api_key: str | None,
155157
disable_telemetry: bool,
156158
debug: bool,
157159
):
158160
setup_cli()
159161

160-
init_cli_logger(log)
162+
init_cli_logger(log, plain)
161163

162164
if "::" in patchflow:
163165
module_path, _, patchflow_name = patchflow.partition("::")
@@ -167,7 +169,7 @@ def cli(
167169

168170
possbile_module_paths = deque((module_path,))
169171

170-
panel = logger.panel("Initializing Patchwork CLI") if debug else nullcontext()
172+
panel = nullcontext() if plain or not debug else logger.panel("Initializing Patchwork CLI")
171173

172174
with panel:
173175
inputs = {}
@@ -227,7 +229,7 @@ def cli(
227229
# treat --key=value as a key-value pair
228230
inputs[key] = value
229231

230-
patchflow_panel = nullcontext() if debug else logger.panel(f"Patchflow {patchflow} inputs")
232+
patchflow_panel = nullcontext() if plain or not debug else logger.panel(f"Patchflow {patchflow} inputs")
231233

232234
with patchflow_panel as _:
233235
if debug is True:

patchwork/logger.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from patchwork.managed_files import HOME_FOLDER, LOG_FILE
1919

2020
# Create a global console object
21-
console = Console()
21+
console = Console(force_terminal=True, no_color=False)
2222

2323
# Add TRACE level to logging
2424
logging.TRACE = logging.DEBUG - 1
@@ -39,10 +39,11 @@ def evict_null_handler():
3939

4040

4141
class TerminalHandler(RichHandler):
42-
def __init__(self, log_level: str):
42+
def __init__(self, log_level: str, plain: bool):
43+
self.plain = plain
4344
super().__init__(
4445
console=console,
45-
rich_tracebacks=True,
46+
rich_tracebacks=not plain,
4647
tracebacks_suppress=[click],
4748
show_time=False,
4849
show_path=False,
@@ -95,22 +96,25 @@ def deregister_progress_bar(self):
9596
@contextlib.contextmanager
9697
def panel(self, title: str):
9798
global console
98-
self.__panel_lines = []
99-
self.__panel_title = title
100-
self.__panel = Panel("", title=title)
101-
renderables = [self.__panel]
102-
if self.__progress_bar is not None:
103-
renderables.append(self.__progress_bar)
104-
105-
self.__live = Live(Group(*renderables), console=console, vertical_overflow="visible")
106-
try:
107-
self.__live.start()
99+
if self.plain:
108100
yield
109-
except Exception as e:
110-
raise e
111-
finally:
112-
self.__reset_live()
113-
self.console.print("\n")
101+
else:
102+
self.__panel_lines = []
103+
self.__panel_title = title
104+
self.__panel = Panel("", title=title)
105+
renderables = [self.__panel]
106+
if self.__progress_bar is not None:
107+
renderables.append(self.__progress_bar)
108+
109+
self.__live = Live(Group(*renderables), console=console, vertical_overflow="visible")
110+
try:
111+
self.__live.start()
112+
yield
113+
except Exception as e:
114+
raise e
115+
finally:
116+
self.__reset_live()
117+
self.console.print("\n")
114118

115119
def emit(self, record: logging.LogRecord) -> None:
116120
markup = getattr(record, "markup", None)
@@ -126,7 +130,8 @@ def emit(self, record: logging.LogRecord) -> None:
126130
if self.__panel is not None:
127131
self.__emit_panel(record)
128132
else:
129-
setattr(record, "markup", True)
133+
if not self.plain:
134+
setattr(record, "markup", True)
130135
super().emit(record)
131136

132137
def __emit_panel(self, record: logging.LogRecord) -> None:
@@ -143,7 +148,7 @@ def inner(record: logging.LogRecord) -> bool:
143148
return inner
144149

145150

146-
def init_cli_logger(log_level: str) -> logging.Logger:
151+
def init_cli_logger(log_level: str, plain: bool) -> logging.Logger:
147152
global logger
148153

149154
evict_null_handler()
@@ -158,7 +163,7 @@ def init_cli_logger(log_level: str) -> logging.Logger:
158163
except FileNotFoundError:
159164
logger.error(f"Unable to create log file: {LOG_FILE}")
160165

161-
th = TerminalHandler(log_level.upper())
166+
th = TerminalHandler(log_level.upper(), plain)
162167
logger.addHandler(th)
163168
setattr(logger, "panel", th.panel)
164169
setattr(logger, "register_progress_bar", th.register_progress_bar)

0 commit comments

Comments
 (0)