diff options
author | Max Murashov <m.murashov@rambler-co.ru> | 2017-11-22 12:09:40 +0300 |
---|---|---|
committer | Ashley Camba <ashwoods@gmail.com> | 2017-11-22 10:28:46 +0100 |
commit | 1dd30934c034df6744bfbb14f9dcbf0e552241d4 (patch) | |
tree | 7cda10a48d359fd62815c63ee5d24a1af0130f31 | |
parent | 8701db08bb9b6db7efcdf6b770c8c1aa95cd0ea4 (diff) | |
download | raven-1dd30934c034df6744bfbb14f9dcbf0e552241d4.tar.gz |
Refactored base code
-rw-r--r-- | raven/__init__.py | 11 | ||||
-rw-r--r-- | raven/base.py | 45 | ||||
-rw-r--r-- | raven/breadcrumbs.py | 9 | ||||
-rw-r--r-- | raven/transport/threaded.py | 11 | ||||
-rw-r--r-- | raven/utils/__init__.py | 5 | ||||
-rw-r--r-- | raven/utils/json.py | 21 | ||||
-rw-r--r-- | raven/utils/stacks.py | 20 | ||||
-rw-r--r-- | raven/versioning.py | 24 |
8 files changed, 56 insertions, 90 deletions
diff --git a/raven/__init__.py b/raven/__init__.py index 1e8b754..cee7da5 100644 --- a/raven/__init__.py +++ b/raven/__init__.py @@ -17,13 +17,9 @@ VERSION = '6.3.0' def _get_git_revision(path): revision_file = os.path.join(path, 'refs', 'heads', 'master') - if not os.path.exists(revision_file): - return None - fh = open(revision_file, 'r') - try: - return fh.read().strip()[:7] - finally: - fh.close() + if os.path.exists(revision_file): + with open(revision_file) as fh: + return fh.read().strip()[:7] def get_revision(): @@ -36,7 +32,6 @@ def get_revision(): path = os.path.join(checkout_dir, '.git') if os.path.exists(path): return _get_git_revision(path) - return None def get_version(): diff --git a/raven/base.py b/raven/base.py index 652e5e4..4fa20f6 100644 --- a/raven/base.py +++ b/raven/base.py @@ -67,10 +67,7 @@ if sys.version_info >= (3, 2): def get_excepthook_client(): - hook = sys.excepthook - client = getattr(hook, 'raven_client', None) - if client is not None: - return client + return getattr(sys.excepthook, 'raven_client', None) class ModuleProxyCache(dict): @@ -235,14 +232,13 @@ class Client(object): self.hook_libraries(hook_libraries) def _format_repos(self, value): - if not value: - return {} result = {} - for path, config in iteritems(value): - if path[0] != '/': - # assume its a module - path = os.path.abspath(__import__(path).__file__) - result[path] = config + if value: + for path, config in iteritems(value): + if path[0] != '/': + # assume its a module + path = os.path.abspath(__import__(path).__file__) + result[path] = config return result def set_dsn(self, dsn=None, transport=None): @@ -333,18 +329,17 @@ class Client(object): >>> # Specify a scheme to use (http or https) >>> print client.get_public_dsn('https') """ - if not self.is_enabled(): - return - url = self.remote.get_public_dsn() - if not scheme: + if self.is_enabled(): + url = self.remote.get_public_dsn() + if scheme: + return '%s:%s' % (scheme, url) + return url - return '%s:%s' % (scheme, url) def _get_exception_key(self, exc_info): # On certain celery versions the tb_frame attribute might # not exist or be `None`. - code_id = 0 - last_id = 0 + code_id = last_id = 0 try: code_id = id(exc_info[2] and exc_info[2].tb_frame.f_code) last_id = exc_info[2] and exc_info[2].tb_lasti or 0 @@ -825,15 +820,11 @@ class Client(object): wildcard_exclusions = (e for e in string_exclusions if e.endswith('*')) class_exclusions = (e for e in exclusions if isclass(e)) - if exc_type in exclusions: - return False - elif exc_type.__name__ in exclusions: - return False - elif exc_name in exclusions: - return False - elif any(issubclass(exc_type, e) for e in class_exclusions): - return False - elif any(exc_name.startswith(e[:-1]) for e in wildcard_exclusions): + if (exc_type in exclusions + or exc_type.__name__ in exclusions + or exc_name in exclusions + or any(issubclass(exc_type, e) for e in class_exclusions) + or any(exc_name.startswith(e[:-1]) for e in wildcard_exclusions)): return False return True diff --git a/raven/breadcrumbs.py b/raven/breadcrumbs.py index ca99a32..3d626d1 100644 --- a/raven/breadcrumbs.py +++ b/raven/breadcrumbs.py @@ -100,15 +100,12 @@ def record(message=None, timestamp=None, level=None, category=None, def _record_log_breadcrumb(logger, level, msg, *args, **kwargs): for handler in special_logging_handlers: - rv = handler(logger, level, msg, args, kwargs) - if rv: + if handler(logger, level, msg, args, kwargs): return handler = special_logger_handlers.get(logger.name) - if handler is not None: - rv = handler(logger, level, msg, args, kwargs) - if rv: - return + if handler is not None and handler(logger, level, msg, args, kwargs): + return def processor(data): formatted_msg = msg diff --git a/raven/transport/threaded.py b/raven/transport/threaded.py index c2f1088..16b275d 100644 --- a/raven/transport/threaded.py +++ b/raven/transport/threaded.py @@ -48,8 +48,7 @@ class AsyncWorker(object): self.start() def main_thread_terminated(self): - self._lock.acquire() - try: + with self._lock: if not self.is_alive(): # thread not started or already stopped - nothing to do return @@ -81,9 +80,6 @@ class AsyncWorker(object): self._thread = None - finally: - self._lock.release() - def _timed_queue_join(self, timeout): """ implementation of Queue.join which takes a 'timeout' argument @@ -127,15 +123,12 @@ class AsyncWorker(object): """ Stops the task thread. Synchronous! """ - self._lock.acquire() - try: + with self._lock: if self._thread: self._queue.put_nowait(self._terminator) self._thread.join(timeout=timeout) self._thread = None self._thread_for_pid = None - finally: - self._lock.release() def queue(self, callback, *args, **kwargs): self._ensure_thread() diff --git a/raven/utils/__init__.py b/raven/utils/__init__.py index 173b856..c56d376 100644 --- a/raven/utils/__init__.py +++ b/raven/utils/__init__.py @@ -127,9 +127,8 @@ def get_versions(module_list=None): _VERSION_CACHE[module_name] = version else: version = _VERSION_CACHE[module_name] - if version is None: - continue - versions[module_name] = version + if version is not None: + versions[module_name] = version return versions diff --git a/raven/utils/json.py b/raven/utils/json.py index 71fefe1..afbd386 100644 --- a/raven/utils/json.py +++ b/raven/utils/json.py @@ -97,17 +97,16 @@ class StreamReader(Codec, codecs.StreamReader): def getregentry(name): - if name != 'safe-utf-8': - return None - return codecs.CodecInfo( - name='safe-utf-8', - encode=safe_encode, - decode=safe_decode, - incrementalencoder=IncrementalEncoder, - incrementaldecoder=IncrementalDecoder, - streamreader=StreamReader, - streamwriter=StreamWriter, - ) + if name == 'safe-utf-8': + return codecs.CodecInfo( + name=name, + encode=safe_encode, + decode=safe_decode, + incrementalencoder=IncrementalEncoder, + incrementaldecoder=IncrementalDecoder, + streamreader=StreamReader, + streamwriter=StreamWriter, + ) codecs.register(getregentry) diff --git a/raven/utils/stacks.py b/raven/utils/stacks.py index 061aebd..c87a64c 100644 --- a/raven/utils/stacks.py +++ b/raven/utils/stacks.py @@ -136,9 +136,8 @@ def iter_stack_frames(frames=None): for frame, lineno in ((f[0], f[2]) for f in frames): f_locals = getattr(frame, 'f_locals', {}) - if _getitem_from_frame(f_locals, '__traceback_hide__'): - continue - yield frame, lineno + if not _getitem_from_frame(f_locals, '__traceback_hide__'): + yield frame, lineno def get_frame_locals(frame, transformer=transform, max_var_size=4096): @@ -204,16 +203,15 @@ def slim_frame_data(frames, frame_allowance=25): frame.pop('post_context', None) remaining -= 1 - if not remaining: - return frames + if remaining: + app_allowance = app_count - remaining + half_max = int(app_allowance / 2) - app_allowance = app_count - remaining - half_max = int(app_allowance / 2) + for frame in app_frames[half_max:-half_max]: + frame.pop('vars', None) + frame.pop('pre_context', None) + frame.pop('post_context', None) - for frame in app_frames[half_max:-half_max]: - frame.pop('vars', None) - frame.pop('pre_context', None) - frame.pop('post_context', None) return frames diff --git a/raven/versioning.py b/raven/versioning.py index 6684851..42f179f 100644 --- a/raven/versioning.py +++ b/raven/versioning.py @@ -46,28 +46,22 @@ def fetch_git_sha(path, head=None): # https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery packed_file = os.path.join(path, '.git', 'packed-refs') if os.path.exists(packed_file): - with open(packed_file, 'r') as fh: + with open(packed_file) as fh: for line in fh: line = line.rstrip() - if not line: - continue - if line[:1] in ('#', '^'): - continue - try: - revision, ref = line.split(' ', 1) - except ValueError: - continue - if ref == head: - return text_type(revision) + if line and line[:1] not in ('#', '^'): + try: + revision, ref = line.split(' ', 1) + except ValueError: + continue + if ref == head: + return text_type(revision) raise InvalidGitRepository( 'Unable to find ref to head "%s" in repository' % (head,)) - fh = open(revision_file, 'r') - try: + with open(revision_file) as fh: return text_type(fh.read()).strip() - finally: - fh.close() def fetch_package_version(dist_name): |