cjnfuncs 2.2__tar.gz → 2.3__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.
- {cjnfuncs-2.2/src/cjnfuncs.egg-info → cjnfuncs-2.3}/PKG-INFO +6 -2
- {cjnfuncs-2.2 → cjnfuncs-2.3}/README.md +5 -1
- {cjnfuncs-2.2 → cjnfuncs-2.3}/pyproject.toml +1 -1
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/SMTP.py +26 -21
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/configman.py +39 -32
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/core.py +8 -8
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/deployfiles.py +4 -4
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/mungePath.py +43 -35
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/resourcelock.py +19 -11
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/timevalue.py +6 -6
- {cjnfuncs-2.2 → cjnfuncs-2.3/src/cjnfuncs.egg-info}/PKG-INFO +6 -2
- {cjnfuncs-2.2 → cjnfuncs-2.3}/LICENSE.txt +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/setup.cfg +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs/__init__.py +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs.egg-info/SOURCES.txt +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs.egg-info/dependency_links.txt +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs.egg-info/entry_points.txt +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs.egg-info/requires.txt +0 -0
- {cjnfuncs-2.2 → cjnfuncs-2.3}/src/cjnfuncs.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cjnfuncs
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3
|
|
4
4
|
Summary: A collection of core functions for tool script writing
|
|
5
5
|
Author-email: Chris Nelson <github@cjnaz.com>
|
|
6
6
|
License: MIT License
|
|
@@ -37,7 +37,7 @@ Requires-Dist: posix-ipc>=1.1.1
|
|
|
37
37
|
|
|
38
38
|
# cjnfuncs - A framework and collection of utility functions for script writing
|
|
39
39
|
|
|
40
|
-
## cjnfuncs is comprised of several modules (links to respective documentation)
|
|
40
|
+
## cjnfuncs is comprised of several modules (follow links to respective documentation)
|
|
41
41
|
|
|
42
42
|
NOTE: Since relative links to other .md files do not work on PyPI, please go to the [cjnfuncs GitHub repo](https://github.com/cjnaz/cjnfuncs) to read the documentation.
|
|
43
43
|
|
|
@@ -75,6 +75,10 @@ Project repo: https://github.com/cjnaz/cjnfuncs
|
|
|
75
75
|
<br/>
|
|
76
76
|
|
|
77
77
|
## Revision history
|
|
78
|
+
- 2.3 240821 - Added mungePath ./ support.
|
|
79
|
+
Resolved check_path_exists() memory leak.
|
|
80
|
+
Added `same_process_ok` to resourcelock.getlock()
|
|
81
|
+
Added prereload_callback to config_item.loadconfig()
|
|
78
82
|
- 2.2 240119 - Added SMTP DKIM support. Set SMTP server connect timeout to EmailRetryWait.
|
|
79
83
|
- 2.1 240104 - Partitioned to separate modules.
|
|
80
84
|
Added modify_configfile.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# cjnfuncs - A framework and collection of utility functions for script writing
|
|
2
2
|
|
|
3
|
-
## cjnfuncs is comprised of several modules (links to respective documentation)
|
|
3
|
+
## cjnfuncs is comprised of several modules (follow links to respective documentation)
|
|
4
4
|
|
|
5
5
|
NOTE: Since relative links to other .md files do not work on PyPI, please go to the [cjnfuncs GitHub repo](https://github.com/cjnaz/cjnfuncs) to read the documentation.
|
|
6
6
|
|
|
@@ -38,6 +38,10 @@ Project repo: https://github.com/cjnaz/cjnfuncs
|
|
|
38
38
|
<br/>
|
|
39
39
|
|
|
40
40
|
## Revision history
|
|
41
|
+
- 2.3 240821 - Added mungePath ./ support.
|
|
42
|
+
Resolved check_path_exists() memory leak.
|
|
43
|
+
Added `same_process_ok` to resourcelock.getlock()
|
|
44
|
+
Added prereload_callback to config_item.loadconfig()
|
|
41
45
|
- 2.2 240119 - Added SMTP DKIM support. Set SMTP server connect timeout to EmailRetryWait.
|
|
42
46
|
- 2.1 240104 - Partitioned to separate modules.
|
|
43
47
|
Added modify_configfile.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -22,7 +22,8 @@ import cjnfuncs.core as core
|
|
|
22
22
|
|
|
23
23
|
# Configs / Constants
|
|
24
24
|
SND_EMAIL_NTRIES = 3 # Number of tries to send email before aborting
|
|
25
|
-
|
|
25
|
+
RETRY_WAIT = '2s' # seconds between retries
|
|
26
|
+
SERVER_TIMEOUT = '2s' # server connection timeout
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
#=====================================================================================
|
|
@@ -47,26 +48,26 @@ Three attempts are made to send the message.
|
|
|
47
48
|
|
|
48
49
|
|
|
49
50
|
### Parameters
|
|
50
|
-
`subj` (default 'Notification message')
|
|
51
|
+
`subj` (str, default 'Notification message')
|
|
51
52
|
- Text message subject field
|
|
52
53
|
- Some SMS/MMS apps display the subj field in bold, some in raw form, and some not at all.
|
|
53
54
|
|
|
54
|
-
`msg` (default ' ')
|
|
55
|
+
`msg` (str, default ' ')
|
|
55
56
|
- Text message body
|
|
56
57
|
|
|
57
|
-
`to` (default 'NotifList')
|
|
58
|
+
`to` (str, default 'NotifList')
|
|
58
59
|
- To whom to send the message. `to` may be either an explicit string list of email addresses
|
|
59
60
|
(whitespace or comma separated) or a config param name (also listing one
|
|
60
61
|
or more whitespace or comma separated email addresses). If the `to` parameter does not
|
|
61
62
|
contain an '@' it is assumed to be a config param.
|
|
62
63
|
- Define `NotifList` in the config file to use the default `to` value.
|
|
63
64
|
|
|
64
|
-
`log` (default False)
|
|
65
|
+
`log` (bool, default False)
|
|
65
66
|
- If True, logs that the message was sent at the WARNING level. If False, logs
|
|
66
67
|
at the DEBUG level. Useful for eliminating separate logging messages in the tool script code.
|
|
67
68
|
The `subj` field is part of the log message.
|
|
68
69
|
|
|
69
|
-
`smtp_config` (
|
|
70
|
+
`smtp_config` (config_item class instance)
|
|
70
71
|
- config_item class instance containing the [SMTP] section and related params
|
|
71
72
|
|
|
72
73
|
|
|
@@ -133,30 +134,30 @@ Three attempts are made to send the message (see `EmailNTries`, below).
|
|
|
133
134
|
|
|
134
135
|
|
|
135
136
|
### Parameters
|
|
136
|
-
`subj`
|
|
137
|
+
`subj` (str)
|
|
137
138
|
- Email subject text
|
|
138
139
|
|
|
139
|
-
`to`
|
|
140
|
+
`to` (str)
|
|
140
141
|
- To whom to send the message. `to` may be either an explicit string list of email addresses
|
|
141
142
|
(whitespace or comma separated) or a config param name in the [SMTP] section (also listing one
|
|
142
143
|
or more whitespace or comma separated email addresses). If the `to` parameter does not
|
|
143
144
|
contain an '@' it is assumed to be a config param.
|
|
144
145
|
|
|
145
|
-
`body` (default None)
|
|
146
|
+
`body` (str, default None)
|
|
146
147
|
- A string message to be sent
|
|
147
148
|
|
|
148
|
-
`filename` (default None)
|
|
149
|
+
`filename` (str, default None)
|
|
149
150
|
- A str or Path to the file to be sent, relative to the `core.tool.cache_dir`, or an absolute path.
|
|
150
151
|
|
|
151
|
-
`htmlfile` (default None)
|
|
152
|
+
`htmlfile` (str, default None)
|
|
152
153
|
- A str or Path to the html formatted file to be sent, relative to the `core.tool.cache_dir`, or an absolute path.
|
|
153
154
|
|
|
154
|
-
`log` (default False)
|
|
155
|
+
`log` (bool, default False)
|
|
155
156
|
- If True, logs that the message was sent at the WARNING level. If False, logs
|
|
156
157
|
at the DEBUG level. Useful for eliminating separate logging messages in the tool script code.
|
|
157
158
|
The `subj` field is part of the log message.
|
|
158
159
|
|
|
159
|
-
`smtp_config` (
|
|
160
|
+
`smtp_config` (config_item class instance)
|
|
160
161
|
- config_item class instance containing the [SMTP] section and related params
|
|
161
162
|
|
|
162
163
|
|
|
@@ -187,7 +188,9 @@ The `subj` field is part of the log message.
|
|
|
187
188
|
|
|
188
189
|
`EmailRetryWait` (seconds, type int, float, or timevalue, default 2s)
|
|
189
190
|
- Number of seconds to wait between retry attempts
|
|
190
|
-
|
|
191
|
+
|
|
192
|
+
`EmailServerTimeout` (seconds, type int, float, or timevalue, default 2s)
|
|
193
|
+
- Server connection timeout
|
|
191
194
|
|
|
192
195
|
`EmailDKIMDomain` (required if using DKIM email signing)
|
|
193
196
|
- The domain of the public-facing SMTP server, eg `mydomain.com`
|
|
@@ -281,7 +284,8 @@ so it may be practical to bundle `EmailFrom` with the server specifics. Place a
|
|
|
281
284
|
|
|
282
285
|
# Gather, check remaining config params
|
|
283
286
|
ntries = smtp_config.getcfg('EmailNTries', SND_EMAIL_NTRIES, types=int, section='SMTP')
|
|
284
|
-
retry_wait = timevalue(smtp_config.getcfg('EmailRetryWait',
|
|
287
|
+
retry_wait = timevalue(smtp_config.getcfg('EmailRetryWait', RETRY_WAIT, types=[int, float, str], section='SMTP')).seconds
|
|
288
|
+
server_timeout = timevalue(smtp_config.getcfg('EmailServerTimeout', SERVER_TIMEOUT, types=[int, float, str], section='SMTP')).seconds
|
|
285
289
|
email_from = smtp_config.getcfg('EmailFrom', types=str, section='SMTP')
|
|
286
290
|
cfg_server = smtp_config.getcfg('EmailServer', types=str, section='SMTP')
|
|
287
291
|
cfg_port = smtp_config.getcfg('EmailServerPort', types=str, section='SMTP').lower()
|
|
@@ -331,11 +335,11 @@ so it may be practical to bundle `EmailFrom` with the server specifics. Place a
|
|
|
331
335
|
|
|
332
336
|
logging.debug (f"Initialize the SMTP server connection for port <{cfg_port}>")
|
|
333
337
|
if cfg_port == "p25":
|
|
334
|
-
server = smtplib.SMTP(cfg_server, 25, timeout=
|
|
338
|
+
server = smtplib.SMTP(cfg_server, 25, timeout=server_timeout)
|
|
335
339
|
elif cfg_port == "p465":
|
|
336
|
-
server = smtplib.SMTP_SSL(cfg_server, 465, timeout=
|
|
340
|
+
server = smtplib.SMTP_SSL(cfg_server, 465, timeout=server_timeout)
|
|
337
341
|
else: # cfg_port == "p587" or "p587tls"
|
|
338
|
-
server = smtplib.SMTP(cfg_server, 587, timeout=
|
|
342
|
+
server = smtplib.SMTP(cfg_server, 587, timeout=server_timeout)
|
|
339
343
|
|
|
340
344
|
if smtp_config.getcfg("EmailVerbose", False, types=[bool], section='SMTP'):
|
|
341
345
|
logging.debug ("Set SMTP connection debuglevel(1)")
|
|
@@ -362,9 +366,10 @@ so it may be practical to bundle `EmailFrom` with the server specifics. Place a
|
|
|
362
366
|
|
|
363
367
|
except Exception as e:
|
|
364
368
|
last_error = e
|
|
365
|
-
if logging.getLogger().level == logging.DEBUG:
|
|
366
|
-
|
|
369
|
+
# if logging.getLogger().level == logging.DEBUG:
|
|
370
|
+
# logging.exception(f"Email send try {trynum} failed:")
|
|
367
371
|
if trynum < ntries -1:
|
|
372
|
+
logging.debug(f"Email send try {trynum} failed. Retry in <{retry_wait} sec>:\n <{e}>")
|
|
368
373
|
time.sleep(retry_wait)
|
|
369
374
|
continue
|
|
370
375
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -46,18 +46,18 @@ The config_item() class provides handling of one or more config file instances.
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
### Instantiation parameters
|
|
49
|
-
`config_file` (default None)
|
|
49
|
+
`config_file` (str, default None)
|
|
50
50
|
- Path to the configuration file, relative to the `core.tool.config_dir` directory, or an absolute path.
|
|
51
51
|
- `None` may be used if the config will be loaded programmatically via `read_string()` or `read_dict()`.
|
|
52
52
|
|
|
53
|
-
`remap_logdirbase` (default True)
|
|
53
|
+
`remap_logdirbase` (bool, default True)
|
|
54
54
|
- If `remap_logdirbase=True` and the tool script is running in _user_ mode (not site mode)
|
|
55
55
|
then the `core.tool.log_dir_base` will be set to `core.tool.config_dir`.
|
|
56
56
|
|
|
57
|
-
`force_str` (default False)
|
|
57
|
+
`force_str` (bool, default False)
|
|
58
58
|
- Causes all params to be loaded as type `str`, overriding the default type identification.
|
|
59
59
|
|
|
60
|
-
`secondary_config` (default False)
|
|
60
|
+
`secondary_config` (bool, default False)
|
|
61
61
|
- Set to `True` when loading additional config files. Disables logging setup related changes.
|
|
62
62
|
- The primary config file should be loaded first before any secondary_config loads, so that logging
|
|
63
63
|
is properly set up.
|
|
@@ -208,7 +208,7 @@ output:
|
|
|
208
208
|
***config_item() class member function***
|
|
209
209
|
|
|
210
210
|
### Parameters
|
|
211
|
-
`section` (default '')
|
|
211
|
+
`section` (str, default '')
|
|
212
212
|
- `section = ''` clears the entire cfg dictionary, including all sections and DEFAULT
|
|
213
213
|
- `section = '<section_name>'` clears just that section
|
|
214
214
|
- `section = 'DEFAULT'` clears just the DEFAULT section
|
|
@@ -290,7 +290,8 @@ output:
|
|
|
290
290
|
flush_on_reload = False,
|
|
291
291
|
force_flush_reload = False,
|
|
292
292
|
isimport = False,
|
|
293
|
-
tolerate_missing = False
|
|
293
|
+
tolerate_missing = False,
|
|
294
|
+
prereload_callback = None):
|
|
294
295
|
"""
|
|
295
296
|
## loadconfig () - Load a configuration file into the cfg dictionary
|
|
296
297
|
```
|
|
@@ -301,7 +302,8 @@ loadconfig(
|
|
|
301
302
|
flush_on_reload = False,
|
|
302
303
|
force_flush_reload = False,
|
|
303
304
|
isimport = False,
|
|
304
|
-
tolerate_missing = False
|
|
305
|
+
tolerate_missing = False,
|
|
306
|
+
prereload_callback = None)
|
|
305
307
|
```
|
|
306
308
|
***config_item() class member function***
|
|
307
309
|
|
|
@@ -315,29 +317,31 @@ feature, and intermittent loss of access to the config file.
|
|
|
315
317
|
|
|
316
318
|
|
|
317
319
|
### Parameters
|
|
318
|
-
`ldcfg_ll` (default 30
|
|
320
|
+
`ldcfg_ll` (int, default 30 (WARNING))
|
|
319
321
|
- Logging level used within `loadconfig()` code for debugging loadconfig() itself
|
|
320
322
|
|
|
321
|
-
`call_logfile` (default None)
|
|
323
|
+
`call_logfile` (str, default None)
|
|
322
324
|
- An absolute path or relative to the `core.tool.log_dir_base` directory
|
|
323
325
|
|
|
324
|
-
`call_logfile_wins` (default False)
|
|
326
|
+
`call_logfile_wins` (bool, default False)
|
|
325
327
|
- If True, the `call_logfile` overrides any `LogFile` in the config file
|
|
326
328
|
|
|
327
|
-
`flush_on_reload` (default False)
|
|
329
|
+
`flush_on_reload` (bool, default False)
|
|
328
330
|
- If the config file will be reloaded (due to a changed timestamp) then clean out the
|
|
329
331
|
`cfg` dictionary first
|
|
330
332
|
|
|
331
|
-
`force_flush_reload` (default False)
|
|
333
|
+
`force_flush_reload` (bool, default False)
|
|
332
334
|
- Forces the `cfg` dictionary to be cleaned out and the config file to be reloaded,
|
|
333
335
|
regardless of whether the config file timestamp has changed
|
|
334
336
|
|
|
335
|
-
`isimport` (default False)
|
|
337
|
+
`isimport` (bool, default False)
|
|
336
338
|
- Internally set True when handling imports. Not used by tool script calls.
|
|
337
339
|
|
|
338
|
-
`tolerate_missing` (default False)
|
|
340
|
+
`tolerate_missing` (bool, default False)
|
|
339
341
|
- Used in a tool script service loop, return `-1` rather than raising `ConfigError` if the config file is inaccessible
|
|
340
342
|
|
|
343
|
+
`prereload_callback` (function, default None)
|
|
344
|
+
- Allows user services to be managed (paused/terminated) before the config is reloaded and logging is reset.
|
|
341
345
|
|
|
342
346
|
### Returns
|
|
343
347
|
- `1` if the config files WAS reloaded
|
|
@@ -470,9 +474,12 @@ a ConfigError if the config file cannot be accessed.
|
|
|
470
474
|
if self.config_timestamp == current_timestamp:
|
|
471
475
|
return 0 # 0 indicates that the config file was NOT (re)loaded
|
|
472
476
|
|
|
473
|
-
# It's an initial load call, or config file has changed
|
|
477
|
+
# It's an initial load call, or config file has changed, or force_flush_reload... Do (re)load
|
|
474
478
|
self.config_timestamp = current_timestamp
|
|
475
479
|
logging.getLogger().setLevel(ldcfg_ll) # Set logging level for remainder of loadconfig call
|
|
480
|
+
if prereload_callback:
|
|
481
|
+
logging.debug("Pre-reload callback user function called")
|
|
482
|
+
prereload_callback()
|
|
476
483
|
logging.info (f"Config <{self.config_file}> file timestamp: {current_timestamp}")
|
|
477
484
|
|
|
478
485
|
if flush_on_reload:
|
|
@@ -541,13 +548,13 @@ flush_on_reload, force_flush_reload, and tolerate_missing.
|
|
|
541
548
|
|
|
542
549
|
|
|
543
550
|
### Parameters
|
|
544
|
-
`str_blob`
|
|
551
|
+
`str_blob` (str)
|
|
545
552
|
- String containing the lines of config data
|
|
546
553
|
|
|
547
|
-
`ldcfg_ll` (default 30
|
|
554
|
+
`ldcfg_ll` (int, default 30 (WARNING))
|
|
548
555
|
- Logging level used within `read_string()` code for debugging read_string() itself
|
|
549
556
|
|
|
550
|
-
`isimport` (default False)
|
|
557
|
+
`isimport` (bool, default False)
|
|
551
558
|
- Internally set True when handling imports. Not used by tool script calls.
|
|
552
559
|
|
|
553
560
|
|
|
@@ -627,10 +634,10 @@ flush_on_reload, force_flush_reload, and tolerate_missing.
|
|
|
627
634
|
Loaded content is added to and/or modifies any previously loaded content.
|
|
628
635
|
|
|
629
636
|
### Parameters
|
|
630
|
-
`param_dict`
|
|
637
|
+
`param_dict` (dict)
|
|
631
638
|
- dictionary to be loaded
|
|
632
639
|
|
|
633
|
-
`section_name` (default ''
|
|
640
|
+
`section_name` (str, default '' (top level))
|
|
634
641
|
- section to load the param_dict into.
|
|
635
642
|
- The section will be created if not yet existing.
|
|
636
643
|
- Content can only be loaded into one section per call to read_dict().
|
|
@@ -713,18 +720,18 @@ This can lead to cleaner tool script code. Either access method may be used, al
|
|
|
713
720
|
|
|
714
721
|
|
|
715
722
|
### Parameters
|
|
716
|
-
`param`
|
|
723
|
+
`param` (str)
|
|
717
724
|
- String name of param to be fetched from cfg
|
|
718
725
|
|
|
719
|
-
`fallback` (default None)
|
|
726
|
+
`fallback` (as-expected type, default None)
|
|
720
727
|
- if provided, is returned if `param` does not exist in cfg
|
|
721
728
|
|
|
722
|
-
`types` (default '[]'
|
|
729
|
+
`types` (single or list of as-expected types, default '[]' (any type accepted))
|
|
723
730
|
- if provided, a ConfigError is raised if the param's value type is not in the list of expected types
|
|
724
731
|
- `types` may be a single type (eg, `types=int`) or a list of types (eg, `types=[int, float]`)
|
|
725
732
|
- Supported types: [str, int, float, bool, list, tuple, dict]
|
|
726
733
|
|
|
727
|
-
`section` (default ''
|
|
734
|
+
`section` (str, default '' (top-level))
|
|
728
735
|
- Select the section from which to get the param value.
|
|
729
736
|
|
|
730
737
|
|
|
@@ -803,22 +810,22 @@ reload call to avoid multiple config reloads.
|
|
|
803
810
|
|
|
804
811
|
|
|
805
812
|
### Parameters
|
|
806
|
-
`param` (default ' ')
|
|
813
|
+
`param` (str, default ' ')
|
|
807
814
|
- The param name, if modifying an existing param or adding a new param
|
|
808
815
|
|
|
809
|
-
`value` (default ' ')
|
|
816
|
+
`value` (as-expected type, default ' ')
|
|
810
817
|
- The new value to be applied to an existing param, or an added param
|
|
811
818
|
- Any comment text (after a '#') in the new value will be prepended to any existing comment text
|
|
812
819
|
|
|
813
|
-
`remove` (default False)
|
|
820
|
+
`remove` (bool, default False)
|
|
814
821
|
- If True, the `param` config file line is removed from the config file
|
|
815
822
|
|
|
816
|
-
`add_if_not_existing` (default False)
|
|
823
|
+
`add_if_not_existing` (bool, default False)
|
|
817
824
|
- Modify an existing param line, or add at the bottom of the config file if it is not existing
|
|
818
825
|
- To add a blank line leave out both `param` and `value`, or set both the `""`
|
|
819
826
|
- To add a comment line specify the comment in the `param` field (eg, `my_config.modify_configfile("# My comment")`)
|
|
820
827
|
|
|
821
|
-
`save` (default False)
|
|
828
|
+
`save` (bool, default False)
|
|
822
829
|
- Write the modified config file content back to the file
|
|
823
830
|
- `save=True` may be specified on the last modification call or an a standalone call.
|
|
824
831
|
|
|
@@ -893,8 +900,8 @@ reload call to avoid multiple config reloads.
|
|
|
893
900
|
|
|
894
901
|
### Parameter
|
|
895
902
|
|
|
896
|
-
`savefile`
|
|
897
|
-
- Path to the output file.
|
|
903
|
+
`savefile` (Path or str)
|
|
904
|
+
- Path to the output file.
|
|
898
905
|
- The config data will be written to an absolute path, or relative to the `core.tool.config_dir`
|
|
899
906
|
|
|
900
907
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
# TODO Identification of the main module / tool script file may be wrong if a script imports a script that then imports cjnfuncs.
|
|
10
10
|
#==========================================================
|
|
@@ -87,8 +87,8 @@ found then user-specific is assumed. No directories are created.
|
|
|
87
87
|
|
|
88
88
|
|
|
89
89
|
### Parameter
|
|
90
|
-
`toolname`
|
|
91
|
-
- Name of the tool
|
|
90
|
+
`toolname` (str)
|
|
91
|
+
- Name of the tool
|
|
92
92
|
|
|
93
93
|
|
|
94
94
|
### Returns
|
|
@@ -208,25 +208,25 @@ the log file declared in the config file:
|
|
|
208
208
|
|
|
209
209
|
|
|
210
210
|
### Parameters
|
|
211
|
-
`call_logfile` (default None)
|
|
211
|
+
`call_logfile` (str, default None)
|
|
212
212
|
- Potential log file passed typically from the tool script. Selected by `call_logfile_wins = True`.
|
|
213
213
|
call_logfile may be an absolute path or relative to the `core.tool.log_dir_base` directory.
|
|
214
214
|
- `None` specifies the console.
|
|
215
215
|
|
|
216
|
-
`call_logfile_wins` (default False)
|
|
216
|
+
`call_logfile_wins` (bool, default False)
|
|
217
217
|
- If True, the `call_logfile` is selected. If False, the `config_logfile` is selected.
|
|
218
218
|
|
|
219
|
-
`config_logfile` (default None)
|
|
219
|
+
`config_logfile` (str, default None)
|
|
220
220
|
- Potential log file passed typically from loadconfig() if there is a `LogFile` param in the
|
|
221
221
|
loaded config. Selected by `call_logfile_wins = False`.
|
|
222
222
|
config_logfile may be an absolute path or relative to the `core.tool.log_dir_base` directory.
|
|
223
223
|
- `None` specifies the console.
|
|
224
224
|
|
|
225
|
-
`ConsoleLogFormat` (default None)
|
|
225
|
+
`ConsoleLogFormat` (str, default None)
|
|
226
226
|
- Overrides the default console logging format: `{module:>15}.{funcName:20} - {levelname:>8}: {message}`.
|
|
227
227
|
- loadconfig() passes `ConsoleLogFormat` from the primary config file, if defined.
|
|
228
228
|
|
|
229
|
-
`FileLogFormat` (default None)
|
|
229
|
+
`FileLogFormat` (str, default None)
|
|
230
230
|
- Overrides the default file logging format: `{asctime} {module:>15}.{funcName:20} {levelname:>8}: {message}`.
|
|
231
231
|
- loadconfig() passes `FileLogFormat` from the primary config file, if defined.
|
|
232
232
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -44,7 +44,7 @@ Distribution files and directory trees are hosted in `<module_root>/deployment_f
|
|
|
44
44
|
|
|
45
45
|
|
|
46
46
|
### Parameters
|
|
47
|
-
`files_list`
|
|
47
|
+
`files_list` (list of dictionaries)
|
|
48
48
|
- A list of dictionaries, each specifying a `source` file or directory tree to be copied to a `target_dir`.
|
|
49
49
|
- `source` - Either an individual file or directory tree within and relative to `<module_root>/deployment_files/`.
|
|
50
50
|
No wildcard support.
|
|
@@ -56,11 +56,11 @@ Distribution files and directory trees are hosted in `<module_root>/deployment_f
|
|
|
56
56
|
- `file_stat` - Permissions set on each created file (default 0o664)
|
|
57
57
|
- `dir_stat` - Permissions set on each created directory (if not already existing, default 0o775)
|
|
58
58
|
|
|
59
|
-
`overwrite`
|
|
59
|
+
`overwrite` (bool, default False)
|
|
60
60
|
- If overwrite=False (default) then only missing files will be copied. If overwrite=True then all files will be overwritten
|
|
61
61
|
if they exist - data may be lost!
|
|
62
62
|
|
|
63
|
-
`missing_ok`
|
|
63
|
+
`missing_ok` (bool, default False)
|
|
64
64
|
- If missing_ok=True then a missing source file or directory is tolerated (non-fatal). This feature is used for testing.
|
|
65
65
|
|
|
66
66
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -32,19 +32,21 @@ User (`~user/`) and environment vars (`$HOME/`) are supported and expanded.
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
### Parameters
|
|
35
|
-
`in_path` (default '')
|
|
35
|
+
`in_path` (Path or str, default '')
|
|
36
36
|
- An absolute or relative path to a file or directory, such as `mydir/myfile.txt`
|
|
37
|
-
- If `in_path` is an absolute path then `base_path` is disregarded.
|
|
37
|
+
- If `in_path` is an absolute path then the `base_path` is disregarded.
|
|
38
|
+
- If `in_path` starts with `./` then the absolute path to the current working directory (cwd) is prepended
|
|
39
|
+
to `in_path`, and the `base_path` is disregarded. See Special handling note, below.
|
|
38
40
|
|
|
39
|
-
`base_path` (default '')
|
|
41
|
+
`base_path` (Path or str, default '')
|
|
40
42
|
- An absolute or relative path to a directory, such as `~/.config/mytool`
|
|
41
43
|
- `base_path` is prepended to `in_path` if `in_path` is a relative path
|
|
42
44
|
- `base_path = ''` (the default) results in a relative path based on the shell current working directory (cwd)
|
|
43
45
|
- `base_path = '.'` results in an absolute path based on the shell cwd
|
|
44
46
|
- `base_path = core.tool.main_dir` results in an absolute path based on the tool script directory
|
|
45
47
|
|
|
46
|
-
`mkdir`
|
|
47
|
-
- Force-make a full directory path. `
|
|
48
|
+
`mkdir` (bool, default False)
|
|
49
|
+
- Force-make a full directory path. `base_path` / `in_path` is understood to be to a directory.
|
|
48
50
|
|
|
49
51
|
|
|
50
52
|
### Returns
|
|
@@ -69,21 +71,40 @@ Attribute | Type | Description
|
|
|
69
71
|
- If `in_path` is a relative path (eg, `mydir/myfile.txt`) portion then the `base_path` is prepended.
|
|
70
72
|
- If both `in_path` and `base_path` are relative then the combined path will also be relative, usually to
|
|
71
73
|
the shell cwd.
|
|
72
|
-
- If `in_path` is an absolute path (eg, `/tmp/mydir/myfile.txt`) then the `base_path` is
|
|
74
|
+
- If `in_path` is an absolute path (eg, `/tmp/mydir/myfile.txt`) then the `base_path` is disregarded.
|
|
75
|
+
- **Special handling for `in_path` starting with `./`:** Normally, paths starting with `.` are relative paths.
|
|
76
|
+
mungePath interprets `in_path` starting with `./` as an absolute path reference to the shell current working
|
|
77
|
+
directory (cwd).
|
|
78
|
+
Often in a tool script a user path input is passed to the `in_path` parameter. Using the `./` prefix, a file in
|
|
79
|
+
the shell cwd may be
|
|
80
|
+
referenced, eg `./myfile`. _Covering the cases, assuming the shell cwd is `/home/me`:_
|
|
81
|
+
|
|
82
|
+
in_path | base_path | .full_path resolves to
|
|
83
|
+
-- | -- | --
|
|
84
|
+
myfile | /tmp | /tmp/myfile
|
|
85
|
+
./myfile | /tmp | /home/me/myfile
|
|
86
|
+
../myfile | /tmp | /tmp/../myfile
|
|
87
|
+
./../myfile | /tmp | /home/me/../myfile
|
|
88
|
+
xyz/myfile | /tmp | /tmp/xyz/myfile
|
|
89
|
+
./xyz/myfile | /tmp | /home/me/xyz/myfile
|
|
90
|
+
|
|
73
91
|
- `in_path` and `base_path` may be type str(), Path(), or PurePath().
|
|
74
92
|
- Symlinks are followed (not resolved).
|
|
75
93
|
- User and environment vars are expanded, eg `~/.config` >> `/home/me/.config`, as does `$HOME/.config`.
|
|
76
94
|
- The `.parent` is the directory containing (above) the `.full_path`. If the object `.is_file` then `.parent` is the
|
|
77
95
|
directory containing the file. If the object `.is_dir` then the `.full_path` includes the end-point directory, and
|
|
78
96
|
`.parent` is the directory above the end-point directory.
|
|
79
|
-
- When using `mkdir=True` the combined `
|
|
97
|
+
- When using `mkdir=True` the combined `base_path` / `in_path` is understood to be a directory path (not
|
|
80
98
|
to a file), and will be created if it does not already exist. (Uses `pathlib.Path.mkdir()`). A FileExistsError
|
|
81
99
|
is raised if you attempt to mkdir on top of an existing file.
|
|
82
100
|
- See [GitHub repo](https://github.com/cjnaz/cjnfuncs) tests/demo-mungePath.py for numerous application examples.
|
|
83
101
|
"""
|
|
84
102
|
|
|
85
|
-
|
|
86
|
-
|
|
103
|
+
in_path = str(in_path)
|
|
104
|
+
base_path = str(base_path)
|
|
105
|
+
|
|
106
|
+
if in_path.startswith('./'):
|
|
107
|
+
in_path = Path.cwd() / in_path
|
|
87
108
|
|
|
88
109
|
in_path_pp = PurePath(os.path.expandvars(os.path.expanduser(str(in_path))))
|
|
89
110
|
|
|
@@ -96,24 +117,12 @@ is raised if you attempt to mkdir on top of an existing file.
|
|
|
96
117
|
|
|
97
118
|
if mkdir:
|
|
98
119
|
Path(in_path_pp).mkdir(parents=True, exist_ok=True)
|
|
99
|
-
# try:
|
|
100
|
-
# Path(in_path_pp).mkdir(parents=True, exist_ok=True)
|
|
101
|
-
# except Exception as e:
|
|
102
|
-
# raise FileExistsError (e)
|
|
103
120
|
|
|
104
121
|
self.parent = Path(in_path_pp.parent)
|
|
105
122
|
self.full_path = Path(in_path_pp)
|
|
106
123
|
|
|
107
124
|
self.name = self.full_path.name
|
|
108
|
-
self.
|
|
109
|
-
self.is_absolute = self.full_path.is_absolute()
|
|
110
|
-
self.is_relative = not self.is_absolute
|
|
111
|
-
try:
|
|
112
|
-
self.is_dir = self.full_path.is_dir()
|
|
113
|
-
self.is_file = self.full_path.is_file()
|
|
114
|
-
except: # Trap if the path does not exist
|
|
115
|
-
self.is_dir = False
|
|
116
|
-
self.is_file = False
|
|
125
|
+
self.refresh_stats()
|
|
117
126
|
|
|
118
127
|
|
|
119
128
|
#=====================================================================================
|
|
@@ -166,7 +175,7 @@ happen on the filesystem. Call refresh_stats() as needed.
|
|
|
166
175
|
#=====================================================================================
|
|
167
176
|
#=====================================================================================
|
|
168
177
|
|
|
169
|
-
def check_path_exists(
|
|
178
|
+
def check_path_exists(inpath, timeout=1):
|
|
170
179
|
"""
|
|
171
180
|
## check_path_exists (path, timeout=1) - With enforced timeout (no hang)
|
|
172
181
|
|
|
@@ -177,10 +186,10 @@ Implementation stolen from https://stackoverflow.com/questions/67819869/how-to-e
|
|
|
177
186
|
|
|
178
187
|
|
|
179
188
|
### Parameters
|
|
180
|
-
`path`
|
|
181
|
-
-
|
|
189
|
+
`path` (Path or str)
|
|
190
|
+
- Path to a file or directory
|
|
182
191
|
|
|
183
|
-
`timeout` (default 1 second)
|
|
192
|
+
`timeout` (int or float, default 1 second)
|
|
184
193
|
- resolution seconds
|
|
185
194
|
|
|
186
195
|
|
|
@@ -188,12 +197,11 @@ Implementation stolen from https://stackoverflow.com/questions/67819869/how-to-e
|
|
|
188
197
|
- True if the path exists
|
|
189
198
|
- False if the path does not exist or the timeout is reached
|
|
190
199
|
"""
|
|
191
|
-
path = Path(path)
|
|
192
|
-
executor = ThreadPoolExecutor(max_workers=1)
|
|
193
|
-
future = executor.submit(path.exists)
|
|
194
|
-
try:
|
|
195
|
-
return future.result(timeout)
|
|
196
|
-
except TimeoutError:
|
|
197
|
-
return False
|
|
198
|
-
|
|
199
200
|
|
|
201
|
+
_path = Path(inpath)
|
|
202
|
+
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
203
|
+
future = executor.submit(_path.exists)
|
|
204
|
+
try:
|
|
205
|
+
return future.result(timeout)
|
|
206
|
+
except TimeoutError:
|
|
207
|
+
return False
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, Copyright
|
|
7
|
+
# Chris Nelson, Copyright 2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -60,8 +60,8 @@ currently 0 (indicating locked), so it is ***recommended*** to have (possibly ex
|
|
|
60
60
|
such as in your interrupt-trapped cleanup code.
|
|
61
61
|
|
|
62
62
|
### Parameters
|
|
63
|
-
`lockname`
|
|
64
|
-
- All processes sharing
|
|
63
|
+
`lockname` (str)
|
|
64
|
+
- All processes sharing a given resource must use the same lockname.
|
|
65
65
|
"""
|
|
66
66
|
|
|
67
67
|
def __init__ (self, lockname):
|
|
@@ -78,28 +78,36 @@ such as in your interrupt-trapped cleanup code.
|
|
|
78
78
|
#=====================================================================================
|
|
79
79
|
#=====================================================================================
|
|
80
80
|
|
|
81
|
-
def get_lock(self, timeout=1):
|
|
81
|
+
def get_lock(self, timeout=1, same_process_ok=False):
|
|
82
82
|
"""
|
|
83
|
-
## get_lock (timeout=1) - Request the resource lock
|
|
83
|
+
## get_lock (timeout=1, same_process_ok=False) - Request the resource lock
|
|
84
84
|
|
|
85
85
|
***resource_lock() class member function***
|
|
86
86
|
|
|
87
87
|
Attempt to acquire/get the lock while waiting up to `timeout` time.
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
By default, get_lock() waits for the lock if it is currently set, whether the lock was set by this
|
|
90
|
+
or another script/job/process.
|
|
91
|
+
|
|
92
|
+
By setting `same_process_ok=True`, then if the lock was previously acquired by this same script/process
|
|
93
|
+
then get_lock() immediately returns True. This allows the script code to not have to track state to
|
|
94
|
+
decide if the lock has previously been acquired before calling get_lock() again, leading to cleaner code.
|
|
92
95
|
|
|
93
96
|
### Parameters
|
|
94
|
-
`timeout` (default 1 second)
|
|
97
|
+
`timeout` (int or float, default 1 second)
|
|
95
98
|
- The max time, in seconds, to wait to acquire the lock
|
|
96
99
|
- None is no timeout - wait forever (Hang forever. Unwise.)
|
|
97
100
|
|
|
101
|
+
`same_process_ok` (bool, default False)
|
|
102
|
+
- If True, then if the current process currently has the lock then get_lock() immediately returns True.
|
|
103
|
+
- If False, then if the lock is currently set by the same process or another process then get_lock() blocks
|
|
104
|
+
with timeout.
|
|
105
|
+
|
|
98
106
|
### Returns
|
|
99
107
|
- True: Lock successfully acquired, timeout time not exceeded
|
|
100
108
|
- False: Lock request failed, timed out
|
|
101
109
|
"""
|
|
102
|
-
if self.I_have_the_lock == True:
|
|
110
|
+
if same_process_ok and self.I_have_the_lock == True:
|
|
103
111
|
return True
|
|
104
112
|
|
|
105
113
|
try:
|
|
@@ -131,7 +139,7 @@ in the same unset state.
|
|
|
131
139
|
unless `force=True`.
|
|
132
140
|
|
|
133
141
|
### Parameter
|
|
134
|
-
`force` (default False)
|
|
142
|
+
`force` (bool, default False)
|
|
135
143
|
- Release the lock regardless of whether or not this process acquired it.
|
|
136
144
|
- Useful for forced cleanup, for example, by the CLI interface.
|
|
137
145
|
- Dangerous if another process had acquired the lock. Be careful.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
#==========================================================
|
|
6
6
|
#
|
|
7
|
-
# Chris Nelson, 2018-
|
|
7
|
+
# Chris Nelson, 2018-2024
|
|
8
8
|
#
|
|
9
9
|
#==========================================================
|
|
10
10
|
|
|
@@ -27,8 +27,8 @@ Supported timevalue units are 's'econds, 'm'inutes, 'h'ours, 'd'ays, and 'w'eeks
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
### Parameters
|
|
30
|
-
`orig_val`
|
|
31
|
-
- The original, passed-in value
|
|
30
|
+
`orig_val` (str, int, or float)
|
|
31
|
+
- The original, passed-in value
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
### Returns
|
|
@@ -101,10 +101,10 @@ def retime(time_sec, unitC):
|
|
|
101
101
|
|
|
102
102
|
|
|
103
103
|
### Parameters
|
|
104
|
-
`time_sec`
|
|
105
|
-
-
|
|
104
|
+
`time_sec` (int or float)
|
|
105
|
+
- The time value in resolution seconds to be converted
|
|
106
106
|
|
|
107
|
-
`unitC`
|
|
107
|
+
`unitC` (str)
|
|
108
108
|
- Target time resolution: "s", "m", "h", "d", or "w" (case insensitive)
|
|
109
109
|
|
|
110
110
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: cjnfuncs
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.3
|
|
4
4
|
Summary: A collection of core functions for tool script writing
|
|
5
5
|
Author-email: Chris Nelson <github@cjnaz.com>
|
|
6
6
|
License: MIT License
|
|
@@ -37,7 +37,7 @@ Requires-Dist: posix-ipc>=1.1.1
|
|
|
37
37
|
|
|
38
38
|
# cjnfuncs - A framework and collection of utility functions for script writing
|
|
39
39
|
|
|
40
|
-
## cjnfuncs is comprised of several modules (links to respective documentation)
|
|
40
|
+
## cjnfuncs is comprised of several modules (follow links to respective documentation)
|
|
41
41
|
|
|
42
42
|
NOTE: Since relative links to other .md files do not work on PyPI, please go to the [cjnfuncs GitHub repo](https://github.com/cjnaz/cjnfuncs) to read the documentation.
|
|
43
43
|
|
|
@@ -75,6 +75,10 @@ Project repo: https://github.com/cjnaz/cjnfuncs
|
|
|
75
75
|
<br/>
|
|
76
76
|
|
|
77
77
|
## Revision history
|
|
78
|
+
- 2.3 240821 - Added mungePath ./ support.
|
|
79
|
+
Resolved check_path_exists() memory leak.
|
|
80
|
+
Added `same_process_ok` to resourcelock.getlock()
|
|
81
|
+
Added prereload_callback to config_item.loadconfig()
|
|
78
82
|
- 2.2 240119 - Added SMTP DKIM support. Set SMTP server connect timeout to EmailRetryWait.
|
|
79
83
|
- 2.1 240104 - Partitioned to separate modules.
|
|
80
84
|
Added modify_configfile.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|