cjnfuncs 3.2__tar.gz → 3.2.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: cjnfuncs
3
- Version: 3.2
3
+ Version: 3.2.2
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
@@ -60,6 +60,12 @@ Project repo: https://github.com/cjnaz/cjnfuncs
60
60
 
61
61
  ## Key changes since the prior major public release (version 3.0)
62
62
 
63
+ 3.2.2 change
64
+ - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
65
+
66
+ 3.2.1 change
67
+ - periodic_log returns True/False whether call was logged - useful for follow on code that needs to run if the logging event happened.
68
+
63
69
  3.2 changes
64
70
  - `persistent_config` - Layered on top of config_item, this new class provides a clean and simple tool for carrying runtime data across tool
65
71
  script restarts and system reboots.
@@ -89,6 +95,8 @@ or using `core.set_logging_level(logging.INFO, 'cjnfuncs.configman')`.
89
95
 
90
96
  ## Revision history
91
97
 
98
+ - 3.2.2 - 261002 - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
99
+ - 3.2.1 - 260526 - periodic_log now returns True/False whether the the call was logged. Minor changes in resourcelock.
92
100
  - 3.2 - 260506 - New features: persistent_config and shared memory block handling
93
101
  - 3.1.1 260207 - separate resourcelock_islocked logger, get_next_dt days/weeks offset
94
102
  - 3.1 251109
@@ -40,6 +40,12 @@ Project repo: https://github.com/cjnaz/cjnfuncs
40
40
 
41
41
  ## Key changes since the prior major public release (version 3.0)
42
42
 
43
+ 3.2.2 change
44
+ - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
45
+
46
+ 3.2.1 change
47
+ - periodic_log returns True/False whether call was logged - useful for follow on code that needs to run if the logging event happened.
48
+
43
49
  3.2 changes
44
50
  - `persistent_config` - Layered on top of config_item, this new class provides a clean and simple tool for carrying runtime data across tool
45
51
  script restarts and system reboots.
@@ -69,6 +75,8 @@ or using `core.set_logging_level(logging.INFO, 'cjnfuncs.configman')`.
69
75
 
70
76
  ## Revision history
71
77
 
78
+ - 3.2.2 - 261002 - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
79
+ - 3.2.1 - 260526 - periodic_log now returns True/False whether the the call was logged. Minor changes in resourcelock.
72
80
  - 3.2 - 260506 - New features: persistent_config and shared memory block handling
73
81
  - 3.1.1 260207 - separate resourcelock_islocked logger, get_next_dt days/weeks offset
74
82
  - 3.1 251109
@@ -1,16 +1,22 @@
1
1
  [project]
2
2
  name = 'cjnfuncs'
3
- version = '3.2'
3
+ version = '3.2.2'
4
+ # 3.2.2 261002 Release - periodic_log [category - <package>.<module>.<function>(<line number>)]
5
+ # 3.2.1 - Release
6
+ # resourcelock info level log on lock unget request ignored if lock not owned by current process and not forced
7
+ # resourcelock --verbose switch
8
+ # periodic_log returns True/False whether call was logged
9
+ # doc fixes
4
10
  # 3.2 - Release - New features: persistent_config and shared memory block handling
5
11
  # 3.1.1 - separate resourcelock_islocked logger, get_next_dt days/weeks offset
6
- # 3.1 - release
12
+ # 3.1 - Release
7
13
  # Support for and use of child loggers
8
14
  # deployfiles create empty dir
9
15
  # Windows fixes
10
16
  # config_item() safe_mode switch
11
17
  # mungePath set_attributes from default False
12
18
  # 3.0.1 - Allow '.' in config section names
13
- # 3.0 - release
19
+ # 3.0 - Release
14
20
  # added get_next_dt
15
21
  # reworked deployfiles and tests
16
22
  # added rwt_debug
@@ -1305,7 +1305,7 @@ save after critical data has been written to the config.
1305
1305
  ### Args
1306
1306
  `exit` (bool, default False)
1307
1307
  - If True, informs the scheduled save thread to do a final save and then terminate
1308
- - If False, the persistent_config data is directly written to the `config_file`
1308
+ - If False, or no scheduled save thread is running, the persistent_config data is directly written to the `config_file`
1309
1309
 
1310
1310
 
1311
1311
  ### Returns
@@ -4,7 +4,7 @@
4
4
 
