smolpy 0.0.1__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 (42) hide show
  1. smolpy-0.0.1/.github/workflows/ci.yml +45 -0
  2. smolpy-0.0.1/.github/workflows/publish.yml +51 -0
  3. smolpy-0.0.1/.github/workflows/release.yml +62 -0
  4. smolpy-0.0.1/.gitignore +36 -0
  5. smolpy-0.0.1/LICENSE +21 -0
  6. smolpy-0.0.1/PKG-INFO +279 -0
  7. smolpy-0.0.1/README.md +256 -0
  8. smolpy-0.0.1/docs/adr/0001-protocol-package-structure.md +63 -0
  9. smolpy-0.0.1/docs/adr/0002-modbus-tcp-implementation.md +128 -0
  10. smolpy-0.0.1/docs/adr/0003-smol-external-dsl.md +156 -0
  11. smolpy-0.0.1/examples/README.md +341 -0
  12. smolpy-0.0.1/examples/example.py +22 -0
  13. smolpy-0.0.1/examples/example_10_clients.py +68 -0
  14. smolpy-0.0.1/examples/example_12_clients.py +71 -0
  15. smolpy-0.0.1/examples/example_20_clients.py +71 -0
  16. smolpy-0.0.1/examples/example_file_transfer.py +82 -0
  17. smolpy-0.0.1/examples/example_mqtt.py +79 -0
  18. smolpy-0.0.1/examples/example_staggered_transfer.py +80 -0
  19. smolpy-0.0.1/examples/example_two_tier.py +98 -0
  20. smolpy-0.0.1/pyproject.toml +56 -0
  21. smolpy-0.0.1/src/smolpy/__init__.py +4 -0
  22. smolpy-0.0.1/src/smolpy/cli.py +178 -0
  23. smolpy-0.0.1/src/smolpy/core/__init__.py +0 -0
  24. smolpy-0.0.1/src/smolpy/core/adapter.py +89 -0
  25. smolpy-0.0.1/src/smolpy/core/link.py +27 -0
  26. smolpy-0.0.1/src/smolpy/core/network.py +168 -0
  27. smolpy-0.0.1/src/smolpy/core/node.py +18 -0
  28. smolpy-0.0.1/src/smolpy/core/observation.py +26 -0
  29. smolpy-0.0.1/src/smolpy/ethernet/__init__.py +0 -0
  30. smolpy-0.0.1/src/smolpy/ethernet/hub.py +11 -0
  31. smolpy-0.0.1/src/smolpy/ethernet/switch.py +17 -0
  32. smolpy-0.0.1/src/smolpy/mqtt/__init__.py +0 -0
  33. smolpy-0.0.1/src/smolpy/mqtt/broker.py +23 -0
  34. smolpy-0.0.1/src/smolpy/sim/__init__.py +0 -0
  35. smolpy-0.0.1/src/smolpy/sim/engine.py +580 -0
  36. smolpy-0.0.1/src/smolpy/viz/__init__.py +0 -0
  37. smolpy-0.0.1/src/smolpy/viz/dashboard.py +464 -0
  38. smolpy-0.0.1/src/smolpy/viz/text_dashboard.py +134 -0
  39. smolpy-0.0.1/tests/__init__.py +0 -0
  40. smolpy-0.0.1/tests/test_dsl.py +45 -0
  41. smolpy-0.0.1/tests/test_simulation.py +242 -0
  42. smolpy-0.0.1/uv.lock +1438 -0
