Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/modules.pyc


صMc@sdZeZddlmZmZddlZddlZddl	Z	ddl
Z
ddlmZm
Z
ddlmZddlmZmZddlmZddlmZeZd	gZedkZerejd
n
ejddZd
Zdd"dYZdd#dYZ defdYZ!defdYZ"defdYZ#dd$dYZ$e$Z%dd%dYZ&ee&ej'e#dZ(dd&dYZ)e)Z*e+dZ,d Z-d!Z.dS('s
This module aims to provide a unified, object-oriented view of Python's
runtime hierarchy.

Python is a very dynamic language with wide variety of introspection utilities.
However, these utilities can be hard to use, because there is no consistent
API.  The introspection API in python is made up of attributes (__name__,
__module__, func_name, etc) on instances, modules, classes and functions which
vary between those four types, utility modules such as 'inspect' which provide
some functionality, the 'imp' module, the "compiler" module, the semantics of
PEP 302 support, and setuptools, among other things.

At the top, you have "PythonPath", an abstract representation of sys.path which
includes methods to locate top-level modules, with or without loading them.
The top-level exposed functions in this module for accessing the system path
are "walkModules", "iterModules", and "getModule".

From most to least specific, here are the objects provided::

                  PythonPath  # sys.path
                      |
                      v
                  PathEntry   # one entry on sys.path: an importer
                      |
                      v
                 PythonModule # a module or package that can be loaded
                      |
                      v
                 PythonAttribute # an attribute of a module (function or class)
                      |
                      v
                 PythonAttribute # an attribute of a function or class
                      |
                      v
                     ...

Here's an example of idiomatic usage: this is what you would do to list all of
the modules outside the standard library's python-files directory::

    import os
    stdlibdir = os.path.dirname(os.__file__)

    from twisted.python.modules import iterModules

    for modinfo in iterModules():
        if (modinfo.pathEntry.filePath.path != stdlibdir
            and not modinfo.isPackage()):
            print 'unpackaged: %s: %s' % (
                modinfo.name, modinfo.filePath.path)