5
5
  #==========================================================
6
6
  #
7
- # Chris Nelson, 2018-2025
7
+ # Chris Nelson, 2018-2026
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
  #==========================================================
@@ -337,8 +337,8 @@ logging.ERROR (40), or logging.CRITICAL (50).
337
337
 
338
338
 
339
339
  ### Behaviors
340
- - NOTE that a child logger that has not been set to a logging level will have a logging level = 0, which Python seems to
341
- treat the same as logging.WARNING (30).
340
+ - NOTE that a child logger that has not been set to a logging level will have a logging level = 0. Python then
341
+ falls back to parent's logging level, etc., and if unset then falls back to the root logging level.
342
342
 
343
343
  """
344
344
 
@@ -400,7 +400,6 @@ values are on the stack or have previously been saved.
400
400
  return known_loggers[logger_name]
401
401
  except:
402
402
  return []
403
- return ll_history
404
403
 
405
404
 
406
405
  def pop_logging_level_stack(logger_name='', clear=False):
@@ -453,13 +452,20 @@ class _periodic_log:
453
452
  self.logger_name = logger_name
454
453
 
455
454
  def plog(self, message, log_level, cat):
456
- now_dt = datetime.datetime.now()
455
+ now_dt = datetime.datetime.now()
457
456
  if now_dt > self.next_dt:
458
457
  if log_level is None:
459
458
  log_level = self.log_level
460
- _logger = logging.getLogger(self.logger_name)
461
- _logger.log(log_level, f"[PLog-{cat}] {message}")
462
- self.next_dt = now_dt + self.log_interval
459
+ logger = logging.getLogger(self.logger_name)
460
+ frame = inspect.currentframe().f_back.f_back
461
+ module = inspect.getmodule(frame)
462
+ module_name = module.__name__ if module else None
463
+ func = frame.f_code.co_name
464
+ lineno = frame.f_lineno
465
+ logger.log(log_level, f"[{cat} - {module_name}.{func}({lineno})] {message}")
466
+ self.next_dt = now_dt + self.log_interval
467
+ return True
468
+ return False
463
469
 
464
470
 
465
471
  def periodic_log(message, category='Cat1', logger_name='', log_interval='10m', log_level=None):
@@ -469,6 +475,9 @@ def periodic_log(message, category='Cat1', logger_name='', log_interval='10m', l
469
475
  Log infrequently so as to avoid flooding the log. The `category` arg provides for independent
470
476
  log intervals for different types of logging events.
471
477
 
478
+ The log message is preceded with `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of
479
+ where the log event came from.
480
+
472
481
 
473
482
  ### Args
474
483
 
@@ -494,7 +503,14 @@ for this category (ignored of subsequent calls).
494
503
 
495
504
 
496
505
  ### Returns
497
- - None
506
+ - True: This call was logged
507
+ - False: This call was skipped due to `log_interval` not being met
508
+ - Raises TypeError if input params are not valid
509
+
510
+
511
+ ### Behaviors and rules
512
+ - A call to periodic_log() returns a boolean (True/False) which may be used to gate further operations, such as
513
+ sending a notification.
498
514
  """
499
515
  if category not in cats:
500
516
  if log_level is None:
@@ -503,4 +519,4 @@ for this category (ignored of subsequent calls).
503
519
  cats[category] = xx
504
520
 
505
521
  p_logger = cats[category]
506
- p_logger.plog(message, log_level, category)
522
+ return p_logger.plog(message, log_level, category)
@@ -6,7 +6,7 @@ Only works on Linux
6
6
 
7
7
  #==========================================================
8
8
  #
9
- # Chris Nelson, Copyright 2024-2025
9
+ # Chris Nelson, Copyright 2024-2026
10
10
  #
11
11
  #==========================================================
12
12
 
@@ -16,7 +16,7 @@ import posix_ipc
16
16
  import mmap
17
17
  import os
18
18
  import datetime
19
- from .core import logging, set_toolname, setuplogging
19
+ from .core import logging, set_toolname, setuplogging, set_logging_level
20
20
 
21
21
  import importlib.metadata
22
22
  __version__ = importlib.metadata.version(__package__ or __name__)
@@ -76,8 +76,8 @@ Use the CLI command `resourcelock <lockname> unget` to manually release the lock
76
76
  resource_lock() uses `posix_ipc.Semaphore`, which is a counter mechanism. `get_lock()`
