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/reflect.pyc


ΔMc)@sAdZddlZddlZddlZddlZddlZddlZddlZddlZyddl	m
Z
Wnek
reZ
nXe
ejdZyddlmZWn!ek
rddlmZnXddlmZddlmZddlmZdd	lmZd
dedYZde
fd
YZdefdYZddfdYZeZdefdYZ ddgdYZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+d Z,e,Z-d!e.fd"YZ/d#e0fd$YZ1d%e1fd&YZ2d'e1fd(YZ3d)Z4d*Z5d+Z6eed,d-dde6Z6d.Z7d/Z8d0Z9d1Z:d2Z;eed,d3ddd4dd5Z=eed,d3ddd4dd6Z>dd7Z?d8Z@dd9ZAdd:ZBddd;ZCdd<ZDdd=ZEd>ZFd?ZGd@ZHdAZIdBZJeGddddddCZKdDZLd#d%d'dEdFdGd
ddddddHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcddg)ZMdS(hst
Standardized versions of various cool and/or strange things that you can do
with Python's reflection capabilities.
iN(tdequet(tStringIO(t
unsignedID(t
deprecated(t_fullyQualifiedName(tVersiontSettablecBs eZdZdZdZRS(sQ
    A mixin class for syntactic sugar.  Lets you assign attributes by
    calling with keyword arguments; for example, C{x(a=b,c=d,y=z)} is the
    same as C{x.a=b;x.c=d;x.y=z}.  The most useful place for this is
    where you don't want to name a variable, but you do want to set
    some attributes; for example, C{X()(y=z,a=b)}.
    cKs||dS(N((tselftkw((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt__init__/scKs1x*|jD]\}}t|||q
W|S(N(titemstsetattr(RR	tkeytval((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt__call__2s(t__name__t
__module__t__doc__R
R(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR's	tAccessorTypecBseZdZdZRS(s;Metaclass that generates properties automatically.

    This is for Python 2.2 and up.

    Using this metaclass for your class will give you explicit accessor
    methods; a method called set_foo, will automatically create a property
    'foo' that uses set_foo as a setter method. Same for get_foo and del_foo.

    Note that this will only work on methods that are present on class
    creation. If you add methods after the class is defined they will not
    automatically become properties. Likewise, class attributes will only
    be used if they are present upon class creation, and no getter function
    was set - if a getter is present, the class attribute will be ignored.

    This is a 2.2-only alternative to the Accessor mixin - just set in your
    class definition::

        __metaclass__ = AccessorType

    c
	Csktj||||i}dddg}xs|jD]e}t||}xMtdD]?}|j||rZ||j|ddddg|<qZqZWq8Wx|jD]\}\}	}
}|	dkrt	||rt||}||d}	q|d}	n|
dkr)|d}
n|dkrD|d	}nt
||t|	|
|d
qWdS(Ntget_tset_tdel_iicSs"||jkr|j|S|SdS(N(t__dict__(tthistvaluetname((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytgetter]scSs.||jkr|j|Std|dS(Nsno such attribute %r(RtAttributeError(RR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRcscSs||j|<dS(N(R(RRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytsetteriscSs|j|=dS(N(R(RR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytdelerlsR(ttypeR
tkeystgetattrtranget
startswitht
setdefaulttNoneRthasattrRtproperty(
RRtbasestdt	accessorstprefixstktvtiRRRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR
Ns&."(RRRR
(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR8stPropertyAccessorcBs&eZdZeZdZdZRS(sA mixin class for Python 2.2 that uses AccessorType.

    This provides compatability with the pre-2.2 Accessor mixin, up
    to a point.

    Extending this class will give you explicit accessor methods; a
    method called set_foo, for example, is the same as an if statement
    in __setattr__ looking for 'foo'.  Same for get_foo and del_foo.

    There are also reallyDel and reallySet methods, so you can
    override specifics in subclasses without clobbering __setattr__
    and __getattr__, or using non-2.1 compatible code.

    There is are incompatibilities with Accessor - accessor
    methods added after class creation will *not* be detected. OTOH,
    this method is probably way faster.

    In addition, class attributes will only be used if no getter
    was defined, and instance attributes will not override getter methods
    whereas in original Accessor the class attribute or instance attribute
    would override the getter method.
    cCs||j|<dS(N(R(RR,R-((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt	reallySetscCs|j|=dS(N(R(RR,((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt	reallyDels(RRRRt
__metaclass__R0R1(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR/qs	tAccessorcBs;eZdZdZdZdZdZdZRS(s
    Extending this class will give you explicit accessor methods; a
    method called C{set_foo}, for example, is the same as an if statement
    in L{__setattr__} looking for C{'foo'}.  Same for C{get_foo} and
    C{del_foo}.  There are also L{reallyDel} and L{reallySet} methods,
    so you can override specifics in subclasses without clobbering
    L{__setattr__} and L{__getattr__}.

    This implementation is for Python 2.1.
    cCsCd|}t|j|r/t|||S|j||dS(Nsset_%s(R&t	__class__R!R0(RR,R-tkstring((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt__setattr__s
cCsOd|}t|j|r,t||Stdt|j|fdS(Nsget_%ss#%s instance has no accessor for: %s(R&R4R!Rtqual(RR,R5((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt__getattr__s
cCsAd|}t|j|r0t||dS|j|dS(Nsdel_%s(R&R4R!R1(RR,R5((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt__delattr__s

cCs=|dkr,|jj|jj|n
||j|<dS(s
        *actually* set self.k to v without incurring side-effects.
        This is a hook to be overridden by subclasses.
        RN(Rtcleartupdate(RR,R-((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR0s
cCs|j|=dS(s
        *actually* del self.k without incurring side-effects.  This is a
        hook to be overridden by subclasses.
        N(R(RR,((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR1s(RRRR6R8R9R0R1(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR3s
				tSummercBseZdZdZRS(s
    Extend from this class to get the capability to maintain 'related
    sums'.  Have a tuple in your class like the following::

        sums=(('amount','credit','credit_total'),
              ('amount','debit','debit_total'))

    and the 'credit_total' member of the 'credit' member of self will
    always be incremented when the 'amount' member of self is
    incremented, similiarly for the debit versions.
    c
CsqxW|jD]L}|d}|d}|d}||kryt||}Wn
d}nX||}t||rVt||}	|	dk	ryt|	|}
Wn
d}
nXt|	||
|qqVq
||kr
t||rVt||}t||dt||}tj|||t|||tj|||qVq
q
Wtj|||dS(sThis method does the work.iiigN(tsumsR!R&R%RR3R0(
RR,R-tsumtattrtobjtobjattrtoldvaltdifftobt	oldobjvaltxty((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR0s6





(RRRR0(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR<stQueueMethodcBs eZdZdZdZRS(s- I represent a method that doesn't exist yet.cCs||_||_dS(N(Rtcalls(RRRI((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR
s	cGs|jj|j|fdS(N(RItappendR(Rtargs((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRs(RRRR
R(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRHs	c
Cstjdtdd|j}|j}|j}|j| }|j}g}|jd||f|r|t	|}|jd||jd|| |jdxDt
||D]/}||}	|jd||	||fqWn|S(	sA
    this is more documentation for myself than useful code.
    sH[v2.5] Use inspect.getargspec instead of twisted.python.reflect.funcinfot
stacklevelis$The function %s accepts %s argumentssIt requires %s argumentssThe arguments required are: %ssadditional arguments are:s%s which has a default of(twarningstwarntDeprecationWarningt	func_codet	func_nametco_argcounttco_varnamest
func_defaultsRJtlenR"(
tfunctiontcodeRtargctargvtdefaultstouttrequiredR.tj((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytfuncinfos(			
	

&iiicCsUttj||jd|j}t||k	rQtd||fn|S(Nt.sCouldn't find %s as %s.(tstrtpickletwhichmoduleRtnamedObjectt	Exception(tfunctqualName((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytfullFuncName!s&cCs|jd|jS(s#Return full import path of a class.R_(RR(tclazz((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyR7(scCsYt|tjks!tdt|j}t||jd}|dkrU|S|S(Nsmust be a class...(	Rttypest	ClassTypetAssertionErrortnamedModuleRR!RR%(Rhtmodulet	currclass((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt
getcurrent-s!cCs$t|dr|jSt|SdS(stReturn the class or type of object 'obj'.
    Returns sensible result for oldstyle and newstyle instances and types.R4N(R&R4R(R@((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytgetClass6scCst|tjks*t|tjkr7t||S|j}t|}t|}t||r||krwtS||_t	Snt
SdS(N(RRitInstanceTypeRjt
isinstanceR4Rot
issubclasstWAStIStISNT(tinstRhtcltcl2((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytisinstAs*
		cCsIt|}|jdd}|}x|D]}t||}q,W|S(sReturn a module given its name.R_i(t
__import__tsplitR!(RttopLeveltpackagestmtp((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRlQs
cCs9|jd}tdj|d }t||dS(s,Get a fully named module-global object.
    R_i(R|RltjoinR!(Rt
classSplitRm((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRc[st_NoModuleFoundcBseZdZRS(s2
    No module was found because none exists.
    (RRR(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRfstInvalidNamecBseZdZRS(sG
    The given name is not a dot-separated list of Python objects.
    (RRR(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRlstModuleNotFoundcBseZdZRS(s_
    The module associated with the given name doesn't exist and it can't be
    imported.
    (RRR(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRrstObjectNotFoundcBseZdZRS(s_
    The object associated with the given name doesn't exist and it can't be
    imported.
    (RRR(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRyscCsyyt|SWn|tk
rtj\}}}xJ|r|jjd}|dksg||krv|||n|j}q9WtnXWntj	j
|dnXdS(s
    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.
    RN(R{tImportErrortsystexc_infottb_framet	f_globalsR%ttb_nextRtmodulestpop(t
importNametexcTypetexcValuetexcTracebacktexecName((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt_importAndCheckStacks
	
cCs|stdn|jd}d|krFtd|fnd}|}x|s|rdj|}yt|}Wqtk
r|jqXqVt|dkrtd|fqVt	d|fqVW|}x!|dD]}t
||}qW|S(	s
    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'.
    sEmpty module nameR_RsOname must be a string giving a '.'-separated list of Python identifiers, not %risNo module named %rs%r does not name an objectN(RR|R%RRRRRURRR!(RtnamesttopLevelPackagetmoduleNamest	trialnameR@tn((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytnamedAnys.	
c
Js|jds||d<n||}d|}e||d}ej}d}|j|sej|}|||<d|_n||}|jd7_de|j}	|d|	}
ej|
}|ej|
<e||	||j	}|||U||S(	sfmacro(name, source, **identifiers)

    This allows you to create macro-like behaviors in python.
    Rs<%s (macro)>texecstwisted.python.reflect.macrosiitmacro_R_(
thas_keytcompileRRRit
ModuleTypetcountR`RR(
Rtfilenametsourcetidentifierst	codeplaceRWtsmttprmtmacrost	macronamettprmmtmymodtdict((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytmacros*


	


		
tTwistedicCs y|jSWnt|SXdS(N(R4R(RF((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt_determineClass	scCsHt|}y|jSWn*yt|SWqDdt|SXnXdS(Ns<BROKEN CLASS AT 0x%x>(RRR`R(RFtc((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt_determineClassNamescCsgy||SWnRt}tjd|t|}|j}d|t||j|fSXdS(s;
    Helper function for L{safe_repr} and L{safe_str}.
    tfiles(<%s instance at 0x%x with %s error:
 %s>N(Rt	tracebackt	print_excRtgetvalueRR(t	formattertotiot	classNamettbValue((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt_safeFormats	cCs
tt|S(s
    safe_repr(anything) -> string

    Returns a string representation of an object, or a string containing a
    traceback, if that object's __repr__ raised an exception.
    (Rtrepr(R((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt	safe_repr-scCs
tt|S(s
    safe_str(anything) -> string

    Returns a string representation of an object, or a string containing a
    traceback, if that object's __str__ raised an exception.
    (RR`(R((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytsafe_str8sisinspect.getmrocCsg}t||||S(sallYourBase(classObj, baseClass=None) -> list of all base
    classes that are subclasses of baseClass, unless it is None,
    in which case all bases will be added.
    (t_accumulateBases(tclassObjt	baseClasstl((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytallYourBaseEscCst|||dS(N(R(RRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytaccumulateBasesPscCsSxL|jD]A}|dks+t||r;|j|nt|||q
WdS(N(t	__bases__R%RsRJR(RRRtbase((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRUscCs i}t||||jS(sAA list of method names with a given prefix in a given class.
    (taddMethodNamesToDictR (Rtprefixtdct((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytprefixedMethodNames\scCsx$|jD]}t||||q
W|dksB||jkrxt|jjD]`\}}|t|}t|tjkrR|t| |krRt|rRd||<qRqRWndS(s
    addMethodNamesToDict(classObj, dict, prefix, baseClass=None) -> dict
    this goes through 'classObj' (and its bases) and puts method names
    starting with 'prefix' in 'dict' with a value of 1. if baseClass isn't
    None, methods will only be added if classObj is-a baseClass

    If the class in question has the methods 'prefix_methodname' and
    'prefix_methodname2', the resulting dict should look something like:
    {"methodname": 1, "methodname2": 1}.
    iN(	RRR%RRRURRitFunctionType(RRRRRRtmethodtoptName((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRdscCs i}t||||jS(s?A list of methods with a given prefix on a given instance.
    (taccumulateMethodstvalues(R@RR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytprefixedMethods{scCs|s|j}nx$|jD]}t||||qWxz|jjD]i\}}|t|}t|tjkrI|t| |krIt|rIt	||||<qIqIWdS(saccumulateMethods(instance, dict, prefix)
    I recurse through the bases of instance.__class__, and add methods
    beginning with 'prefix' to 'dict', in the form of
    {'methodname':*instance*method_object}.
    N(
R4RRRRRURRiRR!(R@RRtcurClassRRRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRscCsbx!|jD]}t|||q
W|dks?||jkr^|j|jj|indS(sAccumulate all attributes of a given name in a class heirarchy into a single dictionary.

    Assuming all class attributes of this name are dictionaries.
    If any of the dictionaries being accumulated have the same key, the
    one highest in the class heirarchy wins.
    (XXX: If "higest" means "closest to the starting class".)

    Ex::

    | class Soy:
    |   properties = {"taste": "bland"}
    |
    | class Plant:
    |   properties = {"colour": "green"}
    |
    | class Seaweed(Plant):
    |   pass
    |
    | class Lunch(Soy, Seaweed):
    |   properties = {"vegan": 1 }
    |
    | dct = {}
    |
    | accumulateClassDict(Lunch, "properties", dct)
    |
    | print dct

    {"taste": "bland", "colour": "green", "vegan": 1}
    N(RtaccumulateClassDictR%R;Rtget(RR?tadictRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRscCsbx!|jD]}t|||q
W|dks?||jkr^|j|jj|gndS(sAccumulate all attributes of a given name in a class heirarchy into a single list.

    Assuming all class attributes of this name are lists.
    N(RtaccumulateClassListR%textendRR(RR?tlistObjRR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRscCs
||kS(N((tatb((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytisSamescCs
||kS(N((RR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytisLikescCsttj|tdS(Nssys.modules(tobjgrepRRR(tgoal((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytmodgrepscCs1t||kp0t|tjo0|j|kS(N(RRrRiRqR4(tstartR((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytisOfTypescCst||tS(N(RR(Rtt((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt
findInstancessc
Cs}|dkrg}n|dkr*i}n|||rI|j|nt||krx|t||krxdSn|dk	r|dkrdS|d8}n||t|<t|tjrEx|jD]n\}}	t||||dt|	d||||t|	|||dt|d||||qWn4t|t	t
tfrxtt
|D];}
t||
|||dt|
d||||qpWnt|tjr9t|j|||d||||t|j|||d	||||t|j|||d
||||n@t|drxD|jjD]3\}}	t|	|||d|||||qXWt|tjryt|j|||d
||||qynt|tjrt||||d||||nqt|tjtjtjtjttjtjtj fsyt!|j"dkr]n|rydGt!|G|GHn|S(s9An insanely CPU-intensive process for finding stuff.
    Niit{t}t[t]s.im_selfs.im_funcs	.im_classRR_s
.__class__s()twrapper_descriptortmethod_descriptortmember_descriptortgetset_descriptorsunknown type(RRRR(#R%RJtidRrRitDictionaryTypeRRRtlistttupleRtxrangeRUR`t
MethodTypetim_selftim_functim_classR&RRqR4tweakreft
ReferenceTypetStringTypestIntTypeRtBuiltinMethodTypet	RegexTypet	FloatTypetNoneTypetFileTypeRR(RRteqtpathtpathstseentshowUnknownstmaxDepthR,R-tidx((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyRsT		
18<&&)+,)	cCstjj|}tjj|}|sCtjj|d }ntjj|d}xVtjj|}tjjtjj|drdtjj||f}q\Pq\|S(s
    Convert a name in the filesystem to the name of the Python module it is.

    This is agressive about getting a module name back from a file; it will
    always return a string.  Agressive means 'sometimes wrong'; it won't look
    at the Python path or try to do any error checking: don't use this method
    unless you already know that the filename you're talking about is a Python
    module.
    iis__init__.pys%s.%s(tosRtabspathtbasenametsplitexttdirnametexistsR(tfntfullNameRtmodName((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pytfilenameToModuleName
s
!RvRtRutOriginalAccessorR^RgR7RoRpRzRlRct
namedClassRRRRRRRRRRRRRRRRRRtfullyQualifiedName((((NRRRRiRaRRtreRMtcollectionsRRRRRRt	cStringIORttwisted.python.utilRttwisted.python.deprecateRRRttwisted.python.versionsRRRtobjectR/R3R
R<RHR^RvRtRuRgR7RoRpRzRlRcRRdRt
ValueErrorRRRRRRRRRRRR%RRRRRRRRRRRRRRRRt__all__(((s:/usr/lib/python2.7/dist-packages/twisted/python/reflect.pyt<module>s


9%3+									
		!	@	$!					

	$					/