From a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4 Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 16 Oct 2023 20:11:52 +0200 Subject: Enhance RPA, add logging. --- pyecsca/misc/cfg.py | 9 +++++++++ pyecsca/misc/utils.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'pyecsca/misc') diff --git a/pyecsca/misc/cfg.py b/pyecsca/misc/cfg.py index ee9c95d..f73eea9 100644 --- a/pyecsca/misc/cfg.py +++ b/pyecsca/misc/cfg.py @@ -121,6 +121,12 @@ class ECConfig: raise ValueError("Bad Mod implementaiton, can be one of 'python', 'gmp' or 'symbolic'.") self._mod_implementation = value +@public +class LoggingConfig: + """Logging configuration.""" + + enabled: bool = True + """Whether logging is enabled.""" @public class Config: @@ -128,9 +134,12 @@ class Config: ec: ECConfig """Configuration for the :py:mod:`pyecsca.ec` package.""" + log: LoggingConfig + """Logging configuration.""" def __init__(self): self.ec = ECConfig() + self.log = LoggingConfig() _config: ContextVar[Config] = ContextVar("config", default=Config()) diff --git a/pyecsca/misc/utils.py b/pyecsca/misc/utils.py index 1e9e9a0..010bbd2 100644 --- a/pyecsca/misc/utils.py +++ b/pyecsca/misc/utils.py @@ -1,6 +1,9 @@ """Just some utilities I promise.""" +import sys from ast import parse +from pyecsca.misc.cfg import getconfig + def pexec(s): return parse(s, mode="exec") @@ -8,3 +11,28 @@ def pexec(s): def peval(s): return parse(s, mode="eval") + + +def in_notebook() -> bool: + """Test whether we are executing in Jupyter notebook.""" + try: + from IPython import get_ipython + if 'IPKernelApp' not in get_ipython().config: # pragma: no cover + return False + except ImportError: + return False + except AttributeError: + return False + return True + + +def log(*args, **kwargs): + """Log a message.""" + if in_notebook() and getconfig().log.enabled: + print(*args, **kwargs) + + +def warn(*args, **kwargs): + """Log a message.""" + if in_notebook() and getconfig().log.enabled: + print(*args, **kwargs, file=sys.stderr) -- cgit v1.2.3-70-g09d2