cgse-common 0.17.1__py3-none-any.whl → 0.17.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cgse-common
3
- Version: 0.17.1
3
+ Version: 0.17.2
4
4
  Summary: Software framework to support hardware testing
5
5
  Author: IvS KU Leuven
6
6
  Maintainer-email: Rik Huygen <rik.huygen@kuleuven.be>, Sara Regibo <sara.regibo@kuleuven.be>
@@ -1,18 +1,18 @@
1
1
  cgse_common/__init__.py,sha256=wTYOpVomEeDFFuqt4Ss9ROSAIa48UUnYCSafdEOx-CU,129
2
2
  cgse_common/cgse.py,sha256=S8MxwJcSzqYNR0wirEscQw5CTg89AKORa8-NHRdrxJM,6642
3
3
  cgse_common/settings.yaml,sha256=PS8HOoxbhwVoQ7zzDtZyhx25RZB6SGq3uXNuJTgtRIw,443
4
- egse/bits.py,sha256=cg6diLN2IwNtnrlZyLgXPfDf9ttcHzS2A7Tjwlwc-lQ,12498
4
+ egse/bits.py,sha256=6UrQdZ8nZ347rDdJ47a1PTWcq3fZt-c3siDjFfBGgiA,12389
5
5
  egse/calibration.py,sha256=a5JDaXTC6fMwQ1M-qrwNO31Ass-yYSXxDQUK_PPsZg4,8818
6
6
  egse/config.py,sha256=M821_d1IfNT17J3tt_TDjh4bGJgGiYviGBYR0I5v5LA,9639
7
7
  egse/counter.py,sha256=7UwBeTAu213xdNdGAOYpUWNQ4jD4yVM1bOG10Ax4UFs,5097
8
- egse/decorators.py,sha256=RT_-TmcHLN3oqha-1xn-YbqT1tEtKA84zr0Qu1KFyKo,27176
8
+ egse/decorators.py,sha256=ZJgXKP8IEniOFd8nHljNeUNpQLAB2Z9q4UiQsnLFzhA,27204
9
9
  egse/device.py,sha256=Ga5MNE96ERuPLDzmb0HF7UfjrTqfp0f_r3NDVZY9l28,13595
10
10
  egse/dicts.py,sha256=dUAq7PTPvs73OrZb2Fh3loxvYv4ifUiK6bBcgrFU77Y,3972
11
- egse/env.py,sha256=2ZeEOyVUp9IANy4S0XK53KUMohelbct7S3lR6iWpeks,31704
12
- egse/exceptions.py,sha256=QB3MZRJizecWOj1cPbvG0UcIqFn7NRJ6rw1xtdNSFxw,1225
11
+ egse/env.py,sha256=MWftGEmodckKbjXxFGm3WsQRnZ96wCDR3bci0KzZP0Y,31705
12
+ egse/exceptions.py,sha256=yhOtO5NkCxgvTyVo8fgTyxauX-ZtiEkxDB76ZdCbPrM,1301
13
13
  egse/heartbeat.py,sha256=2SeZzX3tWFog1rgYThX-iaZPwHYq8TVma2ll7r624Eg,3039
14
14
  egse/hk.py,sha256=AumSpB8SYXes75CB2iiKXfLkMK5IkVDHITFKrf8IT6g,32010
15
- egse/log.py,sha256=Q_TviwuyMam6CwRklaJk1lZM5WIZK_L9GS7wWDy_naM,5251
15
+ egse/log.py,sha256=5sdVQC2DfBlHbOQzY8ChzLjoL7kNDMErTZFaj97aNBE,5350
16
16
  egse/metrics.py,sha256=2hHtJXG0Rn782l2bfmLNBbw6ucC5nf7jPnNzqbhP_Zs,7012
17
17
  egse/observer.py,sha256=xQ7F7NVHqdRZ6IIsBM5M0kMuullMghoR98dwAsjgh0s,1287
18
18
  egse/obsid.py,sha256=y87AYX5mtNEBqEtpEFEec2MhEmo1Hej3Wwi5od84wR8,5848
@@ -25,21 +25,21 @@ egse/ratelimit.py,sha256=JdJxD6UIi9LYngKEsG9zh8bTE9r_56D4EZCnp_fkrI0,9161
25
25
  egse/reload.py,sha256=PzOE0m1tmcNcQPVFH8orMe_cMoQIIiH9Gw2anpQTC40,4717
26
26
  egse/resource.py,sha256=kzNI6kJOE6Jd5QKJs2MkVAycUpwpOTLi1qydh3NSRng,15345