@@ -0,0 +1,45 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ lint-and-test:
11
+ name: Python ${{ matrix.python-version }}
12
+ runs-on: ubuntu-latest
13
+
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ python-version: ["3.11", "3.12"]
18
+
19
+ steps:
20
+ - uses: actions/checkout@v4
21
+
22
+ - name: Install uv
23
+ uses: astral-sh/setup-uv@v4
24
+ with:
25
+ version: "latest"
26
+ enable-cache: true
27
+
28
+ - name: Set up Python ${{ matrix.python-version }}
29
+ run: uv python install ${{ matrix.python-version }}
30
+
31
+ - name: Install dependencies
32
+ run: uv sync --all-extras
33
+
34
+ - name: Lint — ruff check
35
+ run: uv run ruff check src/
36
+
37
+ - name: Lint — ruff format check
38
+ run: uv run ruff format --check src/
39
+
40
+ - name: Type check — mypy
41
+ run: uv run mypy src/smolpy/
42
+ continue-on-error: true # treat type errors as warnings until stubs are complete
43
+
44
+ - name: Run tests
45
+ run: uv run pytest tests/ -v --tb=short
@@ -0,0 +1,51 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+ workflow_call:
8
+
9
+ jobs:
10
+ build:
11
+ name: Build distribution
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ with:
17
+ fetch-depth: 0 # needed for hatch-vcs to read git tags
18
+
19
+ - name: Install uv
20
+ uses: astral-sh/setup-uv@v4
21
+ with:
22
+ version: "latest"
23
+
24
+ - name: Build package
25
+ run: uv build
26
+
27
+ - name: Upload dist
28
+ uses: actions/upload-artifact@v4
29
+ with:
30
+ name: dist
31
+ path: dist/
32
+
33
+ publish:
34
+ name: Publish to PyPI
35
+ needs: build
36
+ runs-on: ubuntu-latest
37
+ environment: pypi
38
+ permissions:
39
+ id-token: write # required for OIDC trusted publishing
40
+
41
+ steps:
42
+ - name: Download dist
43
+ uses: actions/download-artifact@v4
44
+ with:
45
+ name: dist
46
+ path: dist/
47
+
48
+ - name: Publish to PyPI
49
+ uses: pypa/gh-action-pypi-publish@release/v1
50
+ with:
51
+ attestations: false
@@ -0,0 +1,62 @@
1
+ name: Release
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ bump:
7
+ description: Version part to bump
8
+ required: true
9
+ default: patch
10
+ type: choice
11
+ options:
12
+ - patch
13
+ - minor
14
+ - major
15
+
16
+ permissions:
17
+ contents: write # needed to push the new tag
18
+
19
+ jobs:
20
+ tag:
21
+ name: Bump version and tag
22
+ runs-on: ubuntu-latest
23
+
24
+ outputs:
25
+ tag: ${{ steps.version.outputs.tag }}
26
+
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ with:
30
+ fetch-depth: 0
31
+
32
+ - name: Compute next version
33
+ id: version
34
+ run: |
35
+ LATEST=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
36
+ echo "Latest tag: $LATEST"
37
+ VERSION=${LATEST#v}
38
+ MAJOR=$(echo "$VERSION" | cut -d. -f1)
39
+ MINOR=$(echo "$VERSION" | cut -d. -f2)
40
+ PATCH=$(echo "$VERSION" | cut -d. -f3)
41
+ case "${{ inputs.bump }}" in
42
+ major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
43
+ minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
44
+ patch) PATCH=$((PATCH + 1)) ;;
45
+ esac
46
+ NEXT="v${MAJOR}.${MINOR}.${PATCH}"
47
+ echo "Next tag: $NEXT"
48
+ echo "tag=$NEXT" >> "$GITHUB_OUTPUT"
49
+
50
+ - name: Create and push tag
51
+ run: |
52
+ git config user.name "github-actions[bot]"
53
+ git config user.email "github-actions[bot]@users.noreply.github.com"
54
+ git tag "${{ steps.version.outputs.tag }}"
55
+ git push origin "${{ steps.version.outputs.tag }}"
56
+
57
+ publish:
58
+ name: Publish to PyPI
59
+ needs: tag
60
+ uses: ./.github/workflows/publish.yml
61
+ permissions:
62
+ id-token: write
@@ -0,0 +1,36 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.so
5
+ *.egg-info/
6
+ dist/
7
+ build/
8
+
9
+ # Virtual environments
10
+ .venv/
11
+ venv/
12
+
13
+ # uv
14
+ .uv/
15
+
16
+ # Test / coverage
17
+ .pytest_cache/
18
+ .coverage
19
+ htmlcov/
20
+
21
+ # Editors
22
+ .vscode/
23
+ .idea/
24
+ *.swp
25
+
26
+ # macOS
27
+ .DS_Store
28
+
29
+ # mypy cache
30
+ .mypy_cache/
31
+
32
+ # ruff cache
33
+ .ruff_cache/
34
+
35
+ # pyright cache
36
+ pyrightconfig.json
smolpy-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jwszolek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
smolpy-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,279 @@
1
+ Metadata-Version: 2.5
2
+ Name: smolpy
3
+ Version: 0.0.1
4
+ Summary: Network simulation DSL and discrete-event simulator
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Requires-Python: >=3.11
12
+ Requires-Dist: dash>=2.17
13
+ Requires-Dist: dearpygui>=1.6
14
+ Requires-Dist: networkx>=3.3
15
+ Requires-Dist: plotly>=5.22
16
+ Requires-Dist: rich>=13
17
+ Requires-Dist: simpy>=4.1
18
+ Provides-Extra: dev
19
+ Requires-Dist: mypy; extra == 'dev'
20
+ Requires-Dist: pytest>=8; extra == 'dev'
21
+ Requires-Dist: ruff; extra == 'dev'
22
+ Description-Content-Type: text/markdown
23
+
24
+ # SMOLPy
25
+
26
+ [![CI](https://github.com/jwszolek/SMOLPy/actions/workflows/ci.yml/badge.svg)](https://github.com/jwszolek/SMOLPy/actions/workflows/ci.yml)
27
+ [![PyPI version](https://img.shields.io/pypi/v/smolpy)](https://pypi.org/project/smolpy/)
28
+ [![Python](https://img.shields.io/pypi/pyversions/smolpy)](https://pypi.org/project/smolpy/)
29
+ [![License](https://img.shields.io/github/license/jwszolek/SMOLPy)](LICENSE)
30
+
31
+ Python rewrite of SMOL — a Network Description Language and Discrete-Event Simulator for industrial Measurement-Diagnostics-Control (MDC) networks.
32
+
33
+ SMOLPy lets you describe a network topology in pure Python, define traffic flows, and run a discrete-event simulation (powered by SimPy) that produces real metric time-series. A built-in Dear PyGui desktop dashboard shows the topology and live metric charts as the simulation runs.
34
+
35
+ ---
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ uv sync # installs all runtime + dev dependencies
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Quick start
46
+
47
+ ```python
48
+ from smolpy import Network
49
+
50
+ net = Network("office-net")
51
+ host_a = net.adapter("host-A", ip="10.0.0.1")
52
+ server = net.adapter("server", ip="10.0.0.10")
53
+ sw1 = net.switch("sw1", ports=8, mode="store-and-forward")
54
+
55
+ net.link(host_a, sw1, speed=1_000, length=5) # speed in Mb/s, length in metres
56
+ net.link(server, sw1, speed=10_000, length=2)
57
+
58
+ host_a.sends(to=server, rate=8_000, size=1_518, pattern="constant")
59
+
60
+ net.observe("throughput", on=server, every=100) # sample every 100 ms
61
+ net.observe("queue_depth", on=sw1, every=50)
62
+
63
+ result = net.simulate(duration=30_000, live=True) # 30 s simulation with live dashboard
64
+ result.report() # print summary table to terminal
65
+ ```
66
+
67
+ Run it:
68
+
69
+ ```bash
70
+ uv run smolpy run my_script.py
71
+ ```
72
+
73
+ ---
74
+
75
+ ## Dashboard
76
+
77
+ When `live=True` the simulation runs in a background thread while a full-screen Dear PyGui window opens immediately.
78
+
79
+ ### Topology panel (left)
80
+
81
+ Each node is drawn as a coloured circle:
82
+
83
+ | Colour | Node type |
84
+ |---|---|
85
+ | Blue | Adapter (host / server / NIC) |
86
+ | Green | Switch |
87
+ | Orange | Hub |
88
+
89
+ Node fill changes dynamically during the simulation:
90
+
91
+ | Appearance | Meaning |
92
+ |---|---|
93
+ | Dim (faded) | Node idle — no traffic yet |
94
+ | Pulsing bright fill | Node actively **transmitting** (`bytes_sent > 0`) |
95
+ | Solid bright fill | Node forwarding traffic (switch / hub) |
96
+ | Pulsing amber outer ring | Node actively **receiving** data (`bytes_received > 0`) |
97
+
98
+ Animated particles flow along every link to show live traffic direction.
99
+
100
+ ### Metrics panel (right)
101
+
102
+ One chart per observed metric. All series update in real time. Axes auto-scale to fit the data.
103
+
104
+ ### Simulation controls
105
+
106
+ Three controls appear in the title bar during a live simulation:
107
+
108
+ | Control | Effect |
109
+ |---|---|
110
+ | **⏸ Pause** | Freezes simulation time; dashboard stays interactive. Click again to resume. |
111
+ | **▶ Resume** | Continues from the exact pause point. |
112
+ | **⏹ Stop** | Ends the simulation early; plots freeze at the last collected sample. |
113
+
114
+ Status indicator:
115
+ - **● Simulating…** — running
116
+ - **● Paused** — paused by user
117
+ - **● Done** — completed normally
118
+ - **● Stopped** — ended by user
119
+
120
+ ---
121
+
122
+ ## DSL reference
123
+
124
+ ### Topology builders
125
+
126
+ ```python
127
+ adapter = net.adapter("name", ip="10.0.0.1") # NIC / host / server
128
+ switch = net.switch("name", ports=16, mode="store-and-forward")
129
+ hub = net.hub("name", ports=8)
130
+ broker = net.mqtt_broker("name", ip="10.0.2.1") # MQTT message broker
131
+ net.link(a, b, speed=1_000, length=10) # Mb/s and metres
132
+ ```
133
+
134
+ Multiple switches can be chained to model hierarchical topologies:
135
+
136
+ ```python
137
+ core_sw = net.switch("core-sw", ports=16, mode="store-and-forward")
138
+ edge_sw = net.switch("edge-sw", ports=8, mode="store-and-forward")
139
+ net.link(edge_sw, core_sw, speed=1_000, length=5) # inter-switch uplink
140
+ ```
141
+
142
+ ### Traffic
143
+
144
+ ```python
145
+ # Basic Ethernet send
146
+ src.sends(to=dst, rate=8_000, size=1_518, pattern="constant")
147
+
148
+ # Delayed start (useful for staggered scenarios)
149
+ src.sends(to=dst, rate=8_000, size=1_518, pattern="constant", delay_ms=5_000)
150
+
151
+ # MQTT publish (sensor-style, constant-rate)
152
+ sensor.publishes(to=broker, topic="plant/temp", rate=1.0, payload=20, qos=1)
153
+ sensor.publishes(to=broker, topic="plant/temp", rate=1.0, payload=20, qos=0, delay_ms=2_000)
154
+
155
+ # Broker topic routing — must be called before simulate()
156
+ broker.routes("plant/temp", to=[server])
157
+ ```
158
+
159
+ | Parameter | Type | Description |
160
+ |---|---|---|
161
+ | `to` | Adapter | Destination adapter |
162
+ | `rate` | float | Frames per second |
163
+ | `size` | int \| `"imix"` | Frame size in bytes, or Internet Mix distribution |
164
+ | `pattern` | str | `"constant"`, `"poisson"`, or `"bursty"` |
165
+ | `delay_ms` | float | Simulation time before this flow starts (default 0) |
166
+
167
+ **`publishes()` parameters**
168
+
169
+ | Parameter | Type | Description |
170
+ |---|---|---|
171
+ | `to` | MQTTBroker | Target broker |
172
+ | `topic` | str | MQTT topic string |
173
+ | `rate` | float | Messages per second (default 1.0) |
174
+ | `payload` | int | Payload bytes (default 20) |
175
+ | `qos` | int | 0 = fire-and-forget, 1 = PUBACK acknowledgement |
176
+ | `delay_ms` | float | Simulation time before publishing starts (default 0) |
177
+
178
+ **Traffic patterns**
179
+
180
+ | Pattern | Description |
181
+ |---|---|
182
+ | `"constant"` | Fixed inter-frame gap — models a saturated link |
183
+ | `"poisson"` | Exponentially distributed gaps — models random/bursty traffic |
184
+ | `"bursty"` | Pareto-distributed burst lengths — models ON/OFF sources |
185
+
186
+ **Frame sizes**
187
+
188
+ | Value | Description |
189
+ |---|---|
190
+ | integer | Fixed size in bytes (e.g. `512`, `1_518`) |
191
+ | `"imix"` | 40 % × 64 B, 57 % × 594 B, 3 % × 1 518 B |
192
+
193
+ ### Observations
194
+
195
+ ```python
196
+ net.observe(metric, on=node, every=interval_ms)
197
+ ```
198
+
199
+ | Metric | Unit | Observed on |
200
+ |---|---|---|
201
+ | `throughput` | Mb/s | Adapter |
202
+ | `latency` | µs | Adapter |
203
+ | `frame_loss` | % | Adapter |
204
+ | `bytes_sent` | MB | Adapter (sender) |
205
+ | `bytes_received` | MB | Adapter (receiver) |
206
+ | `queue_depth` | frames | Switch |
207
+ | `utilization` | % | Any node |
208
+ | `collision_rate` | /s | Hub |
209
+ | `broker_queue` | msgs | MQTTBroker |
210
+
211
+ ### Simulation
212
+
213
+ ```python
214
+ result = net.simulate(duration=30_000) # headless — silent, fastest
215
+ result = net.simulate(duration=30_000, text=True) # rich text dashboard in terminal
216
+ result = net.simulate(duration=30_000, live=True) # full Dear PyGui desktop window
217
+
218
+ result.report() # print summary table (avg / min / max per metric)
219
+ result.plot() # open static dashboard for a completed result
220
+
221
+ # Export metric time-series (format inferred from extension)
222
+ result.export("results.csv") # long CSV: time_ms, metric, value
223
+ result.export("results.json") # JSON dict of lists-of-pairs
224
+ result.export("out.csv", format="csv") # explicit format override
225
+ ```
226
+
227
+ **Text mode** (`text=True`) displays a live updating table in the terminal — no display server or GUI toolkit required. Ideal for headless servers, SSH sessions, and CI environments.
228
+
229
+ ### Quick demo
230
+
231
+ ```bash
232
+ smolpy demo # built-in 3-client scenario, text mode, no script needed
233
+ ```
234
+
235
+ ---
236
+
237
+ ## MQTT publish-subscribe
238
+
239
+ SMOLPy models application-layer MQTT traffic on top of the standard Ethernet/IP/TCP wire model.
240
+
241
+ ### What is modelled
242
+
243
+ - **Publisher adapters** call `publishes()` to emit periodic MQTT PUBLISH frames at a fixed rate toward an `MQTTBroker` node.
244
+ - **The broker** receives PUBLISH frames and fans out one copy to each registered subscriber per topic (`routes()`). QoS 0 delivers silently; QoS 1 additionally sends a PUBACK frame (58 bytes) back toward the publisher.
245
+ - **Subscriber adapters** receive forwarded copies just like normal Ethernet frames; all standard metrics (`throughput`, `latency`, `bytes_received`) apply.
246
+ - **`broker_queue`** samples the broker's inbound store depth — unprocessed PUBLISH frames waiting to be forwarded. A non-zero and rising queue indicates the broker or its downstream link is becoming a bottleneck.
247
+
248
+ ### Frame size formula
249
+
250
+ ```
251
+ frame_size = 54 (Ethernet+IPv4+TCP) + 2 (MQTT fixed header) + 2 (topic-length field) + len(topic) + (2 if qos > 0 else 0) + payload_bytes
252
+ ```
253
+
254
+ A typical small sensor message (`topic="plant/temperature"`, `payload=20`, `qos=1`) produces a 96-byte frame, roughly 16× smaller than a maximum-size bulk frame (1 518 B).
255
+
256
+ ### Dashboard
257
+
258
+ `MQTTBroker` nodes appear as **purple** circles in the topology panel.
259
+
260
+ ---
261
+
262
+ ## Simulation engine
263
+
264
+ - **MAC-learning switch** — each switch pre-seeds its forwarding table from the topology wiring, eliminating spurious flooding toward silent endpoints (e.g. a server that only receives). Dynamic learning still operates for traffic through intermediate switches.
265
+ - **Store-and-forward model** — transmission delay + propagation delay per hop.
266
+ - **Queuing** — each link direction is an independent SimPy Store; `queue_depth` reports buffered frames at the switch's outbound ports.
267
+ - **Traffic shaping** — constant, Poisson, and Pareto-burst patterns; IMIX frame-size distribution.
268
+ - **Live mode** — simulation runs in 200 chunks (~8 s total wall time); the dashboard reads shared metric arrays between chunks via Python's GIL.
269
+
270
+ ---
271
+
272
+ ## Examples
273
+
274
+ See [`examples/README.md`](examples/README.md) for eight ready-to-run scenarios covering single-switch saturation, oversubscription, two-tier access bottlenecks, and MQTT publish-subscribe.
275
+
276
+ ```bash
277
+ uv run smolpy run examples/example.py
278
+ uv run smolpy run examples/example_two_tier.py
279
+ ```