diff --git a/pflog/pflog.py b/pflog/pflog.py index c957298..aab3988 100644 --- a/pflog/pflog.py +++ b/pflog/pflog.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -__version__ = '1.2.5' +__version__ = '1.2.6' from pathlib import Path @@ -331,15 +331,27 @@ def __call__(self, message:str, *args: Any, **kwds: Any) -> Any: self.options.log = message return self.run() -def tel_logTime(_func: Callable = None, *, - someother:str = '', - pftelDB:str = '', - name:str = '', - log:str = '', - isthis:str = ''): +def tel_logTime(_func:Callable = None, *, + pftelDB:str = '', + event:str = '', + log:str = ''): + """A decorator that times (and logs) a method. + + If called without any arguments, this decorator simply prints the time + (seconds) that a method took to execute. If called with named args, will + transmit the time (and optional log message) as an event to a pftelDB + server. + + Args: + _func (Callable, optional): the function to wrap. Defaults to None. + pftelDB (str, optional): telemetry server address. Defaults to ''. + event (str, optional): the name of this event. Defaults to ''. + log (str, optional): optional log message. Defaults to ''. + """ def decorator_time(func: Callable) -> Callable: @functools.wraps(func) def wrapped(*args, **kwargs) -> Any: + pftelDBlocal:str = '' d_log:dict = {} tagger:pftag.Pftag = pftag.Pftag({}) str_event:str = 'analysisEvent' @@ -350,17 +362,19 @@ def wrapped(*args, **kwargs) -> Any: print(f"{func} executed in {ft} second(s).") try: if pftelDB: - if name: - str_event = name - pftelDB = '/'.join(pftelDB.split('/')[:-1] + [str_event]) + if event: + str_event = event + pftelDBlocal = pftelDB + pftelDBlocal = '/'.join(pftelDB.split('/')[:-1] + [str_event]) d_log:dict = pfprint( - options.pftelDB, + pftelDBlocal, log, appName = str_event, execTime = ft ) except: pass + return f_ret, d_log return wrapped if _func is None: return decorator_time @@ -399,6 +413,7 @@ def pfprint(pftelspec:str, message:str, **kwargs) -> str: logObject:str = "" logCollection:str = "" logEvent:str = "" + pftelspec = pftelspec.strip() d_log = { "status": False, "reply": None, diff --git a/requirements.txt b/requirements.txt index 8b71bae..3cac033 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ pudb loguru pftel-client pftag +pytest-mock diff --git a/setup.py b/setup.py index f16c42d..6b10066 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ def get_version(rel_path: str) -> str: author = 'FNNDSC', author_email = 'rudolph.pienaar@childrens.harvard.edu', url = 'https://github.com/FNNDSC/pflog', - install_requires = ['pudb', 'loguru', 'pftag', 'pftel-client'], + install_requires = ['pudb', 'loguru', 'pftag', 'pftel-client', 'pytest-mock'], packages = ['pflog'], license = 'MIT', entry_points = { diff --git a/tests/test_pflog.py b/tests/test_pflog.py index 324147c..38614df 100644 --- a/tests/test_pflog.py +++ b/tests/test_pflog.py @@ -11,6 +11,7 @@ def test_pfprint() -> None: collection:str = '%timestamp_chrplc|:|-_' IP:str = socket.gethostbyname(socket.gethostname()) + # This test will fail if a local `pftel` server has not been started! pftelURL:str = f"http://{IP}:22223/api/v1/new/{collection}/event" d_log:dict = pflog.pfprint(pftelURL, "hello, world!" , appName = "testApp", execTime = 2.0) @@ -28,22 +29,28 @@ def test_pfprint_validSpecInvalidURL() -> None: d_log:dict = pflog.pfprint(pftelURL, "hello, world!" , appName = "testApp", execTime = 2.0) assert d_log['status'] is False -def test_mocpftelTimed() -> None: +def testmocptelTimedNoArgsDecorator(mocker) -> None: + + mock_print = mocker.patch('builtins.print') + @pflog.tel_logTime + def wait(seconds:float) -> None: + time.sleep(seconds) + wait(2) + assert mock_print.call_count is 1 + +def test_mocpftelTimed(mocker) -> None: """ Send message to the moc listener with a log time """ - pudb.set_trace() - pftelURL:str = r' https://pftel-chris-public.apps.ocp-prod.massopen.cloud/api/v1/timetest/%timestamp/analysis' + mock_print = mocker.patch('builtins.print') + pftelURL:str = r'https://pftel-chris-public.apps.ocp-prod.massopen.cloud/api/v1/timetest/%timestamp/analysis' + @pflog.tel_logTime( - someother = 'something', pftelDB = pftelURL, - name = 'pytest', - log = 'A two second delay logger', - isthis = pftelURL + event = 'pytest', + log = 'A two second delay logger' ) - # @pflog.tel_logTime - def wait(seconds:float): + def wait(seconds:float) -> None: time.sleep(seconds) wait(2) - assert True == True - + assert mock_print.call_count is 2