def lock(self, key, owner, timeout=None, expire=None): if not self.locker: db = self.mongo_database() self.locker = MongoLock(client=db.client, db=db.name) try: with self.locker(key, owner, timeout=timeout, expire=expire): yield except MongoLockLocked: #lock_doc = self.locker.get_lock_info(key) raise errors.Locked(key=key, owner=owner) finally: self.locker.release(key, owner)
class Context(object): def __init__(self, mongo_url=None, verbose=False, log_level='ERROR', log_config=None, log_file=None, trace=False, cache_enable=False, requests_cache_enable=None, requests_cache_path=None, requests_cache_expire=None, debug=False, silent=False, pretty=False, quiet=False): self.mongo_url = mongo_url self.verbose = verbose self.debug = debug self.silent = silent self.pretty = pretty self.quiet = quiet self.trace = trace self.cache_enable = cache_enable self.requests_cache_enable = requests_cache_enable self.requests_cache_path = requests_cache_path self.requests_cache_expire = requests_cache_expire self.log_level = log_level self.log_config = log_config self.log_file = log_file if self.verbose and log_level != "DEBUG": self.log_level = 'INFO' self.locker = None self.client_mongo = None self.db_mongo = None self.client_es = None self.fs_mongo = None self.logger = configure_logging(debug=self.debug, config_file=self.log_config, level=self.log_level) if self.log_file: self._set_log_file() if self.cache_enable: self._set_cache() if self.requests_cache_enable: self._set_requests_cache() if self.trace: from widukind_common import debug debug.TRACE_ENABLE = True atexit.register(debug.flush_logs) def _set_log_file(self): from logging import FileHandler handler = FileHandler(filename=self.log_file) if self.logger.hasHandlers(): handler.setFormatter(self.logger.handlers[0].formatter) self.logger.addHandler(handler) def _set_cache(self): from dlstats import cache cache.configure_cache(cache_url=CACHE_URL) def _set_requests_cache(self): cache_settings = { "backend": 'sqlite', "cache_name": self.requests_cache_path, "expire_after": self.requests_cache_expire, "include_get_headers": True } if self.requests_cache_expire <= 0: cache_settings["expire_after"] = None try: import requests_cache requests_cache.install_cache(**cache_settings) self.log("Use requests cache in %s" % self.requests_cache_path) except ImportError: self.log_warn("requests-cache library is required for enable cache.") def log(self, msg, *args): """Logs a message to stderr.""" if args: msg %= args if self.quiet: self.logger.info(msg) return click.echo(msg, file=sys.stderr) def log_error(self, msg, *args): """Logs a message to stderr.""" if args: msg %= args if self.quiet: self.logger.error(msg) return click.echo(click.style(msg, fg='red'), file=sys.stderr) def log_ok(self, msg, *args): """Logs a message to stdout.""" if args: msg %= args if self.quiet: self.logger.info(msg) return click.echo(click.style(msg, fg='green'), file=sys.stdout) def log_warn(self, msg, *args): """Logs a message to stdout.""" if args: msg %= args if self.quiet: self.logger.warn(msg) return click.echo(click.style(msg, fg='yellow'), file=sys.stdout) def pretty_print(self, obj): pprint.pprint(obj) def mongo_client(self): if not self.client_mongo: self.client_mongo = get_mongo_client(self.mongo_url.strip('"')) return self.client_mongo @contextlib.contextmanager def lock(self, key, owner, timeout=None, expire=None): if not self.locker: db = self.mongo_database() self.locker = MongoLock(client=db.client, db=db.name) try: with self.locker(key, owner, timeout=timeout, expire=expire): yield except MongoLockLocked: #lock_doc = self.locker.get_lock_info(key) raise errors.Locked(key=key, owner=owner) finally: self.locker.release(key, owner) def mongo_database(self): if not self.db_mongo: self.db_mongo = self.mongo_client().get_default_database() return self.db_mongo def mongo_fs(self): db = self.mongo_database() if not self.fs_mongo: self.fs_mongo = gridfs.GridFS(db) return self.fs_mongo