-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.py
More file actions
110 lines (91 loc) · 4.22 KB
/
capture.py
File metadata and controls
110 lines (91 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# --------------------------------------------------------------------------------------
# Capture Class - Output Redirection System
# --------------------------------------------------------------------------------------
# PURPOSE: Provides functionality to capture print() output into memory
# This allows saving console output to files without displaying it on screen
#
# USE CASE: When generating reports, we want to:
# 1. Run all report functions
# 2. Capture their output (instead of printing to console)
# 3. Write the captured output to a file
# --------------------------------------------------------------------------------------
class Capture:
def __init__(self):
"""Initialize the capture system with default print function."""
self.print_func = print # Default: use normal Python print
# ----------------------------------------------------------------------------------
# PRINT CAPTURE SYSTEM
# ----------------------------------------------------------------------------------
# These methods redirect print() output to memory for later file writing
def _format_args(self, *args, sep=" ", end="\n"):
"""
Format print arguments into a single string.
Mimics Python's built-in print() behavior:
- Joins arguments with separator (default: space)
- Adds end character (default: newline)
PARAMETERS:
*args: Values to print (like regular print)
sep: Separator between values
end: String appended after last value
RETURNS: Formatted string ready for storage
"""
return sep.join(str(a) for a in args) + ("" if end is None else end)
def _capture_print(self, *args, sep=" ", end="\n", **kwargs):
"""
Redirect print() output into memory instead of console.
This is used as a replacement for Python's print() function when we want
to save output to a file instead of displaying it.
HOW IT WORKS:
1. Format the arguments like normal print()
2. Store the result in collected_print_lines list
3. Strip trailing newline (we'll add them back when writing to file)
"""
line = self._format_args(*args, sep=sep, end=end)
if line.endswith("\n"):
line = line[:-1] # Remove newline for cleaner storage
self.collected_print_lines.append(line)
def set_print_func(self, func):
"""
Switch between normal print and captured print.
PARAMETERS:
func: Callable to use as print function
- Pass 'print' for normal console output
- Pass '_capture_print' for memory capture
"""
if not callable(func):
raise ValueError("print function must be callable")
self.print_func = func
self.capture_enabled = False
def use_capture(self, enable=True, clear_existing=False):
"""
Enable or disable output capturing.
WHEN ENABLED:
- All calls to self.print_func() will store output in memory
- Nothing appears on the console
WHEN DISABLED:
- self.print_func() works like normal print()
- Output appears on console
PARAMETERS:
enable: True = capture output, False = print normally
clear_existing: If True, clears previously captured lines
"""
if enable:
self.print_func = self._capture_print
self.capture_enabled = True
if clear_existing:
self.collected_print_lines = []
else:
self.print_func = print
self.capture_enabled = False
def get_captured(self):
"""
Get all captured print lines.
RETURNS: Copy of the list containing all captured output lines
"""
return list(self.collected_print_lines)
def clear_captured(self):
"""
Clear the buffer of captured print lines.
Useful for starting fresh capture or freeing memory after saving to file.
"""
self.collected_print_lines = []