27
27
  egse/response.py,sha256=F04uqOYv1ClpHgDLYZlKTuOCSldHs5TezI_4x6zf2Fw,2717
28
- egse/scpi.py,sha256=4OxgNc7HhQKZ7Gsg_tYstQKw_o4BxH8LVslG_X5ecz8,15965
29
- egse/settings.py,sha256=m95NuhLYdmRK4-SKZ4awhGp4X_6vCcb_yLvm7dmY-Dg,15656
28
+ egse/scpi.py,sha256=N4R9w7mN_cDBBaIT_cMLD6XLckndPNfOeOPvCgIOUK0,16231
29
+ egse/settings.py,sha256=eiZ9eGydgF9lNBjHH8VqOgcFDxSdhO6dLs7pYA725lo,16849
30
30
  egse/settings.yaml,sha256=mz9O2QqmiptezsMvxJRLhnC1ROwIHENX0nbnhMaXUpE,190
31
31
  egse/setup.py,sha256=ezPYA3n1P3navdPR3qDxh0qJvZCzGl2bIREEP9n2w3Y,34116
32
32
  egse/signal.py,sha256=f5pyOiNW9iTSIxV_ce5stIfG0ub9MRbaekE85kQOVzs,7992
33
- egse/socketdevice.py,sha256=7Q1DstV94pgilOD_e5BnqprTGcXi-XNijdLTw4m4dRY,14726
33
+ egse/socketdevice.py,sha256=gcte_MRx7zZNmGPw9iHCiCjqkEyZ0Iyz6U5BiIozjPQ,14730
34
34
  egse/state.py,sha256=HdU2MFOlYRbawYRZmizV6Y8MgnZrUF0bx4fXaYU-M_s,3023
35
- egse/system.py,sha256=3aycoK15UvNdk2yxQ8hx4N-s2Ag4BqTTPQ8eFuJa3Ko,76111
35
+ egse/system.py,sha256=DWKOWkqz5HJMW3526VN7wbMAQv1_EuY3oNeavEydUak,77517
36
36
  egse/task.py,sha256=ODSLE05f31CgWsSVcVFFq1WYUZrJMb1LioPTx6VM824,2804
37
- egse/version.py,sha256=e9GvelUZ9mfCDlRju4MWEJeMHJW9kUzK6SKzJpyj91s,6156
37
+ egse/version.py,sha256=vPUsCy9HYR7nKm0Sg6EDoq1JtkBKPCr3kYrt9QYM1B8,6602
38
38
  egse/zmq_ser.py,sha256=YJFupsxuvhI8TJMeS2Hem9oMMcVmSBx0rZv93gvN-hA,3263
39
39
  egse/plugins/metrics/duckdb.py,sha256=E2eeNo3I7ajRuByodaYiPNvC0Zwyc7hsIlhr1W_eXdo,16148
40
40
  egse/plugins/metrics/influxdb.py,sha256=WnAqTWRkAyMSd7W2ASwUAIEwFborrv55iX-umceevFA,8162
41
41
  egse/plugins/metrics/timescaledb.py,sha256=Ug0NWDV1Ky2VeFY6tDZL9xg6AFgnAEh2F_llVPnlRBA,21191
42
- cgse_common-0.17.1.dist-info/METADATA,sha256=DKZF-wlYPiRujjFleFHI1IIgWL2GdLDJ3ySq-gOK1xk,3068
43
- cgse_common-0.17.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
44
- cgse_common-0.17.1.dist-info/entry_points.txt,sha256=xJsPRIDjtADVgd_oEDHVW10wS5LG30Ox_3brVKeyCGw,168
45
- cgse_common-0.17.1.dist-info/RECORD,,
42
+ cgse_common-0.17.2.dist-info/METADATA,sha256=YtL6AJnAcG0svnDmWhjTTZ37wDuXPJPb6pfaGyK56VU,3068
43
+ cgse_common-0.17.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
44
+ cgse_common-0.17.2.dist-info/entry_points.txt,sha256=xJsPRIDjtADVgd_oEDHVW10wS5LG30Ox_3brVKeyCGw,168
45
+ cgse_common-0.17.2.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: hatchling 1.28.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
egse/bits.py CHANGED
@@ -5,7 +5,6 @@ This module contains a number of convenience functions to work with bits, bytes
5
5
  from __future__ import annotations
6
6
 
7
7
  import ctypes
8
- from collections.abc import Iterable
9
8
  from typing import Union
10
9
 
11
10
 
@@ -33,7 +32,7 @@ def extract_bits(value: int, start_position: int, num_bits: int) -> int:
33
32
  return extracted_bits
34
33
 
35
34
 
