-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_resolve.py
More file actions
619 lines (535 loc) · 20.3 KB
/
Copy path_resolve.py
File metadata and controls
619 lines (535 loc) · 20.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
"""Object introspection utilities — resolve paths, inspect, mutate, call."""
from __future__ import annotations
import ast
import contextlib
import inspect
import io
import re
import types
from collections import Counter
from collections.abc import Mapping, Sequence, Set
def resolve(path: str, namespaces: dict[str, object]) -> object:
"""
Resolve a dotted path with optional indexing against registered namespaces.
Uses eval() — intentionally unrestricted for dev use. Handles:
'app' → namespaces['app']
'app.hobos[0].session' → attribute + index traversal
'len(app.hobos)' → arbitrary expressions
"""
return eval(path, {'__builtins__': __builtins__}, namespaces)
# ────────────────────────────────────────────────────────────────────────
# Public resolvers — all return structured dicts
# ────────────────────────────────────────────────────────────────────────
_PREVIEW_HEAD_LINES = 16
_PREVIEW_TAIL_LINES = 8
_PREVIEW_LINE_MAXLEN = 240
_TOP_PATTERN_LIMIT = 8
_TIMESTAMP_PREFIX_RE = re.compile(r'^\[\d+(?:\.\d+)?\]\s*')
_NUMBER_RE = re.compile(r'-?\d+(?:\.\d+)?')
_SPACE_RE = re.compile(r'\s+')
def _clip_preview_line(line: str, *, maxlen: int = _PREVIEW_LINE_MAXLEN) -> str:
"""Bound one preview line to keep summaries compact."""
if len(line) <= maxlen:
return line
return line[: maxlen - 3] + '...'
def _normalize_pattern_line(line: str) -> str:
"""
Normalize noisy lines (timestamps/numbers/spacing) so repeated shapes collapse.
This keeps semantic structure while removing frame-to-frame numeric jitter.
"""
s = _TIMESTAMP_PREFIX_RE.sub('', line)
s = _NUMBER_RE.sub('#', s)
s = _SPACE_RE.sub(' ', s).strip()
return _clip_preview_line(s, maxlen=160)
def _compact_text(
text: str,
*,
label: str,
max_result_chars: int,
max_result_lines: int,
) -> tuple[str, dict[str, object] | None]:
"""
Return text directly when small; otherwise return a bounded preview + summary.
The summary is optimized for model consumption: compact stats + repeated patterns.
"""
line_count = text.count('\n') + (1 if text else 0)
over_chars = max_result_chars > 0 and len(text) > max_result_chars
over_lines = max_result_lines > 0 and line_count > max_result_lines
if not over_chars and not over_lines:
return text, None
lines = text.splitlines() or ['']
head_lines = [_clip_preview_line(line) for line in lines[:_PREVIEW_HEAD_LINES]]
tail_lines: list[str] = []
if len(lines) > (_PREVIEW_HEAD_LINES + _PREVIEW_TAIL_LINES):
tail_lines = [_clip_preview_line(line) for line in lines[-_PREVIEW_TAIL_LINES:]]
pattern_counts = Counter(_normalize_pattern_line(line) for line in lines if line.strip())
top_patterns = [
{'pattern': pattern, 'count': count}
for pattern, count in pattern_counts.most_common(_TOP_PATTERN_LIMIT)
if count > 1
]
summary: dict[str, object] = {
'kind': 'text',
'label': label,
'chars': len(text),
'lines': line_count,
'truncated': True,
'max_result_chars': max_result_chars,
'max_result_lines': max_result_lines,
'head_lines': len(head_lines),
'tail_lines': len(tail_lines),
}
if top_patterns:
summary['top_patterns'] = top_patterns
preview: list[str] = [
f'<{label} truncated: {line_count} lines, {len(text)} chars>',
'[head]',
]
preview.extend(head_lines)
if tail_lines:
preview.append('[tail]')
preview.extend(tail_lines)
return '\n'.join(preview), summary
def _render_result_value(
value: object,
*,
label: str,
max_result_chars: int,
max_result_lines: int,
) -> tuple[str, dict[str, object] | None]:
"""
Render result/stdout for transport with optional compaction metadata.
Strings are transported as plain text (not repr) to avoid escape-noise.
Other values use repr for readability.
"""
if isinstance(value, str):
return _compact_text(
value,
label=label,
max_result_chars=max_result_chars,
max_result_lines=max_result_lines,
)
try:
rendered = repr(value)
except Exception as e:
rendered = f'<repr error: {e}>'
return _compact_text(
rendered,
label=label,
max_result_chars=max_result_chars,
max_result_lines=max_result_lines,
)
def run_code(
code: str,
namespaces: dict[str, object],
*,
max_result_chars: int = 0,
max_result_lines: int = 0,
) -> dict:
"""
Evaluate expression or execute statement(s). Returns structured result.
Jupyter/IPython semantics: if the code contains multiple statements and
the last one is an expression, exec the setup lines and eval the tail.
This means `import foo; foo.bar()` returns bar()'s value instead of 'OK'.
Scoping: uses a SINGLE merged dict for globals+locals so that variables
defined in exec'd code are visible to functions defined in the same block.
(With separate dicts, `exec(code, globs, locals)` puts names in locals,
but nested functions only search globals — classic exec() gotcha.)
Stdout capture: all exec/eval runs with stdout redirected to a StringIO.
This captures print() output and — critically — prevents help() from
opening an interactive pager (StringIO.isatty() → False → no pager).
Captured output is included in the result dict as 'stdout'.
Result compaction:
Set max_result_chars/max_result_lines > 0 to cap very large textual
outputs. The response includes compact preview text plus summary stats.
"""
# Merged namespace: registered objects + builtins in one dict.
# Copy so we don't pollute the registered namespaces with temporaries.
ns: dict = {'__builtins__': __builtins__, **namespaces}
capture = io.StringIO()
def _result(result: object, mode: str) -> dict:
out = capture.getvalue()
rendered_result, result_summary = _render_result_value(
result,
label='result',
max_result_chars=max_result_chars,
max_result_lines=max_result_lines,
)
d: dict = {
'result': rendered_result,
'type': type(result).__qualname__,
'mode': mode,
}
if result_summary is not None:
d['result_summary'] = result_summary
if out:
rendered_stdout, stdout_summary = _render_result_value(
out,
label='stdout',
max_result_chars=max_result_chars,
max_result_lines=max_result_lines,
)
d['stdout'] = rendered_stdout
if stdout_summary is not None:
d['stdout_summary'] = stdout_summary
return d
with contextlib.redirect_stdout(capture):
# Fast path — single expression
try:
result = eval(code, ns)
return _result(result, 'eval')
except SyntaxError:
pass
# Parse AST to check if last statement is an expression
try:
tree = ast.parse(code)
except SyntaxError as e:
return {'error': f'SyntaxError: {e}'}
# If the last statement is an expression (not assignment, import, etc.),
# exec everything before it, then eval the tail — return its value
if tree.body and isinstance(tree.body[-1], ast.Expr):
if len(tree.body) > 1:
setup = ast.Module(body=tree.body[:-1], type_ignores=[])
exec(compile(setup, '<devtools>', 'exec'), ns)
expr = ast.Expression(body=tree.body[-1].value)
result = eval(compile(expr, '<devtools>', 'eval'), ns)
return _result(result, 'eval')
# Pure statements — exec everything, return OK
exec(code, ns)
out = capture.getvalue()
d: dict = {'result': 'OK', 'type': 'NoneType', 'mode': 'exec'}
if out:
rendered_stdout, stdout_summary = _render_result_value(
out,
label='stdout',
max_result_chars=max_result_chars,
max_result_lines=max_result_lines,
)
d['stdout'] = rendered_stdout
if stdout_summary is not None:
d['stdout_summary'] = stdout_summary
return d
def inspect_object(
path: str,
namespaces: dict[str, object],
*,
max_depth: int = 2,
max_items: int = 50,
max_repr_len: int = 200,
) -> dict:
"""Structured inspection — type, repr, attrs. Returns serialized dict directly."""
obj = resolve(path, namespaces)
tree = _serialize_obj(
obj,
max_depth=max_depth,
max_items=max_items,
max_repr_len=max_repr_len,
)
tree['path'] = path
return tree
def get_source(path: str, namespaces: dict[str, object]) -> dict:
"""Get source code of a function, class, or method at the given path."""
obj = resolve(path, namespaces)
# Unwrap properties, classmethods, staticmethods
if isinstance(obj, property):
obj = obj.fget # type: ignore[assignment]
elif isinstance(obj, (classmethod, staticmethod)):
obj = obj.__func__ # type: ignore[union-attr]
try:
src = inspect.getsource(obj)
fname = inspect.getfile(obj)
lineno = inspect.getsourcelines(obj)[1]
return {
'path': path,
'file': fname,
'line': lineno,
'source': src,
}
except (TypeError, OSError) as e:
return {'path': path, 'error': str(e)}
def list_state(namespaces: dict[str, object]) -> dict:
"""Overview of all registered namespaces."""
entries = []
for name, obj in namespaces.items():
entries.append({
'name': name,
'type': type(obj).__qualname__,
'repr': _safe_repr(obj, maxlen=60),
})
return {'namespaces': entries}
def list_path(
path: str,
namespaces: dict[str, object],
*,
max_items: int = 50,
max_repr_len: int = 200,
) -> dict:
"""Shallow listing — table of contents for an object's contents."""
obj = resolve(path, namespaces)
tname = type(obj).__qualname__
node: dict = {'path': path, 'type': tname}
# ── Mappings ──
if isinstance(obj, Mapping) and not isinstance(obj, (str, bytes)):
node['kind'] = 'mapping'
node['length'] = len(obj) # type: ignore[arg-type]
keys = []
for i, k in enumerate(obj):
if i >= max_items:
node['truncated'] = True
break
keys.append(repr(k))
node['keys'] = keys
return node
# ── Sequences & sets ──
if isinstance(obj, (Sequence, Set)) and not isinstance(obj, (str, bytes)):
node['kind'] = 'sequence'
node['length'] = len(obj) # type: ignore[arg-type]
items = []
for i, item in enumerate(obj):
if i >= max_items:
node['truncated'] = True
break
items.append({
'type': type(item).__qualname__,
'repr': _safe_repr(item, maxlen=max_repr_len),
})
node['items'] = items
return node
# ── General object ──
node['kind'] = 'object'
attrs = _get_public_attrs(obj, max_items=max_items)
node['attrs'] = [
{
'name': n,
'type': type(v).__qualname__,
'repr': _safe_repr(v, maxlen=max_repr_len),
}
for n, v in attrs
]
node['methods'] = _get_public_methods(obj, max_items=max_items)
# Truncation check for attrs + methods combined
all_public = [n for n in dir(obj) if not n.startswith('_')]
if len(all_public) > max_items:
node['truncated'] = True
return node
def repr_path(
path: str,
namespaces: dict[str, object],
*,
max_repr_len: int = 200,
) -> dict:
"""Quick type + repr — fastest tool, minimal overhead."""
obj = resolve(path, namespaces)
return {
'path': path,
'type': type(obj).__qualname__,
'repr': _safe_repr(obj, maxlen=max_repr_len),
}
def call_path(
path: str,
namespaces: dict[str, object],
args: list | None = None,
kwargs: dict | None = None,
*,
max_repr_len: int = 200,
) -> dict:
"""Resolve callable at path, call with args/kwargs, return result."""
a = args or []
kw = kwargs or {}
try:
fn = resolve(path, namespaces)
result = fn(*a, **kw) # type: ignore[operator]
return {
'path': path,
'result_type': type(result).__qualname__,
'result_repr': _safe_repr(result, maxlen=max_repr_len),
'ok': True,
}
except Exception as e:
return {
'path': path,
'error': str(e),
'error_type': type(e).__qualname__,
'ok': False,
}
# Regex for bracket indexing at end of path: foo.bar[0] or foo['key']
_BRACKET_TAIL_RE = re.compile(r'^(.+)\[(.+)\]$')
def set_value(
path: str,
namespaces: dict[str, object],
value_expr: str,
) -> dict:
"""Set a value on an object — supports dot attrs and bracket indexing."""
val = eval(value_expr, {'__builtins__': __builtins__}, namespaces)
try:
# Try bracket indexing first: path like 'obj.data[0]' or 'obj.data["key"]'
m = _BRACKET_TAIL_RE.match(path)
if m:
parent_path, key_expr = m.group(1), m.group(2)
parent = resolve(parent_path, namespaces)
key = eval(key_expr, {'__builtins__': __builtins__}, namespaces)
parent[key] = val # type: ignore[index]
else:
# Dot-separated: split into parent + attr
dot = path.rfind('.')
if dot == -1:
# Top-level name — set directly in namespaces
namespaces[path] = val
else:
parent_path, attr = path[:dot], path[dot + 1:]
parent = resolve(parent_path, namespaces)
setattr(parent, attr, val)
return {
'path': path,
'ok': True,
'new_value_repr': _safe_repr(val),
}
except Exception as e:
return {
'path': path,
'ok': False,
'error': str(e),
'error_type': type(e).__qualname__,
}
# ────────────────────────────────────────────────────────────────────────
# Serialization
# ────────────────────────────────────────────────────────────────────────
def _serialize_obj(
obj: object,
*,
max_depth: int = 2,
max_items: int = 50,
max_repr_len: int = 200,
_depth: int = 0,
_seen: set[int] | None = None,
) -> dict:
"""
Recursive bounded serializer — turns an object into a JSON-safe dict.
Returns dict with keys:
type — qualname of the object's class
repr — truncated repr string
And optionally:
attrs — list of {name, type, repr} for public attributes
items — list of serialized children (sequences/sets)
entries — list of {key, value} for mappings
length — element count for sized containers
truncated — true when items/attrs/entries were capped at max_items
Cycle detection via id() prevents infinite loops on self-referential
structures. Depth gating prevents runaway recursion on deep graphs.
"""
if _seen is None:
_seen = set()
tname = type(obj).__qualname__
rstr = _safe_repr(obj, maxlen=max_repr_len)
# Cycle detection — bail with marker
oid = id(obj)
if oid in _seen:
return {'type': tname, 'repr': '<circular ref>'}
_seen.add(oid)
# Base node — always present
node: dict = {'type': tname, 'repr': rstr}
# Length for sized containers
if isinstance(obj, (Mapping, Sequence, Set)) and not isinstance(obj, (str, bytes)):
try:
node['length'] = len(obj) # type: ignore[arg-type]
except Exception:
pass
# At max depth, return just type+repr — no recursion into children
if _depth >= max_depth:
_seen.discard(oid)
return node
# Recurse kwargs for children
rkw = dict(
max_depth=max_depth,
max_items=max_items,
max_repr_len=max_repr_len,
_depth=_depth + 1,
_seen=_seen,
)
# ── Mappings (dict-like) ──
if isinstance(obj, Mapping) and not isinstance(obj, (str, bytes)):
entries = []
items_iter = iter(obj.items())
for i, (k, v) in enumerate(items_iter):
if i >= max_items:
node['truncated'] = True
break
entries.append({
'key': _safe_repr(k, maxlen=max_repr_len),
'value': _serialize_obj(v, **rkw),
})
if entries:
node['entries'] = entries
# ── Sequences & sets (list, tuple, set, frozenset, ...) ──
elif isinstance(obj, (Sequence, Set)) and not isinstance(obj, (str, bytes)):
items = []
items_iter = iter(obj)
for i, item in enumerate(items_iter):
if i >= max_items:
node['truncated'] = True
break
items.append(_serialize_obj(item, **rkw))
if items:
node['items'] = items
# ── General objects — serialize public attrs ──
else:
attrs_list = _get_public_attrs(obj, max_items=max_items)
if attrs_list:
serialized = []
for name, val in attrs_list:
serialized.append({
'name': name,
'type': type(val).__name__,
'repr': _safe_repr(val, maxlen=max_repr_len),
})
node['attrs'] = serialized
# Flag truncation if _get_public_attrs hit its cap
all_public = [n for n in dir(obj) if not n.startswith('_')]
if len(all_public) > max_items:
node['truncated'] = True
_seen.discard(oid)
return node
# ────────────────────────────────────────────────────────────────────────
# Helpers
# ────────────────────────────────────────────────────────────────────────
def _safe_repr(obj: object, maxlen: int = 200) -> str:
"""Repr with truncation and error safety."""
try:
r = repr(obj)
except Exception as e:
return f'<repr error: {e}>'
if len(r) > maxlen:
return r[:maxlen - 3] + '...'
return r
def _get_public_attrs(obj: object, *, max_items: int = 50) -> list[tuple[str, object]]:
"""Get non-dunder, non-callable attributes with their values, sorted."""
attrs = []
for name in sorted(dir(obj)):
if name.startswith('_'):
continue
try:
val = getattr(obj, name)
except Exception:
continue
# Skip methods/functions — we want state, not API surface
if callable(val) and not isinstance(val, (type, types.ModuleType)):
continue
attrs.append((name, val))
if len(attrs) >= max_items:
break
return attrs
def _get_public_methods(obj: object, *, max_items: int = 50) -> list[str]:
"""Get non-dunder callable names — complement to _get_public_attrs."""
methods = []
for name in sorted(dir(obj)):
if name.startswith('_'):
continue
try:
val = getattr(obj, name)
except Exception:
continue
if callable(val) and not isinstance(val, (type, types.ModuleType)):
methods.append(name)
if len(methods) >= max_items:
break
return methods