kenenet 0.4.7__py3-none-any.whl → 0.4.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kenenet/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- import inspect, sys, zhmiscellany, keyboard, mss, time, linecache, types, os, random, pyperclip
1
+ import inspect, sys, zhmiscellany, keyboard, mss, time, linecache, types, os, random, pyperclip, inspect, datetime, atexit
2
2
  import numpy as np
3
3
  from PIL import Image
4
4
  global timings, ospid
@@ -49,66 +49,116 @@ def timer(clock=1):
49
49
  timings[clock] = time.time()
50
50
 
51
51
 
52
- def make_trace_function(ignore_special_vars=True, ignore_functions=True, ignore_classes=True, ignore_modules=True, ignore_file_path=True):
53
- special_vars = {
54
- "__name__", "__doc__", "__package__", "__loader__",
55
- "__spec__", "__annotations__", "__file__", "__cached__"
56
- }
52
+ class Config:
53
+ EXCLUDED_NAMES = {'Config', 'VariableTracker', 'track_variables', 'stop_tracking',
54
+ 'track_frame', 'sys', 'inspect', 'types', 'datetime',
55
+ 'self', 'cls', 'args', 'kwargs', '__class__'}
56
+ EXCLUDED_FILES = {'<string>', '<frozen importlib', 'importlib', 'abc.py', 'typing.py', '_collections_abc.py'}
57
+ SHOW_TIMESTAMPS = True
58
+ EXCLUDE_INTERNALS = True
59
+
60
+
61
+ class VariableTracker:
62
+ _instance = None
63
+
64
+ @classmethod
65
+ def get_instance(cls):
66
+ if cls._instance is None: cls._instance = VariableTracker()
67
+ return cls._instance
68
+
69
+ def __init__(self):
70
+ self.active = False
71
+ self.tracked_module = None
72
+ self.frame_locals = {}
73
+ self.global_vars = {}
74
+
75
+ def fmt(self, v):
76
+ try:
77
+ return repr(v)
78
+ except:
79
+ return f"<{type(v).__name__} object>"
80
+
81
+ def print_change(self, name, old, new, scope="Global"):
82
+ ts = f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')[:-3]}] " if Config.SHOW_TIMESTAMPS else ""
83
+ print(f"{ts}{scope} '{name}' changed from {self.fmt(old)} to {self.fmt(new)}")
57
84
 
58
- def trace_lines(frame, event, arg):
59
- if event == 'line':
60
- filename = frame.f_code.co_filename
61
- lineno = frame.f_lineno
62
- code_line = linecache.getline(filename, lineno).strip()
63
-
64
- # Prevent spamming by ensuring we aren't tracing internal Python locks or infinite loops
65
- if not code_line:
66
- return trace_lines
67
-
68
- header = f"Executing line {lineno}:" if ignore_file_path else f"Executing {filename}:{lineno}:"
69
- quick_print("=" * 60)
70
- quick_print(header, lineno)
71
- quick_print(f" {code_line}", lineno)
72
- quick_print("-" * 60, lineno)
73
- quick_print("Local Variables:", lineno)
74
-
75
- for var, value in frame.f_locals.items():
76
- if ignore_special_vars and var in special_vars:
77
- continue
78
- if ignore_modules and isinstance(value, types.ModuleType):
79
- continue
80
- if ignore_functions and isinstance(value, types.FunctionType):
81
- continue
82
- if ignore_classes and isinstance(value, type):
83
- continue
84
- try:
85
- quick_print(f" {var} = {repr(value)}", lineno)
86
- except (AttributeError, TypeError, Exception):
87
- quick_print(f" {var} = [unreadable]", lineno)
88
-
89
- quick_print("=" * 60, lineno)
90
-
91
- return trace_lines
85
+ def _should_track_name(self, n):
86
+ return not (n.startswith('_') and n not in ('__name__', '__file__')) and n not in Config.EXCLUDED_NAMES
92
87
 