i(tdirnametsplitN(t	Interfacet
implements(tregisterAdapter(tFilePathtUnlistableError(t
ZipArchive(tnamedAnys.pys.pyos.pyccCs"d|ko!d|ko!d|kS(s
    cheezy fake test for proper identifier-ness.

    @param string: a str which might or might not be a valid python identifier.

    @return: True or False
    t t.t-((tstring((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt_isPythonIdentifierQscCs*|jd}t|d}|dkS(Niit__init__(tsplitextt	splitpath(tfpathtextlesstbasend((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt_isPackagePath_st_ModuleIteratorHelpercBsPeZdZdZedZdZdZdZdZ	dZ
RS(s
    This mixin provides common behavior between python module and path entries,
    since the mechanism for searching sys.path and __path__ attributes is
    remarkably similar.
    c

csi}|jjsdSx|jD]}y|j}Wntk
rRq&nX|jx|D]x}|jd}|jt| }|t	kr(t
|sqdn|j|}|jdddkrqdn||krt
||<t|||j}||kst|Vqqd|sdt
|sd|jrNqdn|j|j}xvt	D]n}|jd|}	|	jrj||krjt
||<t||	|j}||kst|VPqjqjWqdWq&WdS(s
        Loop over the modules present below this entry or package on PYTHONPATH.

        For modules which are not packages, this will yield nothing.

        For packages and path entries, this will only yield modules one level
        down; i.e. if there is a package a.b.c, iterModules on a will only
        return a.b.  If you want to descend deeply, use walkModules.

        @return: a generator which yields PythonModule instances that describe
        modules which can be, or have been, imported.
        NiR
iR(tfilePathtexistst
_packagePathstchildrenRtsortRtbasenametlentPYTHON_EXTENSIONSR
t_subModuleNameRtTruetPythonModulet	_getEntrytAssertionErrortisdirtchild(
tselftyieldedtplaceToLookRtpotentialTopLeveltexttpotentialBasenametmodnametpmtinitpy((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytiterModulesosF







ccsB|Vx6|jD](}x|jd|D]}|Vq+WqWdS(s
        Similar to L{iterModules}, this yields self, and then every module in my
        package or entry, and every submodule in each package or entry.

        In other words, this is deep, and L{iterModules} is shallow.
        timportPackagesN(R.twalkModules(R%R/tpackagetmodule((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR0scCs|S(s
        This is a hook to provide packages with the ability to specify their names
        as a prefix to submodules here.
        ((R%tmn((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRscCs
tdS(s
        Implement in subclasses to specify where to look for modules.

        @return: iterable of FilePath-like objects.
        N(tNotImplementedError(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRscCs
tdS(s
        Implement in subclasses to specify what path entry submodules will come
        from.

        @return: a PathEntry instance.
        N(R4(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR!scCsCx0|jD]"}|j|j|kr
|Sq
Wt|dS(s(
        Retrieve a module from below this path or package.

        @param modname: a str naming a module to be loaded.  For entries, this
        is a top-level, undotted package name, and for packages it is the name
        of the module without the package prefix.  For example, if you have a
        PythonModule representing the 'twisted' package, you could use::

            twistedPackageObj['python']['modules']

        to retrieve this module.

        @raise: KeyError if the module is not found.

        @return: a PythonModule.
        N(R.tnameRtKeyError(R%R+R2((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt__getitem__scCs
tdS(sD
        Implemented to raise NotImplementedError for clarity, so that attempting to
        loop over this object won't call __getitem__.

        Note: in the future there might be some sensible default for iteration,
        like 'walkEverything', so this is deliberately untested and undefined
        behavior.
        N(R4(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt__iter__s	(t__name__t
__module__t__doc__R.tFalseR0RRR!R7R8(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRhs	9			
	tPythonAttributecBs>eZdZdZdZdZedZdZRS(sg
    I represent a function, class, or other object that is present.

    @ivar name: the fully-qualified python name of this attribute.

    @ivar onObject: a reference to a PythonModule or other PythonAttribute that
    is this attribute's logical parent.

    @ivar name: the fully qualified python name of the attribute represented by
    this class.
    cCs(||_||_||_||_dS(sJ
        Create a PythonAttribute.  This is a private constructor.  Do not construct
        me directly, use PythonModule.iterAttributes.

        @param name: the FQPN
        @param onObject: see ivar
        @param loaded: always True, for now
        @param pythonValue: the value of the attribute we're pointing to.
        N(R5tonObjectt_loadedtpythonValue(R%R5R>tloadedR@((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRs
			cCsd|jfS(NsPythonAttribute<%r>(R5(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt__repr__	scCs|jS(s	
        Return a boolean describing whether the attribute this describes has
        actually been loaded into memory by importing its module.

        Note: this currently always returns true; there is no Python parser
        support in this module yet.
        (R?(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytisLoadedscCs|jS(s
        Load the value associated with this attribute.

        @return: an arbitrary Python object, or 'default' if there is an error
        loading it.
        (R@(R%tdefault((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytloadsccsIxBtj|jD]+\}}t|jd||t|VqWdS(NR
(tinspectt
getmembersRER=R5R(R%R5tval((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytiterAttributess"(	R9R:R;RRBRCt_nothingRERI(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR=s			
	R cBseZdZdZdZdZdZdZdZe	dZ
dZd	Ze
d
ZdZdZRS(
s?
    Representation of a module which could be imported from sys.path.

    @ivar name: the fully qualified python name of this module.

    @ivar filePath: a FilePath-like object which points to the location of this
    module.

    @ivar pathEntry: a L{PathEntry} instance which this module was located
    from.
    cCsD|jdst||_||_|j|_||_dS(s
        Create a PythonModule.  Do not construct this directly, instead inspect a
        PythonPath or other PythonModule instances.

        @param name: see ivar
        @param filePath: see ivar
        @param pathEntry: see ivar
        s	.__init__N(tendswithR"R5Rtparentt
parentPatht	pathEntry(R%R5RRN((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR0s
			cCs|jS(N(RN(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR!?scCsd|jfS(sK
        Return a string representation including the module name.
        sPythonModule<%r>(R5(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRBBscCs|jjjj|jdk	S(s
        Determine if the module is loaded into sys.modules.

        @return: a boolean: true if loaded, false if not.
        N(RNt
pythonPatht
moduleDicttgetR5tNone(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRCIsccsd|jstdnxBtj|jD]+\}}t|jd||t|Vq1WdS(s 
        List all the attributes defined in this module.

        Note: Future work is planned here to make it possible to list python
        attributes on a module without loading the module by inspecting ASTs or
        bytecode, but currently any iteration of PythonModule objects insists
        they must be loaded, and will use inspect.getmodule.

        @raise NotImplementedError: if this module is not loaded.

        @return: a generator yielding PythonAttribute instances describing the
        attributes of this module.
        s6You can't load attributes from non-loaded modules yet.R
N(RCR4RFRGRER=R5R(R%R5RH((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRIRs
"cCs
t|jS(st
        Returns true if this module is also a package, and might yield something
        from iterModules.
        (RR(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt	isPackagefscCs;y|jjj|jSWn|tk	r0|SnXdS(sf
        Load this module.

        @param default: if specified, the value to return in case of an error.

        @return: a genuine python module.

        @raise: any type of exception.  Importing modules is a risky business;
        the erorrs of any code run at module scope may be raised from here, as
        well as ImportError if something bizarre happened to the system path
        between the discovery of this PythonModule object and the attempt to
        import it.  If you specify a default, the error will be swallowed
        entirely, and not logged.

        @rtype: types.ModuleType.
        N(RNROtmoduleLoaderR5RJ(R%RD((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyREmscCs#t|tstS|j|jkS(s=
        PythonModules with the same name are equal.
        (t
isinstanceR R<R5(R%tother((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt__eq__scCs#t|tstS|j|jkS(sC
        PythonModules with different names are not equal.
        (RUR RR5(R%RV((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt__ne__scCs8|r|jr|jntt|jd|S(NR/(RSREtsuperR R0(R%R/((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR0s
cCs|jd|S(sG
        submodules of this module are prefixed with our name.
        R
(R5(R%R3((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRsccs|jsdS|jr|j}t|drxo|jD]a}||jjkry|jjsnt|jVqA|j	j
j|}|jrA|VqAqAWqn|jVdS(sZ
        Yield a sequence of FilePath-like objects which represent path segments.
        Nt__path__(RSRCREthasattrRZRMtpathRR"RNROt
_smartPath(R%REtfntsmp((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRs(R9R:R;RR!RBRCRIRSRJRERWRXR<R0RR(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR #s										t	PathEntrycBs2eZdZdZdZdZdZRS(s
    I am a proxy for a single entry on sys.path.

    @ivar filePath: a FilePath-like object pointing at the filesystem location
    or archive file where this path entry is stored.

    @ivar pythonPath: a PythonPath instance.
    cCs||_||_dS(sE
        Create a PathEntry.  This is a private constructor.
        N(RRO(R%RRO((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRs	cCs|S(N((R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR!scCsd|jfS(Ns
PathEntry<%r>(R(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRBsccs|jVdS(N(R(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRs(R9R:R;RR!RBR(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR`s
			tIPathImportMappercBseZdZdZRS(sj
    This is an internal interface, used to map importers to factories for
    FilePath-like objects.
    cCsdS(s
        Return a FilePath-like object.

        @param pathLikeString: a path-like string, like one that might be
        passed to an import hook.

        @return: a L{FilePath}, or something like it (currently only a
        L{ZipPath}, but more might be added later).
        N((R%tpathLikeString((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytmapPaths(R9R:R;Rc(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRast_DefaultMapImplcBs!eZdZeedZRS(s/ Wrapper for the default importer, i.e. None.  cCs
t|S(N(R(R%tfsPathString((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRcs(R9R:R;RRaRc(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRds
t_ZipMapImplcBs*eZdZeedZdZRS(s> IPathImportMapper implementation for zipimport.ZipImporter.  cCs
||_dS(N(timporter(R%Rg((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRscCsyt|jj}t|jj}t|}||kr@|S|j|}|}x|D]}|j|}q\W|S(s
        Map the given FS path to a ZipPath, by looking at the ZipImporter's
        "archive" attribute and using it as our ZipArchive root, then walking
        down into the archive from there.

        @return: a L{zippath.ZipPath} or L{zippath.ZipArchive} instance.
        (RRgtarchiveRtsegmentsFromR$(R%RetzatmyPathtitsPathtsegstzptseg((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRcs
(R9R:R;RRaRRc(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRfs
	cCstjS(s
    Provide the default behavior of PythonPath's sys.path factory, which is to
    return the current value of sys.path.

    @return: L{sys.path}
    (tsysR\(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt_defaultSysPathFactoryst
PythonPathcBseZdZd
ejejejed
dZ	dZ
ee
ZdZ
dZdZdZdZdZed	ZRS(s!
    I represent the very top of the Python object-space, the module list in
    sys.path and the modules list in sys.modules.

    @ivar _sysPath: a sequence of strings like sys.path.  This attribute is
    read-only.

    @ivar moduleDict: a dictionary mapping string module names to module
    objects, like sys.modules.

    @ivar sysPathHooks: a list of PEP-302 path hooks, like sys.path_hooks.

    @ivar moduleLoader: a function that takes a fully-qualified python name and
    returns a module, like twisted.python.reflect.namedAny.
    csmdk	rfd}n|dkr3t}n||_|_||_||_||_||_dS(sJ
        Create a PythonPath.  You almost certainly want to use
        modules.theSystemPath, or its aliased methods, rather than creating a
        new instance yourself, though.

        All parameters are optional, and if unspecified, will use 'system'
        equivalents that makes this PythonPath like the global L{theSystemPath}
        instance.

        @param sysPath: a sys.path-like list to use for this PythonPath, to
        specify where to load modules from.

        @param moduleDict: a sys.modules-like dictionary to use for keeping
        track of what modules this PythonPath has loaded.

        @param sysPathHooks: sys.path_hooks-like list of PEP-302 path hooks to
        be used for this PythonPath, to determie which importers should be
        used.

        @param importerCache: a sys.path_importer_cache-like list of PEP-302
        importers.  This will be used in conjunction with the given
        sysPathHooks.

        @param moduleLoader: a module loader function which takes a string and
        returns a module.  That is to say, it is like L{namedAny} - *not* like
        L{__import__}.

        @param sysPathFactory: a 0-argument callable which returns the current
        value of a sys.path-like list of strings.  Specify either this, or
        sysPath, not both.  This alternative interface is provided because the
        way the Python import mechanism works, you can re-bind the 'sys.path'
        name and that is what is used for current imports, so it must be a
        factory rather than a value to deal with modification by rebinding
        rather than modification by mutation.  Note: it is not recommended to
        rebind sys.path.  Although this mechanism can deal with that, it is a
        subtle point which some tools that it is easy for tools which interact
        with sys.path to miss.
        csS(N(((tsysPath(s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt<lambda>NsN(RRRqt_sysPathFactoryt_sysPathRPtsysPathHookst
importerCacheRT(R%RsRPRwRxRTtsysPathFactory((Rss:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR s-						cCs
|jS(sL
        Retrieve the current value of the module search path list.
        (Ru(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt_getSysPathYscCs|}x9d|jkrA|jdj|jjdd }q	Wtt|jrott|j}nt|j}||jkrt	j
d||jfddn|S(sl
        Determine where a given Python module object came from by looking at path
        entries.
        R
isc%s (for module %s) not in path importer cache (PEP 302 violation - check your local configuration).t
stackleveli(R9RPtjoinRRRt__file__RRxtwarningstwarn(R%tmodobjt
topPackageObjtrval((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt_findEntryPathStringas
cCs|jj|t}|tkrqx5|jD]*}y||}Wq+tk
rTq+Xq+W|tkrqd}qqnt|tj|S(s
        Given a path entry from sys.path which may refer to an importer,
        return the appropriate FilePath-like instance.

        @param pathName: a str describing the path.

        @return: a FilePath-like object.
        N(	RxRQRJRwtImportErrorRRRat_theDefaultMapperRc(R%tpathNametimportrthook((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR]s	
ccs5x.|jD]#}|j|}t||Vq
WdS(sm
        Iterate the entries on my sysPath.

        @return: a generator yielding PathEntry objects
        N(RsR]R`(R%Rtfp((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pytiterEntriesscCs|jj|}|dk	rat|j|j||}|j|j}t|||Sd|kr|}x!|jdD]}||}qW|Sx'|j	D]}|j
|kr|SqWt|dS(s
        Get a python module by its given fully-qualified name.

        @param modname: The fully-qualified Python module name to load.

        @type modname: C{str}

        @return: an object representing the module identified by C{modname}

        @rtype: L{PythonModule}

        @raise KeyError: if the module name is not a valid module name, or no
            such module can be identified as loadable.
        R
N(RPRQRRR`R]RR}R RR.R5R6(R%R+tmoduleObjecttpetmptpkgR5R2((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR7s"	cCsd|j|jfS(sO
        Display my sysPath and moduleDict in a string representation.
        sPythonPath(%r,%r)(RsRP(R%((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRBsccs7x0|jD]"}x|jD]}|Vq Wq
WdS(s<
        Yield all top-level modules on my sysPath.
        N(RR.(R%tentryR2((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR.sccs=x6|jD](}x|jdtD]}|Vq&Wq
WdS(s
        Similar to L{iterModules}, this yields every module on the path, then every
        submodule in each package or entry.
        R/N(R.R0R<(R%R/R1R2((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR0sN(R9R:R;RRRptmodulest
path_hookstpath_importer_cacheRRRztpropertyRsRR]RR7RBR.R<R0(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyRrs 3		 			(		cCstjd|S(s}
    Deeply iterate all modules on the global python path.

    @param importPackages: Import packages as they are seen.
    R/(t
theSystemPathR0(R/((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR0scCs
tjS(s
    Iterate all modules and top-level packages on the global Python path, but
    do not descend into packages.

    @param importPackages: Import packages as they are seen.
    (RR.(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyR.scCst|S(s1
    Retrieve a module from the system path.
    (R(t
moduleName((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt	getModules((((((/R;ttypet
__metaclass__tos.pathRRRRpt	zipimportRFR~tzope.interfaceRRttwisted.python.componentsRttwisted.python.filepathRRttwisted.python.zippathRttwisted.python.reflectRtobjectRJRRRtOPTIMIZED_MODEtappendR
RRR=R R`RaRdRRftzipimporterRqRrRR<R0R.R(((s:/usr/lib/python2.7/dist-packages/twisted/python/modules.pyt<module>6sD		
			5