3dprintmon 0.2.0__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.
- 3dprintmon-0.2.0.dist-info/METADATA +238 -0
- 3dprintmon-0.2.0.dist-info/RECORD +26 -0
- 3dprintmon-0.2.0.dist-info/WHEEL +5 -0
- 3dprintmon-0.2.0.dist-info/entry_points.txt +2 -0
- 3dprintmon-0.2.0.dist-info/licenses/LICENSE +21 -0
- 3dprintmon-0.2.0.dist-info/top_level.txt +1 -0
- printmon/__init__.py +5 -0
- printmon/__main__.py +7 -0
- printmon/autostart.py +191 -0
- printmon/cli.py +174 -0
- printmon/config.py +219 -0
- printmon/events.py +204 -0
- printmon/fileinfo.py +112 -0
- printmon/gui/__init__.py +1 -0
- printmon/gui/alert.py +113 -0
- printmon/gui/icons.py +91 -0
- printmon/gui/live.py +161 -0
- printmon/gui/settings_dialog.py +294 -0
- printmon/gui/tray.py +436 -0
- printmon/gui/window.py +198 -0
- printmon/monitor.py +120 -0
- printmon/moonraker.py +232 -0
- printmon/policy.py +84 -0
- printmon/py.typed +0 -0
- printmon/status.py +226 -0
- printmon/version.py +3 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 3dprintmon
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A tray icon that watches your Klipper 3D printers and tells you when a print finishes, fails or needs you.
|
|
5
|
+
Author: Christopher Eslick
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/drak42/3dprintmon
|
|
8
|
+
Project-URL: Repository, https://github.com/drak42/3dprintmon
|
|
9
|
+
Project-URL: Issues, https://github.com/drak42/3dprintmon/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/drak42/3dprintmon/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Funding, https://buymeacoffee.com/drak42
|
|
12
|
+
Keywords: 3d-printing,klipper,moonraker,tray,notifications,monitor,creality,k2
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Environment :: X11 Applications :: Qt
|
|
15
|
+
Classifier: Environment :: MacOS X
|
|
16
|
+
Classifier: Environment :: Win32 (MS Windows)
|
|
17
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
18
|
+
Classifier: Intended Audience :: Manufacturing
|
|
19
|
+
Classifier: Operating System :: MacOS
|
|
20
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
21
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
25
|
+
Classifier: Topic :: Printing
|
|
26
|
+
Classifier: Topic :: System :: Monitoring
|
|
27
|
+
Classifier: Typing :: Typed
|
|
28
|
+
Requires-Python: >=3.12
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Requires-Dist: PySide6>=6.6
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff==0.17.6; extra == "dev"
|
|
35
|
+
Requires-Dist: mypy==2.4.1; extra == "dev"
|
|
36
|
+
Provides-Extra: demo
|
|
37
|
+
Requires-Dist: pillow>=10; extra == "demo"
|
|
38
|
+
Provides-Extra: bundle
|
|
39
|
+
Requires-Dist: pyinstaller>=6.10; extra == "bundle"
|
|
40
|
+
Requires-Dist: pillow>=10; extra == "bundle"
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
|
|
43
|
+
# 3dprintmon
|
|
44
|
+
|
|
45
|
+