36
- def set_bit(value: int, bit) -> int:
35
+ def set_bit(value: int, bit: int) -> int:
37
36
  """
38
37
  Set bit to 1 for the given value.
39
38
 
@@ -47,7 +46,7 @@ def set_bit(value: int, bit) -> int:
47
46
  return value | (1 << bit)
48
47
 
49
48
 
50
- def set_bits(value: int, bits: tuple) -> int:
49
+ def set_bits(value: int, bits: tuple[int, int]) -> int:
51
50
  """
52
51
  Set the given bits in value to 1.
53
52
 
@@ -63,7 +62,7 @@ def set_bits(value: int, bits: tuple) -> int:
63
62
  return value
64
63
 
65
64
 
66
- def clear_bit(value: int, bit) -> int:
65
+ def clear_bit(value: int, bit: int) -> int:
67
66
  """
68
67
  Set bit to 0 for the given value.
69
68
 
@@ -77,7 +76,7 @@ def clear_bit(value: int, bit) -> int:
77
76
  return value & ~(1 << bit)
78
77
 
79
78
 
80
- def clear_bits(value: int, bits: tuple) -> int:
79
+ def clear_bits(value: int, bits: tuple[int, int]) -> int:
81
80
  """
82
81
  Set the given bits in value to 0.
83
82
 
@@ -93,7 +92,7 @@ def clear_bits(value: int, bits: tuple) -> int:
93
92
  return value
94
93
 
95
94
 
96
- def toggle_bit(value: int, bit) -> int:
95
+ def toggle_bit(value: int, bit: int) -> int:
97
96
  """
98
97
  Toggle the bit in the given value.
99
98
 
@@ -107,7 +106,7 @@ def toggle_bit(value: int, bit) -> int:
107
106
  return value ^ (1 << bit)
108
107
 
109
108
 
110
- def bit_set(value: int, bit) -> bool:
109
+ def bit_set(value: int, bit: int) -> bool:
111
110
  """
112
111
  Return True if the bit is set.
113
112
 
@@ -122,27 +121,25 @@ def bit_set(value: int, bit) -> bool:
122
121
  return value & bit_value == bit_value
123
122
 
124
123
 
125
- def bits_set(value: int, *args: Union[int, Iterable[int]]) -> bool:
124
+ def bits_set(value: int, *args: int) -> bool:
126
125
  """
127
126
  Return True if all the bits are set.
128
127
 
129
128
  Args:
130
129
  value (int): the value to check
131
130
  args: a set of indices of the bits to check, starting from 0 at the LSB.
132
- All the indices can be given as separate arguments, or they can be passed
133
- in as a list.
131
+ All the indices shall be given as separate arguments, i.e. unpack a list
132
+ if needed.
134
133
 
135
134
  Returns:
136
135
  True if all the bits are set (1).
137
136
 
138
137
  Examples:
139
- >>> assert bits_set(0b0101_0000_1011, [0, 1, 3, 8, 10])
140
- >>> assert bits_set(0b0101_0000_1011, [3, 8])
141
- >>> assert not bits_set(0b0101_0000_1011, [1, 2, 3])
138
+ >>> assert bits_set(0b0101_0000_1011, 0, 1, 3, 8, 10)
139
+ >>> assert bits_set(0b0101_0000_1011, 3, 8)
140
+ >>> assert not bits_set(0b0101_0000_1011, *[1, 2, 3])
142
141
  """
143
142
 
144
- if len(args) == 1 and isinstance(args[0], list):
145
- args = args[0]
146
143
  return all([bit_set(value, bit) for bit in args])
147
144
 
148
145
 
@@ -506,7 +503,7 @@ def crc_calc(data: list[bytes | int], start: int, len_: int) -> int:
506
503
  Reference:
507
504
  The description of the CRC calculation for RMAP is given in the ECSS document
508
505
  _Space Engineering: SpaceWire - Remote Memory Access Protocol_, section A.3
509
- on page 80 [ECSSEST5052C].
506
+ on page 80 [ECSS-E-ST-50-52C].
510
507
 
