DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
opmonlib.utils Namespace Reference

Classes

class  LoggingFormatter
 
class  LogLevelError
 

Functions

RichHandler setup_rich_handler ()
 
str logging_log_level_from_int (int level)
 
int logging_log_level_from_str (str level)
 
dict[str:str] parse_opmon_conf (logging.Logger log, dict[str:str]|"conffwk.dal.OpMonConf" conf, dict[str:str]|conffwk.dal.OpMonURI uri, str session, str application)
 
str to_string (OpMonId opmon_id)
 
str extract_opmon_file_path (str file_path, OpMonId|None origin=None)
 

Variables

dict logging_log_levels
 
 logging_log_level_keys = list(logging_log_levels.keys())
 
 logging_log_level_values = list(logging_log_levels.values())
 
dict oks_log_levels
 
 oks_log_level_keys = list(oks_log_levels.keys())
 
 oks_log_level_values = list(oks_log_levels.values())
 
dict oks_to_logging_map
 
 log_level_keys = logging_log_level_keys + oks_log_level_keys
 
 log_level_values = logging_log_level_values + logging_log_level_values
 
tuple e_log_levels
 
dict CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}
 
 CONSOLE_THEMES = Theme({"info": "dim cyan", "warning": "magenta", "danger": "bold red"})
 
str full_log_format = "%(asctime)s %(levelname)s %(filename)s %(name)s %(message)s"
 
str rich_log_format = "%(filename)s %(name)s %(message)s"
 
str date_time_format = "[%Y/%m/%d %H:%M:%S]"
 
 time_zone = pytz.utc
 

Function Documentation

◆ extract_opmon_file_path()

str opmonlib.utils.extract_opmon_file_path ( str file_path,
OpMonId | None origin = None )
Verify the file path can be opened.

Definition at line 270 of file utils.py.

270def extract_opmon_file_path(file_path: str, origin: OpMonId | None = None) -> str:
271 """Verify the file path can be opened."""
272 hook = "://"
273 hook_position = file_path.find(hook)
274 fname = None
275 if hook_position == -1:
276 fname = file_path
277 else:
278 fname = file_path[hook_position + len(hook) :]
279
280 if origin:
281 slash_pos = fname.rfind("/")
282 if slash_pos == -1:
283 dot_pos = fname.find(".")
284 else:
285 dot_pos = fname.find(".", slash_pos)
286 origin = to_string(origin)
287 if dot_pos == -1:
288 fname += "." + origin + ".json"
289 else:
290 fname = fname[:dot_pos] + "." + origin + fname[dot_pos:]
291
292 try:
293 with open(fname, "a"):
294 pass
295 except OSError:
296 err_str = f"Can not open file {fname}"
297 raise OSError(err_str) from None
298
299 return fname

◆ logging_log_level_from_int()

str opmonlib.utils.logging_log_level_from_int ( int level)
Get the level name from its int value.

Definition at line 143 of file utils.py.

143def logging_log_level_from_int(level: int) -> str:
144 """Get the level name from its int value."""
145 if not isinstance(level, int):
146 return level
147 for k, v in logging_log_levels.items():
148 if v == level:
149 return k
150 for k, v in oks_log_levels.items():
151 if v == level:
152 return oks_to_logging_map[k]
153 err_str = f"Requested log level with value {level} is not standard ({e_log_levels})"
154 raise ValueError(err_str) from None
155
156

◆ logging_log_level_from_str()

int opmonlib.utils.logging_log_level_from_str ( str level)
Get the level int from its str value.

Definition at line 157 of file utils.py.

157def logging_log_level_from_str(level: str) -> int:
158 """Get the level int from its str value."""
159 if not isinstance(level, str):
160 return level
161 for k, v in logging_log_levels.items():
162 if k == level.upper():
163 return v
164 for k in oks_log_levels.keys():
165 if k == level:
166 return logging_log_levels[oks_to_logging_map[k]]
167 err_str = f"Requested log level with value {level} is not standard ({e_log_levels})"
168 raise ValueError(err_str) from None
169
170

◆ parse_opmon_conf()

dict[str:str] opmonlib.utils.parse_opmon_conf ( logging.Logger log,
dict[str:str] | "conffwk.dal.OpMonConf" conf,
dict[str:str] | conffwk.dal.OpMonURI uri,
str session,
str application )
Parse the OpMonConf and OpMonURI.

Definition at line 171 of file utils.py.

177) -> dict[str:str]:
178 """Parse the OpMonConf and OpMonURI."""
179 if not conf:
180 log.error("Missing opmon configuration, exiting.")
181 sys.exit(1)
182 if not uri:
183 log.error("Missing opmon URI, exiting.")
184 sys.exit(1)
185
186 opmon_type = (
187 uri.get("type") if isinstance(uri, dict) else getattr(uri, "type", None)
188 )
189 if opmon_type:
190 log.debug("Found OpMon type: %s", opmon_type)
191 else:
192 log.debug(
193 "Missing 'type' in the opmon configuration, [yellow]using default value "
194 "'stdout'[/yellow]."
195 )
196 opmon_type = "stdout"
197
198 path = uri.get("path") if isinstance(uri, dict) else getattr(uri, "path", None)
199 if path:
200 log.debug("Found OpMon path: %s", path)
201 elif opmon_type != "stdout":
202 log.error("Missing 'path' in the opmon configuration, exiting.")
203 sys.exit(1)
204 else:
205 if path == []:
206 path = ""
207 log.debug("No OpMon path required for type 'stdout'.")
208
209 bootstrap = None
210 topic = None
211 if opmon_type == "file" and not Path(path).parent.is_dir():
212 err_str = "Requested directory to put file in does not exist."
213 raise ValueError(err_str) from None
214 if "monkafka" in path:
215 bootstrap, topic = path.split("/", 1)
216 if not topic:
217 topic = "opmon_stream"
218 log.debug("Using OpMon topic: [green]'%s'[/green]", topic)
219 log.debug("Using OpMon bootstrap: [green]'%s'[/green]", bootstrap)
220
221 level = (
222 conf.get("level") if isinstance(conf, dict) else getattr(conf, "level", None)
223 )
224 if level:
225 log.debug("Found OpMon level: [green]%s[/green]", level)
226 else:
227 log.debug(
228 "Missing 'level' in the OpMon configuration, [yellow]using default "
229 "'DEBUG'[/yellow]."
230 )
231 level = logging.DEBUG
232
233 interval_s = (
234 conf.get("interval_s")
235 if isinstance(conf, dict)
236 else getattr(conf, "interval_s", None)
237 )
238 if interval_s:
239 log.debug("Found OpMon interval_s: %s", interval_s)
240 else:
241 log.debug(
242 "Missing 'interval_s' in the opmon configuration, [yellow]using default "
243 "10s[/yellow]."
244 )
245 interval_s = 10.0
246
247 return OpMonConf(
248 opmon_type, bootstrap, topic, level, interval_s, path, session, application
249 )
250
251

