def logExceptions(msg="Exception occured", logger=None, basetype=Exception): """Creates a try/catch block that logs exceptions""" try: yield except basetype: if not logger: logger = logging.getLogger(classtools.get_calling_module()) logger.exception(msg)
def run_cli(cls=None): """Handle command line interface invocation of this script""" if cls is None: module = classtools.get_calling_module() cls = classtools.get_class_from_module(module, Script) parser = argument_parser_from_script(cls) args = parser.parse_args() options = args.__dict__ verbose = options.pop("verbose", None) logging.basicConfig(format='[%(asctime)s] [%(name)s] %(message)s', level=(logging.DEBUG if verbose else logging.INFO)) logging.getLogger('requests').setLevel(logging.WARN) instance = cls(options) return instance.run()
def test_get_calling_module(self): self.assertEqual(classtools.get_calling_module(depth=0), self.__module__)
def _addModulesToSet(targetset, *modules): """Add the given modules to the targetset""" if not modules: modules = [classtools.get_calling_module(depth=2)] targetset.update(modules)
def get_script(depth=2): module = classtools.get_calling_module(depth=depth) return classtools.get_class_from_module(module, Script)