[pitivi] Add pitivi/reflect.py.
- From: Edward Hervey <edwardrv src gnome org>
- To: svn-commits-list gnome org
- Subject: [pitivi] Add pitivi/reflect.py.
- Date: Fri, 17 Apr 2009 09:35:29 -0400 (EDT)
commit c23f369bdc66d8f7980c722d1b9213f91970a25e
Author: Alessandro Decina <alessandro decina collabora co uk>
Date: Thu Mar 26 19:20:49 2009 +0100
Add pitivi/reflect.py.
The module contains bits of python reflection code stolen from
twisted.python.reflect that are used by the xml formatters. Original license is
MIT.
---
pitivi/reflect.py | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 151 insertions(+), 0 deletions(-)
diff --git a/pitivi/reflect.py b/pitivi/reflect.py
new file mode 100644
index 0000000..ec81284
--- /dev/null
+++ b/pitivi/reflect.py
@@ -0,0 +1,151 @@
+# -*- test-case-name: twisted.test.test_reflect -*-
+# Copyright (c) 2001-2008 Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+# reflection code shamelessy stolen from twisted http://twistedmatrix.com
+
+import sys
+
+def namedModule(name):
+ """Return a module given its name."""
+ topLevel = __import__(name)
+ packages = name.split(".")[1:]
+ m = topLevel
+ for p in packages:
+ m = getattr(m, p)
+ return m
+
+
+def namedObject(name):
+ """Get a fully named module-global object.
+ """
+ classSplit = name.split('.')
+ module = namedModule('.'.join(classSplit[:-1]))
+ return getattr(module, classSplit[-1])
+
+namedClass = namedObject # backwards compat
+
+class _NoModuleFound(Exception):
+ """
+ No module was found because none exists.
+ """
+
+
+class InvalidName(ValueError):
+ """
+ The given name is not a dot-separated list of Python objects.
+ """
+
+
+class ModuleNotFound(InvalidName):
+ """
+ The module associated with the given name doesn't exist and it can't be
+ imported.
+ """
+
+
+class ObjectNotFound(InvalidName):
+ """
+ The object associated with the given name doesn't exist and it can't be
+ imported.
+ """
+
+
+def _importAndCheckStack(importName):
+ """
+ Import the given name as a module, then walk the stack to determine whether
+ the failure was the module not existing, or some code in the module (for
+ example a dependent import) failing. This can be helpful to determine
+ whether any actual application code was run. For example, to distiguish
+ administrative error (entering the wrong module name), from programmer
+ error (writing buggy code in a module that fails to import).
+
+ @raise Exception: if something bad happens. This can be any type of
+ exception, since nobody knows what loading some arbitrary code might do.
+
+ @raise _NoModuleFound: if no module was found.
+ """
+ try:
+ try:
+ return __import__(importName)
+ except ImportError:
+ excType, excValue, excTraceback = sys.exc_info()
+ while excTraceback:
+ execName = excTraceback.tb_frame.f_globals["__name__"]
+ if (execName is None or # python 2.4+, post-cleanup
+ execName == importName): # python 2.3, no cleanup
+ raise excType, excValue, excTraceback
+ excTraceback = excTraceback.tb_next
+ raise _NoModuleFound()
+ except:
+ # Necessary for cleaning up modules in 2.3.
+ sys.modules.pop(importName, None)
+ raise
+
+
+
+def namedAny(name):
+ """
+ Retrieve a Python object by its fully qualified name from the global Python
+ module namespace. The first part of the name, that describes a module,
+ will be discovered and imported. Each subsequent part of the name is
+ treated as the name of an attribute of the object specified by all of the
+ name which came before it. For example, the fully-qualified name of this
+ object is 'twisted.python.reflect.namedAny'.
+
+ @type name: L{str}
+ @param name: The name of the object to return.
+
+ @raise InvalidName: If the name is an empty string, starts or ends with
+ a '.', or is otherwise syntactically incorrect.
+
+ @raise ModuleNotFound: If the name is syntactically correct but the
+ module it specifies cannot be imported because it does not appear to
+ exist.
+
+ @raise ObjectNotFound: If the name is syntactically correct, includes at
+ least one '.', but the module it specifies cannot be imported because
+ it does not appear to exist.
+
+ @raise AttributeError: If an attribute of an object along the way cannot be
+ accessed, or a module along the way is not found.
+
+ @return: the Python object identified by 'name'.
+ """
+ if not name:
+ raise InvalidName('Empty module name')
+
+ names = name.split('.')
+
+ # if the name starts or ends with a '.' or contains '..', the __import__
+ # will raise an 'Empty module name' error. This will provide a better error
+ # message.
+ if '' in names:
+ raise InvalidName(
+ "name must be a string giving a '.'-separated list of Python "
+ "identifiers, not %r" % (name,))
+
+ topLevelPackage = None
+ moduleNames = names[:]
+ while not topLevelPackage:
+ if moduleNames:
+ trialname = '.'.join(moduleNames)
+ try:
+ topLevelPackage = _importAndCheckStack(trialname)
+ except _NoModuleFound:
+ moduleNames.pop()
+ else:
+ if len(names) == 1:
+ raise ModuleNotFound("No module named %r" % (name,))
+ else:
+ raise ObjectNotFound('%r does not name an object' % (name,))
+
+ obj = topLevelPackage
+ for n in names[1:]:
+ obj = getattr(obj, n)
+
+ return obj
+
+def qual(clazz):
+ """Return full import path of a class."""
+ return clazz.__module__ + '.' + clazz.__name__
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]