◆ setup_rich_handler()

RichHandler opmonlib.utils.setup_rich_handler ( )
Initialize a Rich handler for terminal logging.

Definition at line 125 of file utils.py.

125def setup_rich_handler() -> RichHandler:
126 """Initialize a Rich handler for terminal logging."""
127 try:
128 width = os.get_terminal_size()[0]
129 except OSError:
130 width = 150
131 handler = RichHandler(
132 console=Console(width=width),
133 omit_repeated_times=False,
134 markup=True,
135 rich_tracebacks=True,
136 show_path=False,
137 tracebacks_width=width,
138 )
139 handler.setFormatter(LoggingFormatter(fmt=rich_log_format))
140 return handler
141
142

◆ to_string()

str opmonlib.utils.to_string ( OpMonId opmon_id)
Map the OpMonId to a string.

Definition at line 252 of file utils.py.

252def to_string(opmon_id: OpMonId) -> str:
253 """Map the OpMonId to a string."""
254 ret = opmon_id.get("session")
255 if not ret:
256 err_msg = "Missing session in OpMonId."
257 raise ValueError(err_msg) from None
258
259 application = opmon_id.get("application")
260 if application:
261 ret += "." + application
262
263 substructures = opmon_id.get("substructure")
264 for substructure in substructures:
265 ret += "." + substructure
266
267 return ret
268
269

Variable Documentation

◆ CONSOLE_THEMES

opmonlib.utils.CONSOLE_THEMES = Theme({"info": "dim cyan", "warning": "magenta", "danger": "bold red"})

Definition at line 77 of file utils.py.

◆ CONTEXT_SETTINGS

dict opmonlib.utils.CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}

Definition at line 76 of file utils.py.

◆ date_time_format

str opmonlib.utils.date_time_format = "[%Y/%m/%d %H:%M:%S]"

Definition at line 84 of file utils.py.

◆ e_log_levels

tuple opmonlib.utils.e_log_levels
Initial value:
1= (
2 f"{logging_log_level_keys} python logging or "
3 f"{oks_log_level_keys} for oks log levels."
4)

Definition at line 71 of file utils.py.

◆ full_log_format

str opmonlib.utils.full_log_format = "%(asctime)s %(levelname)s %(filename)s %(name)s %(message)s"

Definition at line 80 of file utils.py.

◆ log_level_keys

opmonlib.utils.log_level_keys = logging_log_level_keys + oks_log_level_keys

Definition at line 47 of file utils.py.

◆ log_level_values

opmonlib.utils.log_level_values = logging_log_level_values + logging_log_level_values

Definition at line 48 of file utils.py.

◆ logging_log_level_keys

opmonlib.utils.logging_log_level_keys = list(logging_log_levels.keys())

Definition at line 27 of file utils.py.

◆ logging_log_level_values

opmonlib.utils.logging_log_level_values = list(logging_log_levels.values())

Definition at line 28 of file utils.py.

◆ logging_log_levels

dict opmonlib.utils.logging_log_levels
Initial value:
1= {
2 "CRITICAL": logging.CRITICAL,
3 "ERROR": logging.ERROR,
4 "WARNING": logging.WARNING,
5 "INFO": logging.INFO,
6 "DEBUG": logging.DEBUG,
7 "NOTSET": logging.NOTSET,
8}

Definition at line 18 of file utils.py.

◆ oks_log_level_keys

opmonlib.utils.oks_log_level_keys = list(oks_log_levels.keys())

Definition at line 37 of file utils.py.

◆ oks_log_level_values

opmonlib.utils.oks_log_level_values = list(oks_log_levels.values())

Definition at line 38 of file utils.py.

◆ oks_log_levels

dict opmonlib.utils.oks_log_levels
Initial value:
1= {
2 "kTopPriority": 0,
3 "kEventDriven": 1073741824,
4 "kDefault": 2147483648,
5 "kLowestPriority": 4294967295,
6}

Definition at line 30 of file utils.py.

◆ oks_to_logging_map

dict opmonlib.utils.oks_to_logging_map
Initial value:
1= {
2 "kTopPriority": "ERROR",
3 "kEventDriven": "WARNING",
4 "kDefault": "INFO",
5 "kLowestPriority": "DEBUG",
6}

Definition at line 40 of file utils.py.

◆ rich_log_format

str opmonlib.utils.rich_log_format = "%(filename)s %(name)s %(message)s"

Definition at line 82 of file utils.py.

◆ time_zone

opmonlib.utils.time_zone = pytz.utc

Definition at line 85 of file utils.py.