From deb6d8644d9b1d08b45a1ea7a453498bdcee2e09 Mon Sep 17 00:00:00 2001 From: giumas Date: Tue, 6 Oct 2015 09:08:19 -0400 Subject: [PATCH] adoption of Python logging mechanism --- HDFCompass.py | 24 +++++++++++ hdf_compass/array_model/__init__.py | 7 +++- hdf_compass/asc_model/__init__.py | 5 +++ hdf_compass/compass_model/__init__.py | 5 +++ hdf_compass/compass_viewer/__init__.py | 21 ++++++---- hdf_compass/compass_viewer/__main__.py | 38 ++++++++++++++++++ hdf_compass/compass_viewer/array/__init__.py | 4 ++ hdf_compass/compass_viewer/array/plot.py | 13 +++--- .../compass_viewer/container/__init__.py | 6 ++- hdf_compass/compass_viewer/container/list.py | 4 ++ hdf_compass/compass_viewer/events.py | 4 ++ hdf_compass/compass_viewer/frame.py | 7 +++- .../compass_viewer}/icons/go-top_24.png | Bin .../compass_viewer}/icons/go-top_32.png | Bin hdf_compass/compass_viewer/image/__init__.py | 5 ++- hdf_compass/compass_viewer/info.py | 4 ++ .../compass_viewer/keyvalue/__init__.py | 4 ++ hdf_compass/compass_viewer/platform.py | 3 ++ hdf_compass/filesystem_model/__init__.py | 5 +++ hdf_compass/hdf5_model/__init__.py | 5 +++ hdf_compass/opendap_model/__init__.py | 6 +++ 21 files changed, 152 insertions(+), 18 deletions(-) create mode 100644 hdf_compass/compass_viewer/__main__.py rename {compass_viewer => hdf_compass/compass_viewer}/icons/go-top_24.png (100%) rename {compass_viewer => hdf_compass/compass_viewer}/icons/go-top_32.png (100%) diff --git a/HDFCompass.py b/HDFCompass.py index 894a114..2310c23 100644 --- a/HDFCompass.py +++ b/HDFCompass.py @@ -9,6 +9,30 @@ # distribution tree. If you do not have access to this file, you may # # request a copy from help@hdfgroup.org. # ############################################################################## +from __future__ import absolute_import, division, print_function, unicode_literals + +import logging + + +class LoggingFilter(logging.Filter): + """ An example of logging filter that disables the logging from a specific module """ + def filter(self, record): + # print(record.name) + if record.name.startswith('hdf_compass.compass_viewer.info'): + return False + return True + + +# logging settings +logger = logging.getLogger() +logger.setLevel(logging.NOTSET) +ch = logging.StreamHandler() +ch.setLevel(logging.DEBUG) # change to WARNING to minimize verbosity, DEBUG for high verbosity +ch_formatter = logging.Formatter('%(levelname)-7s %(name)s.%(funcName)s:%(lineno)d > %(message)s') +ch.setFormatter(ch_formatter) +# ch.addFilter(LoggingFilter()) # uncomment to activate the logging filter +logger.addHandler(ch) from hdf_compass import compass_viewer + compass_viewer.run() diff --git a/hdf_compass/array_model/__init__.py b/hdf_compass/array_model/__init__.py index 2d48b18..d6bae0c 100644 --- a/hdf_compass/array_model/__init__.py +++ b/hdf_compass/array_model/__init__.py @@ -13,12 +13,17 @@ """ Testing model for array types. """ +from __future__ import absolute_import, division, print_function, unicode_literals import numpy as np +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + from hdf_compass import compass_model -DT_CMP = np.dtype([('a', 'i'), ('b', 'f')]) +DT_CMP = np.dtype([(b'a', b'i'), (b'b', b'f')]) DATA = {'a_0d': np.array(1), 'a_1d': np.arange(10), diff --git a/hdf_compass/asc_model/__init__.py b/hdf_compass/asc_model/__init__.py index eee5992..2a2b719 100644 --- a/hdf_compass/asc_model/__init__.py +++ b/hdf_compass/asc_model/__init__.py @@ -17,6 +17,7 @@ See: http://en.wikipedia.org/wiki/Esri_grid for a description of the file format """ +from __future__ import absolute_import, division, print_function, unicode_literals import sys import os.path as op @@ -24,6 +25,10 @@ import numpy as np +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + from hdf_compass import compass_model diff --git a/hdf_compass/compass_model/__init__.py b/hdf_compass/compass_model/__init__.py index f257747..4e4eeba 100644 --- a/hdf_compass/compass_model/__init__.py +++ b/hdf_compass/compass_model/__init__.py @@ -68,6 +68,11 @@ class MyHDF5Image(Image): Of course, this assumes you know enough about the internals of the other person's Store to make your new class useful. """ +from __future__ import absolute_import, division, print_function, unicode_literals + +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) from . import images diff --git a/hdf_compass/compass_viewer/__init__.py b/hdf_compass/compass_viewer/__init__.py index b8d29ee..3c2145c 100644 --- a/hdf_compass/compass_viewer/__init__.py +++ b/hdf_compass/compass_viewer/__init__.py @@ -15,6 +15,7 @@ Defines the App class, along with supporting infrastructure. """ +from __future__ import absolute_import, division, print_function, unicode_literals # Must be at the top, to ensure we're the first to call matplotlib.use. import matplotlib @@ -22,6 +23,10 @@ import wx +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + from hdf_compass import compass_model from .imagesupport import png_to_bitmap @@ -113,7 +118,7 @@ def open_node(node, pos=None): else: newpos = None - print "Top-level open called for ", node + log.debug("Top-level open called for %s" % node) if isinstance(node, compass_model.Container): f = container.ContainerFrame(node, pos=newpos) @@ -154,33 +159,33 @@ def load_plugins(): try: from hdf_compass import filesystem_model except ImportError: - print("Filesystem plugin not loaded") + log.info("Filesystem plugin not loaded") try: from hdf_compass import array_model except ImportError: - print("Array plugin not loaded") + log.info("Array plugin not loaded") try: from hdf_compass import hdf5_model except ImportError: - print("HDF plugin not loaded") + log.info("HDF plugin not loaded") # Coming soon! # try: # from hdf_compass import bag_model # except ImportError: - # print("BAG plugin not loaded") + # log.info("BAG plugin not loaded") try: from hdf_compass import asc_model except ImportError: - print("Ascii grid plugin not loaded") + log.info("Ascii grid plugin not loaded") try: from hdf_compass import opendap_model except ImportError: - print("Opendap plugin not loaded") + log.info("Opendap plugin not loaded") def run(): @@ -204,7 +209,7 @@ def run(): else: url = 'file://' + op.abspath(url) if not open_store(url): - print 'Failed to open "%s"; no handlers'%url + log.warning('Failed to open "%s"; no handlers' % url) f = frame.InitFrame() diff --git a/hdf_compass/compass_viewer/__main__.py b/hdf_compass/compass_viewer/__main__.py new file mode 100644 index 0000000..92f3fda --- /dev/null +++ b/hdf_compass/compass_viewer/__main__.py @@ -0,0 +1,38 @@ +############################################################################## +# Copyright by The HDF Group. # +# All rights reserved. # +# # +# This file is part of the HDF Compass Viewer. The full HDF Compass # +# copyright notice, including terms governing use, modification, and # +# terms governing use, modification, and redistribution, is contained in # +# the file COPYING, which can be found at the root of the source code # +# distribution tree. If you do not have access to this file, you may # +# request a copy from help@hdfgroup.org. # +############################################################################## +from __future__ import absolute_import, division, print_function, unicode_literals + +import logging + + +class LoggingFilter(logging.Filter): + """ An example of logging filter that disables the logging from a specific module """ + def filter(self, record): + # print(record.name) + if record.name.startswith('hdf_compass.compass_viewer.info'): + return False + return True + + +# logging settings +logger = logging.getLogger() +logger.setLevel(logging.NOTSET) +ch = logging.StreamHandler() +ch.setLevel(logging.DEBUG) # change to WARNING to minimize verbosity, DEBUG for high verbosity +ch_formatter = logging.Formatter('%(levelname)-7s %(name)s.%(funcName)s:%(lineno)d > %(message)s') +ch.setFormatter(ch_formatter) +# ch.addFilter(LoggingFilter()) # uncomment to activate the logging filter +logger.addHandler(ch) + +from . import run + +run() diff --git a/hdf_compass/compass_viewer/array/__init__.py b/hdf_compass/compass_viewer/array/__init__.py index ee09d09..5b0db3b 100644 --- a/hdf_compass/compass_viewer/array/__init__.py +++ b/hdf_compass/compass_viewer/array/__init__.py @@ -12,11 +12,15 @@ """ Implements a viewer frame for compass_model.Array. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx import wx.grid from wx.lib.newevent import NewCommandEvent +import logging +log = logging.getLogger(__name__) + from .. import imagesupport from ..frame import NodeFrame from .plot import LinePlotFrame, ContourPlotFrame diff --git a/hdf_compass/compass_viewer/array/plot.py b/hdf_compass/compass_viewer/array/plot.py index 17c060d..93f6453 100644 --- a/hdf_compass/compass_viewer/array/plot.py +++ b/hdf_compass/compass_viewer/array/plot.py @@ -13,6 +13,7 @@ """ Matplotlib window with toolbar. """ +from __future__ import absolute_import, division, print_function, unicode_literals import numpy as np import wx @@ -22,6 +23,9 @@ FigureCanvasWxAgg as FigCanvas, \ NavigationToolbar2WxAgg as NavigationToolbar +import logging +log = logging.getLogger(__name__) + from ..frame import BaseFrame @@ -33,10 +37,9 @@ class PlotFrame(BaseFrame): """ def __init__(self, data, title="a title"): - """ Create a new Matplotlib plotting window for a 1D line plot - """ + """ Create a new Matplotlib plotting window for a 1D line plot """ - print self.__class__.__name__ + log.debug(self.__class__.__name__) BaseFrame.__init__(self, id=wx.ID_ANY, title=title, size=(800, 400)) self.data = data @@ -84,8 +87,8 @@ def draw_figure(self): maxElements = 500 # don't attempt plot more than 500x500 elements rows = self.data.shape[0] cols = self.data.shape[1] - row_stride = rows / maxElements + 1 - col_stride = cols / maxElements + 1 + row_stride = rows // maxElements + 1 + col_stride = cols // maxElements + 1 data = self.data[::row_stride, ::col_stride] xx = np.arange(0, self.data.shape[1], col_stride) yy = np.arange(0, self.data.shape[0], row_stride) diff --git a/hdf_compass/compass_viewer/container/__init__.py b/hdf_compass/compass_viewer/container/__init__.py index 9fe8196..3e91ae3 100644 --- a/hdf_compass/compass_viewer/container/__init__.py +++ b/hdf_compass/compass_viewer/container/__init__.py @@ -17,9 +17,13 @@ Currently list and icon views are supported. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx +import logging +log = logging.getLogger(__name__) + from hdf_compass import compass_model from .. import imagesupport from ..frame import NodeFrame @@ -196,7 +200,7 @@ def on_open(self, evt): new windows. """ newnode = evt.node - print "Got request to open node", newnode.key + log.debug("Got request to open node: %s" % newnode.key) if isinstance(newnode, compass_model.Container): self.go(newnode) else: diff --git a/hdf_compass/compass_viewer/container/list.py b/hdf_compass/compass_viewer/container/list.py index dc29eee..338e1ae 100644 --- a/hdf_compass/compass_viewer/container/list.py +++ b/hdf_compass/compass_viewer/container/list.py @@ -12,9 +12,13 @@ """ Handles list and icon view for Container display. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx +import logging +log = logging.getLogger(__name__) + from hdf_compass import compass_model from ..events import CompassOpenEvent from ..events import ContainerSelectionEvent diff --git a/hdf_compass/compass_viewer/events.py b/hdf_compass/compass_viewer/events.py index 5980f86..ae92baa 100644 --- a/hdf_compass/compass_viewer/events.py +++ b/hdf_compass/compass_viewer/events.py @@ -13,10 +13,14 @@ """ Defines a small number of custom events, which are useful for the GUI. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx from wx.lib.newevent import NewCommandEvent +import logging +log = logging.getLogger(__name__) + ID_COMPASS_OPEN = wx.NewId() diff --git a/hdf_compass/compass_viewer/frame.py b/hdf_compass/compass_viewer/frame.py index 4f2279e..010d269 100644 --- a/hdf_compass/compass_viewer/frame.py +++ b/hdf_compass/compass_viewer/frame.py @@ -17,7 +17,7 @@ Much of the common functionality (e.g. "Open File..." menu item) is implemented here. """ - +from __future__ import absolute_import, division, print_function, unicode_literals import os import sys @@ -25,6 +25,9 @@ import wx from wx.lib.pubsub import pub +import logging +log = logging.getLogger(__name__) + from . import imagesupport from .info import InfoPanel from . import platform @@ -390,7 +393,7 @@ def on_menu_reopen(self, evt): # The requested Node subclass to instantiate. h = self._menu_handlers[id_] - print 'opening', node_being_opened.store, node_being_opened.key + log.debug('opening: %s %s' % (node_being_opened.store, node_being_opened.key)) # Brand new Node instance of the requested type node_new = h(node_being_opened.store, node_being_opened.key) diff --git a/compass_viewer/icons/go-top_24.png b/hdf_compass/compass_viewer/icons/go-top_24.png similarity index 100% rename from compass_viewer/icons/go-top_24.png rename to hdf_compass/compass_viewer/icons/go-top_24.png diff --git a/compass_viewer/icons/go-top_32.png b/hdf_compass/compass_viewer/icons/go-top_32.png similarity index 100% rename from compass_viewer/icons/go-top_32.png rename to hdf_compass/compass_viewer/icons/go-top_32.png diff --git a/hdf_compass/compass_viewer/image/__init__.py b/hdf_compass/compass_viewer/image/__init__.py index f2c422b..9c5b4b9 100644 --- a/hdf_compass/compass_viewer/image/__init__.py +++ b/hdf_compass/compass_viewer/image/__init__.py @@ -13,10 +13,13 @@ """ Implements a simple true-color image viewer. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx import wx.grid -from wx.lib.newevent import NewCommandEvent # FIXME: Unused? + +import logging +log = logging.getLogger(__name__) from ..frame import NodeFrame diff --git a/hdf_compass/compass_viewer/info.py b/hdf_compass/compass_viewer/info.py index 9632f46..986b2a8 100644 --- a/hdf_compass/compass_viewer/info.py +++ b/hdf_compass/compass_viewer/info.py @@ -13,9 +13,13 @@ """ Defines the left-hand side information panel used to display context info. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx +import logging +log = logging.getLogger(__name__) + from hdf_compass import compass_model from .imagesupport import png_to_bitmap from . import platform diff --git a/hdf_compass/compass_viewer/keyvalue/__init__.py b/hdf_compass/compass_viewer/keyvalue/__init__.py index 6964256..9632d05 100644 --- a/hdf_compass/compass_viewer/keyvalue/__init__.py +++ b/hdf_compass/compass_viewer/keyvalue/__init__.py @@ -16,9 +16,13 @@ Keys are strings, values are any data type HDFCompass can understand. Presently this means all NumPy types, plus Python str/unicode. """ +from __future__ import absolute_import, division, print_function, unicode_literals import wx +import logging +log = logging.getLogger(__name__) + from ..frame import NodeFrame diff --git a/hdf_compass/compass_viewer/platform.py b/hdf_compass/compass_viewer/platform.py index 1411ba8..9fe3206 100644 --- a/hdf_compass/compass_viewer/platform.py +++ b/hdf_compass/compass_viewer/platform.py @@ -12,8 +12,11 @@ """ Module for platform- and version-specific feature detection. """ +from __future__ import absolute_import, division, print_function, unicode_literals import sys +import logging +log = logging.getLogger(__name__) MAC = sys.platform == 'darwin' WINDOWS = sys.platform == 'win32' diff --git a/hdf_compass/filesystem_model/__init__.py b/hdf_compass/filesystem_model/__init__.py index 279815b..faf7be4 100644 --- a/hdf_compass/filesystem_model/__init__.py +++ b/hdf_compass/filesystem_model/__init__.py @@ -16,12 +16,17 @@ Subclasses just two node types: Container and Array, representing directories and files respectively. """ +from __future__ import absolute_import, division, print_function, unicode_literals import os import os.path as op import numpy as np +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + from hdf_compass import compass_model diff --git a/hdf_compass/hdf5_model/__init__.py b/hdf_compass/hdf5_model/__init__.py index 6963e49..cdb299f 100644 --- a/hdf_compass/hdf5_model/__init__.py +++ b/hdf_compass/hdf5_model/__init__.py @@ -13,6 +13,7 @@ """ Implementation of compass_model classes for HDF5 files. """ +from __future__ import absolute_import, division, print_function, unicode_literals from itertools import groupby import sys @@ -21,6 +22,10 @@ import h5py +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + # Py2App can't successfully import otherwise from hdf_compass import compass_model diff --git a/hdf_compass/opendap_model/__init__.py b/hdf_compass/opendap_model/__init__.py index f32a814..ef83af4 100644 --- a/hdf_compass/opendap_model/__init__.py +++ b/hdf_compass/opendap_model/__init__.py @@ -9,6 +9,8 @@ # distribution tree. If you do not have access to this file, you may # # request a copy from help@hdfgroup.org. # ############################################################################## +from __future__ import absolute_import, division, print_function, unicode_literals + import posixpath as pp import numpy as np @@ -16,6 +18,10 @@ from pydap.client import open_url from pydap.proxy import ArrayProxy +import logging +log = logging.getLogger(__name__) +log.addHandler(logging.NullHandler()) + from hdf_compass import compass_model