ϪfPPdZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z ddl mZmZddlmZddlmZeej,dZd Zd3d Zd4d Zd5d Zd ZdZeZd3dZGdde Z!Gdde"Z#Gdde#Z$Gdde#Z%dZ&dZ'dZ(dee)de*fdZ+dZ,dZ-d eej\ee*fd!e)de*fd"Z/d#Z0d!e)de*fd$Z1Gd%d&Z2d'Z3d(Z4d3d)Z5d3d*Z6d+Z7d,Z8d-Z9d.Z:d/Z;e8dddddfd0Z **rc< t|S#t$r|cYSwxYw)ak Try to import a module given its name, returning C{default} value if C{ImportError} is raised during import. @param name: Module name as it would have been passed to C{import}. @type name: C{str}. @param default: Value returned in case C{ImportError} is raised while importing the module. @return: Module or default value. )r6 ImportError)r!defaults r requireModuler@s&4  s ceZdZdZy)_NoModuleFoundz2 No module was found because none exists. N__name__ __module__ __qualname____doc__rrrBrBrrBceZdZdZy) InvalidNamezG The given name is not a dot-separated list of Python objects. NrCrHrrrKrKrIrrKceZdZdZy)ModuleNotFoundz_ The module associated with the given name doesn't exist and it can't be imported. NrCrHrrrMrMrrMceZdZdZy)ObjectNotFoundz_ The object associated with the given name doesn't exist and it can't be imported. NrCrHrrrPrPrNrrPc t|S#t$rctj\}}}|r>|jj d}||k(r|j ||j}|r>twxYw)a 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). @param importName: The name of the module to import. @type importName: C{str} @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. rD) r0r>sysexc_infotb_frame f_globalswith_tracebacktb_nextrB) importNameexcTypeexcValue excTracebackexecNames r_importAndCheckStackr]s *%% *-,,.'<#,,66zBH:%--l;;'//L  s A A9. A9c|s td|jd}d|vrtd|d}|dd}|sM|rdj|} t|}n*t |dk(rtd|t|d|sM|}|ddD]}t||}|S#t$r|j Y8wxYw) a 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'. zEmpty module namer/r zMname must be a string giving a '.'-separated list of Python identifiers, not NrzNo module named z does not name an object) rKr1r9r]rBpoprrMrPr,)r!namestopLevelPackage moduleNames trialnamer'ns rnamedAnyres8 -.. JJsOE  U{%) ,  O(K -I ""6y"A5zQ$'7x%@AA$x/G%HII C 12Yc1o J" "! "s B!!B=<B=ct|trd}nd}tjj |}tjj |}|s"tjj |dd}t tjj|d} tjj|}tjjtjj||rBdjt tjj |t |}n |S)aY Convert a name in the filesystem to the name of the Python module it is. This is aggressive about getting a module name back from a file; it will always return a string. Aggressive 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. @param fn: A filesystem path to a module or package; C{bytes} on Python 2, C{bytes} or C{unicode} on Python 3. @return: A hopefully importable module name. @rtype: C{str} s __init__.pyz __init__.pyNr8rz{}.{}) isinstancebytesospathabspathbasenamersplitextdirnameexistsr9format)fninitPyfullNamer modNames rfilenameToModuleNameru9s "ewwr"H 77  B D ww3B(277++D1!45G 77??8, 77>>"'',,x8 9nnRWW--h78W%G  N rclazzreturnc:|jdz|jzS)z- Return full import path of a class. r/)rErD)rvs rqualry_s   c !ENN 22rcP |jS#t$rt|cYSwxYwN)r* BaseExceptionr)xs r_determineClassr~fs){{ Aws %%ct|} |jS#t$r. t|cYS#t$rdt |zcYcYSwxYwwxYw)Nz)r~rDr|strid)r}cs r_determineClassNamermsVA4zz 4 4q6M 4+be3 3 44s)  A 0AA A A  A formatteroct}tj|t|}|j }dj |t ||j|S)a} Helper function for L{safe_repr} and L{safe_str}. Called when C{repr} or C{str} fail. Returns a string containing info about C{o} and the latest exception. @param formatter: C{str} or C{repr}. @type formatter: C{type} @param o: Any object. @rtype: C{str} @return: A string containing information about C{o} and the raised exception. )filez*<{} instance at 0x{:x} with {} error: {}>)r traceback print_excrgetvaluerprrD)rrio classNametbValues r _safeFormatrxsW B R #A&IkkmG 8 ? ? 1  rcX t|S#t$rtt|cYSwxYw)z Returns a string representation of an object, or a string containing a traceback, if that object's __repr__ raised an exception. @param o: Any object. @rtype: C{str} )reprr|rrs r safe_reprrs,$Aw $4##$s ))ct|tr |jdS t |S#t$rYwxYw#t$rt t|cYSwxYw)z Returns a string representation of an object, or a string containing a traceback, if that object's __str__ raised an exception. @param o: Any object. zutf-8)rgrhdecoder|rrrs rsafe_strrsa!U 88G$ $#1v     #3""#s/ > ;;AAceZdZdZdZdZy) QueueMethodz6 I represent a method that doesn't exist yet. c ||_||_yr{)r!calls)selfr!rs r__init__zQueueMethod.__init__s  rcR|jj|j|fyr{)rappendr!)rargss r__call__zQueueMethod.__call__s 499d+,rN)rDrErFrGrrrHrrrrs-rrcttj||jdz|jz}t ||urt d|d|d|S)Nr/zCouldn't find z as )rpickle whichmodulerDr< Exception)funcqualNames r fullFuncNamersW6%%dDMM:;cADMMQH8D(.d8*A>?? Orct|S)z3 Return the class or type of object 'obj'. r)r's rgetClassrs 9rc|jD]}t||||||jvr,|j|jj |iyy)a Accumulate all attributes of a given name in a class hierarchy 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 hierarchy wins. (XXX: If "highest" 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)raccumulateClassDictupdaterget)rattradictrr s rrrs\>""/D$./I););; X&&**445>> class Holder: ... def __init__(self, x): ... self.x = x ... >>> start = Holder({"irrelevant": "ignore", ... "relevant": [7, 1, 3, 5, 7]}) >>> for path in objgrep(start, 7): ... print("start" + path) start.x['relevant'][0] start.x['relevant'][4] This can be useful, for example, when debugging stateful graphs of objects attached to a socket, trying to figure out where a particular connection is attached. @param start: The object to start looking at. @param goal: The object to search for. @param eq: A 2-argument predicate which takes an object found by traversing references starting at C{start}, as well as C{goal}, and returns a boolean. @param path: The prefix of the path to include in every return value; empty by default. @param paths: The result object to append values to; a list of strings. @param seen: A dictionary mapping ints (object IDs) to objects already seen. @param showUnknowns: if true, print a message to C{stdout} when encountering objects that C{objgrep} does not know how to traverse. @param maxDepth: The maximum number of object references to attempt traversing before giving up. If an integer, limit to that many links, if C{None}, unlimited. @return: A list of strings representing python object paths starting at C{start} and terminating at C{goal}. Nrr{}[]z .__self__z .__func__z.__self__.__class__rr/z())wrapper_descriptormethod_descriptormember_descriptorgetset_descriptorz unknown type)rrrgrrrrr tupler enumeraterr MethodType__self____func__r*hasattrrweakref ReferenceTypeintrBuiltinMethodType RegexTypefloatrrrDprint) rreqrjpathsseen showUnknownsmaxDepthrkvidx_elems rrrs| } | % T %yD 5 ?e #  q= A DEO 4x 0D%KKM DDAq AtRd1g!5!; Cd C AtRd1g!5!; Cd C DH LC ED%/ 0#E* NJC E#Jb$*s3x*?#*E M M N@ L= E5++ ,b$*<DtDb$*<DtD(($D;P4PXSWX6 L5  #NN((* 8DAq AtRa 7$ 7 82 L/ E700 1r4$;66, L+        # #   J    e  "   L  nd5k51 Lr)rKrMrPrr6r< namedClassrer@rrrr r(r%rryrrrrrrrrrrufullyQualifiedNamerr{)r )r N)?rGrirrerRrrr collectionsrrrrtypingrrtwisted.python.compatrtwisted.python.deprecater rrcompilerrr r(r%r6r<rr@rrB ValueErrorrKrMrPr]rerur+rryr~rrrrrrrrrrrrrrrr__all__removerHrrrs    .N B ('"T&(/V  + &Y * [[8=@#L3V 3345!3!3T#Y!>?Fs6 $##3#( - -"6J 8='   tn Byr