77
77
  decrements the counter to 0, indicating a locked state. `unget_lock()` increments the
78
78
  counter (non-zero is unlocked). `unget_lock()` wont increment the counter unless the counter is
79
- currently 0 (indicating locked), so it is ***recommended*** to have (possibly extraneous) `unget_lock()` calls,
80
- such as in your interrupt-trapped cleanup code.
79
+ currently 0 (indicating locked), so it is ***recommended*** to place (possibly extraneous) `unget_lock()` calls
80
+ in your interrupt-trapped cleanup code so that interrupted code certainly releases the lock.
81
81
 
82
82
 
83
83
  ### Args
@@ -156,10 +156,10 @@ with timeout.
156
156
 
157
157
  `lock_info` (str, default '')
158
158
  - Optional debugging info string for indicating when and by whom the lock was set. Logged at the debug level.
159
- - The datetime is prepended to lock_info.
159
+ - The current datetime is prepended to lock_info.
160
160
  - A useful lock_info string format might be `<module_name>.<function_name> <get_lock_call_instance_number>`, eg,
161
161
  `tempmon.measure_loop #3`.
162
- - This string remains in place after an unget() call (`is_locked() == False`) for lock history purposes while debugging.
162
+ - This string remains in place after an unget() call for lock history purposes while debugging.
163
163
 
164
164
  ### Returns
165
165
  - True: Lock successfully acquired, timeout time not exceeded
@@ -197,7 +197,7 @@ If the lock was acquired by the current process then release the lock.
197
197
  - If the lock is not currently set then the `unget_lock()` call is discarded, leaving the lock
198
198
  in the same unset state.
199
199
  - If the lock is currently set but _not_ acquired by this process then don't release the lock,
200
- unless `force=True`.
200
+ unless `force=True`. _Lock unget request ignored_ calls are logged at the info level.
201
201
 
202
202
  ### Arg
203
203
  `force` (bool, default False)
@@ -207,7 +207,7 @@ unless `force=True`.
207
207
 
208
208
  `where_called` (str, default '')
209
209
  - Debugging aid string for indicating what code released the lock. Logged at the debug level.
210
- Not stored anywhere, nor available to a later call.
210
+ Not stored nor available to a later call.
211
211
 
212
212
  ### Returns
213
213
  - True: Lock successfully released
@@ -225,7 +225,7 @@ Not stored anywhere, nor available to a later call.
225
225
  resourcelock_logger.debug (f"<{self.lockname[1:]}> lock force released <{where_called}>")
226
226
  return True
227
227
  else:
228
- resourcelock_logger.debug (f"<{self.lockname[1:]}> lock unget request ignored - lock not owned by current process <{where_called}>")
228
+ resourcelock_logger.info (f"<{self.lockname[1:]}> lock unget request ignored - lock not owned by current process <{where_called}>")
229
229
  return False
230
230
  else:
231
231
  resourcelock_logger.debug (f"<{self.lockname[1:]}> Extraneous lock unget request ignored <{where_called}>")
@@ -244,6 +244,9 @@ Not stored anywhere, nor available to a later call.
244
244
 
245
245
  ***resource_lock() class member function***
246
246
 
247
+ Note that `is_locked()` uses a separate child logger - `'cjnfuncs.resourcelock_islocked'` - so that `is_locked()` may be
248
+ checked in a loop without flooding the log. `is_locked()` calls are logged at the debug level.
249
+
247
250
  ### Returns
248
251
  - True if currently locked, else False
