From 2d1b198261ef0badc34b890b820bd5de43a6767d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 30 Oct 2015 13:02:22 +0100 Subject: [PATCH] Some development utils in the octoprint.util package... --- src/octoprint/util/dev.py | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/octoprint/util/dev.py diff --git a/src/octoprint/util/dev.py b/src/octoprint/util/dev.py new file mode 100644 index 00000000..8eba007c --- /dev/null +++ b/src/octoprint/util/dev.py @@ -0,0 +1,66 @@ +# coding=utf-8 +""" +This module provides a bunch of utility methods and helpers FOR DEVELOPMENT ONLY. +""" + +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' + +import contextlib + +@contextlib.contextmanager +def duration_log(context=None, log=None): + """ + Context manager to log the duration of the wrapped code block. + + If no ``log`` function is provided will use a ``debug`` python logger for + ``octoprint.util.dev``. + + ``context`` can be set to give some textual context in the output. + + Arguments: + context (str): A custom string to give some textual context in the output. + log (callable): The log function to use to log the execution duration. + """ + if log is None: + import logging + log = logging.getLogger(__name__).debug + + import time + start = time.time() + try: + yield + finally: + end = time.time() + duration = end - start + + if context: + message = "Execution of {name} took {duration}s" + else: + message = "Execution of codeblock took {duration}s" + + log(message.format(name=context, duration=duration)) + + +def log_duration(log=None, with_args=False): + """ + Decorator that logs the execution duration of the annotated function. + + Arguments: + log (callable): The logging function to use. + with_args (bool): Whether to include the calling arguments in the logged + output or not. + """ + def decorator(f): + def wrapped(*args, **kwargs): + if with_args: + args_str = ", ".join(map(lambda x: repr(x), args)) + kwargs_str = ", ".join(map(lambda k, v: "{}={}".format(k, repr(v)), kwargs.items())) + sep = ", " if args_str and kwargs_str else "" + arguments = "".join([args_str, sep, kwargs_str]) + else: + arguments = "..." + context = "{name}({arguments})".format(name=f.__name__, arguments=arguments) + with duration_log(context=context, log=log): + f(*args, **kwargs) + return wrapped + return decorator