wrapture 1.0.0.dev2__tar.gz → 1.0.0.dev4__tar.gz

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.
Files changed (74) hide show
  1. {wrapture-1.0.0.dev2/src/wrapture.egg-info → wrapture-1.0.0.dev4}/PKG-INFO +19 -21
  2. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/README.md +16 -18
  3. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/pyproject.toml +18 -3
  4. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/__init__.py +98 -2
  5. wrapture-1.0.0.dev4/src/wrapture/__main__.py +148 -0
  6. wrapture-1.0.0.dev4/src/wrapture/asgi.py +620 -0
  7. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/attributes.py +53 -9
  8. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/bindings.py +475 -60
  9. wrapture-1.0.0.dev4/src/wrapture/bootstrap.py +86 -0
  10. wrapture-1.0.0.dev4/src/wrapture/collectors.py +377 -0
  11. wrapture-1.0.0.dev4/src/wrapture/config.py +1413 -0
  12. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/events.py +61 -11
  13. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/exceptions.py +18 -0
  14. wrapture-1.0.0.dev4/src/wrapture/export.py +298 -0
  15. wrapture-1.0.0.dev4/src/wrapture/lifecycle.py +84 -0
  16. wrapture-1.0.0.dev4/src/wrapture/observed.py +422 -0
  17. wrapture-1.0.0.dev4/src/wrapture/outputs.py +219 -0
  18. wrapture-1.0.0.dev4/src/wrapture/scheduler.py +297 -0
  19. wrapture-1.0.0.dev4/src/wrapture/sinks.py +1172 -0
  20. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/stacks.py +49 -7
  21. wrapture-1.0.0.dev4/src/wrapture/timeline.py +555 -0
  22. wrapture-1.0.0.dev4/src/wrapture/tools/__init__.py +8 -0
  23. wrapture-1.0.0.dev4/src/wrapture/tools/__main__.py +51 -0
  24. wrapture-1.0.0.dev4/src/wrapture/tools/convert.py +150 -0
  25. wrapture-1.0.0.dev4/src/wrapture/windows.py +966 -0
  26. wrapture-1.0.0.dev4/src/wrapture/wsgi.py +637 -0
  27. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4/src/wrapture.egg-info}/PKG-INFO +19 -21
  28. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture.egg-info/SOURCES.txt +33 -1
  29. wrapture-1.0.0.dev4/src/wrapture.egg-info/entry_points.txt +2 -0
  30. wrapture-1.0.0.dev4/tests/test_asgi.py +728 -0
  31. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_attribute_events.py +14 -2
  32. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_bindings.py +173 -1
  33. wrapture-1.0.0.dev4/tests/test_bootstrap.py +147 -0
  34. wrapture-1.0.0.dev4/tests/test_collectors.py +214 -0
  35. wrapture-1.0.0.dev4/tests/test_config.py +1309 -0
  36. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_events.py +8 -9
  37. wrapture-1.0.0.dev4/tests/test_export.py +253 -0
  38. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_generator_events.py +9 -4
  39. wrapture-1.0.0.dev4/tests/test_jsonlines.py +406 -0
  40. wrapture-1.0.0.dev4/tests/test_observed.py +264 -0
  41. wrapture-1.0.0.dev4/tests/test_outputs.py +196 -0
  42. wrapture-1.0.0.dev4/tests/test_predicates.py +231 -0
  43. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_recording.py +44 -7
  44. wrapture-1.0.0.dev4/tests/test_runner.py +213 -0
  45. wrapture-1.0.0.dev4/tests/test_sink_library.py +360 -0
  46. wrapture-1.0.0.dev4/tests/test_sinks.py +603 -0
  47. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_stacks.py +56 -0
  48. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_threads.py +67 -2
  49. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_timeline.py +155 -31
  50. wrapture-1.0.0.dev4/tests/test_tools.py +235 -0
  51. wrapture-1.0.0.dev4/tests/test_windows.py +855 -0
  52. wrapture-1.0.0.dev4/tests/test_wsgi.py +668 -0
  53. wrapture-1.0.0.dev2/src/wrapture/timeline.py +0 -364
  54. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/LICENSE +0 -0
  55. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/setup.cfg +0 -0
  56. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/behaviours.py +0 -0
  57. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/capture.py +0 -0
  58. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/eventlogs.py +0 -0
  59. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/iterators.py +0 -0
  60. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/py.typed +0 -0
  61. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture/pytest_plugin.py +0 -0
  62. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture.egg-info/dependency_links.txt +0 -0
  63. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture.egg-info/not-zip-safe +0 -0
  64. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture.egg-info/requires.txt +0 -0
  65. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/src/wrapture.egg-info/top_level.txt +0 -0
  66. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_attributes.py +0 -0
  67. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_behaviours.py +0 -0
  68. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_capture.py +0 -0
  69. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_eventlogs.py +0 -0
  70. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_iterators.py +0 -0
  71. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_pytest_plugin.py +0 -0
  72. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_targets.py +0 -0
  73. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_testing_patterns.py +0 -0
  74. {wrapture-1.0.0.dev2 → wrapture-1.0.0.dev4}/tests/test_version.py +0 -0
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wrapture
3
- Version: 1.0.0.dev2
4
- Summary: Library for attaching bindings to arbitrary Python call sites for monkey patching, testing, tracing and profiling.
3
+ Version: 1.0.0.dev4
4
+ Summary: Monkey patch, test, and trace Python by attaching bindings to call sites, without modifying the code being observed. Built on wrapt.
5
5
  Author-email: Graham Dumpleton <Graham.Dumpleton@gmail.com>
