1. John Paulett
  2. HORNET

Commits

John Paulett  committed 35bd846

Remove dependency of decorator module by switching to the stdlib's functools.wraps

  • Participants
  • Parent commits f94b5e9
  • Branches default

Comments (0)

Files changed (3)

File pavement.py

View file
         script_name = BOOTSTRAP_SCRIPT,
         packages_to_install = ['sphinx==0.6.1', 'mock==0.5.0', 'nose=0.10.4', 
                                'NoseXUnit==0.3.2', 'coverage==2.85', 'networkx', 
-                               'decorator', 'sqlalchemy==0.5.3', 'numpy==1.3.0'], 
+                               'sqlalchemy==0.5.3', 'numpy==1.3.0'], 
                                #FIXME get matplotlib installed 'matplotlib==0.91.1'
     )
 )
 def xmltest():
     """Run the test suite and output JUnit format results."""
     import run_tests
-    run_tests.main(argv=['--with-nosexunit', '--enable-cover', '--source-folder='+SRC_DIR])
+    run_tests.main(argv=['--with-nosexunit', '--enable-cover', '--source-folder=%s' % SRC_DIR])
 
 @task
 @needs('html')

File src/hornet/plugin.py

View file
 
 .. moduleauthor:: John Paulett <john.paulett -at- vanderbilt.edu>
 """
-from decorator import decorator
 from hornet.event import is_accepted_event, EventNotAccepted
 from hornet.file import join
-from hornet.util import simple_decorator
+from functools import wraps
 import logging
 
 logger = logging.getLogger(__name__)
     if not isinstance(plugin, Plugin):
         raise InvalidPlugin, 'Not a subclass of Plugin'
 
-@decorator
 def log(func, *args, **kw):
     """Decorator that will print debug statements at the start and end of
     execution of *func*. Expects *func* to be a method of an instance of
     :func:`hornet.plugin.notify_on_finish` to ensure that the finished log
     statement is printed before all the listeners are notified.
     """
-    self = args[0]
-    info = (self.id, func.__name__)
-    logger.debug('Plugin, "%s", started "%s"' % info)
-    retval = func(*args, **kw)
-    logger.debug('Plugin, "%s", finished "%s"' % info)
-    return retval
+    @wraps(func)
+    def new(*args, **kw):
+        self = args[0]
+        info = (self.id, func.__name__)
+        logger.debug('Plugin, "%s", started "%s"' % info)
+        retval = func(*args, **kw)
+        logger.debug('Plugin, "%s", finished "%s"' % info)
+        return retval
+    return new
 
-@decorator
 def notify_on_finish(func, *args, **kw):
     """Decorator that will notify all the listeners of the the plugin once
     *func* is complete. The notification event, a subclass of 
     Expects *func* to be a method of an instance of
     :class:`hornet.plugin.Plugin`.
     """
-    self = args[0]
-    event = func(*args, **kw)
-    self.notify_listeners(event)
-    return event
+    @wraps(func)
+    def new(*args, **kw):
+        self = args[0]
+        event = func(*args, **kw)
+        self.notify_listeners(event)
+        return event
+    return new
 
-@simple_decorator
 def wrap_notify(func, plugin):
     """Decorator that ensures the event passed to the notify function
     is on of the allowed Events, according to accepted_events.
     """
+    @wraps(func)
     def new(event):
         if is_accepted_event(event, plugin.accepted_events()):
             return func(event)
                 '%s not accepted by plugin, %s' % (event, plugin.id)
     return new
 
-@simple_decorator
 def wrap_setup(func, plugin):
     """Decorator that ensures that the :meth:`hornet.plugin.Plugin.setup`
     method performs certain actions even if overridden. The setup 
     actions include registering plugins.
     """
+    @wraps(func)
     def new():
         for announcers in plugin.listen_to:
             announcers.register(plugin)
         func()
     return new
 
-@simple_decorator
 def wrap_teardown(func, plugin):
     """Decorator that ensures that the :meth:`hornet.plugin.Plugin.teardown`
     method performs certain actions even if overridden. The teardown 
     actions include unregistering plugins and ensures that any errors
     do not get raised.
     """
+    @wraps(func)
     def new():
         for announcer in plugin.listen_to:
             try:
         logger.debug('Plugin, "%s", destroyed' % plugin.id)
     return new
 
-
 class InvalidPlugin(Exception): 
     """The plugin is not a proper implementation of 
     :class:`hornet.plugin.Plugin`
         in select contexts.
         """
         return self._parent
-
-
-        
-           
-        

File src/hornet/util.py

View file
 .. moduleauthor:: John Paulett <john.paulett -at- vanderbilt.edu>
 """
 from collections import defaultdict
-from decorator import decorator
+from functools import wraps
 import operator
 
 class Period(tuple):
         
     return periods
 
-def _memoize(func, *args, **kw):
-    cache = func.cache
-    if kw: # frozenset is used to ensure hashability
-        key = args, frozenset(kw.iteritems())
-    else:
-        key = args
-    try:
-        if key in cache:
-            return cache[key]
-        else:
-            cache[args] = value = func(*args, **kw)
-            return value
-    except TypeError:
-        # always compute on parameters that are unhashable like lists
-        return func(*args, **kw)
-    
-def memoize(f):    
+def memoize(func):    
     """Decorator that caches a function's return value each time it is called.
     If called later with the same arguments, the cached value is returned, and
     not re-evaluated.  From the `Python Decorator Library 
     >>> f(4) # notice how 4 * 1 is returned and not 4 * 100
     4
     """
-    f.cache = {}
-    return decorator(_memoize, f)
+    func.cache = {}
+    @wraps(func)
+    def new(*args, **kw):
+        if kw: # frozenset is used to ensure hashability
+            key = args, frozenset(kw.iteritems())
+        else:
+            key = args
+        try:
+            if key in func.cache:
+                return func.cache[key]
+            else:
+                func.cache[args] = value = func(*args, **kw)
+                return value
+        except TypeError:
+            # always compute on parameters that are unhashable like lists
+            return func(*args, **kw)
+    return new
 
 def count_unique(list):
     """Finds the count of each value in *list*. Returns a dictionary with the