93
- return trace_lines
88
+ def _should_track_frame(self, f):
89
+ if not Config.EXCLUDE_INTERNALS:
90
+ return True
91
+ fn = f.f_code.co_filename
92
+ if any(e in fn for e in Config.EXCLUDED_FILES):
93
+ return False
94
+ # Exclude known internal shutdown and list comprehension functions.
95
+ if f.f_code.co_name in ('tracked_setattr', 'fmt', 'print_change', 'track_globals', 'get_instance',
96
+ '_maintain_shutdown_locks', '_shutdown', '_stop', '<listcomp>'):
97
+ return False
98
+ return True
99
+
100
+ def start_tracking(self, mod_name):
101
+ if self.active: return
102
+ self.tracked_module = sys.modules[mod_name]
103
+ for name, value in self.tracked_module.__dict__.items():
104
+ if self._should_track_name(name):
105
+ self.global_vars[name] = value
106
+ sys.settrace(track_frame)
107
+ self.active = True
108
+ print(f"Variable tracking started at {datetime.datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
109
+
110
+ def stop_tracking(self):
111
+ if not self.active: return
112
+ sys.settrace(None)
113
+ self.frame_locals.clear();
114
+ self.global_vars.clear();
115
+ self.active = False
116
+ print(f"Variable tracking stopped at {datetime.datetime.now().strftime('%H:%M:%S.%f')[:-3]}")
117
+
118
+
119
+ def track_frame(frame, event, arg):
120
+ tracker = VariableTracker.get_instance()
121
+ if not tracker.active or not tracker._should_track_frame(frame):
122
+ return track_frame
123
+ if event != 'line':
124
+ return track_frame
125
+ fid, is_mod = id(frame), frame.f_code.co_name == '<module>'
126
+ scope = "Global" if is_mod else f"Local in '{frame.f_code.co_name}'"
127
+ curr = {n: v for n, v in frame.f_locals.items() if tracker._should_track_name(n)}
128
+ if is_mod:
129
+ for n, v in curr.items():
130
+ if n not in tracker.global_vars:
131
+ tracker.print_change(n, None, v, scope);
132
+ tracker.global_vars[n] = v
133
+ elif tracker.global_vars[n] != v:
134
+ tracker.print_change(n, tracker.global_vars[n], v, scope);
135
+ tracker.global_vars[n] = v
136
+ else:
137
+ if fid in tracker.frame_locals:
138
+ for n, v in curr.items():
139
+ if n not in tracker.frame_locals[fid]:
140
+ tracker.print_change(n, None, v, scope)
141
+ elif tracker.frame_locals[fid][n] != v:
142
+ tracker.print_change(n, tracker.frame_locals[fid][n], v, scope)
143
+ else:
144
+ for n, v in curr.items():
145
+ tracker.print_change(n, None, v, scope)
146
+ tracker.frame_locals[fid] = curr.copy()
147
+ if event == 'return' and not is_mod and fid in tracker.frame_locals:
148
+ del tracker.frame_locals[fid]
149
+ return track_frame
94
150
 
95
151
 
96
- def activate_tracing(ignore_special_vars=True, ignore_functions=True, ignore_classes=True, ignore_modules=True, ignore_file_path=True):
97
- trace_func = make_trace_function(
98
- ignore_special_vars=ignore_special_vars,
99
- ignore_functions=ignore_functions,
100
- ignore_classes=ignore_classes,
101
- ignore_modules=ignore_modules,
102
- ignore_file_path=ignore_file_path
103
- )
104
- sys.settrace(trace_func)
105
- sys._getframe().f_trace = trace_func
106
- quick_print("Tracing activated.")
152
+ def track_variables():
153
+ cf = inspect.currentframe().f_back
154
+ mod = cf.f_globals['__name__']
155
+ VariableTracker.get_instance().start_tracking(mod)
156
+ cf.f_trace = track_frame
157
+ atexit.register(stop_tracking)
107
158
 
108
159
 
109
- def deactivate_tracing():
110
- sys.settrace(None)
111
- quick_print("Tracing deactivated.")
160
+ def stop_tracking():
161
+ VariableTracker.get_instance().stop_tracking()
112
162
 
113
163
  def pp(msg='caca', subdir=None, pps=3):
114
164
  import os, subprocess
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: kenenet
3
- Version: 0.4.7
3
+ Version: 0.4.9
4
4
  Summary: dude what the fuck even is this package
5
5
  Home-page: https://www.youtube.com/@KiddyKene
6
6
  Author: kiddykene
@@ -0,0 +1,5 @@
1
+ kenenet/__init__.py,sha256=lW5HqrpZo8FAxzi1WgSAr4Bo-aweG0S0H-RNQ2OlVn8,8644
2
+ kenenet-0.4.9.dist-info/METADATA,sha256=jmh9EbdAn04ICRraoI8tOibDzbuencrMX2vzF0wnxh0,633
3
+ kenenet-0.4.9.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
4
+ kenenet-0.4.9.dist-info/top_level.txt,sha256=gUsWXLrM0jF4b4nbYJZdksdFewIx_F3xOF-zER8fMuQ,8
5
+ kenenet-0.4.9.dist-info/RECORD,,
@@ -1,5 +0,0 @@
1
- kenenet/__init__.py,sha256=vJt7E0HsbvWtXvt8dEKjYGzQI0EhhPCzaNz6yR9wcQQ,6660
2
- kenenet-0.4.7.dist-info/METADATA,sha256=tdLr-XGfLjvoe4EoPZ8nIyz2Ahu0bycv7jDNwuPb3k0,633
3
- kenenet-0.4.7.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
4
- kenenet-0.4.7.dist-info/top_level.txt,sha256=gUsWXLrM0jF4b4nbYJZdksdFewIx_F3xOF-zER8fMuQ,8
5
- kenenet-0.4.7.dist-info/RECORD,,