cjnfuncs 3.0__tar.gz → 3.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.
- {cjnfuncs-3.0/src/cjnfuncs.egg-info → cjnfuncs-3.1}/PKG-INFO +20 -23
- {cjnfuncs-3.0 → cjnfuncs-3.1}/README.md +16 -21
- {cjnfuncs-3.0 → cjnfuncs-3.1}/SMTP.md +35 -20
- {cjnfuncs-3.0 → cjnfuncs-3.1}/configman.md +83 -53
- {cjnfuncs-3.0 → cjnfuncs-3.1}/core.md +81 -34
- {cjnfuncs-3.0 → cjnfuncs-3.1}/deployfiles.md +45 -10
- {cjnfuncs-3.0 → cjnfuncs-3.1}/mungePath.md +36 -25
- cjnfuncs-3.1/pyproject.toml +52 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/resourcelock.md +75 -44
- {cjnfuncs-3.0 → cjnfuncs-3.1}/rwt.md +56 -22
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/SMTP.py +44 -30
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/configman.py +121 -103
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/core.py +106 -47
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/deployfiles.py +34 -23
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/mungePath.py +8 -6
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/resourcelock.py +44 -30
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/rwt.py +86 -78
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/timevalue.py +5 -5
- {cjnfuncs-3.0 → cjnfuncs-3.1/src/cjnfuncs.egg-info}/PKG-INFO +20 -23
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs.egg-info/requires.txt +2 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/timevalue.md +5 -5
- cjnfuncs-3.0/pyproject.toml +0 -39
- {cjnfuncs-3.0 → cjnfuncs-3.1}/LICENSE +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/MANIFEST.in +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/setup.cfg +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs/__init__.py +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs.egg-info/SOURCES.txt +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs.egg-info/dependency_links.txt +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs.egg-info/entry_points.txt +0 -0
- {cjnfuncs-3.0 → cjnfuncs-3.1}/src/cjnfuncs.egg-info/top_level.txt +0 -0
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cjnfuncs
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.1
|
|
4
4
|
Summary: A collection of core functions for tool script writing
|
|
5
5
|
Author-email: Chris Nelson <github@cjnaz.com>
|
|
6
6
|
License-Expression: MIT
|
|
7
7
|
Project-URL: repository, https://github.com/cjnaz/cjnfuncs
|
|
8
|
+
Keywords: cjnaz,library,cjnfuncs
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
10
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
9
11
|
Requires-Python: >=3.9
|
|
10
12
|
Description-Content-Type: text/markdown
|
|
@@ -13,7 +15,7 @@ Requires-Dist: requests
|
|
|
13
15
|
Requires-Dist: appdirs>=1.4
|
|
14
16
|
Requires-Dist: dkimpy
|
|
15
17
|
Requires-Dist: importlib_resources
|
|
16
|
-
Requires-Dist: posix-ipc>=1.1.1
|
|
18
|
+
Requires-Dist: posix-ipc>=1.1.1; sys_platform == "linux"
|
|
17
19
|
Dynamic: license-file
|
|
18
20
|
|
|
19
21
|
# cjnfuncs - A framework and collection of utility functions for script writing
|
|
@@ -34,7 +36,7 @@ module | Description/Purpose
|
|
|
34
36
|
[SMTP](SMTP.md) | Send notification and email messages
|
|
35
37
|
|
|
36
38
|
Developed and tested on Python 3.9.21 and supported on all higher Python versions.
|
|
37
|
-
Developed on Linux.
|
|
39
|
+
Developed on Linux. Supported on Windows, except for the resourcelock module (posix-ipc module dependency).
|
|
38
40
|
|
|
39
41
|
In this documentation, "tool script" refers to a Python project that imports and uses cjnfuncs. Some may be simple scripts, and others may themselves be installed packages.
|
|
40
42
|
|
|
@@ -56,35 +58,30 @@ Project repo: https://github.com/cjnaz/cjnfuncs
|
|
|
56
58
|
|
|
57
59
|
<br/>
|
|
58
60
|
|
|
59
|
-
## Key changes since the prior major public release (version
|
|
61
|
+
## Key changes since the prior major public release (version 3.0)
|
|
60
62
|
|
|
61
|
-
|
|
62
|
-
- If a network share (or an internet resource) is
|
|
63
|
-
unavailable or extremely slow then the tool script can hang indefinitely. `run_with_timeout()` can wrap any function call
|
|
64
|
-
(any callable - built-in, standard library, installed package, or defined within the tool script) with an enforced timeout
|
|
65
|
-
and number of retries on timeout so that your code can make rational decisions when there are access issues.
|
|
63
|
+
- Several issues with Windows support were fixed.
|
|
66
64
|
|
|
67
|
-
`
|
|
68
|
-
- When you want this, you want this. Its used in configman, for instance, to allow for debug logging within the configman code (valuable for cjnfuncs debug and regression testing). If you are developing a hardware sensor driver for example, you can enable debug logging of calls to your driver code
|
|
69
|
-
and restore the level before return.
|
|
70
|
-
You can use `set_logging_level()` standalone, or with later `restore_logging_level()` calls.
|
|
65
|
+
- `configman.config_item()` now supports a safe_mode switch, which will speed up Windows usage of loadconfig() at some risk. See the note on `config_item()`.
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
|
|
67
|
+
- Named child loggers are now implemented on several cjnfuncs modules. By default, logging from cjnfuncs modules is disabled (logging events are usually at the INFO or DEBUG
|
|
68
|
+
level, while the default logging level is set to WARNING). For example, logging from configman may be enabled by `logging.getLogger('cjnfuncs.configman').setLevel(logging.INFO)`,
|
|
69
|
+
or using `core.set_logging.level(logging.INFO, 'cjnfuncs.configman')`.
|
|
74
70
|
|
|
75
|
-
|
|
76
|
-
- A lot of what I develop are process monitoring tools. These typically implement a `while True` loop where various operations get triggered periodically on their own schedules. `get_next_dt()` provides a simple and clean way to get the next `datetime` to do an operation (in my usage, as defined in a config file). `get_next_dt()` supports arbitrary times of day and days of week. For example, trigger generating a summary report every Thursday at 9AM:
|
|
71
|
+
- Changed mungePath set_attributes from default True to default False to avoid long processing time on Windows by default.
|
|
77
72
|
|
|
78
|
-
|
|
79
|
-
gen_summary()
|
|
80
|
-
generate_summary_dt = get_next_dt('9:00, 'Thursday')
|
|
81
|
-
|
|
82
|
-
`mungePath(..., set_attributes=False)` switch
|
|
83
|
-
- On network outages, my apps would slow to a crawl. The root of the issue was in checking if a network file exists, with no timeout mechanism. The pathlib `.exists()`, `.is_dir()`, and `is_file()` methods are all susceptible to indefinate hangs. The set_attributues switch has been added to mungePath, with a default value of False. _Note that this is a change to default behavior._ If set True then `refresh_stats()` is called (or you can call it directly yourself, as needed). `refresh_stats()` and `check_file_exists()` have been rewritten to utilize `run_with_timeout()`, with a default timeout of 1.0 seconds. I then `periodic_log()` the access problem. Ah, so much more stable.
|
|
73
|
+
- deployfiles can now create an empty target directory.
|
|
84
74
|
|
|
85
75
|
<br/>
|
|
86
76
|
|
|
87
77
|
## Revision history
|
|
78
|
+
- 3.1 251109
|
|
79
|
+
Support for and use of child loggers,
|
|
80
|
+
deployfiles create empty dir,
|
|
81
|
+
Windows fixes,
|
|
82
|
+
config_item() safe_mode switch,
|
|
83
|
+
mungePath set_attributes from default False
|
|
84
|
+
- 3.0.1 251005 - Allow '.' in config section names
|
|
88
85
|
- 3.0 250705 - Added run_with_timeout, set / restore_logging_level, periodic_logging, get_next_dt. Functional change to mungePath.
|
|
89
86
|
- 2.5 250206 - Added multi-line and quoted string support to configman
|
|
90
87
|
- 2.4.1 241118 - resource_lock only init lock_info if not existing
|
|
@@ -16,7 +16,7 @@ module | Description/Purpose
|
|
|
16
16
|
[SMTP](SMTP.md) | Send notification and email messages
|
|
17
17
|
|
|
18
18
|
Developed and tested on Python 3.9.21 and supported on all higher Python versions.
|
|
19
|
-
Developed on Linux.
|
|
19
|
+
Developed on Linux. Supported on Windows, except for the resourcelock module (posix-ipc module dependency).
|
|
20
20
|
|
|
21
21
|
In this documentation, "tool script" refers to a Python project that imports and uses cjnfuncs. Some may be simple scripts, and others may themselves be installed packages.
|
|
22
22
|
|
|
@@ -38,35 +38,30 @@ Project repo: https://github.com/cjnaz/cjnfuncs
|
|
|
38
38
|
|
|
39
39
|
<br/>
|
|
40
40
|
|
|
41
|
-
## Key changes since the prior major public release (version
|
|
41
|
+
## Key changes since the prior major public release (version 3.0)
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
- If a network share (or an internet resource) is
|
|
45
|
-
unavailable or extremely slow then the tool script can hang indefinitely. `run_with_timeout()` can wrap any function call
|
|
46
|
-
(any callable - built-in, standard library, installed package, or defined within the tool script) with an enforced timeout
|
|
47
|
-
and number of retries on timeout so that your code can make rational decisions when there are access issues.
|
|
43
|
+
- Several issues with Windows support were fixed.
|
|
48
44
|
|
|
49
|
-
`
|
|
50
|
-
- When you want this, you want this. Its used in configman, for instance, to allow for debug logging within the configman code (valuable for cjnfuncs debug and regression testing). If you are developing a hardware sensor driver for example, you can enable debug logging of calls to your driver code
|
|
51
|
-
and restore the level before return.
|
|
52
|
-
You can use `set_logging_level()` standalone, or with later `restore_logging_level()` calls.
|
|
45
|
+
- `configman.config_item()` now supports a safe_mode switch, which will speed up Windows usage of loadconfig() at some risk. See the note on `config_item()`.
|
|
53
46
|
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
- Named child loggers are now implemented on several cjnfuncs modules. By default, logging from cjnfuncs modules is disabled (logging events are usually at the INFO or DEBUG
|
|
48
|
+
level, while the default logging level is set to WARNING). For example, logging from configman may be enabled by `logging.getLogger('cjnfuncs.configman').setLevel(logging.INFO)`,
|
|
49
|
+
or using `core.set_logging.level(logging.INFO, 'cjnfuncs.configman')`.
|
|
56
50
|
|
|
57
|
-
|
|
58
|
-
- A lot of what I develop are process monitoring tools. These typically implement a `while True` loop where various operations get triggered periodically on their own schedules. `get_next_dt()` provides a simple and clean way to get the next `datetime` to do an operation (in my usage, as defined in a config file). `get_next_dt()` supports arbitrary times of day and days of week. For example, trigger generating a summary report every Thursday at 9AM:
|
|
51
|
+
- Changed mungePath set_attributes from default True to default False to avoid long processing time on Windows by default.
|
|
59
52
|
|
|
60
|
-
|
|
61
|
-
gen_summary()
|
|
62
|
-
generate_summary_dt = get_next_dt('9:00, 'Thursday')
|
|
63
|
-
|
|
64
|
-
`mungePath(..., set_attributes=False)` switch
|
|
65
|
-
- On network outages, my apps would slow to a crawl. The root of the issue was in checking if a network file exists, with no timeout mechanism. The pathlib `.exists()`, `.is_dir()`, and `is_file()` methods are all susceptible to indefinate hangs. The set_attributues switch has been added to mungePath, with a default value of False. _Note that this is a change to default behavior._ If set True then `refresh_stats()` is called (or you can call it directly yourself, as needed). `refresh_stats()` and `check_file_exists()` have been rewritten to utilize `run_with_timeout()`, with a default timeout of 1.0 seconds. I then `periodic_log()` the access problem. Ah, so much more stable.
|
|
53
|
+
- deployfiles can now create an empty target directory.
|
|
66
54
|
|
|
67
55
|
<br/>
|
|
68
56
|
|
|
69
57
|
## Revision history
|
|
58
|
+
- 3.1 251109
|
|
59
|
+
Support for and use of child loggers,
|
|
60
|
+
deployfiles create empty dir,
|
|
61
|
+
Windows fixes,
|
|
62
|
+
config_item() safe_mode switch,
|
|
63
|
+
mungePath set_attributes from default False
|
|
64
|
+
- 3.0.1 251005 - Allow '.' in config section names
|
|
70
65
|
- 3.0 250705 - Added run_with_timeout, set / restore_logging_level, periodic_logging, get_next_dt. Functional change to mungePath.
|
|
71
66
|
- 2.5 250206 - Added multi-line and quoted string support to configman
|
|
72
67
|
- 2.4.1 241118 - resource_lock only init lock_info if not existing
|
|
@@ -37,7 +37,7 @@ EmailPass mypassword
|
|
|
37
37
|
EmailFrom me@myserver.com
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
And
|
|
40
|
+
And this code:
|
|
41
41
|
```
|
|
42
42
|
#!/usr/bin/env python3
|
|
43
43
|
# ***** SMTP_ex1.py *****
|
|
@@ -47,8 +47,8 @@ from cjnfuncs.configman import config_item
|
|
|
47
47
|
from cjnfuncs.deployfiles import deploy_files
|
|
48
48
|
from cjnfuncs.SMTP import snd_notif, snd_email
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
set_toolname('SMTP_ex1')
|
|
51
|
+
logging.getLogger('cjnfuncs.deployfiles').setLevel(logging.INFO)
|
|
52
52
|
|
|
53
53
|
deploy_files([
|
|
54
54
|
{ 'source': 'SMTP_ex1.cfg', 'target_dir': 'USER_CONFIG_DIR' },
|
|
@@ -65,7 +65,7 @@ try:
|
|
|
65
65
|
log=True,
|
|
66
66
|
smtp_config=myconfig)
|
|
67
67
|
except SndEmailError as e:
|
|
68
|
-
logging.warning(f"snd_notif() failed:\n {e}")
|
|
68
|
+
logging.warning(f"snd_notif() failed:\n {type(e).__name__}: {e}")
|
|
69
69
|
|
|
70
70
|
try:
|
|
71
71
|
snd_email(subj='My first email send',
|
|
@@ -74,14 +74,15 @@ try:
|
|
|
74
74
|
log=True,
|
|
75
75
|
smtp_config=myconfig)
|
|
76
76
|
except SndEmailError as e:
|
|
77
|
-
logging.warning(f"snd_email() failed:\n {e}")
|
|
77
|
+
logging.warning(f"snd_email() failed:\n {type(e).__name__}: {e}")
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
And finally, running the code produces this output, and a couple sent messages:
|
|
81
81
|
```
|
|
82
82
|
$ ./SMTP_ex1.py
|
|
83
|
-
|
|
84
|
-
Deployed
|
|
83
|
+
deployfiles.deploy_files - INFO: Created /home/me/.config/SMTP_ex1
|
|
84
|
+
deployfiles.deploy_files - INFO: Deployed /home/me/.config/SMTP_ex1/SMTP_ex1.cfg
|
|
85
|
+
deployfiles.deploy_files - INFO: Deployed /home/me/.config/SMTP_ex1/creds_SMTP
|
|
85
86
|
SMTP.snd_notif - WARNING: Notification sent <My first text message> <This is pretty clean interface!>
|
|
86
87
|
SMTP.snd_email - WARNING: Email sent <My first email send>
|
|
87
88
|
```
|
|
@@ -188,6 +189,19 @@ config that contains the [SMTP] section).
|
|
|
188
189
|
- Example: Given `'4805551212@vzwpix.com 4805551213 +14805551214, +44123456'`, list_to() returns: `['+14805551212', '+14805551213', '+14805551214', '+44123456']`
|
|
189
190
|
|
|
190
191
|
|
|
192
|
+
<br>
|
|
193
|
+
|
|
194
|
+
## Controlling logging from within smtp code
|
|
195
|
+
|
|
196
|
+
Logging within the SMTP module uses the `cjnfuncs.smtp` named/child logger. By default this logger is set to the `logging.WARNING` level,
|
|
197
|
+
meaning that no logging messages are produced from within the SMTP code. For validation and debug purposes, logging from within SMTP code
|
|
198
|
+
can be enabled by setting the logging level for this module's logger from within the tool script code:
|
|
199
|
+
|
|
200
|
+
logging.getLogger('cjnfuncs.smtp').setLevel(logging.DEBUG)
|
|
201
|
+
|
|
202
|
+
# Or alternately, use the core module set_logging_level() function:
|
|
203
|
+
set_logging_level (logging.DEBUG, 'cjnfuncs.smtp')
|
|
204
|
+
|
|
191
205
|
|
|
192
206
|
<a id="links"></a>
|
|
193
207
|
|
|
@@ -231,20 +245,19 @@ Three attempts are made to send the message.
|
|
|
231
245
|
|
|
232
246
|
`urls_list` (list, default [])
|
|
233
247
|
- A list of url strings to be passed to the message sending plugin module, which should pass them to the messaging service.
|
|
234
|
-
-
|
|
235
|
-
urls then included them in the `msg` body text.
|
|
248
|
+
- If not using a messaging service then this list is discarded, in which case include the URLs in the `msg` body.
|
|
236
249
|
|
|
237
250
|
`to` (str, default 'NotifList')
|
|
238
251
|
- To whom to send the message. `to` may be either an explicit string list of email addresses
|
|
239
252
|
(whitespace or comma separated) or a config param name (also listing one
|
|
240
253
|
or more whitespace or comma separated email addresses). If the `to` arg does not
|
|
241
|
-
contain an '@' it is assumed to be a config param.
|
|
254
|
+
contain an '@' it is assumed to be a config param name.
|
|
242
255
|
- Define `NotifList` in the config file to use the default `to` value.
|
|
243
256
|
|
|
244
257
|
`log` (bool, default False)
|
|
245
|
-
- If True, logs that the message was sent at the WARNING level. If False, logs
|
|
246
|
-
at the DEBUG level. Useful for eliminating separate logging messages in the tool script code.
|
|
247
|
-
The `subj` field
|
|
258
|
+
- If True, logs that the message was sent at the WARNING level (using the root logger). If False, logs
|
|
259
|
+
at the DEBUG level (using the 'cjnfuncs.smtp' logger). Useful for eliminating separate logging messages in the tool script code.
|
|
260
|
+
The `subj` field and `msg` body are included in the log message.
|
|
248
261
|
|
|
249
262
|
`smtp_config` (config_item class instance)
|
|
250
263
|
- config_item class instance containing the [SMTP] section and related params
|
|
@@ -283,7 +296,7 @@ default `to` arg value.
|
|
|
283
296
|
|
|
284
297
|
|
|
285
298
|
### Behaviors and rules
|
|
286
|
-
- `snd_notif()` uses `snd_email()` to send the message. See `snd_email()` for related setup.
|
|
299
|
+
- `snd_notif()` uses `snd_email()` to send the message (if not using a messaging service). See `snd_email()` for related setup.
|
|
287
300
|
|
|
288
301
|
<br/>
|
|
289
302
|
|
|
@@ -313,20 +326,22 @@ Three attempts are made to send the message (see `EmailNTries`, below).
|
|
|
313
326
|
- To whom to send the message. `to` may be either an explicit string list of email addresses
|
|
314
327
|
(whitespace or comma separated) or a config param name in the [SMTP] section (also listing one
|
|
315
328
|
or more whitespace or comma separated email addresses). If the `to` arg does not
|
|
316
|
-
contain an '@' it is assumed to be a config param.
|
|
329
|
+
contain an '@' it is assumed to be a config param name.
|
|
317
330
|
|
|
318
331
|
`body` (str, default None)
|
|
319
332
|
- A string message to be sent
|
|
320
333
|
|
|
321
334
|
`filename` (str, default None)
|
|
322
|
-
- A str or Path to the file
|
|
335
|
+
- A str or Path to the file who's content will be sent as the body of the message
|
|
336
|
+
- The file path is relative to the `core.tool.cache_dir`, or an absolute path
|
|
323
337
|
|
|
324
338
|
`htmlfile` (str, default None)
|
|
325
|
-
- A str or Path to the
|
|
339
|
+
- A str or Path to the file who's HTML-formatted content will be sent as the body of the message
|
|
340
|
+
- The file path is relative to the `core.tool.cache_dir`, or an absolute path
|
|
326
341
|
|
|
327
342
|
`log` (bool, default False)
|
|
328
|
-
- If True, logs that the message was sent at the WARNING level. If False, logs
|
|
329
|
-
at the DEBUG level. Useful for eliminating separate logging messages in the tool script code.
|
|
343
|
+
- If True, logs that the message was sent at the WARNING level (using the root logger). If False, logs
|
|
344
|
+
at the DEBUG level (using the 'cjnfuncs.smpt' logger). Useful for eliminating separate logging messages in the tool script code.
|
|
330
345
|
The `subj` field is part of the log message.
|
|
331
346
|
|
|
332
347
|
`smtp_config` (config_item class instance)
|
|
@@ -350,7 +365,7 @@ The `subj` field is part of the log message.
|
|
|
350
365
|
- Password for `EmailServer` login, if required by the server
|
|
351
366
|
|
|
352
367
|
`DontEmail` (default False)
|
|
353
|
-
- If True, messages are not sent. Useful for debug. Also blocks `snd_notif()` messages.
|
|
368
|
+
- If True, messages are not sent. Useful for debug. Also blocks `snd_notif()` messages not sent thru a messaging service.
|
|
354
369
|
|
|
355
370
|
`EmailVerbose` (default False)
|
|
356
371
|
- If True, detailed transactions with the SMTP server are sent to stdout. Useful for debug.
|
|
@@ -254,7 +254,7 @@ bad_float: 52.3.5
|
|
|
254
254
|
|
|
255
255
|
Notables (See **** NOTE # in the above example config file and code):
|
|
256
256
|
1. loadconfig() looks for `LogLevel` abd `LogFile` and sets the root logger accordingly. If you want to
|
|
257
|
-
change the console or file logging format you may also define `ConsoleLogFormat` or `FileLogFormat`, respectively. Logging setups only apply for the primary/master config (`config_item(secondary_config = False)`). The logging level _within_ loadconfig() is
|
|
257
|
+
change the console or file logging format you may also define `ConsoleLogFormat` or `FileLogFormat`, respectively. Logging setups only apply for the primary/master config (`config_item(secondary_config = False)`). The logging level _within_ loadconfig() is controlled by the child logger `cjnfuncs.configman` (default WARNING level, see below).
|
|
258
258
|
2. loadconfig() accepts most any character in a param name, except the comment character `#`, or the param-value separator characters whitespace, `=`, or `:`.
|
|
259
259
|
3. loadconfig() attempts to load a value as a type `int`, `bool`, `float`, `list`, `dict`, or `tuple`, if the value has the correct syntax for that type. The fallback is to type `str`. Loading all params as type `str` can be forced: `my_config = config_item('configman_ex2.cfg', force_str=True)`.
|
|
260
260
|
4. Sections are supported, and are accessed as `my_config.getcfg('NotifList', section='SMTP')`. Only
|
|
@@ -348,8 +348,8 @@ core.tool.log_dir_base : /home/me/.config/configman_ex3
|
|
|
348
348
|
|
|
349
349
|
Notables:
|
|
350
350
|
1. At the startup of the service script, with `loadconfig(tolerate_missing=False)`, the config file must be accessible or a `ConfigError` will be raised. This should be trapped and gracefully handled.
|
|
351
|
-
2. With `loadconfig(tolerate_missing=True)`, `-1` will be returned if the config file is not currently accessible. You will want to add code to output this warning only once, so as to not flood the log. tolerate_missing=True allows the config file to be placed on a network file system.
|
|
352
|
-
3. loadconfig() will return `1` if the config file timestamp has changed (`0` if not changed).
|
|
351
|
+
2. With `loadconfig(tolerate_missing=True)`, `-1` will be returned if the config file is not currently accessible. You will want to add code to output this warning only once, so as to not flood the log. tolerate_missing=True allows the config file to be placed on a network file system. Also see `core.periodic_log()`.
|
|
352
|
+
3. loadconfig() will return `1` if the config file timestamp has changed (`0` if not changed). If the config file was changed the `loadconfig(flush_on_reload=True)` call will have purged all cfg data and reloaded it from the file.
|
|
353
353
|
4. If this is a `reloaded` case (versus `first`), then cleanup work may be needed prior to the following resource setups.
|
|
354
354
|
5. Threads and asyncio should use local copies of cfg data so that they don't crash when the cfg data temporarily disappears during the loadconfig() reload. Also see loadconfig's `prereload_callback` parameter.
|
|
355
355
|
|
|
@@ -433,6 +433,21 @@ Notables:
|
|
|
433
433
|
Mapping Protocol Access | No | Yes
|
|
434
434
|
Save to file | Yes | Yes
|
|
435
435
|
|
|
436
|
+
<br>
|
|
437
|
+
|
|
438
|
+
## Controlling logging from within configman code
|
|
439
|
+
|
|
440
|
+
Logging within the configman module uses the `cjnfuncs.configman` named/child logger. By default this logger is set to the `logging.WARNING` level,
|
|
441
|
+
meaning that no logging messages are produced from within the configman code. For validation and debug purposes, logging from within configman code
|
|
442
|
+
can be enabled by setting the logging level for this module's logger from within the tool script code:
|
|
443
|
+
|
|
444
|
+
logging.getLogger('cjnfuncs.configman').setLevel(logging.DEBUG) # or logging.INFO
|
|
445
|
+
|
|
446
|
+
# Or alternately, use the core module set_logging_level() function:
|
|
447
|
+
set_logging_level (logging.DEBUG, 'cjnfuncs.configman')
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
436
451
|
|
|
437
452
|
<a id="links"></a>
|
|
438
453
|
|
|
@@ -461,8 +476,7 @@ Notables:
|
|
|
461
476
|
|
|
462
477
|
---
|
|
463
478
|
|
|
464
|
-
# Class config_item (config_file=None, remap_logdirbase=True, force_str=False, secondary_config=False) - Create a configuration instance
|
|
465
|
-
|
|
479
|
+
# Class config_item (config_file=None, remap_logdirbase=True, force_str=False, secondary_config=False, safe_mode=False) - Create a configuration instance
|
|
466
480
|
The config_item() class provides handling of one or more config file instances. Class methods include:
|
|
467
481
|
- Config file loading and reloading - `loadconfig()`
|
|
468
482
|
- Loading config data from strings and dictionaries - `read_string()`, `read_dict()`
|
|
@@ -490,6 +504,10 @@ then the `core.tool.log_dir_base` will be set to `core.tool.config_dir`.
|
|
|
490
504
|
- The primary config file should be loaded first before any secondary_config loads, so that logging
|
|
491
505
|
is properly set up.
|
|
492
506
|
|
|
507
|
+
`safe_mode` (bool, default False)
|
|
508
|
+
- If `safe_mode=True` then timeouts are enforced when checking for config files.
|
|
509
|
+
- If `safe_mode=False` then checks for the existence of config files runs the risk of application hang.
|
|
510
|
+
- See Behavior NOTE below.
|
|
493
511
|
|
|
494
512
|
### Useful class attributes
|
|
495
513
|
The current values of all public class attributes may be printed using `print(my_config)`.
|
|
@@ -521,26 +539,44 @@ instantiation `config_file` is a relative path (uses mungePath)
|
|
|
521
539
|
|
|
522
540
|
|
|
523
541
|
### Behaviors and rules
|
|
524
|
-
|
|
542
|
+
1. More than one `config_item()` may be created and loaded. This allows for configuration data to be partitioned
|
|
525
543
|
as desired. Each defined config is loaded to its own instance-specific `cfg` dictionary. Only one config_item()
|
|
526
544
|
instance should be considered the primary, while other instances should be tagged with `secondary_config=True`.
|
|
527
545
|
Logging setups are controlled only by the primary config instance.
|
|
528
546
|
Also see the loadconfig() `import` feature.
|
|
529
|
-
|
|
547
|
+
|
|
548
|
+
1. NOTE: On Linux the cost of `safe_mode=True` when searching for a quickly found config file is negligible
|
|
549
|
+
(Linux uses process fork), but on Windows (uses process spawn) the minimum execution time for a file
|
|
550
|
+
existence check can be 1 to 2 seconds. When `safe_mode=False` and the config file is on a network share
|
|
551
|
+
drive, the file existence check can cause a indefinite blocking hang of your tool script if the network
|
|
552
|
+
goes down (true for both Linux and Windows). So, use `safe_mode=False` if your config files are on a local
|
|
553
|
+
drive, and use `safe_mode=True` if your config files are on a network drive. Recognize that on Windows
|
|
554
|
+
this will perhaps cause significant delays.
|
|
555
|
+
|
|
556
|
+
1. Initially in _user_ mode, after the `set_toolname()` call, `core.tool.log_dir_base`
|
|
530
557
|
(the log directory) is set to the `core.tool.user_data_dir`.
|
|
531
558
|
Once `config_item()` is called the `core.tool.log_dir_base` is _remapped_ to
|
|
532
559
|
`core.tool.config_dir`. This is the author's style preference (centralize primary files, and
|
|
533
560
|
reduce spreading files around the file system).
|
|
534
561
|
To disable this remap, in the `config_item()` call set `remap_logdirbase=False`.
|
|
535
562
|
This remapping is not done in site mode.
|
|
536
|
-
|
|
563
|
+
|
|
564
|
+
1. A different log base directory may be set by user code by setting `core.tool.log_dir_base` to a different
|
|
537
565
|
path after the `set_toolname()` call and before the `config_item()` call, for example
|
|
538
566
|
`core.tool.log_dir_base = "/var/log"` may be desirable in site mode.
|
|
539
|
-
|
|
567
|
+
|
|
568
|
+
1. A different config directory may be set by user code by setting `core.tool.config_dir` to a different
|
|
540
569
|
path after the `set_toolname()` call and before the `config_item()` call, for example
|
|
541
570
|
`core.tool.config_dir = core.tool.main_dir` sets the config dir to the same as the tool script's
|
|
542
571
|
directory. With `remap_logdirbase=True`, the log dir will also be set to the tool script's directory.
|
|
543
|
-
|
|
572
|
+
|
|
573
|
+
1. Details of the configuration instance may be printed, eg, `print(my_config)`.
|
|
574
|
+
|
|
575
|
+
1. Logging of the operations within loadconfig() and (other configman methods) may be controlled with setting
|
|
576
|
+
the named child logger `cjnfuncs.configman` to INFO or DEBUG level (the default logging level is WARNING which
|
|
577
|
+
produces no logging events from this module), eg:
|
|
578
|
+
|
|
579
|
+
logging.getLogger('cjnfuncs.configman').setLevel(logging.INFO)
|
|
544
580
|
|
|
545
581
|
<br/>
|
|
546
582
|
|
|
@@ -551,7 +587,6 @@ directory. With `remap_logdirbase=True`, the log dir will also be set to the to
|
|
|
551
587
|
# loadconfig () - Load a configuration file into the cfg dictionary
|
|
552
588
|
```
|
|
553
589
|
loadconfig(
|
|
554
|
-
ldcfg_ll = 30,
|
|
555
590
|
call_logfile = None,
|
|
556
591
|
call_logfile_wins = False,
|
|
557
592
|
flush_on_reload = False,
|
|
@@ -573,9 +608,6 @@ feature, and intermittent loss of access to the config file.
|
|
|
573
608
|
|
|
574
609
|
|
|
575
610
|
### Args
|
|
576
|
-
`ldcfg_ll` (int, default 30 (WARNING))
|
|
577
|
-
- Logging level used within `loadconfig()` code for debugging loadconfig() itself
|
|
578
|
-
|
|
579
611
|
`call_logfile` (Path or str, default None)
|
|
580
612
|
- If `call_logfile` is passed on the loadconfig() call, and `call_logfile_wins=True`, then any `LogFile`
|
|
581
613
|
specified in the config file is overridden. This feature allows for interactive usage modes where
|
|
@@ -613,90 +645,92 @@ regardless of whether the config file timestamp has changed
|
|
|
613
645
|
|
|
614
646
|
|
|
615
647
|
### Behaviors and rules
|
|
616
|
-
|
|
648
|
+
1. See `getcfg()`, below, for accessing loaded config data. The class instance-specific `cfg` dictionary may be
|
|
617
649
|
directly accessed as well.
|
|
618
650
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
651
|
+
1. The format of a config file is param=value pairs.
|
|
652
|
+
- Separating the param and value_portion may be whitespace, `=` or `:` (multiples allowed).
|
|
653
|
+
- Param names can contain all valid characters, except the separators or `#`, and cannot start with `[`.
|
|
622
654
|
|
|
623
|
-
|
|
624
|
-
|
|
655
|
+
1. Sections and a DEFAULT section are supported. Section name are enclosed in `[ ]`.
|
|
656
|
+
- Leading and trailing whitespace is trimmed off of the section name, and embedded whitespace is retained.
|
|
625
657
|
EG: `[ hello my name is Fred ]` becomes section name `'hello my name is Fred'`.
|
|
626
|
-
|
|
658
|
+
- Section names can contain most all characters, except `]`.
|
|
627
659
|
|
|
628
|
-
|
|
660
|
+
1. **Native int, float, bool, list, tuple, dict, str support** - Bool true/false is case insensitive. A str
|
|
629
661
|
type is stored in the `cfg` dictionary if none of the other types can be resolved for a given value_portion.
|
|
630
662
|
Automatic typing avoids most explicit type casting clutter in the tool script. Be careful to error trap
|
|
631
663
|
for type errors (eg, expecting a float but user input error resulted in a str). Also see the
|
|
632
664
|
getcfg() `types=[]` arg for basic type enforcement.
|
|
633
665
|
|
|
634
|
-
|
|
666
|
+
1. **Quoted strings** - If a value_portion cannot be resolved to a Python native type then it is loaded as a str,
|
|
635
667
|
eg `My_name = George` loads George as a str. A value_portion may be forced to be loaded as a str by using
|
|
636
668
|
quotes, eg `Some_number_as_str = "2.54"` forces the value_portion to be loaded as a str rather than a float. Supported
|
|
637
669
|
quote types: `"..."`, `'...'`, (triple-double quotes), and `'''...'''`. `My_name = George`,
|
|
638
670
|
`My_name : "George"`, `My_name '''George'''`, etc., are identical when loaded.
|
|
639
671
|
Quoted strings may contain all valid characters, including '#' which normally starts a comment.
|
|
640
672
|
|
|
641
|
-
|
|
673
|
+
1. **Multi-line values** - A param's value_portion may be specified over multiple lines for readability by placing
|
|
642
674
|
the `\` line continuation character as the last non-whitespace character on the line before any comment.
|
|
643
675
|
The parser strips comments and leading/trailing whitespace, then concatenates the multi-line value_portion segments
|
|
644
676
|
into a single line (single space separated) in the loaded config. Comments may be placed on each line.
|
|
645
677
|
NOTE: For a multi-line param that will be loaded as a str, avoid using quotes as results may be strange.
|
|
646
678
|
|
|
647
|
-
|
|
679
|
+
1. **Logging setup** - `loadconfig()` calls `cjnfuncs.core.setuplogging()`. The `logging` handle is available for
|
|
648
680
|
import by other modules (`from cjnfuncs.core import logging`). By default, logging will go to the
|
|
649
681
|
console (stdout) filtered at the WARNING/30 level. Don't call `setuplogging()` directly if using loadconfig().
|
|
650
682
|
|
|
651
|
-
|
|
683
|
+
1. **Logging level control** - Optional `LogLevel` in the primary config file will set the root logging level after
|
|
652
684
|
the config file has been loaded. If LogLevel is not specified in the primary config file, then
|
|
653
|
-
the logging level is left unchanged (the Python default logging level
|
|
654
|
-
The tool script code may also manually/explicitly set the logging level _after the initial `loadconifig()` call_
|
|
685
|
+
the root logging level is left unchanged (the Python default logging level is 30/WARNING).
|
|
686
|
+
The tool script code may also manually/explicitly set the root logging level _after the initial `loadconifig()` call_
|
|
655
687
|
and this value will be retained over later calls to loadconfig, thus allowing for a command line `--verbose`
|
|
656
|
-
switch feature. Note that logging done _within_ loadconfig()
|
|
688
|
+
switch feature. Note that logging done _within_ loadconfig() uses the `cjnfuncs.configmap` child/named logger.
|
|
689
|
+
`logging.getLogger('cjnfuncs.configman').setLevel(logging.INFO)` (or DEBUG) enables diagnostic logging from
|
|
690
|
+
loadconfig.
|
|
657
691
|
|
|
658
|
-
|
|
692
|
+
1. **Log file options** - Where to log has two separate fields: `call_logifle` in the call to loadconfig(), and
|
|
659
693
|
`LogFile` in the loaded primary config file, with `call_logfile_wins` selecting which is used. This mechanism allows for
|
|
660
694
|
a command line `--log-file` switch to override a _default_ log file defined in the config file. If the selected
|
|
661
695
|
logging location is `None` then output goes to the console (stdout).
|
|
662
696
|
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
697
|
+
call_logfile_wins | call_logfile | Config LogFile | Results
|
|
698
|
+
--|--|--|--
|
|
699
|
+
False (default) | ignored | None (default) | Console
|
|
700
|
+
False (default) | ignored | file_path | To the config LogFile
|
|
701
|
+
True | None (default) | ignored | Console
|
|
702
|
+
True | file_path | ignored | To the call_logfile
|
|
669
703
|
|
|
670
|
-
|
|
704
|
+
1. **Logging format** - cjnfuncs has default format strings for console and file logging.
|
|
671
705
|
These defaults may be overridden by defining `ConsoleLogFormat` and/or `FileLogFormat`
|
|
672
706
|
in the config file.
|
|
673
707
|
|
|
674
|
-
|
|
708
|
+
1. **Import nested config files** - loadconfig() supports `Import` (case insensitive). The imported file path
|
|
675
709
|
is relative to the `core.tool.config_dir`, if not an absolute path.
|
|
676
710
|
The specified file is imported as if the params were in the main config file. Nested imports are allowed.
|
|
677
711
|
Sections are not allowed within an imported file - only in the main/top-level config file.
|
|
678
712
|
A prime usage of `import` is to place email server credentials in your home directory with user-only readability,
|
|
679
713
|
then import them in the tool script config file as such: `import ~/creds_SMTP`.
|
|
680
714
|
|
|
681
|
-
|
|
715
|
+
1. **Config reload if changed, `flush_on_reload`, and `force_flush_reload`** - loadconfig() may be called
|
|
682
716
|
periodically by the tool script, such as in a service loop.
|
|
683
717
|
If the config file timestamp is unchanged then loadconfig() immediately returns `0`.
|
|
684
718
|
If the timestamp has changed then the config file will be reloaded and `1` is returned to indicate to
|
|
685
719
|
the tool script to do any post-config-load operations.
|
|
686
|
-
|
|
720
|
+
- If `flush_on_reload=True` (default False) then the instance-specific `cfg` dictionary
|
|
687
721
|
will be cleaned/purged before the config file is reloaded. If `flush_on_reload=False` then the config
|
|
688
722
|
file will be reloaded on top of the existing `cfg` dictionary contents (if a param was
|
|
689
723
|
deleted in the config
|
|
690
724
|
file it will still exist in `cfg` after the reload). [lanmonitor](https://github.com/cjnaz/lanmonitor) uses the
|
|
691
725
|
`flush_on_reload=True` feature.
|
|
692
|
-
|
|
726
|
+
- `force_flush_reload=True` (default False) forces both a clear/flush of the `cfg` dictionary and then a fresh
|
|
693
727
|
reload of the config file.
|
|
694
|
-
|
|
728
|
+
- **Note** that if using threading then a thread should be paused while the config file
|
|
695
729
|
is being reloaded with `flush_on_reload=True` or `force_flush_reload=True` since the params will disappear briefly.
|
|
696
730
|
Use the `prereload_callback` mechanism to manage any code dependencies before the cfg dictionary is purged.
|
|
697
|
-
|
|
731
|
+
- Changes to imported files are not tracked for changes.
|
|
698
732
|
|
|
699
|
-
|
|
733
|
+
1. **Tolerating intermittent config file access** - When implementing a service loop, if `tolerate_missing=True`
|
|
700
734
|
(default False) then loadconfig() will return `-1` if the config file cannot be accessed, informing the
|
|
701
735
|
tool script of the problem for appropriate handling (typically logging the event then ignoring the problem for
|
|
702
736
|
the current iteration). If `tolerate_missing=False` then loadconfig() will raise a ConfigError if the config file
|
|
@@ -731,12 +765,6 @@ flush_on_reload, force_flush_reload, and tolerate_missing.
|
|
|
731
765
|
### Returns
|
|
732
766
|
- A ConfigError is raised if there are parsing issues
|
|
733
767
|
- A ConfigError is also raised if an imported config file cannot accessed
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
### Behaviors and rules
|
|
737
|
-
- read_string() does not modify the logging level (ldcfg_ll is not an arg). If called from user code,
|
|
738
|
-
the logging level will be as set in the user code. Logging statements within read_string() is at the debug level.
|
|
739
|
-
- See loadconfig() for config loading Behaviors and rules.
|
|
740
768
|
|
|
741
769
|
<br/>
|
|
742
770
|
|
|
@@ -768,23 +796,25 @@ Loaded content is added to and/or modifies any previously loaded content.
|
|
|
768
796
|
```
|
|
769
797
|
new_config = config_item() # config need not be associated with a file
|
|
770
798
|
|
|
771
|
-
|
|
799
|
+
main_contents = {
|
|
772
800
|
'a' : 6,
|
|
773
801
|
'b' : 7.0,
|
|
774
802
|
'c' : [6, 7.0, 42, 'hi']
|
|
775
803
|
}
|
|
804
|
+
new_config.read_dict(main_contents)
|
|
805
|
+
|
|
776
806
|
sect_contents = {
|
|
777
807
|
'd' : ('hi', 'there'),
|
|
778
808
|
'e' : {'hi':'Hi!', 'there':'There!'},
|
|
779
809
|
'f' : [6, 7.0, 42, 'hi']
|
|
780
810
|
}
|
|
811
|
+
new_config.read_dict(sect_contents, 'A section')
|
|
812
|
+
|
|
781
813
|
def_contents = {
|
|
782
814
|
'g' : 'Hi',
|
|
783
815
|
'h' : True,
|
|
784
816
|
'i' : False
|
|
785
817
|
}
|
|
786
|
-
new_config.read_dict(main_contents)
|
|
787
|
-
new_config.read_dict(sect_contents, 'A section')
|
|
788
818
|
new_config.read_dict(def_contents, 'DEFAULT')
|
|
789
819
|
```
|
|
790
820
|
|