249
252
  """
@@ -387,8 +390,6 @@ Commands:
387
390
 
388
391
  set_toolname (TOOLNAME)
389
392
  setuplogging()
390
- logging.getLogger('cjnfuncs.resourcelock').setLevel(logging.DEBUG)
391
- logging.getLogger('cjnfuncs.resourcelock_islocked').setLevel(logging.DEBUG)
392
393
 
393
394
  GET_TIMEOUT = 0.5
394
395
  TRACE_INTERVAL = 0.5
@@ -407,30 +408,39 @@ Commands:
407
408
  help="After a successful get, unget the lock in (float) sec")
408
409
  parser.add_argument('-u', '--update', type=float, default=TRACE_INTERVAL,
409
410
  help=f"Trace update interval (default {TRACE_INTERVAL} sec)")
411
+ parser.add_argument('-v', '--verbose', action='count', default=0,
412
+ help="Print status and activity messages (-vv for debug level logging)")
410
413
  parser.add_argument('-V', '--version', action='version', version=f"{TOOLNAME} {__version__}",
411
414
  help="Print version number and exit")
412
415
  args = parser.parse_args()
413
416
 
414
417
 
418
+ ll = [logging.WARNING, logging.INFO, logging.DEBUG][args.verbose]
419
+ set_logging_level (ll, logger_name='cjnfuncs.resourcelock')
420
+ set_logging_level (ll, logger_name='cjnfuncs.resourcelock_islocked')
421
+
415
422
  lock = resource_lock(args.LockName)
416
423
 
417
424
  if args.Command == "get":
418
425
  _timeout = args.get_timeout
419
426
  if _timeout == -1:
420
427
  _timeout = None
421
- get_status = lock.get_lock(timeout=_timeout, lock_info=args.message)
422
- if get_status and args.auto_unget:
423
- print (f"Release lock after <{args.auto_unget}> sec delay")
424
- sleep(args.auto_unget)
425
- lock.unget_lock(where_called=f'{args.message} - auto unget')
428
+ get_result = lock.get_lock(timeout=_timeout, lock_info=args.message)
429
+ print (f"<{args.LockName}> get_lock called - Returned <{get_result}>, Currently locked: <{lock.is_locked()}>, Lock info: <{lock.get_lock_info()}>")
430
+ if get_result and args.auto_unget:
431
+ print (f"Release lock after <{args.auto_unget}> sec delay")
432
+ sleep(args.auto_unget)
433
+ unget_result = lock.unget_lock(where_called=f'{args.message} - auto unget')
434
+ print (f"<{args.LockName}> unget_lock called - Returned <{unget_result}>, Currently locked: <{lock.is_locked()}>, Lock info: <{lock.get_lock_info()}>")
426
435
 
427
436
  elif args.Command == "unget":
428
- lock.unget_lock(force=True, where_called=args.message)
437
+ unget_result = lock.unget_lock(force=True, where_called=args.message)
438
+ print (f"<{args.LockName}> unget_lock called - Returned <{unget_result}>, Currently locked: <{lock.is_locked()}>, Lock info: <{lock.get_lock_info()}>")
429
439
 
430
440
  elif args.Command == "state":
431
- lock.is_locked()
441
+ print (f"<{args.LockName}> Currently locked: <{lock.is_locked()}>, Lock info: <{lock.get_lock_info()}>")
432
442
 
433
443
  elif args.Command == "trace":
434
444
  while True:
435
- lock.is_locked()
445
+ print (f"<{args.LockName} Currently locked: <{lock.is_locked()}>, Lock info: <{lock.get_lock_info()}>")
436
446
  sleep (args.update)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cjnfuncs
3
- Version: 3.2
3
+ Version: 3.2.2
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
@@ -60,6 +60,12 @@ Project repo: https://github.com/cjnaz/cjnfuncs
60
60
 
61
61
  ## Key changes since the prior major public release (version 3.0)
62
62
 
63
+ 3.2.2 change
64
+ - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
65
+
66
+ 3.2.1 change
67
+ - periodic_log returns True/False whether call was logged - useful for follow on code that needs to run if the logging event happened.
68
+
63
69
  3.2 changes
64
70
  - `persistent_config` - Layered on top of config_item, this new class provides a clean and simple tool for carrying runtime data across tool
65
71
  script restarts and system reboots.
@@ -89,6 +95,8 @@ or using `core.set_logging_level(logging.INFO, 'cjnfuncs.configman')`.
89
95
 
90
96
  ## Revision history
91
97
 
98
+ - 3.2.2 - 261002 - periodic_log output now includes `[category - <package>.<module>.<function>(<line number>)]` for debug traceability of where the log event came from.
99
+ - 3.2.1 - 260526 - periodic_log now returns True/False whether the the call was logged. Minor changes in resourcelock.
92
100
  - 3.2 - 260506 - New features: persistent_config and shared memory block handling
93
101
  - 3.1.1 260207 - separate resourcelock_islocked logger, get_next_dt days/weeks offset
94
102
  - 3.1 251109
File without changes
File without changes
File without changes
File without changes