[Assorted-commits] SF.net SVN: assorted:[1598] python-commons/tags
Brought to you by:
yangzhang
From: <yan...@us...> - 2010-04-01 18:21:30
|
Revision: 1598 http://assorted.svn.sourceforge.net/assorted/?rev=1598&view=rev Author: yangzhang Date: 2010-04-01 18:21:19 +0000 (Thu, 01 Apr 2010) Log Message: ----------- forgot to tag the 0.6 release Added Paths: ----------- python-commons/tags/0.6/ python-commons/tags/0.6/README python-commons/tags/0.6/publish.bash python-commons/tags/0.6/setup.py python-commons/tags/0.6/src/commons/__init__.py python-commons/tags/0.6/src/commons/async.py python-commons/tags/0.6/src/commons/control.py python-commons/tags/0.6/src/commons/decs.py python-commons/tags/0.6/src/commons/files.py python-commons/tags/0.6/src/commons/log.py python-commons/tags/0.6/src/commons/misc.py python-commons/tags/0.6/src/commons/path.py python-commons/tags/0.6/src/commons/progress.py python-commons/tags/0.6/src/commons/seqs.py python-commons/tags/0.6/src/commons/servers.py python-commons/tags/0.6/src/commons/sqlhash.py python-commons/tags/0.6/src/commons/startup.py python-commons/tags/0.6/src/commons/structs.py Removed Paths: ------------- python-commons/tags/0.6/README python-commons/tags/0.6/publish.bash python-commons/tags/0.6/setup.py python-commons/tags/0.6/src/commons/__init__.py python-commons/tags/0.6/src/commons/async.py python-commons/tags/0.6/src/commons/control.py python-commons/tags/0.6/src/commons/decs.py python-commons/tags/0.6/src/commons/files.py python-commons/tags/0.6/src/commons/log.py python-commons/tags/0.6/src/commons/misc.py python-commons/tags/0.6/src/commons/progress.py python-commons/tags/0.6/src/commons/seqs.py python-commons/tags/0.6/src/commons/servers.py python-commons/tags/0.6/src/commons/startup.py python-commons/tags/0.6/src/commons/structs.py Property changes on: python-commons/tags/0.6 ___________________________________________________________________ Added: svn:mergeinfo + Deleted: python-commons/tags/0.6/README =================================================================== --- python-commons/trunk/README 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/README 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,77 +0,0 @@ -[documentation](doc) - -Overview --------- - -Python Commons is a general-purpose library for Python. To get a sense of -what it provides, please glance over the [documentation](doc). - -Requirements ------------- - -- [Python](http://python.org/) 2.5 -- [setuptools](http://peak.telecommunity.com/DevCenter/setuptools) 0.6 - -Certain sub-modules have extra requirements: - -- `async` requires [Twisted](http://twistedmatrix.com/trac/) 2.5 -- `files` requires [path](http://www.jorendorff.com/articles/python/path/) 2.2 - -This library has only been tested on Linux. - -Setup ------ - -To install, run `easy_install python-commons`, or download the source tarball -and run `python setup.py install`. - -Related Work ------------- - -- [ASPN Cookbook]: a valuable repository of Python snippets -- [AIMA Utilities]: accompaniment to a popular AI textbook - -[ASPN Cookbook]: http://aspn.activestate.com/ASPN/Cookbook/Python -[AIMA Utilities]: http://aima.cs.berkeley.edu/python/utils.py - -Changes -------- - -version 0.6, 2008-10-?? - -- ??? (review!) -- released for [MIT 6.00 courseware](http://assorted.sf.net/mit600/) - -version 0.5, 2008-05-14 - -- added `cp1252_to_unicode()` -- made `setup()` more flexible -- released for - [gbookmark2delicious](http://gbookmark2delicious.googlecode.com/) - -version 0.4, 2008-05-08 - -- removed extraneous debug print statements -- added `logout()` context manager -- added `seq()`, `default_if_none()` -- fixed missing `import` bug -- released for [Mailing List - Filter](http://assorted.sf.net/mailing-list-filter/) - -version 0.3, 2008-04-30 - -- added versioned guards -- added file memoization -- added retry with exp backoff -- added `countstep()` -- released for - [gbookmark2delicious](http://gbookmark2delicious.googlecode.com/) - -version 0.2, 2008-02-04 - -- added `clients`, `setup` -- released for [icedb](http://cartel.csail.mit.edu/icedb/) - -version 0.1, 2007-03-24 - -- initial release Copied: python-commons/tags/0.6/README (from rev 1451, python-commons/trunk/README) =================================================================== --- python-commons/tags/0.6/README (rev 0) +++ python-commons/tags/0.6/README 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,93 @@ +[documentation](doc) + +Overview +-------- + +Python Commons is a general-purpose library for Python. To get a sense of +what it provides, please glance over the [documentation](doc). + +Requirements +------------ + +- [Python](http://python.org/) 2.5 +- [setuptools](http://peak.telecommunity.com/DevCenter/setuptools) 0.6 + +Certain sub-modules have extra requirements: + +- `async` requires [Twisted](http://twistedmatrix.com/trac/) 2.5 +- `files` requires [path](http://www.jorendorff.com/articles/python/path/) 2.2 + +This library has only been tested on Linux. + +Setup +----- + +To install, run `easy_install python-commons`, or download the source tarball +and run `python setup.py install`. + +Related Work +------------ + +- [ASPN Cookbook]: a valuable repository of Python snippets +- [AIMA Utilities]: accompaniment to a popular AI textbook + +[ASPN Cookbook]: http://aspn.activestate.com/ASPN/Cookbook/Python +[AIMA Utilities]: http://aima.cs.berkeley.edu/python/utils.py + +Changes +------- + +version 0.6, 2008-10-?? + +- to strs added unwrap, indent, unindent, remove_empty_lines, underline, + dos2unix, quotejs, unicode2html, html2unicode, nat_lang_join, or_join, + and_join +- to misc added sendmail, days, settimeout, run, TerminalController +- imported and updated jorendorff's path.py +- to startup added command_name +- to files added read_file, write_file, write_or_rm, is_nonempty_file +- added sqlhash and sqlhash backend for shelf +- to structs added dicts2structs, structs2dicts; FreeStruct renamed to + free_struct +- to seqs added span, group_as_subseqs +- decs.pickle_memoized uses protocol 2 +- general tweaks; epydoc fixes +- python 2.6-ready +- released for [Mailing List Filter], [MIT 6.00] courseware + +[Mailing List Filter]: http://assorted.sourceforge.net/mailing-list-filter/ +[MIT 6.00]: http://web.mit.edu/~6.00/ + +version 0.5, 2008-05-14 + +- added `cp1252_to_unicode()` +- made `setup()` more flexible +- released for + [gbookmark2delicious](http://gbookmark2delicious.googlecode.com/) + +version 0.4, 2008-05-08 + +- removed extraneous debug print statements +- added `logout()` context manager +- added `seq()`, `default_if_none()` +- fixed missing `import` bug +- released for [Mailing List + Filter](http://assorted.sf.net/mailing-list-filter/) + +version 0.3, 2008-04-30 + +- added versioned guards +- added file memoization +- added retry with exp backoff +- added `countstep()` +- released for + [gbookmark2delicious](http://gbookmark2delicious.googlecode.com/) + +version 0.2, 2008-02-04 + +- added `clients`, `setup` +- released for [icedb](http://cartel.csail.mit.edu/icedb/) + +version 0.1, 2007-03-24 + +- initial release Deleted: python-commons/tags/0.6/publish.bash =================================================================== --- python-commons/trunk/publish.bash 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/publish.bash 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -post-stage() { - epydoc -o $stagedir/doc src/commons/ -} - -echo 'Remember to keep versions in sync in all three locations:' -echo '__init__.py, README (Changes), setup.bash, and setup.py' - -fullname='Python Commons' -version=0.5 -license=psf -websrcs=( README ) -rels=( pypi: ) -. assorted.bash "$@" Copied: python-commons/tags/0.6/publish.bash (from rev 1451, python-commons/trunk/publish.bash) =================================================================== --- python-commons/tags/0.6/publish.bash (rev 0) +++ python-commons/tags/0.6/publish.bash 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +post-stage() { + epydoc -v -o $stagedir/doc src/commons/ +} + +echo 'Remember to keep versions in sync in all three locations:' +echo '__init__.py, README (Changes), setup.bash, and setup.py' + +fullname='Python Commons' +version=0.6 +license=psf +websrcs=( README ) +rels=( pypi: ) +. assorted.bash "$@" Deleted: python-commons/tags/0.6/setup.py =================================================================== --- python-commons/trunk/setup.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/setup.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,43 +0,0 @@ -#!/usr/bin/env python -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -import os,sys -sys.path.insert( 0, os.path.join( os.path.dirname( sys.argv[0] ), 'src' ) ) -from commons import setup - -pkg_info_text = """ -Metadata-Version: 1.1 -Name: python-commons -Version: 0.5 -Author: Yang Zhang -Author-email: yaaang NOSPAM at REMOVECAPS gmail -Home-page: http://assorted.sourceforge.net/python-commons -Summary: Python Commons -License: Python Software Foundation License -Description: General-purpose library of utilities and extensions to the - standard library. -Keywords: Python,common,commons,utility,utilities,library,libraries -Platform: any -Provides: commons -Classifier: Development Status :: 4 - Beta -Classifier: Environment :: No Input/Output (Daemon) -Classifier: Intended Audience :: Developers -Classifier: License :: OSI Approved :: Python Software Foundation License -Classifier: Operating System :: OS Independent -Classifier: Programming Language :: Python -Classifier: Topic :: Communications -Classifier: Topic :: Database -Classifier: Topic :: Internet -Classifier: Topic :: Software Development :: Libraries :: Python Modules -Classifier: Topic :: System -Classifier: Topic :: System :: Filesystems -Classifier: Topic :: System :: Logging -Classifier: Topic :: System :: Networking -Classifier: Topic :: Text Processing -Classifier: Topic :: Utilities -""" - -setup.run_setup( pkg_info_text, - #scripts = ['frontend/py_hotshot.py'], - ) Copied: python-commons/tags/0.6/setup.py (from rev 1451, python-commons/trunk/setup.py) =================================================================== --- python-commons/tags/0.6/setup.py (rev 0) +++ python-commons/tags/0.6/setup.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +import os,sys +sys.path.insert( 0, os.path.join( os.path.dirname( sys.argv[0] ), 'src' ) ) +from commons import setup + +pkg_info_text = """ +Metadata-Version: 1.1 +Name: python-commons +Version: 0.6 +Author: Yang Zhang +Author-email: yaaang NOSPAM at REMOVECAPS gmail +Home-page: http://assorted.sourceforge.net/python-commons +Summary: Python Commons +License: Python Software Foundation License +Description: General-purpose library of utilities and extensions to the + standard library. +Keywords: Python,common,commons,utility,utilities,library,libraries +Platform: any +Provides: commons +Classifier: Development Status :: 4 - Beta +Classifier: Environment :: No Input/Output (Daemon) +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: Python Software Foundation License +Classifier: Operating System :: OS Independent +Classifier: Programming Language :: Python +Classifier: Topic :: Communications +Classifier: Topic :: Database +Classifier: Topic :: Internet +Classifier: Topic :: Software Development :: Libraries :: Python Modules +Classifier: Topic :: System +Classifier: Topic :: System :: Filesystems +Classifier: Topic :: System :: Logging +Classifier: Topic :: System :: Networking +Classifier: Topic :: Text Processing +Classifier: Topic :: Utilities +""" + +setup.run_setup( pkg_info_text, + #scripts = ['frontend/py_hotshot.py'], + ) Deleted: python-commons/tags/0.6/src/commons/__init__.py =================================================================== --- python-commons/trunk/src/commons/__init__.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/__init__.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,40 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -U{Python Commons<http://assorted.sf.net/python-commons>} is a -general-purpose library. - -@author: Yang Zhang -@copyright: Yang Zhang unless otherwise noted -@license: PSF -""" - -__version__ = ( 0, 5, 0 ) -__all__ = [ 'async', - 'control', - 'decs', - 'environ', - 'exceps', - 'files', - 'interp', - 'log', - 'misc', - 'networking', - 'progress', - 'seqs', - 'servers', - 'startup', - 'strs', - 'structs', - 'threads', - 'trace' ] - -# TODO more resources: -# http://aima.cs.berkeley.edu/python/utils.py -# http://aspn.activestate.com/ASPN/Cookbook/Python -# http://aspn.activestate.com/ASPN/Cookbook/Python?&recipe_status=editors -# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486 -# interesting: -# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259174 -# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473790 Copied: python-commons/tags/0.6/src/commons/__init__.py (from rev 1451, python-commons/trunk/src/commons/__init__.py) =================================================================== --- python-commons/tags/0.6/src/commons/__init__.py (rev 0) +++ python-commons/tags/0.6/src/commons/__init__.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,40 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +""" +U{Python Commons<http://assorted.sf.net/python-commons>} is a +general-purpose library. + +@author: Yang Zhang +@copyright: Yang Zhang unless otherwise noted +@license: PSF +""" + +__version__ = ( 0, 6, 0 ) +__all__ = [ 'async', + 'control', + 'decs', + 'environ', + 'exceps', + 'files', + 'interp', + 'log', + 'misc', + 'networking', + 'progress', + 'seqs', + 'servers', + 'startup', + 'strs', + 'structs', + 'threads', + 'trace' ] + +# TODO more resources: +# http://aima.cs.berkeley.edu/python/utils.py +# http://aspn.activestate.com/ASPN/Cookbook/Python +# http://aspn.activestate.com/ASPN/Cookbook/Python?&recipe_status=editors +# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486 +# interesting: +# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259174 +# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473790 Deleted: python-commons/tags/0.6/src/commons/async.py =================================================================== --- python-commons/trunk/src/commons/async.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/async.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,77 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -Asynchronous utilities for use with the -U{Twisted<http://twistedmatrix.com/>} library. -""" - -from twisted.internet import defer, reactor - -def async_sleep( seconds ): - """ - Acts as the asynchronous version of C{time.sleep}. - - @param seconds: The amount of time in seconds to sleep. - @type seconds: float - - @return: The L{defer.Deferred} that will be fired on wake-up. - @rtype: defer.Deferred - """ - d = defer.Deferred() - reactor.callLater( seconds, lambda: d.callback( None ) ) - return d - -def asynchronized( func ): - """ - Acts as the asynchronous version of a C{synchronized} decorator - (inspired by Java's keyword). Ensures that no event-handling path - (the asynchronous equivalent to threads) will enter the function - if another one is currently in it (which can happen if the current - "thread" is itself waiting on a deferred, thus giving the reactor - an opportunity to execute the competing "thread"). - - Provides additional protection against U{Twisted bug - 411<http://twistedmatrix.com/trac/ticket/411>}. In this case, if - too many acquisition requests are chained onto the deferred which - is fired on release, then the stack will overflow. This is - circumvented by maintaining a queue of closures and deferreds - which is checked on each release, to keep the callback chain - "flat." - - @param func: The function to decorate. - @type func: function - - @return: The "thread-safe" version of the function. - @rtype: function - """ - def wrapper( self, *args, **kwargs ): - try: - q = self._sync_queue - except AttributeError: - q = self._sync_queue = [] - def release( result ): - invoke, d = q[0] - # actually perform the callback on the outer - # deferred; the deferred running this cb() should - # just terminate and be discarded - d.callback( result ) - - del q[0] - if len( q ) > 0: - invoke, d = q[0] - # we're tempted to do just run invoke() directly, but - # that would lead to the same stack explosion - reactor.callLater( 0, invoke ) - return result - def invoke(): - return defer.maybeDeferred( func, self, *args, **kwargs ).addCallback( release ) - d = defer.Deferred() - q.append( ( invoke, d ) ) - if len( q ) == 1: - reactor.callLater( 0, invoke ) - return d - wrapper.__name__ = func.__name__ - wrapper.__dict__ = func.__dict__ - wrapper.__doc__ = func.__doc__ - return wrapper Copied: python-commons/tags/0.6/src/commons/async.py (from rev 1450, python-commons/trunk/src/commons/async.py) =================================================================== --- python-commons/tags/0.6/src/commons/async.py (rev 0) +++ python-commons/tags/0.6/src/commons/async.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,77 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +""" +Asynchronous utilities for use with the +U{Twisted<http://twistedmatrix.com/>} library. +""" + +from twisted.internet import defer, reactor + +def async_sleep( seconds ): + """ + Acts as the asynchronous version of C{time.sleep}. + + @param seconds: The amount of time in seconds to sleep. + @type seconds: float + + @return: The C{defer.Deferred} that will be fired on wake-up. + @rtype: defer.Deferred + """ + d = defer.Deferred() + reactor.callLater( seconds, lambda: d.callback( None ) ) + return d + +def asynchronized( func ): + """ + Acts as the asynchronous version of a C{synchronized} decorator + (inspired by Java's keyword). Ensures that no event-handling path + (the asynchronous equivalent to threads) will enter the function + if another one is currently in it (which can happen if the current + "thread" is itself waiting on a deferred, thus giving the reactor + an opportunity to execute the competing "thread"). + + Provides additional protection against U{Twisted bug + 411<http://twistedmatrix.com/trac/ticket/411>}. In this case, if + too many acquisition requests are chained onto the deferred which + is fired on release, then the stack will overflow. This is + circumvented by maintaining a queue of closures and deferreds + which is checked on each release, to keep the callback chain + "flat." + + @param func: The function to decorate. + @type func: function + + @return: The "thread-safe" version of the function. + @rtype: function + """ + def wrapper( self, *args, **kwargs ): + try: + q = self._sync_queue + except AttributeError: + q = self._sync_queue = [] + def release( result ): + invoke, d = q[0] + # actually perform the callback on the outer + # deferred; the deferred running this cb() should + # just terminate and be discarded + d.callback( result ) + + del q[0] + if len( q ) > 0: + invoke, d = q[0] + # we're tempted to do just run invoke() directly, but + # that would lead to the same stack explosion + reactor.callLater( 0, invoke ) + return result + def invoke(): + return defer.maybeDeferred( func, self, *args, **kwargs ).addCallback( release ) + d = defer.Deferred() + q.append( ( invoke, d ) ) + if len( q ) == 1: + reactor.callLater( 0, invoke ) + return d + wrapper.__name__ = func.__name__ + wrapper.__dict__ = func.__dict__ + wrapper.__doc__ = func.__doc__ + return wrapper Deleted: python-commons/tags/0.6/src/commons/control.py =================================================================== --- python-commons/trunk/src/commons/control.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/control.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,59 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -"""Processes, idling, and signals.""" - -from logging import * -import os, signal, time - -def suicide(): - """Force-kill the current process.""" - os.kill( os.getpid(), signal.SIGKILL ) - -############################################################################# - -class Sleeper( object ): - """Sleeper class. Goes to sleep forever.""" - @classmethod - def sleep( cls ): - """ - Calls L{time.sleep} for 100000 seconds at a time in a - while-true loop. - """ - while True: - time.sleep( 100000 ) - -############################################################################# - -class SigTerm( Exception ): pass - -def handle_signal( signum, frame ): - """ - Generic signal handler that logs the received signal to the - "signal" logging channel (log level INFO) and then raises - L{SigTerm} if it was not previously raised. - - @param signum: The signal's numeric value (see C{man 7 signal}). - - @param frame: Ignored. - - @raise SigTerm: Only once. - """ - global got_signal - info( 'signal', 'got signal', str( signum ) ) - if not got_signal: - got_signal = True - raise SigTerm - -def handle_signals( *sigs ): - """ - For each given signal, register the L{handle_signal} as its - handler. - - @param sigs: The set of sigs to iterate over. - @type sigs: set - """ - global got_signal - got_signal = False - for sig in sigs: - signal.signal( signal.SIGTERM, handle_signal ) Copied: python-commons/tags/0.6/src/commons/control.py (from rev 1450, python-commons/trunk/src/commons/control.py) =================================================================== --- python-commons/tags/0.6/src/commons/control.py (rev 0) +++ python-commons/tags/0.6/src/commons/control.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,59 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +"""Processes, idling, and signals.""" + +from logging import * +import os, signal, time + +def suicide(): + """Force-kill the current process.""" + os.kill( os.getpid(), signal.SIGKILL ) + +############################################################################# + +class Sleeper( object ): + """Sleeper class. Goes to sleep forever.""" + @classmethod + def sleep( cls ): + """ + Calls C{time.sleep} for 100000 seconds at a time in a + while-true loop. + """ + while True: + time.sleep( 100000 ) + +############################################################################# + +class SigTerm( Exception ): pass + +def handle_signal( signum, frame ): + """ + Generic signal handler that logs the received signal to the + "signal" logging channel (log level INFO) and then raises + L{SigTerm} if it was not previously raised. + + @param signum: The signal's numeric value (see C{man 7 signal}). + + @param frame: Ignored. + + @raise SigTerm: Only once. + """ + global got_signal + info( 'signal', 'got signal', str( signum ) ) + if not got_signal: + got_signal = True + raise SigTerm + +def handle_signals( *sigs ): + """ + For each given signal, register the L{handle_signal} as its + handler. + + @param sigs: The set of sigs to iterate over. + @type sigs: set + """ + global got_signal + got_signal = False + for sig in sigs: + signal.signal( signal.SIGTERM, handle_signal ) Deleted: python-commons/tags/0.6/src/commons/decs.py =================================================================== --- python-commons/trunk/src/commons/decs.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/decs.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,156 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -Decorators and decorator utilities. - -@todo: Move the actual decorators to modules based on their topic. -""" - -from __future__ import with_statement -import functools, inspect, xmlrpclib -from cPickle import * - -def wrap_callable(any_callable, before, after): - """ - Wrap any callable with before/after calls. - - From the Python Cookbook. Modified to support C{None} for - C{before} or C{after}. - - @copyright: O'Reilly Media - - @param any_callable: The function to decorate. - @type any_callable: function - - @param before: The pre-processing procedure. If this is C{None}, then no pre-processing will be done. - @type before: function - - @param after: The post-processing procedure. If this is C{None}, then no post-processing will be done. - @type after: function - """ - def _wrapped(*a, **kw): - if before is not None: - before( ) - try: - return any_callable(*a, **kw) - finally: - if after is not None: - after( ) - # In 2.4, only: _wrapped.__name__ = any_callable.__name__ - return _wrapped - -class GenericWrapper( object ): - """ - Wrap all of an object's methods with before/after calls. This is - like a decorator for objects. - - From the I{Python Cookbook}. - - @copyright: O'Reilly Media - """ - def __init__(self, obj, before, after, ignore=( )): - # we must set into __dict__ directly to bypass __setattr__; so, - # we need to reproduce the name-mangling for double-underscores - clasname = 'GenericWrapper' - self.__dict__['_%s__methods' % clasname] = { } - self.__dict__['_%s__obj' % clasname] = obj - for name, method in inspect.getmembers(obj, inspect.ismethod): - if name not in ignore and method not in ignore: - self.__methods[name] = wrap_callable(method, before, after) - def __getattr__(self, name): - try: - return self.__methods[name] - except KeyError: - return getattr(self.__obj, name) - def __setattr__(self, name, value): - setattr(self.__obj, name, value) - -########################################################## - -def xmlrpc_safe(func): - """ - Makes a procedure "XMLRPC-safe" by returning 0 whenever the inner - function returns C{None}. This is useful because XMLRPC requires - return values, and 0 is commonly used when functions don't intend - to return anything. - - Also, if the procedure returns a boolean, it will be wrapped in - L{xmlrpclib.Boolean}. - - @param func: The procedure to decorate. - @type func: function - """ - @functools.wraps(func) - def wrapper(*args,**kwargs): - result = func(*args,**kwargs) - if result is not None: - if type( result ) == bool: - return xmlrpclib.Boolean( result ) - else: - return result - else: - return 0 - return wrapper - -########################################################## - -def file_memoized(serializer, deserializer, pathfunc): - """ - The string result of the given function is saved to the given path. - - Example:: - - @file_memoized(lambda x,f: f.write(x), - lambda f: f.read(), - lambda: "/tmp/cache") - def foo(): return "hello" - - @file_memoized(pickle.dump, - pickle.load, - lambda x,y: "/tmp/cache-%d-%d" % (x,y)) - def foo(x,y): return "hello %d %d" % (x,y) - - @param serializer: The function to serialize the return value into a - string. This should take the return value object and - the file object. - @type serializer: function - - @param deserializer: The function te deserialize the cache file contents - into the return value. This should take the file - object and return a string. - type: deserializer: function - - @param pathfunc: Returns the path where the files should be saved. This - should be able to take the same arguments as the original - function. - @type pathfunc: str - """ - def dec(func): - @functools.wraps(func) - def wrapper(*args, **kwargs): - p = pathfunc(*args, **kwargs) - try: - with file(p) as f: - return deserializer(f) - except IOError, (errno, errstr): - if errno != 2: raise - with file(p, 'w') as f: - x = func(*args, **kwargs) - serializer(x, f) - return x - return wrapper - return dec - -def file_string_memoized(pathfunc): - """ - Wrapper around L{file_memoized} that expects the decorated function to - return strings, so the string is written verbatim. - """ - return file_memoized(lambda x,f: f.write(x), lambda f: f.read(), pathfunc) - -def pickle_memoized(pathfunc): - """ - Wrapper around L{file_memoized} that uses pickle. - """ - return file_memoized(dump, load, pathfunc) Copied: python-commons/tags/0.6/src/commons/decs.py (from rev 1450, python-commons/trunk/src/commons/decs.py) =================================================================== --- python-commons/tags/0.6/src/commons/decs.py (rev 0) +++ python-commons/tags/0.6/src/commons/decs.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,158 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +""" +Decorators and decorator utilities. + +@todo: Move the actual decorators to modules based on their topic. +""" + +from __future__ import with_statement +import functools, inspect, xmlrpclib +from cPickle import * + +def wrap_callable(any_callable, before, after): + """ + Wrap any callable with before/after calls. + + From the Python Cookbook. Modified to support C{None} for + C{before} or C{after}. + + @copyright: O'Reilly Media + + @param any_callable: The function to decorate. + @type any_callable: function + + @param before: The pre-processing procedure. If this is C{None}, then no pre-processing will be done. + @type before: function + + @param after: The post-processing procedure. If this is C{None}, then no post-processing will be done. + @type after: function + """ + def _wrapped(*a, **kw): + if before is not None: + before( ) + try: + return any_callable(*a, **kw) + finally: + if after is not None: + after( ) + # In 2.4, only: _wrapped.__name__ = any_callable.__name__ + return _wrapped + +class GenericWrapper( object ): + """ + Wrap all of an object's methods with before/after calls. This is + like a decorator for objects. + + From the I{Python Cookbook}. + + @copyright: O'Reilly Media + """ + def __init__(self, obj, before, after, ignore=( )): + # we must set into __dict__ directly to bypass __setattr__; so, + # we need to reproduce the name-mangling for double-underscores + clasname = 'GenericWrapper' + self.__dict__['_%s__methods' % clasname] = { } + self.__dict__['_%s__obj' % clasname] = obj + for name, method in inspect.getmembers(obj, inspect.ismethod): + if name not in ignore and method not in ignore: + self.__methods[name] = wrap_callable(method, before, after) + def __getattr__(self, name): + try: + return self.__methods[name] + except KeyError: + return getattr(self.__obj, name) + def __setattr__(self, name, value): + setattr(self.__obj, name, value) + +########################################################## + +def xmlrpc_safe(func): + """ + Makes a procedure "XMLRPC-safe" by returning 0 whenever the inner + function returns C{None}. This is useful because XMLRPC requires + return values, and 0 is commonly used when functions don't intend + to return anything. + + Also, if the procedure returns a boolean, it will be wrapped in + C{xmlrpclib.Boolean}. + + @param func: The procedure to decorate. + @type func: function + """ + @functools.wraps(func) + def wrapper(*args,**kwargs): + result = func(*args,**kwargs) + if result is not None: + if type( result ) == bool: + return xmlrpclib.Boolean( result ) + else: + return result + else: + return 0 + return wrapper + +########################################################## + +def file_memoized(serializer, deserializer, pathfunc): + """ + The string result of the given function is saved to the given path. + + Example:: + + @file_memoized(lambda x,f: f.write(x), + lambda f: f.read(), + lambda: "/tmp/cache") + def foo(): return "hello" + + @file_memoized(pickle.dump, + pickle.load, + lambda x,y: "/tmp/cache-%d-%d" % (x,y)) + def foo(x,y): return "hello %d %d" % (x,y) + + @param serializer: The function to serialize the return value into a + string. This should take the return value object and + the file object. + @type serializer: function + + @param deserializer: The function te deserialize the cache file contents + into the return value. This should take the file + object and return a string. + @type deserializer: function + + @param pathfunc: Returns the path where the files should be saved. This + should be able to take the same arguments as the original + function. + @type pathfunc: str + """ + def dec(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + p = pathfunc(*args, **kwargs) + try: + with file(p) as f: + return deserializer(f) + except IOError, (errno, errstr): + if errno != 2: raise + with file(p, 'w') as f: + x = func(*args, **kwargs) + serializer(x, f) + return x + return wrapper + return dec + +def file_string_memoized(pathfunc): + """ + Wrapper around L{file_memoized} that expects the decorated function to + return strings, so the string is written verbatim. + """ + return file_memoized(lambda x,f: f.write(x), lambda f: f.read(), pathfunc) + +def pickle_memoized(pathfunc): + """ + Wrapper around L{file_memoized} that uses pickle. + """ + bindump = lambda x,f: dump(x,f,2) + binload = lambda x,f: load(x,f,2) + return file_memoized(bindump, binload, pathfunc) Deleted: python-commons/tags/0.6/src/commons/files.py =================================================================== --- python-commons/trunk/src/commons/files.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/files.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,205 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -File and directory manipulation. - -@var invalid_filename_chars: The characters which are usually -prohibited on most modern file systems. - -@var invalid_filename_chars_regex: A regex character class constructed -from L{invalid_filename_chars}. -""" - -from __future__ import with_statement - -__all__ = ''' -soft_makedirs -temp_dir -cleanse_filename -invalid_filename_chars -invalid_filename_chars_regex -disk_double_buffer -versioned_guard -versioned_cache -read_file -write_file -write_or_rm -is_nonempty_file -'''.split() - -import os, re, tempfile -from cPickle import * -from path import path - -def soft_makedirs( path ): - """ - Emulate C{mkdir -p} (doesn't complain if it already exists). - - @param path: The path of the directory to create. - @type path: str - - @raise OSError: If it cannot create the directory. It only - swallows OS error 17. - """ - try: - os.makedirs( path ) - except OSError, ex: - if ex.errno == 17: - pass - else: - raise - -def temp_dir( base_dir_name, do_create_subdir = True ): - """ - Get a temporary directory without polluting top-level /tmp. This follows - Ubuntu's conventions, choosing a temporary directory name based on - the given name plus the user name to avoid user conflicts. - - @param base_dir_name: The "name" of the temporary directory. This - is usually identifies the purpose of the directory, or the - application to which the temporary directory belongs. E.g., if joe - calls passes in C{"ssh-agent"} on a standard Linux/Unix system, - then the full path of the temporary directory will be - C{"/tmp/ssh-agent-joe"}. - @type base_dir_name: str - - @param do_create_subdir: If C{True}, then creates a - sub-sub-directory within the temporary sub-directory (and returns - the path to that). The sub-sub-directory's name is randomized - (uses L{tempfile.mkdtemp}). - @type do_create_subdir: bool - - @return: The path to the temporary (sub-)sub-directory. - @rtype: str - """ - base_dir_name += '-' + os.environ[ 'USER' ] - base_dir = path( tempfile.gettempdir() ) / base_dir_name - soft_makedirs( base_dir ) - if do_create_subdir: - return tempfile.mkdtemp( dir = base_dir ) - else: - return base_dir - -invalid_filename_chars = r'*|\/:<>?' -invalid_filename_chars_regex = r'[*|\\\/:<>?]' - -def cleanse_filename( filename ): - """ - Replaces all problematic characters in a filename with C{"_"}, as - specified by L{invalid_filename_chars}. - - @param filename: The filename to cleanse. - @type filename: str - """ - pattern = invalid_filename_chars_regex - return re.sub( pattern, '_', filename ) - -class disk_double_buffer( object ): - """ - A simple disk double-buffer. One file is for reading, the other is for - writing, and a facility for swapping the two roles is provided. - """ - def __init__( self, path_base, do_persist = True ): - self.paths = map( path, [ path_base + '.0', path_base + '.1' ] ) - self.do_persist = do_persist - self.switch_status = path( path_base + '.switched' ) - if not do_persist or not self.switch_status.exists(): - self.w, self.r = 0, 1 # default - else: - self.w, self.r = 1, 0 - self.reload_files() - def reload_files( self ): - self.writer = file( self.paths[ self.w ], 'w' ) - if not self.paths[ self.r ].exists(): - self.paths[ self.r ].touch() - self.reader = file( self.paths[ self.r ] ) - def switch( self ): - self.close() - if self.do_persist: - if self.w == 0: self.switch_status.touch() - else: self.switch_status.remove() - self.r, self.w = self.w, self.r - self.reload_files() - def write( self, x ): - self.writer.write( x ) - def read( self, len = 8192 ): - return self.reader.read( len ) - def close( self ): - self.reader.close() - self.writer.close() - -def versioned_guard(path, fresh_version): - """ - Maintain a version object. This is useful for working with versioned - caches. - - @param path: The path to the file containing the cached version object. - @type path: str - - @param fresh_version: The actual latest version that the cached version - should be compared against. - @type fresh_version: object (any type that can be compared) - - @return: True iff the cached version is obsolete (less than the fresh - version or doesn't exist). - @rtype: bool - """ - cache_version = None - try: - with file( path ) as f: cache_version = load(f) - except IOError, (errno, errstr): - if errno != 2: raise - if cache_version is None or fresh_version > cache_version: - with file( path, 'w' ) as f: dump(fresh_version, f) - return True - else: - return False - -def versioned_cache(version_path, fresh_version, cache_path, cache_func): - """ - If fresh_version is newer than the version in version_path, then invoke - cache_func and cache the result in cache_path (using pickle). - - Note the design flaw with L{versioned_guard}: the updated version value is - stored immediately, rather than after updating the cache. - - @param version_path: The path to the file version. - @type version_path: str - - @param fresh_version: The actual, up-to-date version value. - @type fresh_version: object (any type that can be compared) - - @param cache_path: The path to the cached data. - @type cache_path: str - - @param cache_func: The function that produces the fresh data to be cached. - @type cache_func: function (no arguments) - """ - if versioned_guard( version_path, fresh_version ): - # cache obsolete, force-fetch new data - result = cache_func() - with file(cache_path, 'w') as f: dump(result, f) - return result - else: - # cache up-to-date (should be available since dlcs-timestamp exists!) - with file(cache_path) as f: return load(f) - -def read_file(path): - f = file(path) - try: return f.read() - finally: f.close() - -def write_file(path, contents): - f = file(path,'w') - try: f.write(contents) - finally: f.close() - -def write_or_rm(p, contents): - 'Write the file or remove it if contents is empty.' - p = path(p) - if contents.strip(): write_file(p, contents) - elif p.isfile(): p.remove() - -def is_nonempty_file(path): - return path.isfile() and read_file(path).strip() != '' Copied: python-commons/tags/0.6/src/commons/files.py (from rev 1450, python-commons/trunk/src/commons/files.py) =================================================================== --- python-commons/tags/0.6/src/commons/files.py (rev 0) +++ python-commons/tags/0.6/src/commons/files.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,205 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +""" +File and directory manipulation. + +@var invalid_filename_chars: The characters which are usually +prohibited on most modern file systems. + +@var invalid_filename_chars_regex: A regex character class constructed +from L{invalid_filename_chars}. +""" + +from __future__ import with_statement + +__all__ = ''' +soft_makedirs +temp_dir +cleanse_filename +invalid_filename_chars +invalid_filename_chars_regex +disk_double_buffer +versioned_guard +versioned_cache +read_file +write_file +write_or_rm +is_nonempty_file +'''.split() + +import os, re, tempfile +from cPickle import * +from path import path + +def soft_makedirs( path ): + """ + Emulate C{mkdir -p} (doesn't complain if it already exists). + + @param path: The path of the directory to create. + @type path: str + + @raise OSError: If it cannot create the directory. It only + swallows OS error 17. + """ + try: + os.makedirs( path ) + except OSError, ex: + if ex.errno == 17: + pass + else: + raise + +def temp_dir( base_dir_name, do_create_subdir = True ): + """ + Get a temporary directory without polluting top-level /tmp. This follows + Ubuntu's conventions, choosing a temporary directory name based on + the given name plus the user name to avoid user conflicts. + + @param base_dir_name: The "name" of the temporary directory. This + is usually identifies the purpose of the directory, or the + application to which the temporary directory belongs. E.g., if joe + calls passes in C{"ssh-agent"} on a standard Linux/Unix system, + then the full path of the temporary directory will be + C{"/tmp/ssh-agent-joe"}. + @type base_dir_name: str + + @param do_create_subdir: If C{True}, then creates a + sub-sub-directory within the temporary sub-directory (and returns + the path to that). The sub-sub-directory's name is randomized + (uses C{tempfile.mkdtemp}). + @type do_create_subdir: bool + + @return: The path to the temporary (sub-)sub-directory. + @rtype: str + """ + base_dir_name += '-' + os.environ[ 'USER' ] + base_dir = path( tempfile.gettempdir() ) / base_dir_name + soft_makedirs( base_dir ) + if do_create_subdir: + return tempfile.mkdtemp( dir = base_dir ) + else: + return base_dir + +invalid_filename_chars = r'*|\/:<>?' +invalid_filename_chars_regex = r'[*|\\\/:<>?]' + +def cleanse_filename( filename ): + """ + Replaces all problematic characters in a filename with C{"_"}, as + specified by L{invalid_filename_chars}. + + @param filename: The filename to cleanse. + @type filename: str + """ + pattern = invalid_filename_chars_regex + return re.sub( pattern, '_', filename ) + +class disk_double_buffer( object ): + """ + A simple disk double-buffer. One file is for reading, the other is for + writing, and a facility for swapping the two roles is provided. + """ + def __init__( self, path_base, do_persist = True ): + self.paths = map( path, [ path_base + '.0', path_base + '.1' ] ) + self.do_persist = do_persist + self.switch_status = path( path_base + '.switched' ) + if not do_persist or not self.switch_status.exists(): + self.w, self.r = 0, 1 # default + else: + self.w, self.r = 1, 0 + self.reload_files() + def reload_files( self ): + self.writer = file( self.paths[ self.w ], 'w' ) + if not self.paths[ self.r ].exists(): + self.paths[ self.r ].touch() + self.reader = file( self.paths[ self.r ] ) + def switch( self ): + self.close() + if self.do_persist: + if self.w == 0: self.switch_status.touch() + else: self.switch_status.remove() + self.r, self.w = self.w, self.r + self.reload_files() + def write( self, x ): + self.writer.write( x ) + def read( self, len = 8192 ): + return self.reader.read( len ) + def close( self ): + self.reader.close() + self.writer.close() + +def versioned_guard(path, fresh_version): + """ + Maintain a version object. This is useful for working with versioned + caches. + + @param path: The path to the file containing the cached version object. + @type path: str + + @param fresh_version: The actual latest version that the cached version + should be compared against. + @type fresh_version: object (any type that can be compared) + + @return: True iff the cached version is obsolete (less than the fresh + version or doesn't exist). + @rtype: bool + """ + cache_version = None + try: + with file( path ) as f: cache_version = load(f) + except IOError, (errno, errstr): + if errno != 2: raise + if cache_version is None or fresh_version > cache_version: + with file( path, 'w' ) as f: dump(fresh_version, f) + return True + else: + return False + +def versioned_cache(version_path, fresh_version, cache_path, cache_func): + """ + If fresh_version is newer than the version in version_path, then invoke + cache_func and cache the result in cache_path (using pickle). + + Note the design flaw with L{versioned_guard}: the updated version value is + stored immediately, rather than after updating the cache. + + @param version_path: The path to the file version. + @type version_path: str + + @param fresh_version: The actual, up-to-date version value. + @type fresh_version: object (any type that can be compared) + + @param cache_path: The path to the cached data. + @type cache_path: str + + @param cache_func: The function that produces the fresh data to be cached. + @type cache_func: function (no arguments) + """ + if versioned_guard( version_path, fresh_version ): + # cache obsolete, force-fetch new data + result = cache_func() + with file(cache_path, 'w') as f: dump(result, f) + return result + else: + # cache up-to-date (should be available since dlcs-timestamp exists!) + with file(cache_path) as f: return load(f) + +def read_file(path): + f = file(path) + try: return f.read() + finally: f.close() + +def write_file(path, contents): + f = file(path,'w') + try: f.write(contents) + finally: f.close() + +def write_or_rm(p, contents): + 'Write the file or remove it if contents is empty.' + p = path(p) + if contents.strip(): write_file(p, contents) + elif p.isfile(): p.remove() + +def is_nonempty_file(path): + return path.isfile() and read_file(path).strip() != '' Deleted: python-commons/tags/0.6/src/commons/log.py =================================================================== --- python-commons/trunk/src/commons/log.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/log.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,129 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -Convenience functions for the standard L{logging} library. - -Functions are mostly self-explanatory. -""" - -from strs import * -from itertools import chain, imap -import logging, logging.handlers, sys -from logging import getLogger - -# TODO cleanup / reorganize this section - -def fmt( flag, *args ): - return ' '.join( chain( [ '%-20s:' % flag ], imap( str, args ) ) ) - -def log( level, flag, *args ): - getLogger( flag ).log( level, fmt( flag, *args ) ) - -def exception( flag, *args ): - getLogger( flag ).exception( fmt( flag, *args ) ) - -def critical( flag, *args ): - log( logging.CRITICAL, flag, *args ) - -def error( flag, *args ): - log( logging.ERROR, flag, *args ) - -def warning( flag, *args ): - log( logging.WARNING, flag, *args ) - -def debug( flag, *args ): - log( logging.DEBUG, flag, *args ) - -def info( flag, *args ): - log( logging.INFO, flag, *args ) - -def die( flag, *args ): - error( flag, *args ) - sys.exit( 1 ) - -def tailor_logs( flag, *prefix ): - """ - Avoid repetitive typing! Usage: - - debug,info,warning,_,_ = tailor_logs('myflag','some','prefix') - """ - def new_critical( *args ): critical( flag, * prefix + args ) - def new_debug( *args ): debug( flag, * prefix + args ) - def new_error( *args ): error( flag, * prefix + args ) - def new_info( *args ): info( flag, * prefix + args ) - def new_warning( *args ): warning( flag, * prefix + args ) - return new_debug, new_info, new_warning, new_error, new_critical - -def config_logging( level = logging.INFO, - do_console = False, - do_file = False, - do_server = False, - flags = [], - path = None, - log_format = '%(asctime)s %(levelname)-8s %(message)s', - log_date_format = None ): - """ - Configures L{logging}, promoting some common configurations. - - @param level: The default log-level threshold for all - loggers. (Note that this is not the level for all handlers.) - @type level: int - - @param do_console: Whether to enable the console handler. - @type do_console: bool - - @param do_file: Whether to enable the file handler. - @type do_file: bool - - @param do_server: Whether to enable a network socket stream - handler. - @type do_server: bool - - @param flags: The set of flags on which to lower the threshold to - L{logging.DEBUG}. - @type flags: iterable - - @param path: The path of the log file (only considered if - C{do_file} is C{True}. - @type path: str - - @param log_format: The log message formatting template. - @type log_format: str - - @param log_date_format: The timestamp formatting template. - @type log_date_format: str - """ - formatter = logging.Formatter( log_format, log_date_format ) - - logging.getLogger( '' ).setLevel( level ) - logging.raiseExceptions = False - - # log to console - if do_console: - handler = logging.StreamHandler() - handler.setLevel( logging.DEBUG ) - handler.setFormatter( formatter ) - logging.getLogger( '' ).addHandler( handler ) - - # log to files - if do_file and path is not None: - handler = logging.FileHandler( path, 'w' ) - handler.setFormatter( formatter ) - handler.setLevel( logging.DEBUG ) - logging.getLogger( '' ).addHandler( handler ) - - # log to the log server - if do_server: - handler = logging.handlers.SocketHandler( 'localhost', - logging.handlers.DEFAULT_TCP_LOGGING_PORT ) - handler.setLevel( logging.DEBUG ) - logging.getLogger( '' ).addHandler( handler ) - - for flag in flags: - logging.getLogger( flag ).setLevel( logging.DEBUG ) - -#### logging.raiseExceptions = False -#### config_path = get_usr_conf( 'LOGGING_CONFIG_PATH', 'logging.ini' ) -#### if config_path is not None: -#### logging.config.fileConfig( config_path ) Copied: python-commons/tags/0.6/src/commons/log.py (from rev 1450, python-commons/trunk/src/commons/log.py) =================================================================== --- python-commons/tags/0.6/src/commons/log.py (rev 0) +++ python-commons/tags/0.6/src/commons/log.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -0,0 +1,142 @@ +# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- +# vim:ft=python:et:sw=4:ts=4 + +""" +Convenience functions for the standard L{logging} library. + +Functions are mostly self-explanatory. +""" + +from strs import * +from itertools import chain, imap +import logging, logging.handlers, sys +from logging import getLogger + +__all__ = """ +fmt +log +exception +critical +error +warning +info +die +tailor_logs +config_logging +""".split() + +# TODO cleanup / reorganize this section + +def fmt( flag, *args ): + return ' '.join( chain( [ '%-20s:' % flag ], imap( unicode, args ) ) ) + +def log( level, flag, *args ): + getLogger( flag ).log( level, fmt( flag, *args ) ) + +def exception( flag, *args ): + getLogger( flag ).exception( fmt( flag, *args ) ) + +def critical( flag, *args ): + log( logging.CRITICAL, flag, *args ) + +def error( flag, *args ): + log( logging.ERROR, flag, *args ) + +def warning( flag, *args ): + log( logging.WARNING, flag, *args ) + +def debug( flag, *args ): + log( logging.DEBUG, flag, *args ) + +def info( flag, *args ): + log( logging.INFO, flag, *args ) + +def die( flag, *args ): + error( flag, *args ) + sys.exit( 1 ) + +def tailor_logs( flag, *prefix ): + """ + Avoid repetitive typing! Usage:: + + debug,info,warning,_,_ = tailor_logs('myflag','some','prefix') + """ + def new_critical( *args ): critical( flag, * prefix + args ) + def new_debug( *args ): debug( flag, * prefix + args ) + def new_error( *args ): error( flag, * prefix + args ) + def new_info( *args ): info( flag, * prefix + args ) + def new_warning( *args ): warning( flag, * prefix + args ) + return new_debug, new_info, new_warning, new_error, new_critical + +def config_logging( level = logging.INFO, + do_console = False, + do_file = False, + do_server = False, + flags = [], + path = None, + log_format = '%(asctime)s %(levelname)-8s %(message)s', + log_date_format = None ): + """ + Configures L{logging}, promoting some common configurations. + + @param level: The default log-level threshold for all + loggers. (Note that this is not the level for all handlers.) + @type level: int + + @param do_console: Whether to enable the console handler. + @type do_console: bool + + @param do_file: Whether to enable the file handler. + @type do_file: bool + + @param do_server: Whether to enable a network socket stream + handler. + @type do_server: bool + + @param flags: The set of flags on which to lower the threshold to + C{logging.DEBUG}. + @type flags: iterable + + @param path: The path of the log file (only considered if + C{do_file} is C{True}. + @type path: str + + @param log_format: The log message formatting template. + @type log_format: str + + @param log_date_format: The timestamp formatting template. + @type log_date_format: str + """ + formatter = logging.Formatter( log_format, log_date_format ) + + logging.getLogger( '' ).setLevel( level ) + logging.raiseExceptions = False + + # log to console + if do_console: + handler = logging.StreamHandler() + handler.setLevel( logging.DEBUG ) + handler.setFormatter( formatter ) + logging.getLogger( '' ).addHandler( handler ) + + # log to files + if do_file and path is not None: + handler = logging.FileHandler( path, 'w' ) + handler.setFormatter( formatter ) + handler.setLevel( logging.DEBUG ) + logging.getLogger( '' ).addHandler( handler ) + + # log to the log server + if do_server: + handler = logging.handlers.SocketHandler( 'localhost', + logging.handlers.DEFAULT_TCP_LOGGING_PORT ) + handler.setLevel( logging.DEBUG ) + logging.getLogger( '' ).addHandler( handler ) + + for flag in flags: + logging.getLogger( flag ).setLevel( logging.DEBUG ) + +#### logging.raiseExceptions = False +#### config_path = get_usr_conf( 'LOGGING_CONFIG_PATH', 'logging.ini' ) +#### if config_path is not None: +#### logging.config.fileConfig( config_path ) Deleted: python-commons/tags/0.6/src/commons/misc.py =================================================================== --- python-commons/trunk/src/commons/misc.py 2009-03-01 19:45:28 UTC (rev 1240) +++ python-commons/tags/0.6/src/commons/misc.py 2010-04-01 18:21:19 UTC (rev 1598) @@ -1,324 +0,0 @@ -# -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- -# vim:ft=python:et:sw=4:ts=4 - -""" -Miscellanea. -""" - -__all__ = ''' -TerminalController -cleartimeout -days -default_if_none -generate_bit_fields -remove_colors -run -sendmail -seq -settimeout -tc -timeout_exception -wall_clock -'''.split() - -# -# Email -# ... [truncated message content] |