511
508
  """
512
509
  crc: int = 0
@@ -548,7 +545,7 @@ def s16(value: int) -> int:
548
545
  >>> s16(0b1000_0000_0001_0001)
549
546
  -32751
550
547
 
551
- The 'bin()' fuction will return a strange representation of this number:
548
+ The 'bin()' function will return a strange representation of this number:
552
549
 
553
550
  >>> bin(-32751)
554
551
  '-0b111111111101111'
egse/decorators.py CHANGED
@@ -15,8 +15,8 @@ from typing import Optional
15
15
 
16
16
  import rich
17
17
 
18
- from egse.system import get_caller_info
19
18
  from egse.log import logger
19
+ from egse.system import get_caller_info
20
20
 
21
21
 
22
22
  def static_vars(**kwargs):
@@ -316,7 +316,10 @@ def debug(func):
316
316
 
317
317
 
318
318
  def profile_func(
319
- output_file: str = None, sort_by: str = "cumulative", lines_to_print: int = None, strip_dirs: bool = False
319
+ output_file: str | None = None,
320
+ sort_by: str = "cumulative",
321
+ lines_to_print: int | None = None,
322
+ strip_dirs: bool = False,
320
323
  ) -> Callable:
321
324
  """A time profiler decorator.
322
325
 
@@ -346,7 +349,7 @@ def profile_func(
346
349
  decorator](https://gist.github.com/ekhoda/2de44cf60d29ce24ad29758ce8635b78).
347
350
 
348
351
  Inspired by and modified the profile decorator of Giampaolo Rodola:
349
- [profile decorato](http://code.activestate.com/recipes/577817-profile-decorator/).
352
+ [profile decorator](http://code.activestate.com/recipes/577817-profile-decorator/).
350
353
 
351
354
 
352
355
  """
egse/env.py CHANGED
@@ -72,8 +72,8 @@ import os
72
72
  import warnings
73
73
  from pathlib import Path
74
74
 
75
- from dotenv import load_dotenv as _load_dotenv
76
75
  from dotenv import find_dotenv
76
+ from dotenv import load_dotenv as _load_dotenv
77
77
  from rich.console import Console
78
78
 
79
79
  from egse.decorators import static_vars
@@ -697,6 +697,7 @@ def env_var(**kwargs: str | int | float | bool | None):
697
697
  def main(args: list | None = None): # pragma: no cover
698
698
  import argparse
699
699
  import sys
700
+
700
701
  import rich
701
702
 
702
703
  parser = argparse.ArgumentParser()
egse/exceptions.py CHANGED
@@ -61,3 +61,6 @@ class Abort(RuntimeError):
61
61
 
62
62
  class InitialisationError(Error):
63
63
  """Raised when an initialisation failed."""
64
+
65
+
66
+ InitializationError = InitialisationError # Alias with American spelling
egse/log.py CHANGED
@@ -18,6 +18,7 @@ __all__ = [
18
18
  "LOG_DATE_FORMAT_FULL",
19
19
  "LOG_DATE_FORMAT_CLEAN",
20
20
  "logger",
21
+ "logging", # export to guarantee that this module is imported and initialized before users use logging
21
22
  "root_logger",
22
23
  "egse_logger",
23
24
  "get_log_level_from_env",
@@ -84,7 +85,7 @@ class NonEGSEFilter(logging.Filter):
84
85
  return not record.name.startswith("egse")
85
86
 
86
87
 
87
- def get_log_level_from_env(env_var: str = "LOG_LEVEL", default: int = logging.INFO):
88
+ def get_log_level_from_env(env_var: str = "LOG_LEVEL", default: str = "INFO") -> int:
88
89
  """Read the log level from an environment variable."""
89
90
  log_level_str = os.getenv(env_var, default)
90
91
 
@@ -95,18 +96,16 @@ def get_log_level_from_env(env_var: str = "LOG_LEVEL", default: int = logging.IN
95
96
  if 10 <= log_level <= 50:
96
97
  return log_level
97
98
  else:
98
- logging.warning(
99
- f"Log level {log_level} outside standard range (10-50). Using {logging.getLevelName(default)}."
100
- )
101
- return default
99
+ logging.warning(f"Log level {log_level} outside standard range (10-50). Using {default.upper()}.")
100
+ return logging._nameToLevel[default.upper()]
102
101
 
103
102
  except ValueError:
104
103
  log_level_str = log_level_str.upper()
105
104
  try:
106
105
  return getattr(logging, log_level_str)
107
106
  except AttributeError:
108
- logging.error(f"Invalid LOG_LEVEL '{log_level_str}'. Using {logging.getLevelName(default)}.")
109
- return default
107
+ logging.error(f"Invalid LOG_LEVEL '{log_level_str}'. Using {default.upper()}.")
108
+ return logging._nameToLevel[default.upper()]
110
109
 
111
110
 
112
111
  egse_logger = logging.getLogger("egse")
@@ -145,7 +144,7 @@ if __name__ == "__main__":
145
144
  Environment variables:
146
145
  - LOG_LEVEL=debug|info|warning|critical
147
146
  - LOG_FORMAT=full|clean
148
-
147
+
149
148
  Example logging statements
150
149
  - logging level set to INFO
151
150
  - logging format set to full
egse/scpi.py CHANGED
@@ -9,12 +9,15 @@ from egse.device import AsyncDeviceTransport
9
9
  from egse.device import DeviceConnectionError
10
10
  from egse.device import DeviceError
11
11
  from egse.device import DeviceTimeoutError
12
+ from egse.env import bool_env
12
13
  from egse.log import logger
13
14
 
14
15
  DEFAULT_READ_TIMEOUT = 1.0 # seconds
15
16
  DEFAULT_CONNECT_TIMEOUT = 3.0 # seconds
16
17
  IDENTIFICATION_QUERY = "*IDN?"
17
18
 
19
+ VERBOSE_DEBUG = bool_env("VERBOSE_DEBUG")
20
+
18
21
  SEPARATOR = b"\n"
19
22
  SEPARATOR_STR = SEPARATOR.decode()
20
23
 
@@ -75,8 +78,7 @@ class AsyncSCPIInterface(AsyncDeviceInterface, AsyncDeviceTransport):
75
78
  self._connect_lock = asyncio.Lock()
76
79
  """Prevents multiple, simultaneous connect or disconnect attempts."""
77
80
  self._io_lock = asyncio.Lock()
78
- """Prevents multiple coroutines from attempting to read, write or query from the same stream
79
- at the same time."""
81
+ """Prevents multiple coroutines from attempting to read, write or query from the same stream at the same time."""
80
82
 
81
83
  def is_simulator(self) -> bool:
82
84
  return False
@@ -166,6 +168,10 @@ class AsyncSCPIInterface(AsyncDeviceInterface, AsyncDeviceTransport):
166
168
 
167
169
  self._is_connection_open = True
168
170
 
171
+ response = await self.read_string()
172
+ if VERBOSE_DEBUG:
173
+ logger.debug(f"Response after connection: {response}")
174
+
169
175
  logger.debug(f"Successfully connected to {self._device_name}.")
170
176
 
171
177
  except asyncio.TimeoutError as exc:
@@ -338,23 +344,23 @@ class AsyncSCPIInterface(AsyncDeviceInterface, AsyncDeviceTransport):
338
344
  if not self._is_connection_open or self._writer is None:
339
345
  raise DeviceConnectionError(self._device_name, "Device not connected, use connect() first")
340
346
 
341
- # Ensure command ends with newline
347
+ # Ensure command ends with the required terminator
342
348
  if not command.endswith(SEPARATOR_STR):
343
349
  command += SEPARATOR_STR
344
350
 
345
- logger.info(f"-----> {command}")
351
+ logger.info(f"-----> {command=}")
346
352
  self._writer.write(command.encode())
347
353
  await self._writer.drain()
348
354
 
349
- # First, small delay to allow device to prepare response
355
+ # First, small delay to allow the device to prepare a response
350
356
  await asyncio.sleep(0.01)
351
357
 
352
- # Try to read until newline (common SCPI terminator)
358
+ # Try to read until the required separator (common SCPI terminator)
353
359
  try:
354
360
  response = await asyncio.wait_for(
355
361
  self._reader.readuntil(separator=SEPARATOR), timeout=self.read_timeout
356
362
  )
357
- logger.info(f"<----- {response}")
363
+ logger.info(f"<----- {response=}")
358
364
  return response
359
365
 
360
366
  except asyncio.IncompleteReadError as exc:
egse/settings.py CHANGED
@@ -380,6 +380,37 @@ class Settings:
380
380
 
381
381
  return msg.rstrip()
382
382
 
383
+ @classmethod
384
+ def from_string(cls, yaml_string: str, group: str | None = None) -> attrdict:
385
+ """
386
+ Creates a Settings object from a YAML string.
387
+
388
+ Args:
389
+ yaml_string (str): the YAML configuration as a string
390
+
391
+ Returns:
392
+ An attribute dictionary with all the settings from the given string.
393
+ """
394
+ try:
395
+ yaml_document = yaml.load(yaml_string, Loader=SAFE_LOADER)
396
+ settings = attrdict({name: value for name, value in yaml_document.items()}, label="Settings")
397
+ except yaml.YAMLError as exc:
398
+ logger.error(exc)
399
+ raise SettingsError("Error loading YAML document from string") from exc
400
+
401
+ if not settings:
402
+ logger.warning(
403
+ "The Settings YAML string is empty. No local settings were loaded, an empty dictionary is returned."
404
+ )
405
+
406
+ if group:
407
+ if group in settings:
408
+ settings = attrdict({name: value for name, value in settings[group].items()}, label=group)
409
+ else:
410
+ raise SettingsError(f"Group name '{group}' is not defined in the provided YAML string.")
411
+
412
+ return settings
413
+
383
414
 
384
415
  def main(args: list | None = None): # pragma: no cover
385
416
  # We provide convenience to inspect the settings by calling this module directly from Python.
egse/socketdevice.py CHANGED
@@ -155,7 +155,7 @@ class SocketDevice(DeviceConnectionInterface, DeviceTransport):
155
155
 
156
156
  try:
157
157
  while True:
158
- # compute remaining timeout for select, this is needed because we read in different parts
158
+ # compute the remaining timeout for select, this is needed because we read in different parts
159
159
  # until ETX is received, and we want to receive the complete messages including ETX within
160
160
  # the read timeout.
161
161
  if end_time is None:
egse/system.py CHANGED
@@ -10,8 +10,6 @@ The module has external dependencies to:
10
10
 
11
11
  """
12
12
 
13
- from __future__ import annotations
14
-
15
13
  import asyncio
16
14
  import builtins
17
15
  import collections
@@ -30,6 +28,7 @@ import os
30
28
  import platform # For getting the operating system name
31
29
  import re
32
30
  import shutil
31
+ import signal
33
32
  import socket
34
33
  import subprocess # For executing a shell command
35
34
  import sys
@@ -60,7 +59,6 @@ from rich.text import Text
60
59
  from rich.tree import Tree
61
60
  from typer.core import TyperCommand
62
61
 
63
- import signal
64
62
  from egse.log import logger
65
63
 
66
64
  EPOCH_1958_1970 = 378691200
@@ -107,7 +105,7 @@ class Periodic:
107
105
  interval: float,
108
106
  *,
109
107
  name: str | None = None,
110
- callback: Callable = None,
108
+ callback: Callable | None = None,
111
109
  repeat: int | None = None,
112
110
  skip: bool = True,
113
111
  pause: bool = False,
@@ -202,7 +200,7 @@ class Periodic:
202
200
  """Triggers the Timer's action: either call its callback, or logs a message."""
203
201
 
204
202
  if self._callback is None:
205
- self._logger.warning(f"Periodic No callback provided for interval timer {self.name}.")
203
+ self._logger.warning(f"Periodic - No callback provided for interval timer {self.name}.")
206
204
  return
207
205
 
208
206
  try:
@@ -211,7 +209,7 @@ class Periodic:
211
209
  self._logger.debug("Caught CancelledError on callback function in Periodic.")
212
210
  raise
213
211
  except Exception as exc:
214
- self._logger.error(f"{type(exc).__name__} caught: {exc}")
212
+ self._logger.error(f"{type_name(exc)} caught: {exc}")
215
213
 
216
214
  @property
217
215
  def interval(self):
@@ -370,8 +368,8 @@ def ignore_m_warning(modules=None):
370
368
  modules = [modules]
371
369
 
372
370
  try:
373
- import warnings
374
371
  import re
372
+ import warnings
375
373
 
376
374
  msg = "'{module}' found in sys.modules after import of package"
377
375
  for module in modules:
@@ -390,7 +388,10 @@ def now(utc: bool = True):
390
388
 
391
389
 
392
390
  def format_datetime(
393
- dt: Union[str, datetime.datetime] = None, fmt: str = None, width: int = 6, precision: int = 3
391
+ dt: str | datetime.datetime | datetime.date | None = None,
392
+ fmt: str | None = None,
393
+ width: int = 6,
394
+ precision: int = 3,
394
395
  ) -> str:
395
396
  """Format a datetime as YYYY-mm-ddTHH:MM:SS.μs+0000.
396
397
 
@@ -452,6 +453,10 @@ def format_datetime(
452
453
  if fmt:
453
454
  timestamp = dt.strftime(fmt)
454
455
  else:
456
+ # If dt is a date (not datetime), convert to datetime at midnight
457
+ if isinstance(dt, datetime.date) and not isinstance(dt, datetime.datetime):
458
+ dt = datetime.datetime.combine(dt, datetime.time.min)
459
+
455
460
  width = min(width, precision)
456
461
  timestamp = (
457
462
  f"{dt.strftime('%Y-%m-%dT%H:%M')}:"
@@ -727,18 +732,29 @@ def get_host_ip() -> Optional[str]:
727
732
  return None
728
733
 
729
734
 
730
- def get_current_location():
735
+ def get_current_location() -> tuple[str, int, str]:
731
736
  """
732
737
  Returns the location where this function is called, i.e. the filename, line number, and function name.
738
+
739
+ If the location cannot be determined, ("", 0, "") is returned.
733
740
  """
734
- frame = inspect.currentframe().f_back
741
+ frame = inspect.currentframe()
742
+ logger.debug(f"{frame = }")
743
+ if frame is None:
744
+ return "", 0, ""
735
745
 
736
- filename = inspect.getframeinfo(frame).filename
737
- line_number = inspect.getframeinfo(frame).lineno
738
- function_name = inspect.getframeinfo(frame).function
746
+ previous_frame = frame.f_back
747
+ logger.debug(f"{previous_frame = }")
748
+ if previous_frame is None:
749
+ return "", 0, ""
750
+
751
+ filename = inspect.getframeinfo(previous_frame).filename
752
+ line_number = inspect.getframeinfo(previous_frame).lineno
753
+ function_name = inspect.getframeinfo(previous_frame).function
739
754
 
740
755
  # Clean up to prevent reference cycles
741
756
  del frame
757
+ del previous_frame
742
758
 
743
759
  return filename, line_number, function_name
744
760
 
@@ -1364,7 +1380,7 @@ def chdir(dirname=None):
1364
1380
 
1365
1381
 
1366
1382
  @contextlib.contextmanager
1367
- def env_var(**kwargs: dict[str, str]):
1383
+ def env_var(**kwargs: str):
1368
1384
  """
1369
1385
  Context manager to run some code that need alternate settings for environment variables.
1370
1386
 
@@ -1540,6 +1556,7 @@ def read_last_lines(filename: str | Path, num_lines: int) -> List[str]:
1540
1556
  sanity_check(num_lines >= 0, "the number of lines to read shall be a positive number or zero.")
1541
1557
 
1542
1558
  if not filename.exists():
1559
+ logger.warning(f"File does not exist: {filename}")
1543
1560
  return []
1544
1561
 
1545
1562
  # Declaring variable to implement exponential search
@@ -2321,22 +2338,44 @@ def caffeinate(pid: int = None):
2321
2338
  subprocess.Popen([shutil.which("caffeinate"), "-i", "-w", str(pid)])
2322
2339
 
2323
2340
 
2324
- def redirect_output_to_log(output_fn: str, append: bool = False) -> TextIO:
2341
+ def redirect_output_to_log(output_fn: str, append: bool = False, overwrite=True) -> TextIO:
2325
2342
  """
2326
- Open file in the log folder where process output will be redirected.
2327
-
2343
+ Open the file in the log folder where the current process output will be redirected.
2328
2344
  When no location can be determined, the user's home directory will be used.
2329
2345
 
2330
- The file is opened in text mode at the given location and the stream (file descriptor) will be returned.
2346
+ The file will be opened in text mode.
2347
+
2348
+ Args:
2349
+ output_fn: the name of the output file
2350
+ append: True to append to the file, False to overwrite
2351
+ overwrite: when False and the file exists, an exception is raised
2352
+
2353
+ Returns:
2354
+ The file stream (TextIO) where output can be redirected to.
2355
+
2356
+ Raises:
2357
+ FileExistsError: when the output file exists, append is False and overwrite is False.
2331
2358
  """
2332
2359
 
2333
- try:
2334
- from egse.env import get_log_file_location
2360
+ if Path(output_fn).is_absolute():
2361
+ output_path = Path(output_fn)
2362
+ else:
2363
+ try:
2364
+ from egse.env import get_log_file_location
2365
+
2366
+ location = get_log_file_location()
2367
+ output_path = Path(location, output_fn).expanduser()
2368
+ except ValueError:
2369
+ output_path = Path.home() / output_fn
2335
2370
 
2336
- location = get_log_file_location()
2337
- output_path = Path(location, output_fn).expanduser()
2338
- except ValueError:
2339
- output_path = Path.home() / output_fn
2371
+ output_path.parent.mkdir(parents=True, exist_ok=True)
2372
+
2373
+ if output_path.exists() and not append:
2374
+ if not overwrite:
2375
+ raise FileExistsError(
2376
+ f"Output file {output_path!s} already exists and will be overwritten. "
2377
+ f"Use overwrite=True to allow overwriting."
2378
+ )
2340
2379
 
2341
2380
  out = open(output_path, "a" if append else "w")
2342
2381
 
egse/version.py CHANGED
@@ -31,7 +31,7 @@ __all__ = [
31
31
  ]
32
32
 
33
33
 
34
- def get_version_from_settings_file_raw(group_name: str, location: Path | str = None) -> str:
34
+ def get_version_from_settings_file_raw(group_name: str, location: Path | str | None = None) -> str:
35
35
  """
36
36
  Reads the VERSION field from the `settings.yaml` file in raw mode, meaning the file
37
37
  is not read using the PyYAML module, but using the `readline()` function of the file
@@ -66,7 +66,7 @@ def get_version_from_settings_file_raw(group_name: str, location: Path | str = N
66
66
  return version
67
67
 
68
68
 
69
- def get_version_from_settings(group_name: str, location: Path = None):
69
+ def get_version_from_settings(group_name: str, location: Path | None = None, yaml_string: str | None = None) -> str:
70
70
  """
71
71
  Reads the VERSION field from the `settings.yaml` file. This function first tries to load the proper Settings
72
72
  and Group and if that fails uses the raw method.
@@ -74,6 +74,7 @@ def get_version_from_settings(group_name: str, location: Path = None):
74
74
  Args:
75
75
  group_name: major group name that contains the VERSION field, i.e. Common-EGSE or PLATO_TEST_SCRIPTS.
76
76
  location: the location of the `settings.yaml` file or None in which case the location of this file is used.
77
+ yaml_string: optional YAML string to read the settings from instead of a file.
77
78
 
78
79
  Raises:
79
80
  A RuntimeError when the group_name is incorrect and unknown or the VERSION field is not found.
@@ -81,10 +82,14 @@ def get_version_from_settings(group_name: str, location: Path = None):
81
82
  Returns:
82
83
  The version from the `settings.yaml` file as a string.
83
84
  """
84
- from egse.settings import Settings, SettingsError
85
+ from egse.settings import Settings
86
+ from egse.settings import SettingsError
85
87
 
86
88
  try:
87
- settings = Settings.load(group_name, location=location)
89
+ if location is None and yaml_string is not None:
90
+ settings = Settings.from_string(yaml_string, group=group_name)
91
+ else:
92
+ settings = Settings.load(group_name, location=location)
88
93
  version = settings.VERSION
89
94
  except (ModuleNotFoundError, SettingsError):
90
95
  version = get_version_from_settings_file_raw(group_name, location=location)
@@ -92,7 +97,7 @@ def get_version_from_settings(group_name: str, location: Path = None):
92
97
  return version
93
98
 
94
99
 
95
- def get_version_from_git(location: str = None):
100
+ def get_version_from_git(location: str | Path | None = None) -> str:
96
101
  """
97
102
  Returns the Git version number for the repository at the given location.
98
103
 
@@ -121,12 +126,12 @@ def get_version_from_git(location: str = None):
121
126
  proc = subprocess.run(
122
127
  ["git", "describe", "--tags", "--long", "--always"], stderr=subprocess.PIPE, stdout=subprocess.PIPE
123
128
  )
124
- if proc.stderr:
125
- version = None
126
129
  if proc.stdout:
127
130
  version = proc.stdout.strip().decode("ascii")
131
+ else:
132
+ version = "0.0.0"
128
133
  except subprocess.CalledProcessError:
129
- version = None
134
+ version = "0.0.0"
130
135
 
131
136
  return version
132
137
 
@@ -144,12 +149,13 @@ def get_version_installed(package_name: str) -> str:
144
149
  from egse.system import chdir
145
150
 
146
151
  with chdir(Path(__file__).parent):
147
- from importlib.metadata import version, PackageNotFoundError
152
+ from importlib.metadata import PackageNotFoundError
153
+ from importlib.metadata import version as get_version
148
154
 
149
155
  try:
150
- version = version(package_name)
156
+ version = get_version(package_name)
151
157
  except PackageNotFoundError:
152
- version = None
158
+ version = "0.0.0"
153
159
 
154
160
  return version
155
161
 
@@ -162,11 +168,12 @@ VERSION = get_version_installed("cgse-common")
162
168
  # The __PYPI_VERSION__ is the version number that will be used for the installation.
163
169
  # The version will appear in PyPI and also as the `installed version`.
164
170
 
165
- __PYPI_VERSION__ = VERSION.split("+")[0]
171
+ __PYPI_VERSION__ = VERSION.split("+")[0] if VERSION else "0.0.0"
166
172
 
167
173
 
168
- if __name__ == "__main__":
174
+ def main():
169
175
  import rich
176
+
170
177
  from egse.plugin import entry_points
171
178
 
172
179
  if VERSION:
@@ -178,3 +185,7 @@ if __name__ == "__main__":
178
185
  for ep in entry_points("cgse.version"):
179
186
  if installed_version := get_version_installed(ep.name):
180
187
  rich.print(f"Installed version for {ep.name}= [bold default]{installed_version}[/]")
188
+
189
+
190
+ if __name__ == "__main__":
191
+ main()