|
|
46
|
+
|
|
47
|
+
[](https://pypi.org/project/3dprintmon/)
|
|
48
|
+
[](https://pypi.org/project/3dprintmon/)
|
|
49
|
+
[](https://pepy.tech/projects/3dprintmon)
|
|
50
|
+
[](https://github.com/drak42/3dprintmon/actions/workflows/ci.yml)
|
|
51
|
+
[](https://github.com/drak42/3dprintmon/blob/main/LICENSE)
|
|
52
|
+
[](https://buymeacoffee.com/drak42)
|
|
53
|
+
|
|
54
|
+
A small tray icon that watches your 3D printers' current jobs and tells you
|
|
55
|
+
when a print **finishes**, **fails**, is **cancelled** or **paused**, when its
|
|
56
|
+
**first layer is done**, when it's **almost finished**, or when a printer stops
|
|
57
|
+
answering. For Klipper printers with Moonraker (such as the Creality K2 Plus,
|
|
58
|
+
Voron or any Mainsail/Fluidd printer), on Linux, macOS and Windows.
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
*The notification is drawn as an example; your desktop shows it in its own style.*
|
|
63
|
+
|
|
64
|
+
## Quick start
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pipx install 3dprintmon # from PyPI
|
|
68
|
+
3dprintmon add "K2 Plus" 192.168.1.20 # your printer's address (Moonraker, port 7125)
|
|
69
|
+
3dprintmon autostart on # start with the computer (optional)
|
|
70
|
+
3dprintmon # the icon appears in the tray
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Or download a ready-to-run app (no Python needed) from the
|
|
74
|
+
[latest release](https://github.com/drak42/3dprintmon/releases/latest):
|
|
75
|
+
|
|
76
|
+
| System | Download | First start |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| Windows | `3dprintmon-X.Y.Z-windows.zip` | Unzip, run `3dprintmon.exe`. Windows may warn about an unknown publisher: **More info → Run anyway** |
|
|
79
|
+
| macOS (Apple silicon) | `3dprintmon-X.Y.Z-macos.zip` | Unzip, move `3dprintmon.app` to Applications, then **right-click → Open** the first time (the app isn't signed by Apple) |
|
|
80
|
+
| Linux | `3dprintmon-X.Y.Z-linux.tar.gz` | Extract, run `3dprintmon/3dprintmon` |
|
|
81
|
+
|
|
82
|
+
Add printers from **Settings…** in the tray menu.
|
|
83
|
+
|
|
84
|
+
## Features
|
|
85
|
+
|
|
86
|
+
- **Several printers at once.** The icon shows the most urgent one, with a
|
|
87
|
+
progress ring while printing; the tooltip, menu and window list them all.
|
|
88
|
+
- **Live:** follows each printer over Moonraker's websocket, so the icon and
|
|
89
|
+
notifications change within a second, with a regular check as a safety net.
|
|
90
|
+
- **Notifications** for finished, failed, cancelled and paused prints, printer
|
|
91
|
+
errors, a printer that stops answering mid-print, the **first layer done**
|
|
92
|
+
(time to check it stuck) and **almost done** (10 minutes before the end, or
|
|
93
|
+
your choice). Each can be turned off.
|
|
94
|
+
- **Failures are hard to miss:** they stay in an alert window until you dismiss
|
|
95
|
+
them, with a short chime.
|
|
96
|
+
- **Quiet hours and snooze:** hold back the routine notifications at night or
|
|
97
|
+
for a while; failures still show, silently.
|
|
98
|
+
- **Status window** with each job's **thumbnail**, progress, time printing and
|
|
99
|
+
left, temperatures, the printer's message, and a **camera picture** while
|
|
100
|
+
printing (from Moonraker's webcam or a snapshot address you set).
|
|
101
|
+
- **Settings window** for printers and every option; no file editing needed.
|
|
102
|
+
- **Starts with the computer** if you want it to.
|
|
103
|
+
- **Reads, never writes:** it never sends a command to a printer.
|
|
104
|
+
|
|
105
|
+
| Status window | Tray menu |
|
|
106
|
+
|---|---|
|
|
107
|
+
|  |  |
|
|
108
|
+
|
|
109
|
+
The icon at a glance:
|
|
110
|
+
|
|
111
|
+

|
|
112
|
+
|
|
113
|
+
## Use
|
|
114
|
+
|
|
115
|
+
Click the icon to show or hide the status window; right-click (or click, on
|
|
116
|
+
macOS) for the menu: each printer's status, **Check now**, each printer's web
|
|
117
|
+
page, **Snooze notifications**, **Settings…**, **Start when I log in** and
|
|
118
|
+
**Quit**. Closing the window leaves the icon running.
|
|
119
|
+
|
|
120
|
+
### Settings window
|
|
121
|
+
|
|
122
|
+

|
|
123
|
+
|
|
124
|
+
Add a printer with its name and **Moonraker address** (such as
|
|
125
|
+
`http://192.168.1.20:7125`; port 7125 is assumed). **Test** checks it answers.
|
|
126
|
+
Optionally give its **web page** (Fluidd, Mainsail or the printer's own; the
|
|
127
|
+
address without the port is used otherwise), a **camera snapshot** address
|
|
128
|
+
(a URL that returns one JPEG or PNG picture; Moonraker's webcam is used if
|
|
129
|
+
it has one) and an **API key** if Moonraker asks for one.
|
|
130
|
+
|
|
131
|
+
### When something fails
|
|
132
|
+
|
|
133
|
+
A failed print, a printer error or a printer that stops answering mid-print
|
|
134
|
+
shows a notification, plays a chime and opens a small window that stays until
|
|
135
|
+
you dismiss it, even during quiet hours (silently then):
|
|
136
|
+
|
|
137
|
+

|
|
138
|
+
|
|
139
|
+
### Commands
|
|
140
|
+
|
|
141
|
+
The same program works in a terminal:
|
|
142
|
+
|
|
143
|
+

|
|
144
|
+
|
|
145
|
+
| Command | What it does |
|
|
146
|
+
|---|---|
|
|
147
|
+
| `3dprintmon` | Start the tray |
|
|
148
|
+
| `3dprintmon status` | Check every printer once and print a line each (exit status 1 if one is offline) |
|
|
149
|
+
| `3dprintmon add NAME URL` | Add a printer (`--web-url`, `--api-key` optional), or change one with that name |
|
|
150
|
+
| `3dprintmon remove NAME` / `list` | Stop watching a printer / list them |
|
|
151
|
+
| `3dprintmon autostart on`, `off`, `status` | Start with the computer, or not |
|
|
152
|
+
| `3dprintmon --printer URL` | Watch that printer only, without changing the settings (repeat for several) |
|
|
153
|
+
| `3dprintmon config-path` | Print where the settings file is |
|
|
154
|
+
| `3dprintmon --debug` | Log every check to the terminal |
|
|
155
|
+
|
|
156
|
+
## Settings file
|
|
157
|
+
|
|
158
|
+
The settings window and `3dprintmon add` write it; you can also edit it (then
|
|
159
|
+
restart 3dprintmon). It is at `~/.config/3dprintmon/config.toml` on Linux,
|
|
160
|
+
`~/Library/Application Support/3dprintmon/config.toml` on macOS and
|
|
161
|
+
`%APPDATA%\3dprintmon\config.toml` on Windows (`3dprintmon config-path`).
|
|
162
|
+
|
|
163
|
+
```toml
|
|
164
|
+
poll_seconds = 5 # how often to check without live updates (60 s while live)
|
|
165
|
+
notify = ["finished", "failed", "cancelled", "paused", "printer_error", "offline",
|
|
166
|
+
"first_layer", "almost_done"]
|
|
167
|
+
almost_done_minutes = 10 # 0 turns the almost-done notice off
|
|
168
|
+
quiet_hours = "22:00-07:00" # "" for none
|
|
169
|
+
sound = "problems" # "problems", "all" or "off"
|
|
170
|
+
alert_problems = true # keep failures on screen until dismissed
|
|
171
|
+
live_updates = true # follow printers over Moonraker's websocket
|
|
172
|
+
camera = true # camera picture in the window while printing
|
|
173
|
+
|
|
174
|
+
[[printers]]
|
|
175
|
+
name = "K2 Plus"
|
|
176
|
+
url = "http://192.168.1.20:7125"
|
|
177
|
+
|
|
178
|
+
[[printers]]
|
|
179
|
+
name = "Voron"
|
|
180
|
+
url = "http://voron.local:7125"
|
|
181
|
+
web_url = "http://voron.local"
|
|
182
|
+
camera_url = "http://voron.local/webcam/?action=snapshot"
|
|
183
|
+
api_key = "only if Moonraker asks for one"
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## How it knows
|
|
187
|
+
|
|
188
|
+
- **Job state, progress, times, temperatures** come from Klipper through
|
|
189
|
+
Moonraker (`print_stats`, `display_status`, `virtual_sdcard`, `webhooks`,
|
|
190
|
+
`gcode_move`, the heaters).
|
|
191
|
+
- **Time left** uses the slicer's estimate for the file, else progress so far.
|
|
192
|
+
- **Thumbnail, layer count and estimate** come from Moonraker's file metadata,
|
|
193
|
+
or, when Moonraker hasn't indexed them (the Creality K2), from the first and
|
|
194
|
+
last few kilobytes of the G-code file itself.
|
|
195
|
+
- **First layer done** uses the printer's layer counter when it reports one,
|
|
196
|
+
otherwise the nozzle staying above the first layer's height for 20 seconds
|
|
197
|
+
(so a quick Z-hop doesn't count).
|
|
198
|
+
- A job already under way when 3dprintmon starts isn't announced again.
|
|
199
|
+
|
|
200
|
+
## Platform notes
|
|
201
|
+
|
|
202
|
+
- **Linux:** needs a desktop with a system tray. On GNOME, install the
|
|
203
|
+
*AppIndicator and KStatusNotifierItem Support* extension; KDE, Cinnamon, XFCE
|
|
204
|
+
and others have one built in. Without a tray, the status window opens
|
|
205
|
+
instead. Notifications use the desktop's notification service; starting at
|
|
206
|
+
login adds `~/.config/autostart/3dprintmon.desktop`.
|
|
207
|
+
- **macOS:** the icon sits in the menu bar. The first notification may ask for
|
|
208
|
+
permission (System Settings → Notifications). Starting at login adds a
|
|
209
|
+
LaunchAgent in `~/Library/LaunchAgents`.
|
|
210
|
+
- **Windows:** the icon sits in the notification area (it may be under the ^
|
|
211
|
+
arrow at first; drag it out to keep it visible). Starting at login adds a
|
|
212
|
+
value under the current user's `Run` key.
|
|
213
|
+
|
|
214
|
+
## Troubleshooting
|
|
215
|
+
|
|
216
|
+
| Problem | What to do |
|
|
217
|
+
|---|---|
|
|
218
|
+
| "offline (Cannot reach …)" | Check the printer is on and the address and port are right: `3dprintmon status`. Moonraker's port is usually 7125 |
|
|
219
|
+
| "Moonraker answered 401" | Moonraker wants an API key: add it in Settings or with `--api-key` |
|
|
220
|
+
| No icon on Linux | Install a tray extension (see Platform notes); the window opens meanwhile |
|
|
221
|
+
| No notifications | Check the system's notification settings allow them; on Linux a notification service must be running |
|
|
222
|
+
| No thumbnail | The file has none (some slicers or settings leave them out) |
|
|
223
|
+
| No camera picture | Moonraker lists no webcam: set the printer's **camera snapshot** address to a URL that returns a single picture |
|
|
224
|
+
| Updates only every minute | The live connection isn't working (a proxy in the way?); 3dprintmon falls back to checking. Turn off live updates to check every `poll_seconds` instead |
|
|
225
|
+
| Time left looks wrong | It uses the slicer's estimate when the file has one, otherwise progress so far |
|
|
226
|
+
|
|
227
|
+
## Development
|
|
228
|
+
|
|
229
|
+
See [CONTRIBUTING.md](https://github.com/drak42/3dprintmon/blob/main/CONTRIBUTING.md),
|
|
230
|
+
the [CHANGELOG](https://github.com/drak42/3dprintmon/blob/main/CHANGELOG.md) and
|
|
231
|
+
the [Roadmap](https://github.com/drak42/3dprintmon/blob/main/docs/Roadmap.md).
|
|
232
|
+
|
|
233
|
+
## Support
|
|
234
|
+
|
|
235
|
+
3dprintmon is free and open source. If it saves you a trip to the printer, you
|
|
236
|
+
can [buy me a coffee](https://buymeacoffee.com/drak42) ☕. Bug reports, ideas
|
|
237
|
+
and pull requests are just as welcome: see
|
|
238
|
+
[CONTRIBUTING.md](https://github.com/drak42/3dprintmon/blob/main/CONTRIBUTING.md).
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
3dprintmon-0.2.0.dist-info/licenses/LICENSE,sha256=PxfZDbhsVHOKhJiECLkqB0DrX4g55IO77WIgBmVA5ho,1075
|
|
2
|
+
printmon/__init__.py,sha256=Y1MYWuMX6Be8L6PmGr-q0Ty9lnuGm0hob7yhmvIQHxg,173
|
|
3
|
+
printmon/__main__.py,sha256=vEf73FP2lOkPVClSX8R4Yss7GJbjwb6oAl6gbpPX2E4,111
|
|
4
|
+
printmon/autostart.py,sha256=XtzULfmupedYeWKmCk0NCjyBH184yKkghrZAxB6-T88,6949
|
|
5
|
+
printmon/cli.py,sha256=i2i2JOfhIafkN7BmNNF2cJik3Ss1fje1_hMK0u4WAos,7003
|
|
6
|
+
printmon/config.py,sha256=zawHeVZaaWQRawR6ewAWmU-0HULVnLJiatO25dsugnw,9218
|
|
7
|
+
printmon/events.py,sha256=Fz6I3MNW1RPymv2i6BHe0rmbkMcYyOBByGyMvShbT0I,8160
|
|
8
|
+
printmon/fileinfo.py,sha256=zH5Gxyg0UNYnD71xksQw-C9qYDy2Jh7_Q_XDCLlz0Uk,4328
|
|
9
|
+
printmon/monitor.py,sha256=WGXbTFXhdf_E5amJ0RoquKWuNDJ0Hz3g8rqQpETTCmM,4660
|
|
10
|
+
printmon/moonraker.py,sha256=-ktdGkjCAOGLkR49tfA3KKkgPt4zBctU6Swto94bhd0,9714
|
|
11
|
+
printmon/policy.py,sha256=Pkrh68454MqUFf_YWhEyeVd1qeguuMeE5PceTM1O9zA,2841
|
|
12
|
+
printmon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
printmon/status.py,sha256=75WiT6SlFh70xP97UyXalh5li5yjQmOk7hbseEl4Ejc,8092
|
|
14
|
+
printmon/version.py,sha256=nE_ZPCjpes8Lz5AjJ9l5JyJGDgziFwJ8BGcGHT8mX48,78
|
|
15
|
+
printmon/gui/__init__.py,sha256=q3Pb3UC13xvRXxg6svHZnqQw-5nvpO8aQTsrRiJMOAU,76
|
|
16
|
+
printmon/gui/alert.py,sha256=5Z62MQartD6GkgHwiuayi-Kg4eSOb0MWGw7STmYDyI0,3857
|
|
17
|
+
printmon/gui/icons.py,sha256=5lvQtAwV1o78xSRHoVhGzs97jXCweDt7uu0gVapxqbI,3455
|
|
18
|
+
printmon/gui/live.py,sha256=ijSMP0oT41aSRmXIgfUeLTgLm10PywOhwr9aWEdRd_g,5793
|
|
19
|
+
printmon/gui/settings_dialog.py,sha256=d6WtL8daqN9of6hW4qHxwApaavz5Vvsbpct5kUE6CPw,11525
|
|
20
|
+
printmon/gui/tray.py,sha256=kFq3IUWKn6twLi_zL_eZIprqzkBc2vPkwnYJNGaiOCk,17599
|
|
21
|
+
printmon/gui/window.py,sha256=-bMcjpw2BX3BgG8JUxrpfxlXUVvTrU-QT5PaI-8Mstk,7634
|
|
22
|
+
3dprintmon-0.2.0.dist-info/METADATA,sha256=se0JlR5sBmxzP6drvn9NQBGFwNvhtQowPsZ6GMpH1mM,12893
|
|
23
|
+
3dprintmon-0.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
24
|
+
3dprintmon-0.2.0.dist-info/entry_points.txt,sha256=4VNSXbfP6HNDh7b9hiRIbi1mOPWstVtLXpK-mNaCAc8,49
|
|
25
|
+
3dprintmon-0.2.0.dist-info/top_level.txt,sha256=40hAEw2z2MXa1Cp0EDtLKLBZ1E4Vh0RWRlyWvSk7lZk,9
|
|
26
|
+
3dprintmon-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Christopher Eslick
|
|
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.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
printmon
|
printmon/__init__.py
ADDED
printmon/__main__.py
ADDED
printmon/autostart.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Start 3dprintmon when the user logs in, on Linux, macOS and Windows.
|
|
2
|
+
|
|
3
|
+
- **Linux:** a desktop entry in ``~/.config/autostart`` (the XDG autostart
|
|
4
|
+
folder every desktop reads).
|
|
5
|
+
- **macOS:** a LaunchAgent in ``~/Library/LaunchAgents`` that runs at login.
|
|
6
|
+
- **Windows:** a value under ``HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run``.
|
|
7
|
+
|
|
8
|
+
Only the current user's settings are touched, never system-wide ones. The
|
|
9
|
+
command started is the installed ``3dprintmon`` program, the packaged app, or
|
|
10
|
+
``python -m printmon`` (with ``pythonw`` on Windows so no console window opens).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import plistlib
|
|
15
|
+
import shlex
|
|
16
|
+
import shutil
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
from collections.abc import Mapping, Sequence
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Protocol
|
|
22
|
+
|
|
23
|
+
APP_ID = "io.github.drak42.3dprintmon"
|
|
24
|
+
RUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
|
|
25
|
+
VALUE_NAME = "3dprintmon"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AutostartError(OSError):
|
|
29
|
+
"""Raised when starting at login cannot be turned on or off."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Registry(Protocol):
|
|
33
|
+
"""The part of the Windows registry used: one value under the current user's Run key."""
|
|
34
|
+
|
|
35
|
+
def get(self, name: str) -> str | None:
|
|
36
|
+
"""Return the value called ``name``, or None."""
|
|
37
|
+
|
|
38
|
+
def set(self, name: str, value: str) -> None:
|
|
39
|
+
"""Set the value called ``name``."""
|
|
40
|
+
|
|
41
|
+
def delete(self, name: str) -> None:
|
|
42
|
+
"""Delete the value called ``name`` if it exists."""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def launch_command() -> list[str]:
|
|
46
|
+
"""Return the command that starts 3dprintmon's tray."""
|
|
47
|
+
if getattr(sys, "frozen", False): # a packaged app
|
|
48
|
+
return [sys.executable]
|
|
49
|
+
program = shutil.which("3dprintmon")
|
|
50
|
+
if program:
|
|
51
|
+
return [program]
|
|
52
|
+
python = Path(sys.executable)
|
|
53
|
+
if sys.platform == "win32":
|
|
54
|
+
windowless = python.with_name("pythonw.exe")
|
|
55
|
+
if windowless.exists():
|
|
56
|
+
python = windowless
|
|
57
|
+
return [str(python), "-m", "printmon"]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class Autostart:
|
|
61
|
+
"""Turn starting at login on and off for the current user."""
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
*,
|
|
66
|
+
platform: str | None = None,
|
|
67
|
+
home: Path | None = None,
|
|
68
|
+
environ: Mapping[str, str] | None = None,
|
|
69
|
+
registry: Registry | None = None,
|
|
70
|
+
command: Sequence[str] | None = None,
|
|
71
|
+
) -> None:
|
|
72
|
+
"""Create the autostart helper; the arguments are replaced in tests."""
|
|
73
|
+
self._platform = platform or sys.platform
|
|
74
|
+
self._home = home or Path.home()
|
|
75
|
+
self._environ = os.environ if environ is None else environ
|
|
76
|
+
self._registry = registry
|
|
77
|
+
self._command = list(command) if command is not None else launch_command()
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def location(self) -> str:
|
|
81
|
+
"""Return where the autostart setting lives, for messages."""
|
|
82
|
+
if self._platform == "win32":
|
|
83
|
+
return rf"HKCU\{RUN_KEY}\{VALUE_NAME}"
|
|
84
|
+
return str(self._file())
|
|
85
|
+
|
|
86
|
+
def is_enabled(self) -> bool:
|
|
87
|
+
"""Return whether 3dprintmon starts at login."""
|
|
88
|
+
if self._platform == "win32":
|
|
89
|
+
return self._windows_registry().get(VALUE_NAME) is not None
|
|
90
|
+
return self._file().is_file()
|
|
91
|
+
|
|
92
|
+
def enable(self) -> None:
|
|
93
|
+
"""Start 3dprintmon at login from now on."""
|
|
94
|
+
try:
|
|
95
|
+
if self._platform == "win32":
|
|
96
|
+
self._windows_registry().set(VALUE_NAME, subprocess.list2cmdline(self._command))
|
|
97
|
+
return
|
|
98
|
+
path = self._file()
|
|
99
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
if self._platform == "darwin":
|
|
101
|
+
path.write_bytes(
|
|
102
|
+
plistlib.dumps(
|
|
103
|
+
{
|
|
104
|
+
"Label": APP_ID,
|
|
105
|
+
"ProgramArguments": self._command,
|
|
106
|
+
"RunAtLoad": True,
|
|
107
|
+
"ProcessType": "Interactive",
|
|
108
|
+
}
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
else:
|
|
112
|
+
path.write_text(self._desktop_entry(), encoding="utf-8")
|
|
113
|
+
except OSError as error:
|
|
114
|
+
raise AutostartError(f"Could not turn on starting at login: {error}") from error
|
|
115
|
+
|
|
116
|
+
def disable(self) -> None:
|
|
117
|
+
"""Stop starting 3dprintmon at login."""
|
|
118
|
+
try:
|
|
119
|
+
if self._platform == "win32":
|
|
120
|
+
self._windows_registry().delete(VALUE_NAME)
|
|
121
|
+
else:
|
|
122
|
+
self._file().unlink(missing_ok=True)
|
|
123
|
+
except OSError as error:
|
|
124
|
+
raise AutostartError(f"Could not turn off starting at login: {error}") from error
|
|
125
|
+
|
|
126
|
+
def _file(self) -> Path:
|
|
127
|
+
"""Return the autostart file on Linux or macOS."""
|
|
128
|
+
if self._platform == "darwin":
|
|
129
|
+
return self._home / "Library" / "LaunchAgents" / f"{APP_ID}.plist"
|
|
130
|
+
config = self._environ.get("XDG_CONFIG_HOME") or str(self._home / ".config")
|
|
131
|
+
return Path(config) / "autostart" / "3dprintmon.desktop"
|
|
132
|
+
|
|
133
|
+
def _desktop_entry(self) -> str:
|
|
134
|
+
"""Return the Linux desktop entry."""
|
|
135
|
+
return "\n".join(
|
|
136
|
+
[
|
|
137
|
+
"[Desktop Entry]",
|
|
138
|
+
"Type=Application",
|
|
139
|
+
"Name=3dprintmon",
|
|
140
|
+
"Comment=Watch your 3D printers from the system tray",
|
|
141
|
+
f"Exec={shlex.join(self._command)}",
|
|
142
|
+
"Icon=printer",
|
|
143
|
+
"Terminal=false",
|
|
144
|
+
"X-GNOME-Autostart-enabled=true",
|
|
145
|
+
"",
|
|
146
|
+
]
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
def _windows_registry(self) -> Registry:
|
|
150
|
+
"""Return the registry access (the real one unless replaced)."""
|
|
151
|
+
if self._registry is None:
|
|
152
|
+
self._registry = WindowsRunKey()
|
|
153
|
+
return self._registry
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class WindowsRunKey:
|
|
157
|
+
"""The current user's ``Run`` key in the Windows registry."""
|
|
158
|
+
|
|
159
|
+
def get(self, name: str) -> str | None:
|
|
160
|
+
"""Return the value called ``name``, or None."""
|
|
161
|
+
if sys.platform != "win32":
|
|
162
|
+
raise AutostartError("The Windows registry is only on Windows.")
|
|
163
|
+
import winreg
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY) as key:
|
|
167
|
+
value, _ = winreg.QueryValueEx(key, name)
|
|
168
|
+
except FileNotFoundError:
|
|
169
|
+
return None
|
|
170
|
+
return str(value)
|
|
171
|
+
|
|
172
|
+
def set(self, name: str, value: str) -> None:
|
|
173
|
+
"""Set the value called ``name``."""
|
|
174
|
+
if sys.platform != "win32":
|
|
175
|
+
raise AutostartError("The Windows registry is only on Windows.")
|
|
176
|
+
import winreg
|
|
177
|
+
|
|
178
|
+
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, RUN_KEY) as key:
|
|
179
|
+
winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
|
|
180
|
+
|
|
181
|
+
def delete(self, name: str) -> None:
|
|
182
|
+
"""Delete the value called ``name`` if it exists."""
|
|
183
|
+
if sys.platform != "win32":
|
|
184
|
+
raise AutostartError("The Windows registry is only on Windows.")
|
|
185
|
+
import winreg
|
|
186
|
+
|
|
187
|
+
try:
|
|
188
|
+
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, winreg.KEY_SET_VALUE) as key:
|
|
189
|
+
winreg.DeleteValue(key, name)
|
|
190
|
+
except FileNotFoundError:
|
|
191
|
+
pass
|