diff --git a/apptools/logger/api.py b/apptools/logger/api.py index 4e2093320..4607658fa 100644 --- a/apptools/logger/api.py +++ b/apptools/logger/api.py @@ -1,4 +1,3 @@ from .logger import add_log_queue_handler, create_log_file_handler from .logger import FORMATTER, LEVEL, LogFileHandler from .log_point import log_point -from .filtering_handler import FilteringHandler diff --git a/apptools/logger/filtering_handler.py b/apptools/logger/filtering_handler.py deleted file mode 100644 index cdb8f315c..000000000 --- a/apptools/logger/filtering_handler.py +++ /dev/null @@ -1,193 +0,0 @@ -# ------------------------------------------------------------------------------ -# Copyright (c) 2005, Enthought, Inc. -# All rights reserved. -# -# This software is provided without warranty under the terms of the BSD -# license included in enthought/LICENSE.txt and may be redistributed only -# under the conditions described in the aforementioned license. The license -# is also available online at http://www.enthought.com/licenses/BSD.txt -# Thanks for using Enthought open source! -# -# Author: Enthought, Inc. -# Description: -# ------------------------------------------------------------------------------ -""" A log handler that allows filtering of messages by origin. """ - - -# Standard library imports. -from __future__ import print_function -import logging, inspect, os - -# Local imports. -# -# fixme: This module was just copied over from 'envisage.core' (so -# that we don't rely on Envisage here!). Where should this module go? -from .util import get_module_name - - -class FilteringHandler(logging.Handler): - """A log handler that allows filtering of messages by origin. - - Example - ------- - :: - - from apptools.logger.api import DebugHandler, logger - - handler = FilteringHandler( - include = { - 'envisage.core' : True - }, - - exclude = { - 'envisage.core.application' : False - } - ) - - logger.addHandler(handler) - - Notes - ----- - - The boolean value specified for each name in the include and exclude - dictionaries indicates whether or not the include or exclude should pertain - to any sub-packages and modules. - - The above example includes all log messages from anything contained in, or - under the 'envisage.core' package, EXCEPT for any log messages - from the 'envisage.core.application' module. - - """ - - ########################################################################### - # 'object' interface. - ########################################################################### - - def __init__(self, include=None, exclude=None): - """ Creates a new handler. """ - - # Base class constructor. - logging.Handler.__init__(self) - - # Packages or modules to include. - self.include = include is not None and include or {} - - # Packages or modules to exclude. - self.exclude = exclude is not None and exclude or {} - - return - - ########################################################################### - # 'Handler' interface. - ########################################################################### - - def emit(self, record): - """ Emits a log record. """ - - # Get the name of the module that the logger was called from. - module_name = self._get_module_name() - - if len(self.include) == 0 or self._include(module_name): - if len(self.exclude) == 0 or not self._exclude(module_name): - self.filtered_emit(record) - - return - - ########################################################################### - # 'Handler' interface. - ########################################################################### - - def filtered_emit(self, record): - """ Emits a log record if it has not been filtered. """ - - print(record.getMessage()) - - return - - ########################################################################### - # Private interface. - ########################################################################### - - def _get_module_name(self): - """ Returns the module that the logger was actually called from. """ - - # fixem: Ahem.... what can I say... this gets us to the actual caller! - frame = inspect.currentframe() - frame = frame.f_back.f_back.f_back.f_back.f_back.f_back.f_back - - # This returns a tuple containing the last 5 elements of the frame - # record which are:- - # - # - the filename - # - the line number - # - the function name - # - the list of lines of context from the source code - # - the index of the current line within that list - filename, lineno, funcname, source, index = inspect.getframeinfo(frame) - - # The plugin definition's location is the directory containing the - # module that it is defined in. - self.location = os.path.dirname(filename) - - # We can't use 'inspect.getmodulename' here as it gets confused because - # of our import hook in Envisage 8^( - # - # e.g. for the core plugin definition:- - # - # using inspect -> 'core_plugin_definition' - # using ours -> 'envisage.core.core_plugin_definition' - return get_module_name(filename) - - def _include(self, module_name): - """ Is the module name in the include set? """ - - for item, include_children in self.include.items(): - if item == module_name: - include = True - break - - elif include_children and self._is_child_of(item, module_name): - include = True - break - - else: - include = False - - return include - - def _exclude(self, module_name): - """ Is the module name in the exclude set? """ - - for item, exclude_children in self.exclude.items(): - if item == module_name: - exclude = True - break - - elif exclude_children and self._is_child_of(item, module_name): - exclude = True - break - - else: - exclude = False - - return exclude - - def _is_child_of(self, x, y): - """Is 'y' a child symbol of 'x'? - - e.g. - - 'foo.bar.baz' (y) is a child of 'foo.bar' (x) - - """ - - if y.startswith(x): - x_atoms = x.split(".") - y_atoms = y.split(".") - - is_child_of = y_atoms[: len(x_atoms)] == x_atoms - - else: - is_child_of = False - - return is_child_of diff --git a/apptools/logger/util.py b/apptools/logger/util.py deleted file mode 100644 index 0852ec8f7..000000000 --- a/apptools/logger/util.py +++ /dev/null @@ -1,149 +0,0 @@ -""" Utility functions. - -fixme: I don't like random collections of utility functions! Where should -this go? - -""" - - -# Standard library imports. -import os -from os.path import basename, dirname, isdir, splitdrive, splitext -from zipfile import is_zipfile, ZipFile - - -def get_module_name(filename): - """Get the fully qualified module name for a filename. - - For example, if the filename is - - /enthought/envisage/core/core_plugin_definition.py - - this method would return - - envisage.core.core_plugin_definition - - """ - - if os.path.exists(filename): - # Get the name of the module minus the '.py' - module, ext = os.path.splitext(os.path.basename(filename)) - - # Start with the actual module name. - module_path = [module] - - # If the directory is a Python package then add it to the module path. - # return self.is_folder and '__init__.py' in os.listdir(self.path) - - parent = dirname(filename) - while isdir(parent) and "__init__.py" in os.listdir(parent): - bname = basename(parent) - module_path.insert(0, splitext(bname)[0]) - parent = dirname(parent) - - module_name = ".".join(module_path) - - # If the file does not exist then it might be a zip file path. - else: - module_name = get_module_name_from_zip(filename) - - return module_name - - -# fixme: WIP -def get_module_name_from_zip(filename): - - # first, find the zip file in the path - filepath = filename - zippath = None - while ( - not is_zipfile(filepath) - and splitdrive(filepath)[1] != "\\" - and splitdrive(filepath)[1] != "/" - ): - filepath, tail = os.path.split(filepath) - if zippath is not None: - zippath = tail + "/" + zippath - else: - zippath = tail - - if not is_zipfile(filepath): - return None - - # if the split left a preceding slash on the zippath then remove - # it - if zippath.startswith("\\") or zippath.startswith("/"): - zippath = zippath[1:] - - # replace any backwards slashes with forward slashes - zippath = zippath.replace("\\", "/") - - # Get the name of the module minus the '.py' - module, ext = splitext(basename(zippath)) - - # Start with the actual module name. - module_path = [module] - - # to get the module name, we walk through the zippath until we - # find a parent directory that does NOT have a __init__.py file - z = ZipFile(filepath) - - parentpath = dirname(zippath) - while path_exists_in_zip(z, parentpath + "/__init__.py"): - module_path.insert(0, basename(parentpath)) - parentpath = dirname(parentpath) - - z.close() - - return ".".join(module_path) - - -# fixme: WIP -def path_exists_in_zip(zfile, path): - - try: - zfile.getinfo(path) - exists = True - except: - exists = False - - return exists - - -# fixme: WIP -def is_zip_path(path): - """ Returns True if the path refers to a zip file. """ - - filepath = path - while ( - not is_zipfile(filepath) - and splitdrive(filepath)[1] != "\\" - and splitdrive(filepath)[1] != "/" - ): - filepath = dirname(filepath) - - return is_zipfile(filepath) - - -# fixme: WIP -def get_zip_path(filename): - """Returns the path to the zip file contained in the filename. - - fixme: An example here would help. - - """ - - filepath = filename - zippath = None - while ( - not is_zipfile(filepath) - and splitdrive(filepath)[1] != "\\" - and splitdrive(filepath)[1] != "/" - ): - filepath, tail = os.path.split(filepath) - if zippath is not None: - zippath = tail + "/" + zippath - else: - zippath = tail - - return zippath diff --git a/docs/releases/upcoming/217.removal.rst b/docs/releases/upcoming/217.removal.rst new file mode 100644 index 000000000..273174c7a --- /dev/null +++ b/docs/releases/upcoming/217.removal.rst @@ -0,0 +1 @@ +Remove ``apptools.logger.filtering_handler`` and ``apptools.logger.util`` submodules (#217) \ No newline at end of file