pymailfeedback 0.1.0__tar.gz → 0.1.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pymailfeedback
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Email yourself automatically when a long-running Python script succeeds or fails — with traceback, attachments, and more
5
5
  Author-email: Daniele Mascali <danielemascali@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/dmascali/pymailfeedback
@@ -13,6 +13,7 @@ Requires-Python: >=3.7
13
13
  Description-Content-Type: text/markdown
14
14
 
15
15
  # pymailfeedback
16
+ [![PyPI version](https://badge.fury.io/py/pymailfeedback.svg)](https://pypi.org/project/pymailfeedback/)
16
17
 
17
18
  **Never wonder if your long-running script finished or crashed.**
18
19
 
@@ -1,4 +1,5 @@
1
1
  # pymailfeedback
2
+ [![PyPI version](https://badge.fury.io/py/pymailfeedback.svg)](https://pypi.org/project/pymailfeedback/)
2
3
 
3
4
  **Never wonder if your long-running script finished or crashed.**
4
5
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pymailfeedback"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "Email yourself automatically when a long-running Python script succeeds or fails — with traceback, attachments, and more"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.7"
@@ -234,7 +234,7 @@ def _shutdown_computer():
234
234
  system = platform.system()
235
235
  try:
236
236
  if system == "Windows":
237
- os.system("shutdown /s /t 0")
237
+ os.system("shutdown /s /f /t 0")
238
238
  elif system == "Linux":
239
239
  os.system("sudo shutdown -h now")
240
240
  elif system == "Darwin":
@@ -253,6 +253,25 @@ def _handle_shutdown(shutdown, shutdown_delay):
253
253
  _shutdown_computer()
254
254
 
255
255
 
256
+ def _get_function_file(func):
257
+ """
258
+ Returns the source file of the function's defining module, using the
259
+ module's __file__ attribute directly (more reliable than
260
+ inspect.getsourcefile, which can resolve to the wrong file when
261
+ multiple scripts define a function with the same name in __main__).
262
+ """
263
+ module_name = getattr(func, "__module__", None)
264
+ if module_name:
265
+ module = sys.modules.get(module_name)
266
+ if module and getattr(module, "__file__", None):
267
+ return module.__file__
268
+ # Fallback to inspect if the module lookup fails
269
+ try:
270
+ return inspect.getsourcefile(func)
271
+ except (TypeError, OSError):
272
+ return None
273
+
274
+
256
275
  # ---------------------------------------------------------------------------
257
276
  # HTML EMAIL BUILDER
258
277
  # ---------------------------------------------------------------------------
@@ -368,12 +387,8 @@ def sendstatus(to_addresses=None, verbose=None, shutdown=False, shutdown_delay=6
368
387
  username, computername = "Unknown", "Unknown"
369
388
 
370
389
  func_name = func.__name__
371
- try:
372
- func_file = inspect.getsourcefile(func)
373
- file_label = os.path.basename(func_file) if func_file else "unknown_file"
374
- except TypeError:
375
- func_file = "Unknown path"
376
- file_label = "unknown_file"
390
+ func_file = _get_function_file(func)
391
+ file_label = os.path.basename(func_file) if func_file else "unknown_file"
377
392
 
378
393
  exit_status = 0
379
394
  error_traceback = ""
@@ -0,0 +1,138 @@
1
+ def _async_send_and_shutdown(
2
+ to_addresses, subject, plain_body, attachments, html_body,
3
+ shutdown, shutdown_delay,
4
+ sender_email, sender_password, smtp_server, smtp_port
5
+ ):
6
+ """Runs in a separate background process to ensure email delivery
7
+ survives main interpreter crashes, GUI errors, or module teardown."""
8
+ global _SENDER_EMAIL, _SENDER_PASSWORD, _SMTP_SERVER, _SMTP_PORT
9
+ _SENDER_EMAIL = sender_email
10
+ _SENDER_PASSWORD = sender_password
11
+ _SMTP_SERVER = smtp_server
12
+ _SMTP_PORT = smtp_port
13
+
14
+ try:
15
+ sendmsg(to_addresses, subject, plain_body, attachments, html_body=html_body)
16
+ except Exception as e:
17
+ print(f"[pymailfeedback] Failed to send email in background process: {e}", file=sys.stderr)
18
+
19
+ _handle_shutdown(shutdown, shutdown_delay)
20
+
21
+
22
+ def sendstatus(to_addresses=None, verbose=None, shutdown=False, shutdown_delay=60):
23
+ """
24
+ Decorator to send the execution status via email using a background process.
25
+ """
26
+ _ensure_config_loaded()
27
+ if verbose is None:
28
+ verbose = _DEFAULT_VERBOSE
29
+
30
+ if _is_main_process():
31
+ print(
32
+ f"[pymailfeedback] sendstatus decorator configured with recipient(s): {to_addresses or _DEFAULT_RECIPIENT}, "
33
+ f"shutdown: {shutdown}, shutdown_delay: {shutdown_delay} seconds.", file=sys.stderr)
34
+
35
+ def decorator(func):
36
+ @wraps(func)
37
+ def wrapper(*args, **kwargs):
38
+ global _TIC_TIMES
39
+ start_time = time.time()
40
+ _TIC_TIMES.append(start_time)
41
+
42
+ try:
43
+ username = getpass.getuser()
44
+ computername = socket.gethostname()
45
+ except Exception:
46
+ username, computername = "Unknown", "Unknown"
47
+
48
+ func_name = func.__name__
49
+ try:
50
+ func_file = inspect.getsourcefile(func)
51
+ file_label = os.path.basename(func_file) if func_file else "unknown_file"
52
+ except TypeError:
53
+ func_file = "Unknown path"
54
+ file_label = "unknown_file"
55
+
56
+ exit_status = 0
57
+ error_traceback = ""
58
+ try:
59
+ result = func(*args, **kwargs)
60
+ return result
61
+ except Exception:
62
+ exit_status = 1
63
+ error_traceback = traceback.format_exc()
64
+ raise
65
+ finally:
66
+ elapsed = time.time() - start_time
67
+ elapsed_str = readsec(elapsed)
68
+
69
+ if exit_status == 1:
70
+ subject = f"[{file_label}] {func_name} — ❌ FAILURE"
71
+ title_color = "#d32f2f"
72
+ content_lines = [
73
+ f"<b>Dear {username},</b>",
74
+ f"Function <b>{func_name}</b> in <b>{file_label}</b> has failed with the following error:",
75
+ f"<pre style='color:#d32f2f; font-weight:bold; background:#fdecea; padding:8px; border-radius:4px; white-space:pre-wrap;'>{error_traceback}</pre>",
76
+ ]
77
+ plain_content = [
78
+ f"Dear {username},",
79
+ f"Function {func_name} in {file_label} failed with the following error:",
80
+ error_traceback,
81
+ ]
82
+ else:
83
+ subject = f"[{file_label}] {func_name} — ✅ SUCCESS"
84
+ title_color = "#2e7d32"
85
+ content_lines = [
86
+ f"<b>Dear {username},</b>",
87
+ f"Function <b>{func_name}</b> in <b>{file_label}</b> completed successfully.",
88
+ ]
89
+ plain_content = [
90
+ f"Dear {username},",
91
+ f"Function {func_name} in {file_label} completed successfully.",
92
+ ]
93
+
94
+ footer_lines = [
95
+ f"Machine: {computername}",
96
+ f"Full path: {func_file}",
97
+ f"Elapsed time: {elapsed_str}",
98
+ f"Verbose level: {verbose}",
99
+ ]
100
+
101
+ html_body = _build_html_body(title_color, content_lines, footer_lines)
102
+ plain_body = _build_plain_body(plain_content, footer_lines)
103
+
104
+ attachments = []
105
+ if exit_status == 1 and verbose > 0:
106
+ _, _, tb = sys.exc_info()
107
+ extracted_tb = traceback.extract_tb(tb)
108
+
109
+ if verbose >= 1 and func_file and os.path.exists(func_file):
110
+ attachments.append(func_file)
111
+
112
+ if verbose >= 2:
113
+ module_file = Path(__file__).resolve()
114
+ for frame in extracted_tb:
115
+ if Path(frame.filename) == module_file:
116
+ continue
117
+ if os.path.exists(frame.filename) and frame.filename not in attachments:
118
+ attachments.append(frame.filename)
119
+
120
+ # SPAWN BACKGROUND PROCESS FOR EMAIL & SHUTDOWN
121
+ # This guarantees delivery even if matplotlib/Tkinter crashes the main thread.
122
+ try:
123
+ p = mp.Process(
124
+ target=_async_send_and_shutdown,
125
+ args=(
126
+ to_addresses, subject, plain_body, attachments, html_body,
127
+ shutdown, shutdown_delay,
128
+ _SENDER_EMAIL, _SENDER_PASSWORD, _SMTP_SERVER, _SMTP_PORT
129
+ )
130
+ )
131
+ p.daemon = True
132
+ p.start()
133
+ except Exception as e:
134
+ print(f"[pymailfeedback] Failed to launch background notification process: {e}", file=sys.stderr)
135
+
136
+ return wrapper
137
+
138
+ return decorator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pymailfeedback
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Email yourself automatically when a long-running Python script succeeds or fails — with traceback, attachments, and more
5
5
  Author-email: Daniele Mascali <danielemascali@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/dmascali/pymailfeedback
@@ -13,6 +13,7 @@ Requires-Python: >=3.7
13
13
  Description-Content-Type: text/markdown
14
14
 
15
15
  # pymailfeedback
16
+ [![PyPI version](https://badge.fury.io/py/pymailfeedback.svg)](https://pypi.org/project/pymailfeedback/)
16
17
 
17
18
  **Never wonder if your long-running script finished or crashed.**
18
19
 
@@ -2,6 +2,7 @@ README.md
2
2
  pyproject.toml
3
3
  src/pymailfeedback/__init__.py
4
4
  src/pymailfeedback/core.py
5
+ src/pymailfeedback/develop_scratch.py
5
6
  src/pymailfeedback.egg-info/PKG-INFO
6
7
  src/pymailfeedback.egg-info/SOURCES.txt
7
8
  src/pymailfeedback.egg-info/dependency_links.txt
File without changes