m M5?c@sdZdkTdklZdkZdkZdkZdkZdkZdkZdk Z dZ dZ dZ ei eidZdZd Zd d jZd d jZeed Zd ZeZddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd d d d d fZeeeidZed eeeiieiiei dZ!dfdYZ"e"Z#e i$dZ%e i$dZ&ei'e%e&dZ(eedZ)edZ*ddZ+dZ,eedZ-dZ.dZ/dZ0d ei1dZ2dZ3d Z4eed!Z5eed"Z6ed#Z7d$Z8d%Z9d&ei:d'Z;ee;d(Z<dd)Z=d*Z>d+fd,YZ?d d-Z@d.ZAd d/ZBd0ZChZDeeDeieiEd1ZFd2ZGeHd3ZIeHd4ZJeHeKeLeMd5ZNeIeieOd6ZPeiQd7ZReiQd8ZSeiiTeiiUeiieiVd9ZWd:d;ZXd<d=d>ZYd?ZZei[d@Z\ei[dAZ]dS(Bs> Tools - Add-ons for Python written in C for performance. Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com Copyright (c) 2000-2001, eGenix.com Software GmbH; mailto:info@egenix.com See the documentation for further information on copyrights, or contact the author. All Rights Reserved. (t*(s __version__NcGsjt|djogSnttttt|||f}|i t t |dt|S(s sortedby(sequence,*indices) Returns a list representing the sequence sorted ascending by the fields pointed to by the additional arguments (indices). sequence must be at least two-dimensional, e.g. a list of tuples. iiN(i( tlentsequencetapplyttuplesttupletextracttliststindicestxtsorttmaptget(RRR ((t9/data/zmath/lib/python2.4/site-packages/mx/Tools/Tools.pytsortedbys + cGsat|djogSnt|djot||dSnttt||SdS(sF projection(sequence,*indices) Experimental function that extracts columns from tables (sequence of sequences). If only one index is given, a list of all elements in that dimension is returned. For more indices, the list will contain tuples with entries for each given dimension. iiN(RRRRRR(RR((R t projection&s cCs^|g|}t|d}||}x0t|D]"\}}||||||R;RR,((R t hexencodes  c Cs"|p|Sn|djoti}n|djoh}d} nd} g} xo|D]g}|||} || o| i | q\|dj o|i| djoq\q\d|| tabspathtisdirR,t filefiltertmatchtlevelst scanfilestlistdirtkeys( R@RARLRJRDR>RIRNRGRFRHRE((R RMs0    # (tDictScancBs8tZdZdZdZedZedZRS(s Forward iterator for Python dictionaries. Note that no precaution is taken to insure that the dictionary is not modified in between calls to the __getitem__ method. It is the user's responsibility to ensure that the dictionary is neither modified, nor changed in size, since this would result in skipping entries or double occurance of items in the scan. The iterator inherits all methods from the underlying dictionary for convenience. cCs||_d|_dS(Ni(t dictionarytselftposition(RRRQ((R t__init__s cCs d|_dS(s6 Resets the iterator to its initial position. iN(RRRS(RR((R tresetscCs+||i|i\}}|_||fS(sn "for x in iterator" interface. Note: for loops are cancelled by raising an IndexError. N(tdictscanRRRQRStktv(RRtindexRVRWRX((R Rs!cCs||i|S(sC Inherit all other methods from the underlying dictionary. N(tgetattrRRRQR'(RRR'RZ((R t __getattr__*s( t__name__t __module__t__doc__RTRURVRRZR[(((R RPs    s\s*(-?\d+)\s*$s\s*(-?\d+)\s*-\s*(-?\d+)\s*$c Csg}|i} x||dD]}|i|}|o!| t|i dqn|i|}|o<t t|i \}}t||d|t|)qqW|S(s Converts a textual representation of integer numbers and ranges to a Python list. Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2 Values are appended to the created list in the order specified in the string. t,iiN(RR,tsplittstentrytintegerRKtmtinttgroupst integerRangeR tstarttendtrangeR( RaR`RcRgRiRhRdRRbR,((R tsrange8s  %cCsy dk}WnZtj oN|djotiidd}n|djo d}n||fSnXyZ|djo'|djo d}n|i}n|i |}|i |d}Wn|i j onX||fS(s Tries to return the fully qualified (hostname, ip) for the given hostname. If hostname is None, the default name of the local host is chosen. ip then defaults to '127.0.0.1' if not given. The function modifies the input data according to what it finds using the socket module. If that doesn't work the input data is returned unchanged. NtHOSTNAMEs 127.0.0.1i( tsockett ImportErrorthostnameR2RBtenvironR tipt gethostnamet gethostbynamet gethostbyaddrterror(RoRqRm((R t fqhostnameSs&        cCsRt|\}}ti|dd}t|djo|dfSnt|S(s Tries to determine the domain name of the given hostname and returns a tuple (host, domain). If hostname is not given, the default name of the local host is chosen as reference. t.iR9N(RvRoRqtstringR`RRR(RoRqR((R t splitdomainss R9cCs)dk}y|iSWn |SnXdS(s Return the user name of the user running the current process. If no user name can be determined, default is returned. N(tgetpasstgetusertdefault(R|Rz((R tusernames  cCs|ii}|i}|i}|i}d}x^t dt |dD]D}|t||}||joPn|t||d}qFW|S(s^ Calculate the correct line number of the traceback given in tb (even with -O on) iiiN(ttbttb_frameR6R;t co_lnotabttabtco_firstlinenotlinettb_lastitstopattaddrRjRRR=(R~R;RRRRR((R t tb_linenos     cBsBdk}e|d}|id|i|}|||UdS(s? Execute a byte compiled file filename in globals, locals Ntrbi( tmarshaltopentfilenametftreadtloadtcodetglobalsR1(RRR1RRR((R texecpycs   cCs2dk}t|d}|id|i|S(s\ Load the code from a byte compiled file filename and return it as code object. NRi(RRRRRR(RRR((R tloadpycs   cBs_dk}dk}|ii|o|i|Sn|i|}||iU||i|<|S(sA Imports a code object as module name. Returns the previously registered module in case the module name was already imported. name has to be the full package name (pkg.pkg.mod) for the module; package local names are not supported and will result in top-level modules to be created. N( timpR"tmodulesR&R't new_moduleRdRt__dict__(R'RR"RRd((R t import_codes   cCsStd|d |ddt|d}td|}td|}|S( s Format a sequence of adjancent pairs into a list of 2-tuples, e.g. 'abcdef' gives [('a','b'),('c','d'), ('e','f')] iiiicCs|dS(Ni(R (R ((R tscCs|d S(Ni(R (R ((R RsN(ii(R R2RRRtfilter(RR((R t pairs2tupless +cCsXy ddWn|dii}nX|o$x!t|D]}|i}q=Wn|S(s Return the execution frame level positions up the execution stack (defaulting to the current frame). WARNING: Storing the frame in variables will cause circular references which could result in the frames and associated objects to live forever. iiiN(texc_infoRtf_backR$tlevelttrangeR(RRRR$((R t exec_frames   cCsB|i}|idodSn|it|d|d|iiD]-}|i |pt |||||ii D]-}|i |pt ||||R tchr(R R((R tlong2strs.tModuleFinalizationcBstZdZdZRS(NcCs ||_dS(N(tfunctionRRtfini(RRR((R RTscCs|idS(N(RRR(RR((R t__del__s(R\R]RTR(((R Rs cCsny ddWnUtidi}xt|D]}|i}q2W|ii }|ii }~nX||fS(ss func_info() Returns a tuple (name,filename) giving the name of the calling function (*) and the filename where it is defined. Note that this only works if the calling function is a Python function or method (because only these create new execution frames). When called from e.g. a builtin function like map(), it will return information about the function from where the builtin function was called. (*) level indicates how far up the calling stack to look for the information. Default is one level meaning: the calling function. iiiN( R"RRR$RRRRR6tco_nameR't co_filenameR(RR'RR$R((R t func_infos     c Cst|do |i}n|i}|i}|i}t |i | }|i ot |t |i }x|i D]}yt|}Wn d}nXt |djo|d d}n||}|ddjo d}nd||f||<|d }qxWn|id @o&|id |i ||d }n|id @o&|id |i ||d }nd|ti|dfS(sfunc_sig(func) Returns the signature of a Python function/method as string. Keyword initializers are also shown using repr(). Representations longer than 100 bytes are truncated. XXX Anonymous argument ((a,b,c)=(1,2,3)) are not supported and probably never will be since they require disassembling the byte code which is bound to fail once byte code optimizers find their way into every Pythoneers home... tim_funcs ids...iRws(...)s%s=%siiRis**s%s(%s)s, N(R tfuncRt func_codeRRtfnamet co_argcounttcallargstlistt co_varnamestargst func_defaultsRRR|treprtrtargtco_flagsR,RxR>( RRRRRR|RRR((R tfunc_sigs:          c Cssy ddWntidi}nXxt|D]}|i}q6W|i}|i }g}|i }|id@o|d}n|id@o|d}nxx|i| D]i}yt|i|}Wn d}nXt|djo|d d}n|id ||fqW|io#d |iii|iif} nd } ~~d |ti|d | fS(sU func_call() Returns a string explaining which parameters where passed to the calling function (*) and from which file and line number it was invoked. Same comments as for func_info(). Note that line number information is only correct when running Python in non-optimized mode (i.e. without -O). Sample return string: 'test(a=1, b=2, c=3, args=()) # called from "Tools.py":353' (*) level indicates how far up the calling stack to look for the information. Default is one level meaning: the calling function. iiiiis ids...s%s=%ss# called from "%s":%is# called from s %s(%s) %ss, N(R"RRR$RRRRR6RRRRRRRRRXRR%RRR,Rtf_linenotwhereRxR>( RRRR$RXRRRRR((R t func_call<s<        #cCsy ddWntidii}nX|i}|i}x8|i D]*\}}|i |p|||(R RRR>RRRR,((R Rs     cCs$dkl}|i||dS(si Return the modification date/time as DateTime instance. Needs mxDateTime to be installed. (sDateTimeiN(tmxtDateTimeRtstatR(RRR((R tfiledates cCs||dS(s# Return the file size in bytes iN(RR(RR((R tfilesizescCs\y||}Wntj onXy||}Wntj onX|||S(s Return the corresponding absolute path for path. path is expanded in the usual shell ways before joining it with the current working directory. N(t expandvarsRRt expanduserR>RC(RRRR>RC((R RH s s c Csydkl}||}|}|i}|i}||}x,|o$||||||}qCW|i S(s{ Break data into multiple lines at column. Uses linebreak as end-of-line indicator (defaults to LF). (sStringION( t cStringIOtStringIOR:tinfiletoutfileRtwritetcolumntchunkt linebreaktgetvalue( R:RRRRRRRR((R t_addlinebreaks!s        sic Csdkl}dkl}||} |}| i}|i}|d|d@|f|d}x(|o ||||d}qhW|d|i S(N(sStringIO(sb2a_uus begin %o %s ii-s end ( RRtbinasciitb2a_uuR:RRRRtmodeRRR( R:RR RRRRRR R((R t _uu_encode4s        c Cs6dkl}dkl}|t} |}| i}|i}x@|}dG|G|p t dn|d djoPqDqDWx|}| p |djoPny||}WnLti j o=}t|d d d @d dd }||| }nX||qW|p t dn|iS(N(sStringIO(sa2b_uuis...s"Missing "begin" line in input dataitbeginsend ii i?iisTruncated input data(RRR ta2b_uutinputRRtreadlineRRat ValueErrorR:tErrorRXR=tnbytesR( R:RRRRRaRRXRR((R t _uu_decodeEs8          $ c Csk||}|djodk}|i|Sn5|djp |djoHdkl}dk}||}|djot |dSn|Sn|djodk }t i dt d @tftid }x=t|d jo)t i |i|tid }qWt i d |i|Sn5y|i|SWn tj otd |nXdS(sm Encode data using the given encoding. Possible values for encoding include: 'base64' - BASE 64 encoding 'hex' - HEX encoding (no line breaks) 'hexlines' - HEX encoding (with CR line breaks) In Python 2.0 and up, encoding may also be an encoding supported natively by Python via the codec registry. tbase64Nthexthexlines(sstr2hexiHtuus begin %o %s ii-is end sunknown encoding "%s"(tlowertencodingRt encodestringR:t mx.TextToolststr2hexRtresultR R tout_fileRR R'tin_fileRRRR tencodeRR( R:RRRRRR RR((R t encodedatajs4          cCs||}|djodk}|i|Sn|djp |djo<dkl}ti|}ti |d}||SnOydk l }Wn t j otd|nX||d }||SdS( sm Decode data using the given encoding. Possible values for encoding include: 'base64' - BASE 64 encoding 'hex' - HEX encoding (no line breaks) 'hexlines' - HEX encoding (with CR line breaks) In Python 2.0 and up, encoding may also be an encoding supported natively by Python via the codec registry. RNRR(shex2strR9(slookupsunknown encoding "%s"i(RRRt decodestringR:Rthex2strRxR`RR>tcodecstlookupRnRtdecode(R:RRRRR%R(R'((R t decodedatas     (^R^tmxToolst __version__toperatorttypesRxRR"RBtreRRRRRR!R+R/tTruetFalseR2R3R5R8ttruthtnonzerot_hexcodeR=R>R?RRIRNRMRPt DictItemstcompilet _integerREt_integerRangeRER`RkRvRyR}RRRRRRRRRRRRRRRRRRRRRRRRt_basemethod_cacheRRRRR<tcode64R RRjtcode256RRRRRRRRCRHR R RRR#R)(CR R7RRkRR+RRR6R5RR<RMRR,RRRRRRR2RvRRRRR3R/R.RRRR+R/RR}R9RxR RHR)R8R"RR#RPR8R-RRR0RRRR?R!RRR:R4RRRRBRRy((R t?s ?         -50          ?   - 1 N    *  %1