From: Diana Clarke Date: Tue, 20 Nov 2012 00:06:47 +0000 (-0500) Subject: just a pep8 pass of lib/sqlalchemy/ X-Git-Tag: rel_0_8_0b2~33^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=648778afb2d8c3314dbad83438954d69dfa48b7b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git just a pep8 pass of lib/sqlalchemy/ --- diff --git a/lib/sqlalchemy/event.py b/lib/sqlalchemy/event.py index c702d9d34a..bf996ae3c5 100644 --- a/lib/sqlalchemy/event.py +++ b/lib/sqlalchemy/event.py @@ -12,6 +12,7 @@ from itertools import chain CANCEL = util.symbol('CANCEL') NO_RETVAL = util.symbol('NO_RETVAL') + def listen(target, identifier, fn, *args, **kw): """Register a listener function for the given target. @@ -40,6 +41,7 @@ def listen(target, identifier, fn, *args, **kw): raise exc.InvalidRequestError("No such event '%s' for target '%s'" % (identifier, target)) + def listens_for(target, identifier, *args, **kw): """Decorate a function as a listener for the given target + identifier. @@ -60,6 +62,7 @@ def listens_for(target, identifier, *args, **kw): return fn return decorate + def remove(target, identifier, fn): """Remove an event listener. @@ -75,12 +78,14 @@ def remove(target, identifier, fn): _registrars = util.defaultdict(list) + def _is_event_name(name): return not name.startswith('_') and name != 'dispatch' + class _UnpickleDispatch(object): - """Serializable callable that re-generates an instance of :class:`_Dispatch` - given a particular :class:`.Events` subclass. + """Serializable callable that re-generates an instance of + :class:`_Dispatch` given a particular :class:`.Events` subclass. """ def __call__(self, _parent_cls): @@ -90,6 +95,7 @@ class _UnpickleDispatch(object): else: raise AttributeError("No class with a 'dispatch' member present.") + class _Dispatch(object): """Mirror the event listening definitions of an Events class with listener collections. @@ -154,9 +160,11 @@ class _Dispatch(object): if _is_event_name(attr): getattr(self, attr).for_modify(self).clear() + def _event_descriptors(target): return [getattr(target, k) for k in dir(target) if _is_event_name(k)] + class _EventMeta(type): """Intercept new Event subclasses and create associated _Dispatch classes.""" @@ -165,6 +173,7 @@ class _EventMeta(type): _create_dispatcher_class(cls, classname, bases, dict_) return type.__init__(cls, classname, bases, dict_) + def _create_dispatcher_class(cls, classname, bases, dict_): """Create a :class:`._Dispatch` class corresponding to an :class:`.Events` class.""" @@ -182,6 +191,7 @@ def _create_dispatcher_class(cls, classname, bases, dict_): setattr(dispatch_cls, k, _DispatchDescriptor(dict_[k])) _registrars[k].append(cls) + def _remove_dispatcher(cls): for k in dir(cls): if _is_event_name(k): @@ -189,10 +199,10 @@ def _remove_dispatcher(cls): if not _registrars[k]: del _registrars[k] + class Events(object): """Define event listening functions for a particular target type.""" - __metaclass__ = _EventMeta @classmethod @@ -225,6 +235,7 @@ class Events(object): def _clear(cls): cls.dispatch._clear() + class _DispatchDescriptor(object): """Class-level attributes on :class:`._Dispatch` classes.""" @@ -357,6 +368,7 @@ class _EmptyListener(object): def __nonzero__(self): return bool(self.parent_listeners) + class _CompoundListener(object): _exec_once = False @@ -479,8 +491,10 @@ class _JoinedDispatchDescriptor(object): ) return ret + class _JoinedListener(_CompoundListener): _exec_once = False + def __init__(self, parent, name, local): self.parent = parent self.name = name @@ -527,4 +541,3 @@ class dispatcher(object): return self.dispatch_cls obj.__dict__['dispatch'] = disp = self.dispatch_cls(cls) return disp - diff --git a/lib/sqlalchemy/events.py b/lib/sqlalchemy/events.py index 61392ea629..07661c9192 100644 --- a/lib/sqlalchemy/events.py +++ b/lib/sqlalchemy/events.py @@ -216,6 +216,7 @@ class DDLEvents(event.Events): """ + class SchemaEventTarget(object): """Base class for elements that are the targets of :class:`.DDLEvents` events. @@ -235,6 +236,7 @@ class SchemaEventTarget(object): self._set_parent(parent) self.dispatch.after_parent_attach(self, parent) + class PoolEvents(event.Events): """Available events for :class:`.Pool`. @@ -334,6 +336,7 @@ class PoolEvents(event.Events): """ + class ConnectionEvents(event.Events): """Available events for :class:`.Connectable`, which includes :class:`.Connection` and :class:`.Engine`. @@ -661,4 +664,3 @@ class ConnectionEvents(event.Events): :meth:`.TwoPhaseTransaction.prepare` was called. """ - diff --git a/lib/sqlalchemy/exc.py b/lib/sqlalchemy/exc.py index 3c4a64704e..1334d63f2a 100644 --- a/lib/sqlalchemy/exc.py +++ b/lib/sqlalchemy/exc.py @@ -6,14 +6,15 @@ """Exceptions used with SQLAlchemy. -The base exception class is :class:`.SQLAlchemyError`. Exceptions which are raised as a -result of DBAPI exceptions are all subclasses of +The base exception class is :class:`.SQLAlchemyError`. Exceptions which are +raised as a result of DBAPI exceptions are all subclasses of :class:`.DBAPIError`. """ import traceback + class SQLAlchemyError(Exception): """Generic error class.""" @@ -25,14 +26,17 @@ class ArgumentError(SQLAlchemyError): """ + class NoForeignKeysError(ArgumentError): """Raised when no foreign keys can be located between two selectables during a join.""" + class AmbiguousForeignKeysError(ArgumentError): """Raised when more than one foreign key matching can be located between two selectables during a join.""" + class CircularDependencyError(SQLAlchemyError): """Raised by topological sorts when a circular dependency is detected. @@ -64,9 +68,11 @@ class CircularDependencyError(SQLAlchemyError): return self.__class__, (None, self.cycles, self.edges, self.args[0]) + class CompileError(SQLAlchemyError): """Raised when an error occurs during SQL compilation""" + class IdentifierError(SQLAlchemyError): """Raised when a schema name is beyond the max character limit""" @@ -75,15 +81,14 @@ class DisconnectionError(SQLAlchemyError): """A disconnect is detected on a raw DB-API connection. This error is raised and consumed internally by a connection pool. It can - be raised by the :meth:`.PoolEvents.checkout` event - so that the host pool forces a retry; the exception will be caught - three times in a row before the pool gives up and raises - :class:`~sqlalchemy.exc.InvalidRequestError` regarding the connection attempt. + be raised by the :meth:`.PoolEvents.checkout` event so that the host pool + forces a retry; the exception will be caught three times in a row before + the pool gives up and raises :class:`~sqlalchemy.exc.InvalidRequestError` + regarding the connection attempt. """ - class TimeoutError(SQLAlchemyError): """Raised when a connection pool times out on getting a connection.""" @@ -95,23 +100,30 @@ class InvalidRequestError(SQLAlchemyError): """ + class NoInspectionAvailable(InvalidRequestError): """A subject passed to :func:`sqlalchemy.inspection.inspect` produced no context for inspection.""" + class ResourceClosedError(InvalidRequestError): """An operation was requested from a connection, cursor, or other object that's in a closed state.""" + class NoSuchColumnError(KeyError, InvalidRequestError): """A nonexistent column is requested from a ``RowProxy``.""" + class NoReferenceError(InvalidRequestError): """Raised by ``ForeignKey`` to indicate a reference cannot be resolved.""" + class NoReferencedTableError(NoReferenceError): - """Raised by ``ForeignKey`` when the referred ``Table`` cannot be located.""" + """Raised by ``ForeignKey`` when the referred ``Table`` cannot be + located. + """ def __init__(self, message, tname): NoReferenceError.__init__(self, message) self.table_name = tname @@ -119,9 +131,12 @@ class NoReferencedTableError(NoReferenceError): def __reduce__(self): return self.__class__, (self.args[0], self.table_name) + class NoReferencedColumnError(NoReferenceError): - """Raised by ``ForeignKey`` when the referred ``Column`` cannot be located.""" + """Raised by ``ForeignKey`` when the referred ``Column`` cannot be + located. + """ def __init__(self, message, tname, cname): NoReferenceError.__init__(self, message) self.table_name = tname @@ -131,6 +146,7 @@ class NoReferencedColumnError(NoReferenceError): return self.__class__, (self.args[0], self.table_name, self.column_name) + class NoSuchTableError(InvalidRequestError): """Table does not exist or is not visible to a connection.""" @@ -166,6 +182,7 @@ if sys.version_info < (2, 5): # Moved to orm.exc; compatibility definition installed by orm import until 0.6 UnmappedColumnError = None + class StatementError(SQLAlchemyError): """An error occurred during execution of a SQL statement. @@ -207,6 +224,7 @@ class StatementError(SQLAlchemyError): def __unicode__(self): return self.__str__() + class DBAPIError(StatementError): """Raised when the execution of a database operation fails. @@ -219,13 +237,14 @@ class DBAPIError(StatementError): raise the same exception type for any given error condition. :class:`DBAPIError` features :attr:`~.StatementError.statement` - and :attr:`~.StatementError.params` attributes which supply context regarding - the specifics of the statement which had an issue, for the + and :attr:`~.StatementError.params` attributes which supply context + regarding the specifics of the statement which had an issue, for the typical case when the error was raised within the context of emitting a SQL statement. - The wrapped exception object is available in the :attr:`~.StatementError.orig` attribute. - Its type and properties are DB-API implementation specific. + The wrapped exception object is available in the + :attr:`~.StatementError.orig` attribute. Its type and properties are + DB-API implementation specific. """ @@ -243,11 +262,12 @@ class DBAPIError(StatementError): # not a DBAPI error, statement is present. # raise a StatementError if not isinstance(orig, dbapi_base_err) and statement: + msg = traceback.format_exception_only( + orig.__class__, orig)[-1].strip() return StatementError( - "%s (original cause: %s)" % ( - str(orig), - traceback.format_exception_only(orig.__class__, orig)[-1].strip() - ), statement, params, orig) + "%s (original cause: %s)" % (str(orig), msg), + statement, params, orig + ) name, glob = orig.__class__.__name__, globals() if name in glob and issubclass(glob[name], DBAPIError): diff --git a/lib/sqlalchemy/inspection.py b/lib/sqlalchemy/inspection.py index f9c345dce5..905ec1e4da 100644 --- a/lib/sqlalchemy/inspection.py +++ b/lib/sqlalchemy/inspection.py @@ -33,6 +33,7 @@ in a forwards-compatible way. from . import util, exc _registrars = util.defaultdict(list) + def inspect(subject, raiseerr=True): """Produce an inspection object for the given target. @@ -73,6 +74,7 @@ def inspect(subject, raiseerr=True): type_) return ret + def _inspects(*types): def decorate(fn_or_cls): for type_ in types: @@ -84,5 +86,6 @@ def _inspects(*types): return fn_or_cls return decorate + def _self_inspects(*types): - _inspects(*types)(True) \ No newline at end of file + _inspects(*types)(True) diff --git a/lib/sqlalchemy/interfaces.py b/lib/sqlalchemy/interfaces.py index f904bdf31a..4a06f05a88 100644 --- a/lib/sqlalchemy/interfaces.py +++ b/lib/sqlalchemy/interfaces.py @@ -14,6 +14,7 @@ event system. from . import event, util + class PoolListener(object): """Hooks into the lifecycle of connections in a :class:`.Pool`. @@ -89,7 +90,6 @@ class PoolListener(object): if hasattr(listener, 'checkin'): event.listen(self, 'checkin', listener.checkin) - def connect(self, dbapi_con, con_record): """Called once for each new DB-API connection or Pool's ``creator()``. @@ -148,6 +148,7 @@ class PoolListener(object): """ + class ConnectionProxy(object): """Allows interception of statement execution by Connections. @@ -161,11 +162,13 @@ class ConnectionProxy(object): cursor level executions, e.g.:: class MyProxy(ConnectionProxy): - def execute(self, conn, execute, clauseelement, *multiparams, **params): + def execute(self, conn, execute, clauseelement, + *multiparams, **params): print "compiled statement:", clauseelement return execute(clauseelement, *multiparams, **params) - def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): + def cursor_execute(self, execute, cursor, statement, + parameters, context, executemany): print "raw statement:", statement return execute(cursor, statement, parameters, context) @@ -195,7 +198,7 @@ class ConnectionProxy(object): event.listen(self, 'before_execute', adapt_execute) def adapt_cursor_execute(conn, cursor, statement, - parameters,context, executemany, ): + parameters, context, executemany): def execute_wrapper( cursor, @@ -245,14 +248,13 @@ class ConnectionProxy(object): event.listen(self, 'commit_twophase', adapt_listener(listener.commit_twophase)) - def execute(self, conn, execute, clauseelement, *multiparams, **params): """Intercept high level execute() events.""" - return execute(clauseelement, *multiparams, **params) - def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): + def cursor_execute(self, execute, cursor, statement, parameters, + context, executemany): """Intercept low-level cursor execute() events.""" return execute(cursor, statement, parameters, context) @@ -306,4 +308,3 @@ class ConnectionProxy(object): """Intercept commit_twophase() events.""" return commit_twophase(xid, is_prepared) - diff --git a/lib/sqlalchemy/log.py b/lib/sqlalchemy/log.py index 5370c64313..463ca972f1 100644 --- a/lib/sqlalchemy/log.py +++ b/lib/sqlalchemy/log.py @@ -19,7 +19,6 @@ instance only. import logging import sys -from . import util # set initial level to WARN. This so that # log statements don't occur in the absense of explicit @@ -28,13 +27,17 @@ rootlogger = logging.getLogger('sqlalchemy') if rootlogger.level == logging.NOTSET: rootlogger.setLevel(logging.WARN) + def _add_default_handler(logger): handler = logging.StreamHandler(sys.stdout) handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s %(name)s %(message)s')) logger.addHandler(handler) + _logged_classes = set() + + def class_logger(cls, enable=False): logger = logging.getLogger(cls.__module__ + "." + cls.__name__) if enable == 'debug': @@ -56,6 +59,7 @@ class Identified(object): def _should_log_info(self): return self.logger.isEnabledFor(logging.INFO) + class InstanceLogger(object): """A logger adapter (wrapper) for :class:`.Identified` subclasses. @@ -167,6 +171,7 @@ class InstanceLogger(object): level = self.logger.getEffectiveLevel() return level + def instance_logger(instance, echoflag=None): """create a logger for an instance that implements :class:`.Identified`.""" @@ -191,6 +196,7 @@ def instance_logger(instance, echoflag=None): instance.logger = logger + class echo_property(object): __doc__ = """\ When ``True``, enable log output for this element. diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index caef60c2ff..5518b1e222 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -16,14 +16,18 @@ regular DB-API connect() methods to be transparently managed by a SQLAlchemy connection pool. """ -import weakref, time, traceback +import time +import traceback +import weakref from . import exc, log, event, events, interfaces, util from .util import queue as sqla_queue from .util import threading, memoized_property, \ chop_traceback + proxies = {} + def manage(module, **params): """Return a proxy for a DB-API module that automatically pools connections. @@ -46,6 +50,7 @@ def manage(module, **params): except KeyError: return proxies.setdefault(module, _DBProxy(module, **params)) + def clear_managers(): """Remove all current DB-API 2.0 managers. @@ -159,7 +164,8 @@ class Pool(log.Identified): dispatch = event.dispatcher(events.PoolEvents) - @util.deprecated(2.7, "Pool.add_listener is deprecated. Use event.listen()") + @util.deprecated( + 2.7, "Pool.add_listener is deprecated. Use event.listen()") def add_listener(self, listener): """Add a :class:`.PoolListener`-like object to this pool. @@ -384,8 +390,10 @@ def _finalize_fairy(connection, connection_record, pool, ref, echo): pool.dispatch.checkin(connection, connection_record) pool._return_conn(connection_record) + _refs = set() + class _ConnectionFairy(object): """Proxies a DB-API connection and provides return-on-dereference support.""" @@ -515,6 +523,7 @@ class _ConnectionFairy(object): self.connection = None self._connection_record = None + class SingletonThreadPool(Pool): """A Pool that maintains one connection per thread. @@ -590,12 +599,16 @@ class SingletonThreadPool(Pool): self._cleanup() return c + class DummyLock(object): + def acquire(self, wait=True): return True + def release(self): pass + class QueuePool(Pool): """A :class:`.Pool` that imposes a limit on the number of open connections. @@ -794,6 +807,7 @@ class QueuePool(Pool): def checkedout(self): return self._pool.maxsize - self._pool.qsize() + self._overflow + class NullPool(Pool): """A Pool which does not pool connections. @@ -878,9 +892,10 @@ class StaticPool(Pool): def _do_get(self): return self.connection + class AssertionPool(Pool): - """A :class:`.Pool` that allows at most one checked out connection at any given - time. + """A :class:`.Pool` that allows at most one checked out connection at + any given time. This will raise an exception if more than one connection is checked out at a time. Useful for debugging code that is using more connections @@ -936,6 +951,7 @@ class AssertionPool(Pool): self._checkout_traceback = traceback.format_stack() return self._conn + class _DBProxy(object): """Layers connection pooling behavior on top of a standard DB-API module. diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index ddca43a6c6..ddea44dab6 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -16,11 +16,13 @@ import codecs import re import datetime + def str_to_datetime_processor_factory(regexp, type_): rmatch = regexp.match # Even on python2.6 datetime.strptime is both slower than this code # and it does not support microseconds. has_named_groups = bool(regexp.groupindex) + def process(value): if value is None: return None @@ -42,12 +44,14 @@ def str_to_datetime_processor_factory(regexp, type_): return type_(*map(int, m.groups(0))) return process + def boolean_to_int(value): if value is None: return None else: return int(value) + def py_fallback(): def to_unicode_processor_factory(encoding, errors=None): decoder = codecs.getdecoder(encoding) @@ -125,4 +129,3 @@ try: except ImportError: globals().update(py_fallback()) -