6
6
  License-Expression: BSD-2-Clause
7
7
  Project-URL: Homepage, https://github.com/GrahamDumpleton/wrapture
8
8
  Project-URL: Documentation, https://wrapture.readthedocs.io
9
9
  Project-URL: Bug Tracker, https://github.com/GrahamDumpleton/wrapture/issues/
10
- Keywords: wrapper,monkey patching,tracing,testing,profiling
10
+ Keywords: wrapper,monkey patching,tracing,testing
11
11
  Classifier: Development Status :: 2 - Pre-Alpha
12
12
  Classifier: Programming Language :: Python :: 3
13
13
  Classifier: Programming Language :: Python :: 3.12
@@ -44,9 +44,9 @@ It is a sibling project to [wrapt](https://github.com/GrahamDumpleton/wrapt)
44
44
  and [autowrapt](https://github.com/GrahamDumpleton/autowrapt), building on the
45
45
  safe monkey-patching machinery wrapt provides.
46
46
 
47
- > **Status: early development.** The monkey patching and unit testing layers
48
- > are implemented; the tracing and profiling layers are designed but not
49
- > built. Development previews are published to PyPI; the API may still
47
+ > **Status: early development.** The monkey patching, unit testing and
48
+ > ad-hoc tracing layers are implemented, including WSGI and ASGI request
49
+ > tracing. Development previews are published to PyPI; the API may still
50
50
  > shift before 1.0.0.
51
51
 
52
52
  ## Installation
@@ -98,7 +98,7 @@ failure, or transform one argument while the real code keeps running.
98
98
 
99
99
  ## What it does
100
100
 
101
- One mechanism, four uses, in increasing order of machinery:
101
+ One mechanism, three uses, in increasing order of machinery:
102
102
 
103
103
  1. **Monkey patching.** A clean lifecycle and behaviour vocabulary over
104
104
  wrapt's `wrap_object()`. Point at a method by name and stub it, fail it,
@@ -119,16 +119,17 @@ One mechanism, four uses, in increasing order of machinery:
119
119
  3. **Ad-hoc tracing.** Attach bindings to a running application, including
120
120
  one you cannot modify or redeploy, and emit a structured, nested trace to
121
121
  process or chart elsewhere. Name a handful of methods and a call tree
122
- appears; no code changes required.
123
-
124
- 4. **Targeted profiling.** Use a binding as a *scope* within which CPython's
125
- own profiling machinery is active, so you can profile one subsystem of a
126
- live process instead of everything.
127
-
128
- The distinction that matters: most tracing and profiling tools either need
129
- the code to have been written with them in mind, or can only be switched on
130
- for the whole program at once. wrapture needs neither: you point at a method
131
- by name and a trace appears.
122
+ appears; no code changes required: with a `wrapture.toml` naming the
123
+ methods and a sink, `python -m wrapture manage.py runserver` traces the
124
+ application untouched. With [autowrapt](https://github.com/GrahamDumpleton/autowrapt)
125
+ installed, not even the launcher is needed: `AUTOWRAPT_BOOTSTRAP=wrapture`
126
+ in the environment applies the same config at interpreter startup, so the
127
+ program starts with plain `python`.
128
+
129
+ The distinction that matters: most tracing tools either need the code to
130
+ have been written with them in mind, or can only be switched on for the
131
+ whole program at once. wrapture needs neither: you point at a method by
132
+ name and a trace appears.
132
133
 
133
134
  ## Why
134
135
 
@@ -142,8 +143,7 @@ nested trace, assert on it or export it, in tests or in production":
142
143
  `InMemorySpanExporter`) require the code to already be instrumented.
143
144
  - `sys.settrace` tools (`hunter`, `snoop`) give a firehose with no assertion
144
145
  API.
145
- - `cProfile` cannot scope to a subsystem in a live process, and APM agents
146
- are all-or-nothing products rather than a toolkit.
146
+ - APM agents are all-or-nothing products rather than a toolkit.
147
147
 
148
148
  wrapture fills that gap: a targeted call tree with normalized arguments and
149
149
  return values, produced by naming the methods you care about, usable as a
@@ -153,8 +153,6 @@ testing assertion library, a tracing tool, or both at once.
153
153
 
154
154
  - **Not a replacement for `unittest.mock`.** It complements mocking where
155
155
  code has seams; it exists for the code that doesn't.
156
- - **Not a sampling profiler.** `py-spy` and `austin` do that better and
157
- without distortion.
158
156
  - **Not a production APM.** It is a toolkit that APM-like things could be
159
157
  built on.
160
158
  - **Not an OpenTelemetry competitor.** It should emit to OTel, not replace
@@ -13,9 +13,9 @@ It is a sibling project to [wrapt](https://github.com/GrahamDumpleton/wrapt)
13
13
  and [autowrapt](https://github.com/GrahamDumpleton/autowrapt), building on the
14
14
  safe monkey-patching machinery wrapt provides.
15
15
 
16
- > **Status: early development.** The monkey patching and unit testing layers
17
- > are implemented; the tracing and profiling layers are designed but not
18
- > built. Development previews are published to PyPI; the API may still
16
+ > **Status: early development.** The monkey patching, unit testing and
17
+ > ad-hoc tracing layers are implemented, including WSGI and ASGI request
18
+ > tracing. Development previews are published to PyPI; the API may still
19
19
  > shift before 1.0.0.
20
20
 
21
21
  ## Installation
@@ -67,7 +67,7 @@ failure, or transform one argument while the real code keeps running.
67
67
 
68
68
  ## What it does
69
69
 
70
- One mechanism, four uses, in increasing order of machinery:
70
+ One mechanism, three uses, in increasing order of machinery:
71
71
 
72
72
  1. **Monkey patching.** A clean lifecycle and behaviour vocabulary over
73
73
  wrapt's `wrap_object()`. Point at a method by name and stub it, fail it,
@@ -88,16 +88,17 @@ One mechanism, four uses, in increasing order of machinery:
88
88
  3. **Ad-hoc tracing.** Attach bindings to a running application, including
89
89
  one you cannot modify or redeploy, and emit a structured, nested trace to
90
90
  process or chart elsewhere. Name a handful of methods and a call tree
91
- appears; no code changes required.
92
-
93
- 4. **Targeted profiling.** Use a binding as a *scope* within which CPython's
94
- own profiling machinery is active, so you can profile one subsystem of a
95
- live process instead of everything.
96
-
97
- The distinction that matters: most tracing and profiling tools either need
98
- the code to have been written with them in mind, or can only be switched on
99
- for the whole program at once. wrapture needs neither: you point at a method
100
- by name and a trace appears.
91
+ appears; no code changes required: with a `wrapture.toml` naming the
92
+ methods and a sink, `python -m wrapture manage.py runserver` traces the
93
+ application untouched. With [autowrapt](https://github.com/GrahamDumpleton/autowrapt)
94
+ installed, not even the launcher is needed: `AUTOWRAPT_BOOTSTRAP=wrapture`
95
+ in the environment applies the same config at interpreter startup, so the
96
+ program starts with plain `python`.
97
+
98
+ The distinction that matters: most tracing tools either need the code to
99
+ have been written with them in mind, or can only be switched on for the
100
+ whole program at once. wrapture needs neither: you point at a method by
101
+ name and a trace appears.
101
102
 
102
103
  ## Why
103
104
 
@@ -111,8 +112,7 @@ nested trace, assert on it or export it, in tests or in production":
111
112
  `InMemorySpanExporter`) require the code to already be instrumented.
112
113
  - `sys.settrace` tools (`hunter`, `snoop`) give a firehose with no assertion
113
114
  API.
114
- - `cProfile` cannot scope to a subsystem in a live process, and APM agents
115
- are all-or-nothing products rather than a toolkit.
115
+ - APM agents are all-or-nothing products rather than a toolkit.
116
116
 
117
117
  wrapture fills that gap: a targeted call tree with normalized arguments and
118
118
  return values, produced by naming the methods you care about, usable as a
@@ -122,8 +122,6 @@ testing assertion library, a tracing tool, or both at once.
122
122
 
123
123
  - **Not a replacement for `unittest.mock`.** It complements mocking where
124
124
  code has seams; it exists for the code that doesn't.
125
- - **Not a sampling profiler.** `py-spy` and `austin` do that better and
126
- without distortion.
127
125
  - **Not a production APM.** It is a toolkit that APM-like things could be
128
126
  built on.
129
127
  - **Not an OpenTelemetry competitor.** It should emit to OTel, not replace
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
  [project]
8
8
  name = "wrapture"
9
9
  dynamic = ["version"]
10
- description = "Library for attaching bindings to arbitrary Python call sites for monkey patching, testing, tracing and profiling."
10
+ description = "Monkey patch, test, and trace Python by attaching bindings to call sites, without modifying the code being observed. Built on wrapt."
11
11
  readme = "README.md"
12
12
  license = "BSD-2-Clause"
13
13
  license-files = ["LICENSE"]
@@ -15,7 +15,7 @@ authors = [
15
15
  {name = "Graham Dumpleton", email = "Graham.Dumpleton@gmail.com"}
16
16
  ]
17
17
  requires-python = ">=3.12"
18
- keywords = ["wrapper", "monkey patching", "tracing", "testing", "profiling"]
18
+ keywords = ["wrapper", "monkey patching", "tracing", "testing"]
19
19
  classifiers = [
20
20
  "Development Status :: 2 - Pre-Alpha",
21
21
  "Programming Language :: Python :: 3",
@@ -33,6 +33,18 @@ Homepage = "https://github.com/GrahamDumpleton/wrapture"
33
33
  Documentation = "https://wrapture.readthedocs.io"
34
34
  "Bug Tracker" = "https://github.com/GrahamDumpleton/wrapture/issues/"
35
35
 
36
+ # The zero-code injection trigger, in the shape autowrapt consumes:
37
+ # AUTOWRAPT_BOOTSTRAP=wrapture names this entry point group, which
38
+ # autowrapt hands to wrapt.discover_post_import_hooks() at interpreter
39
+ # startup. Each entry maps a trigger module to a callback; hooking os,
40
+ # which is always already imported by then, makes the callback fire
41
+ # immediately at startup. Inert metadata unless autowrapt is installed
42
+ # and the environment variable is set; wrapture itself has no
43
+ # dependency on autowrapt and never imports it.
44
+
45
+ [project.entry-points.wrapture]
46
+ os = "wrapture.bootstrap:bootstrap"
47
+
36
48
  [project.optional-dependencies]
37
49
  dev = [
38
50
  "mypy",
@@ -49,7 +61,7 @@ docs = [
49
61
  [tool.setuptools]
50
62
  package-dir = {"" = "src"}
51
63
  zip-safe = false
52
- packages = ["wrapture"]
64
+ packages = ["wrapture", "wrapture.tools"]
53
65
 
54
66
  [tool.setuptools.package-data]
55
67
  wrapture = ["py.typed"]
@@ -73,6 +85,9 @@ dev = [
73
85
  # getting started page run as doctests and cannot rot.
74
86
  testpaths = ["tests", "docs"]
75
87
  addopts = "--doctest-glob=*.md"
88
+ # ELLIPSIS lets a transcript elide values that vary between runs, such
89
+ # as the elapsed time on a printer's closing line.
90
+ doctest_optionflags = ["ELLIPSIS"]
76
91
 
77
92
  [tool.mypy]
78
93
  python_version = "3.12"
@@ -1,7 +1,7 @@
1
1
  """
2
2
  Wrapture is a library for attaching bindings to arbitrary Python call sites,
3
3
  without modifying the code being observed, for use in monkey patching,
4
- testing, tracing and profiling.
4
+ testing and tracing.
5
5
  """
6
6
 
7
7
 
@@ -17,11 +17,14 @@ def _format_version(parts: tuple[str, ...]) -> str:
17
17
  )
18
18
 
19
19
 
20
- __version_info__ = ("1", "0", "0", "dev2")
20
+ __version_info__ = ("1", "0", "0", "dev4")
21
21
  __version__ = _format_version(__version_info__)
22
22
 
23
23
  from wrapt import MISSING
24
24
 
25
+ from .asgi import (
26
+ ASGIMiddleware,
27
+ )
25
28
  from .behaviours import (
26
29
  CallBehaviour,
27
30
  DeleteBehaviour,
@@ -33,10 +36,24 @@ from .bindings import (
33
36
  BindingGroup,
34
37
  binding,
35
38
  bindings,
39
+ discover,
36
40
  )
37
41
  from .capture import (
38
42
  redact,
39
43
  )
44
+ from .collectors import (
45
+ Aggregate,
46
+ Counter,
47
+ PathStats,
48
+ )
49
+ from .config import (
50
+ AppliedConfig,
51
+ Config,
52
+ ObserveEntry,
53
+ SetupEntry,
54
+ find_config,
55
+ load_config,
56
+ )
40
57
  from .eventlogs import (
41
58
  EventLog,
42
59
  )
@@ -45,13 +62,22 @@ from .events import (
45
62
  )
46
63
  from .exceptions import (
47
64
  AlreadyAppliedError,
65
+ ConfigError,
66
+ ConfigWarning,
48
67
  DeferredTargetError,
49
68
  ExpectationNotMetError,
50
69
  NeverAppliedError,
51
70
  NotImplementedYetError,
52
71
  RecordingGapWarning,
72
+ SinkErrorWarning,
53
73
  WrongModeError,
54
74
  )
75
+ from .export import (
76
+ canonical,
77
+ chrome_trace,
78
+ load_events,
79
+ mermaid,
80
+ )
55
81
  from .iterators import (
56
82
  IteratorAbandonBehaviour,
57
83
  IteratorErrorBehaviour,
@@ -60,8 +86,28 @@ from .iterators import (
60
86
  IteratorProxy,
61
87
  iterator,
62
88
  )
89
+ from .lifecycle import (
90
+ shutdown,
91
+ )
92
+ from .observed import (
93
+ ObservedCallable,
94
+ observed,
95
+ )
96
+ from .sinks import (
97
+ Depth,
98
+ Fanout,
99
+ Filter,
100
+ JSONLines,
101
+ Printer,
102
+ Sample,
103
+ Sink,
104
+ add_sink,
105
+ flush_sinks,
106
+ remove_sink,
107
+ )
63
108
  from .stacks import (
64
109
  StackFrame,
110
+ clear_stacks,
65
111
  stack_frames,
66
112
  )
67
113
  from .timeline import (
@@ -69,40 +115,90 @@ from .timeline import (
69
115
  Timeline,
70
116
  annotate,
71
117
  current_event,
118
+ propagate,
72
119
  timeline,
73
120
  )
121
+ from .windows import (
122
+ Collector,
123
+ Report,
124
+ Run,
125
+ Window,
126
+ window,
127
+ )
128
+ from .wsgi import (
129
+ WSGIMiddleware,
130
+ )
74
131
 
75
132
  __all__ = [
76
133
  "MISSING",
134
+ "ASGIMiddleware",
135
+ "Aggregate",
77
136
  "AlreadyAppliedError",
137
+ "AppliedConfig",
78
138
  "Binding",
139
+ "Collector",
140
+ "Report",
141
+ "Run",
142
+ "Window",
79
143
  "BindingGroup",
80
144
  "CallBehaviour",
145
+ "Config",
146
+ "ConfigError",
147
+ "ConfigWarning",
148
+ "Counter",
81
149
  "DeferredTargetError",
82
150
  "DeleteBehaviour",
151
+ "Depth",
83
152
  "Event",
84
153
  "EventLog",
85
154
  "ExpectationNotMetError",
155
+ "Fanout",
156
+ "Filter",
86
157
  "GetBehaviour",
87
158
  "IteratorAbandonBehaviour",
88
159
  "IteratorErrorBehaviour",
89
160
  "IteratorFinishBehaviour",
90
161
  "IteratorItemBehaviour",
91
162
  "IteratorProxy",
163
+ "JSONLines",
92
164
  "NeverAppliedError",
93
165
  "NotImplementedYetError",
166
+ "ObserveEntry",
167
+ "ObservedCallable",
168
+ "PathStats",
169
+ "Printer",
94
170
  "RecordingGapWarning",
171
+ "Sample",
95
172
  "SetBehaviour",
173
+ "SetupEntry",
174
+ "Sink",
175
+ "SinkErrorWarning",
96
176
  "StackFrame",
97
177
  "Tape",
98
178
  "Timeline",
179
+ "WSGIMiddleware",
99
180
  "WrongModeError",
181
+ "add_sink",
100
182
  "annotate",
101
183
  "binding",
102
184
  "bindings",
185
+ "canonical",
186
+ "chrome_trace",
187
+ "clear_stacks",
103
188
  "current_event",
189
+ "discover",
190
+ "find_config",
191
+ "flush_sinks",
104
192
  "iterator",
193
+ "load_config",
194
+ "load_events",
195
+ "mermaid",
196
+ "observed",
197
+ "propagate",
105
198
  "redact",
199
+ "remove_sink",
200
+ "shutdown",
106
201
  "stack_frames",
107
202
  "timeline",
203
+ "window",
108
204
  ]
@@ -0,0 +1,148 @@
1
+ """Run a Python program with a wrapture config applied first.
2
+
3
+ python -m wrapture [--config PATH] (-m MODULE | SCRIPT) [ARGS...]
4
+
5
+ The config is resolved, loaded and applied before the target runs, so
6
+ patches land before the target module imports anything: the same
7
+ ordering guarantee the injection path gives. The target then runs as
8
+ __main__ with sys.argv rebuilt to the target and its arguments,
9
+ exactly as python itself would have run it.
10
+
11
+ This module is the runner's entry point only, the same private -m
12
+ convention as pdb, cProfile and coverage: run it, do not import it.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import os
18
+ import runpy
19
+ import sys
20
+ from collections.abc import Sequence
21
+ from dataclasses import dataclass
22
+ from typing import NoReturn
23
+
24
+ from .config import find_config, load_config
25
+ from .exceptions import ConfigError
26
+
27
+ _USAGE = """\
28
+ usage: python -m wrapture [--config PATH] (-m MODULE | SCRIPT) [ARGS...]
29
+
30
+ Apply a wrapture config, then run the target as __main__ with ARGS as
31
+ its arguments. With --config the named TOML file is used; without it
32
+ the standard precedence chain locates one: a path in WRAPTURE_CONFIG,
33
+ wrapture.toml in the current directory, then a [tool.wrapture] table
34
+ in pyproject.toml. Everything after the target belongs to the target,
35
+ so wrapture's own options must come before it.
36
+
37
+ Trace files a run produces can be rendered in other formats with
38
+ python -m wrapture.tools convert.
39
+ """
40
+
41
+
42
+ @dataclass(frozen=True)
43
+ class _Invocation:
44
+ config: str | None
45
+ module: str | None
46
+ script: str | None
47
+ arguments: tuple[str, ...]
48
+
49
+
50
+ def _usage_error(message: str) -> NoReturn:
51
+ print(f"wrapture: {message}", file=sys.stderr)
52
+ print(_USAGE, file=sys.stderr, end="")
53
+ raise SystemExit(2)
54
+
55
+
56
+ def _parse(argv: Sequence[str]) -> _Invocation:
57
+ """Split the command line into wrapture's own options and the
58
+ target with its arguments.
59
+
60
+ Parsing stops at the first target: everything after -m MODULE or
61
+ the script path is the target's own, however option-like it
62
+ looks, matching how python itself treats a command line.
63
+ """
64
+
65
+ config: str | None = None
66
+ pending = list(argv)
67
+
68
+ while pending:
69
+ argument = pending[0]
70
+
71
+ if argument in ("-h", "--help"):
72
+ print(_USAGE, end="")
73
+ raise SystemExit(0)
74
+
75
+ if argument == "--config":
76
+ if len(pending) < 2:
77
+ _usage_error("--config requires a path")
78
+ config = pending[1]
79
+ del pending[:2]
80
+ continue
81
+
82
+ if argument.startswith("--config="):
83
+ config = argument.partition("=")[2]
84
+ if not config:
85
+ _usage_error("--config requires a path")
86
+ del pending[0]
87
+ continue
88
+
89
+ if argument == "-m":
90
+ if len(pending) < 2:
91
+ _usage_error("-m requires a module name")
92
+ return _Invocation(config, pending[1], None, tuple(pending[2:]))
93
+
94
+ if argument.startswith("-"):
95
+ _usage_error(f"unknown option {argument!r}")
96
+
97
+ return _Invocation(config, None, argument, tuple(pending[1:]))
98
+
99
+ _usage_error("a target is required: -m MODULE or a script path")
100
+
101
+
102
+ def main(argv: Sequence[str] | None = None) -> None:
103
+ """Parse the command line, apply the config, and run the target."""
104
+
105
+ invocation = _parse(sys.argv[1:] if argv is None else argv)
106
+
107
+ # Resolve and apply the config before anything else happens, so
108
+ # patches and setup hooks are in place before the target module
109
+ # imports anything. Finding no config is an error rather than a
110
+ # silent untraced run.
111
+
112
+ source = invocation.config if invocation.config is not None else find_config()
113
+
114
+ if source is None:
115
+ print(
116
+ "wrapture: no config found: pass --config PATH, set"
117
+ " WRAPTURE_CONFIG, or provide wrapture.toml or a"
118
+ " [tool.wrapture] table in pyproject.toml in the current"
119
+ " directory",
120
+ file=sys.stderr,
121
+ )
122
+ raise SystemExit(1)
123
+
124
+ try:
125
+ load_config(source).apply()
126
+ except ConfigError as exc:
127
+ print(f"wrapture: {exc}", file=sys.stderr)
128
+ raise SystemExit(1) from None
129
+
130
+ # Hand over to the target as python itself would have run it. For
131
+ # a module target, alter_sys replaces sys.argv[0] with the
132
+ # module's own file, matching python -m; for a script, its
133
+ # directory joins the front of sys.path, matching python script.py.
134
+
135
+ if invocation.module is not None:
136
+ sys.argv[:] = [invocation.module, *invocation.arguments]
137
+ runpy.run_module(invocation.module, run_name="__main__", alter_sys=True)
138
+ else:
139
+ script = invocation.script
140
+ assert script is not None
141
+
142
+ sys.argv[:] = [script, *invocation.arguments]
143
+ sys.path.insert(0, os.path.dirname(os.path.abspath(script)))
144
+ runpy.run_path(script, run_name="__main__")
145
+
146
+
147
+ if __name__ == "__main__":
148
+ main()