Ñò PhEc @s†ddkZddkZddkZddkZddkTddkiZdfd„ƒYZdfd„ƒYZdfd„ƒYZ dS( iÿÿÿÿN(t*tObjectGeneratorcBsƒeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „Zd „ZRS(sl generates myghty object files. uses the visitor pattern combined with the Compiler's flyweight pattern to efficiently loop through a tokenized page object and produce appropriate code. an ObjectGenerator is stateful and is not threadsafe. the clone() method can be used to create copies of this object for use in multiple threads. cCs tƒ‚dS(N(tNotImplementedError(tselft block_objtcontext((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_code_blockscCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_code_single_linescCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_text_linescCs tƒ‚dS(N(R(Rtcompiled((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_component_blockscCs tƒ‚dS(N(R(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_def_blockscCs tƒ‚dS(N(R(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_method_blockscCs tƒ‚dS(N(R(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_closure_blockscCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_substitutionscCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_component_call scCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytvisit_component_content_call!scCs tƒ‚dS(N(R(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt visit_component_content_call_end"scCs tƒ‚dS(N(R(RtcompilerR tstream((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytgenerate$scKs tƒ‚dS(s‚creates a clone of this ObjectGenerator. allow the Prototype pattern to be used in creating generators for other threads.N(R(Rtparams((s1/usr/lib/python2.6/site-packages/myghty/objgen.pytclone&s(t__name__t __module__t__doc__RRRR R R R RRRRRR(((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRs            tPythonGeneratorcBs@eZd„Zd„Zd„Zd„Zd„Zd„Zd„Ze d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„Zd„Ze e d„Zd„Zde e d„Zd!d„Zd„Zd„Zd„Ze d„Zd„Zd„Zd„Z d„Z!d„Z"d „Z#RS("cKstƒS(s‰creates a clone of this ObjectGenerator. allow the Prototype pattern to be used in creating generators for use in other threads.(R(RR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR/scCs‡t|ƒ|_||_||_d|_g|_t|_d|_|i dƒ|_ |i dƒ|_ |i |ƒ|ii ƒdS(Ntiÿÿÿÿtsharedt threadonce(t PythonPrintertprinterRR t textbuffert indentstacktFalsetin_blocktlast_asserted_linetblock_has_codet uses_sharedtuses_threadoncetaccept_visitortclose(RRR R((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR4s       cCs|iƒ|i|ƒ}|iidƒ|i||ƒ|i|ƒ}x0|D](}|ii|ƒ}|ii|ƒqOW|iidƒdS(sVhandles a <%python> block, adjusting indent to match the current indent level.sBEGIN CODE BLOCKsEND CODE BLOCKN( t _flush_textt get_linesRt print_commentt_assert_line_commenttget_line_numberRtpost_process_pythontadd_normalized_block_line(RRRtlinestlinenumtline((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyREs cCsF|ip|i||ƒn|i|ii|i|ƒƒ7_dS(s¨handles regular lines of text. Text line printing is buffered so that consecutive lines of text can be combined into a single print statementN(R R-Rtpost_process_texttget_line(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRXs cCs|i||dtƒdS(Ntcontent(t_generate_component_callR"(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRascCs=|idd||ƒ|i||ƒ|i|ƒ|_dS(Ns def %s():tcomponent_content(t _print_pythonR-R.t_cc_line(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRdscCs|i||dtƒdS(NR6(R7tTrue(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRiscCs0|i|ƒ}|i||ƒ|ii|ƒod}|g}nd}d|g}|i|ƒo|id|i|ƒƒn|tjop|idƒ|i|ƒ|ijo|i d||ƒn|i dd|t i |d ƒfg||ƒn*|i d|t i |d ƒf||ƒdS( Nsm.closure_with_contentsm.execute_components'%s'sargs = argdict(%s)scontent = component_contenttpassRs%s(%s)s, ( t get_componentR-tclosuresthas_keytget_argstappendR;R.R:R9tstringtjoin(RRRR6tnametfuncnametargs((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR7ls"    %cCs6|i||ƒ|i|i|ƒ||dtƒdS(Ntnormalize_indent(R-R9R5R;(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRƒscCss|i|ƒ}|o#d|i|ƒt|ƒf}nd|i|ƒ}|i||ƒ|i|||ƒdS(Ns m.write(m.apply_escapes(%s, %s))s m.write(%s)(t get_escapesR5treprR-R9(RRRtescapestcode((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRˆs #cCsB|i|ƒx!|iƒD]}|i|ƒqW|i|ƒdS(N(t_generate_module_headertget_named_blocksR(t_generate_component_class(RR tblock((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR “s   cCs|i|dtdtƒdS(Ntis_subcomponentt is_method(RNR;R"(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR œscCs|i|dtdtƒdS(NRPRQ(RNR;(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR Ÿsc s€|i|ƒ}|ii|iidt|iƒ‰t|i|i<‡fd†}g}g}|ii ƒD]}|i o ||qoqo~}t|ƒo#|i t i t||ƒƒƒng} |ii ƒD]}|i p | |q×q×~ } t| ƒo#|i t i t|| ƒƒƒn|dˆdg7}d|it i |ƒf} |i| ||ƒ|i|ƒdS(Nt cs7|ipdˆ|i|ifSdˆ|ifSdS(Ns %s%s = %s, s %s%s, (trequiredRDtdefault(targ(tindent(s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt format_argªs s s**_comp_paramss def %s(%s):(t get_compiledRt indentstringRVtlenRDR;R>RFtvaluesRSRARBRCtmapR9t_generate_run_body( RRRR RWRFt_[1]RUtreqargt_[2]t nonreqargRK((RVs1/usr/lib/python2.6/site-packages/myghty/objgen.pyR £s'8 #8 #c CsL|iid|i|iiƒtiƒfƒ|idj o|iid|iƒn|ii dƒ|ii dƒ|ii dƒ|ii dƒ|ii dtiƒƒ|ii dt |ii ƒƒƒ|i d ƒo|i |d ƒn|ioß|iƒ|i|d d td td dgƒ|i |dƒ|ioC|iƒ|i|dd td td dgƒ|i |dƒn|i|ƒ|io.|iƒ|iidƒ|ii dƒn|iƒne|ioZ|iƒ|i|dd td td dgƒ|i |dƒ|i|ƒ|iƒndS(s{generates the top header of the file, the <%once> section, and the dynamic subs if the module has a <%shared> blocks%File: %s CompilerID: %s Timestamp: %ss-*- encoding: %s -*-sCfrom myghty.component import Component, FileComponent, SubComponents import myghty.request as requestsimport myghty.argssfrom myghty.util import *s_CREATION_TIME = %ds_MAGIC_NUMBER = %stoncetthread_local_initializertappend_globalstappend_componentRFRRtrequest_local_initializerRs s return request_local_initializerN(RR,tfileidRt get_object_idttimetasctimetencodingtNonetprint_python_lineRItget_magic_numberR%t _output_blockR't _push_indentt_generate_python_defR;R"R&t_generate_dynamic_subst _pop_indenttprint_raw_line(RR ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRLÆsB/    %  %     % c Csàxe|iƒ|gD]P}|iƒ|i|dd|i|iƒdtƒ|i|ƒ|iƒqW|ii dƒxQ|iƒ|gD]<}|ii d|i|iƒd|i|iƒfƒqŒW|ii dƒdS(Nt methodnamesrun_%stis_dynamic_methodsreturn {s '%s' : %s,t}( RMRpt_generate_run_definet_format_class_nameRDR;R]RsRRm(RR RO((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRrús ) :cKs¤|iƒ|i|||ip|i}|i|d|ƒ|p|i|ƒn|iƒ|io4|ii dƒ|ii d|i |i ƒƒndS(sšgenerates the class definition of a component, all methods within the class, and a static accessor function if the component is the lead componenttadd_dynamic_calls sHdef get_component(interpreter, csource): return %s(interpreter, csource)N( Rpt_generate_initR&R'RxR]Rstin_mainRRtRmRyRD(RR Rtdynamic((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRNs   cCs,d|i|i|iƒ|it|ƒfS(sEgenerates a constructor call to create a subcomponent (def or method)s%'%s' : %s('%s', self, is_method = %s)(RDRyRI(RR RQ((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt"_generate_subcomponent_constructors c s­|o d}nd}ˆiidƒˆiidˆi|iƒ|fƒˆiidƒˆiidtit‡fd†|idƒƒd ƒƒˆiid tit‡fd †|id ƒƒd ƒƒˆiid ˆi |i ƒƒˆiidˆi |i ƒƒˆiidtig}|i i ƒD]}|dt|ƒq9~d ƒƒˆiidƒ|io@ˆioˆiidƒq»ˆioˆiidƒq»n|idƒoYˆi|ddtdtddgƒˆi|dƒˆiidƒˆiidƒn(|i idƒoˆiidƒnˆiidƒˆiidtˆio|iƒƒˆiidtˆio|iƒƒdS( s.generates the __init__ method for a component t SubComponentt FileComponents s class %s(%s):sdef do_component_init(self):sself.defs = {%s}csˆi|tƒS((R~R"(tc(R(s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt/stdefs, sself.methods = {%s}csˆi|tƒS((R~R;(R(R(s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR‚3stmethodsself.flags = %ssself.attr = %ssself.arguments = [%s]s myghty.args.s"self.creationtime = _CREATION_TIMEs8self.thread_local_initializer = thread_local_initializers:self.request_local_initializer = request_local_initializertfilterRdReRFtfRs,self.filter = self._init_filter_func(filter)ttrims&self.filter = self._init_filter_func()s&def uses_request_local(self):return %ss%def uses_thread_local(self):return %sN(RRtRmRyRDRBRCR\RMt_repr_key_valuetflagstattrRFR[RIR|R'R&R%RqR;R"RoR?(RR RPRQt superclassR^RU((Rs1/usr/lib/python2.6/site-packages/myghty/objgen.pyR{!s> &    N   %'cs0dtit‡fd†ˆiƒƒdƒdS(Nt{csd|ˆ|fS(s '%s' : %s((tk(tkeyvalue(s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR‚Qss, Rw(RBRCR\tkeys(RRŽ((RŽs1/usr/lib/python2.6/site-packages/myghty/objgen.pyRˆOstdo_run_componentcCsh|_|i||d|p| d|p| ƒ\}}|pdg|}n|}|iidƒ|iddjo$|ii|i|idƒƒn|iid|ti|ƒ|fƒ|iidƒ|o3|iid |i |i ƒti|ƒfƒndS( NRdResself, sBEGIN BLOCK argsRFiÿÿÿÿsdef %s(%s %s):sEND BLOCK argss#return self._call_dynamic('%s', %s)( R>t_generate_method_argsRR,t start_blockst _line_commentRmRBRCRyRD(RR RuRvRzRFRVtdefargs((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRxTs 4$& c Csˆ|i||d|d|ƒ\}}|dj o-g}|D]} || dq<~|}n|iid|ti|ƒ|fƒdS(NRdRes, sdef %s(%s %s):(R‘RlRRmRBRC( RR RuRdReRFtgenargsRVR^RU((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRqks$ -c sm|ii|iidt|ƒ‰‡fd†}ddg}|o |td„|iiƒ7}n|oég}|iiƒD]}|i o ||qq~} t| ƒo#|i t i t|| ƒƒƒng} |iiƒD]}|i p | |qéqé~ } t| ƒo#|i t i t|| ƒƒƒn|dˆdg7}n|dg7}|ˆfS(NRRcs7|ipdˆ|i|ifSdˆ|ifSdS(Ns %s%s = %s, s %s%s, (RSRDRT(RU(RV(s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRWxs sm, sARGS, cSsd|S(s%s, ((tx((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR‚ƒss s**_comp_params( RRYRVRZR\Rt allow_globalsRFR[RSRARBRC( RR RuRdReRWRFR^RUR_R`Ra((RVs1/usr/lib/python2.6/site-packages/myghty/objgen.pyR‘qs$  8 #8 # cCsìt}|idƒo|iidƒnxldD]d}|i|ƒpq1n|djo$|iidƒ|iidƒn|i||ƒt}q1W|p|iidƒn|iidƒ|idƒo|iidƒndS( Ntcleanupstry:tinittbodysfinally:R<R(R™RšR˜(R"R%RRmRoR;(RR t has_blockstkey((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR]•s"  cCsÕ|iƒ|i|ƒ}t}t}|ip'|iid|ƒt|_t}nx$|D]}t}|i||ƒq]W|o|iƒn|o!|iid|ƒt|_n|i ƒ|ii ƒ|S(svtraverses the lines of flyweight objects in a block object (such as init, once, body) and outputs python codesBEGIN BLOCK %ss END BLOCK %s( Rptget_block_iteratorR"R#RR,R;R(R*Rstclear_whitespace_stack(RR t block_typetiterthas_codetoutermost_blockRK((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRo³s(       cCs•|iƒ|i||ƒt|ƒtijo;x^|D],}|ii|ƒ|ii|d|ƒq7Wn'|ii|ƒ|ii|d|ƒdS(sµprints python code, performing the convenience steps of flushing the existing text buffer, printing line number tracking comments, and post processing of python codeRGN( R*R-ttypettypestListTypeRR/RRm(RRKRRRGR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR9Ñs cCsU|i|ƒ|ijo8|ii|i|i|ƒƒƒ|i|ƒ|_ndS(N(R.R$RR,R“(RRR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR-ás"cCsd|S(NsSOURCE LINE %s((Rt linenumber((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR“æscCs|ipdS|ii|iƒ}tidd|ƒ}|iio|iid|ƒn|iid|ƒd|_dS(Ns(['\\])s\\\1sm.write('''%s''')sm.write(u'''%s''')R(R RR4tretsubtdisable_unicodeRRm(Rtbuffer((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR*és  cCs2tidd|ƒ}tidd|ƒ}d|S(Ns\.t_s[^\d\w_]R(R§R¨(RRD((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRyôscCs|ii|iiƒdS(sFstores the current indentation level of the Python printer on a stack.N(R!RARRV(R((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRpùscCs|iiƒ}||i_dS(sTrestores the indentation level of the Python printer to what we last stored.N(R!tpopRRV(RRV((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRsýsN($RRRRRRRRRR"R7RRR R R R RLRrRNR~R{RˆRxRlRqR‘R]RoR9R-R“R*RyRpRs(((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR,s@           # 4  .   $       RcBs†eZdZd„Zeed„Zd„Zd„Zdd„Zd„Z d„Z d d „Z d „Z d „Z d „Zd„ZRS(suprints Python code, keeping track of indentation level. PythonPrinter has two basic modes, "print_python_line" mode which prints one line at a time and calculates whitespace on the fly, and "add_normalized_block_line" mode where you add lines to a buffer, and then at the end print them all out as they were stored, except relative to the current indent.cCsMd|_g|_d|_g|_||_g|_t|_|iƒdS(Nis ( RVt indent_detailRYt spacestackRt line_bufferR"tin_indent_linest_reset_multi_line_flags(RR((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt__init__ s       c Cs|ip|iƒt|_nt}tid|ƒptid|ƒo t}nt}|oAti|ƒ}tid|ƒidƒ}t |i ƒdjo|o|i i |ƒq’nÐt |ƒt |i dƒjo|o|i i |ƒq’n‘t |ƒt |i dƒjop|i i ƒ|i djoO|i d8_ t |iƒdjotidƒ‚n|ii ƒt}q’nti|ƒ}nd }| o€| ox| p|i||ƒo]|i djoI|i d8_ t |iƒdjotidƒ‚n|ii ƒq3n|ii|i|ƒdƒtid |ƒotid |ƒ}|o8|idƒ}|i d7_ |ii ||fƒqd }tid |ƒ} | o)|i d7_ |ii ||fƒqnd S( sŒprints a line to the output buffer, preceded by a blank indentation string of proportional size to the current indent counter. If the line ends with a colon, the indentation counter is incremented after printing. If the line is blank, the indentation counter is decremented. if normalize_indent is set to true, the line is printed with its existing whitespace "normalized" to the current indentation counter; additionally, its existing whitespace is measured and compared against a stack of whitespace strings grabbed from other normalize_indent calls, which is used to adjust the current indentation counter. basically, "normalize_indent" is used with % lines in a template, and non-normalize_indent is used by the objectgenerator's internally generated lines of code. s^\s*#s^\s*$s^([ ]*)iiiÿÿÿÿsToo many whitespace closuress s:[ \t]*(?:#.*)?$s^\s*(if|try|elif|while|for)s(^\s*(def|class|else|elif|except|finally)N(R°tflush_normalized_blockR;R"R§tmatchRBt expandtabstgroupRZR®RAR¬RVR­t exceptiontCompilertlstripRlt_is_unindentorRtwritet _indent_linetsearch( RR3RGt is_commenttdecreased_indentthastexttspaceR´tindentortm2((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRm*s^        cCstt|iƒdjotS|id\}}|djotS||jotStid|ƒ}|ptStS(súlooks at the keyword and its whitespace that was most recently responsible for incrementing the indent, and compares against the keyword (if any) and whitespace for the given line, to see if an unindent operation should occuriiÿÿÿÿs^\s*(else|elif|except|finally)N(RZR­R"RlR§R´R;(RR3RÁtindspaceRÂR´((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRº–s  cCs|id|dtƒdS(Ns# R¾(RmR;(Rtcomment((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR,ÁsRcCs!tid||i|i|ƒS(Ns^%s(R§R¨RYRV(RR3t stripspace((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR¼ÄscCstt|_|_dS(N(R"t backslashedt triplequoted(R((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR±ÇscCs‡|ip|i}tid|ƒo t|_n t|_ttid|ƒƒ}|djp|ddjo|i |_n|S(Ns\\$s \"\"\"|\'\'\'iii(RÇRÈR§R½R;R"RZtfindall(RR3t current_statettriples((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt_in_multi_lineËs  icCs#t|_|iit|gƒdS(sadds a line to the deffered print buffer with the "multiline" flag set to true, so that this line will come out with its original indentation intact.N(R"R°R¯RAR;(RR3t indentlevel((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRtàs cCs#t|_|iit|gƒdS(s adds a line to the deferred print buffer. When the deferred print buffer is flushed, its lines will be scanned for the initial whitespace amount. lines with that much or greater whitespace will have their whitespace adjusted to line up with the whitespace that has been established by the last print_indent_line call. Lines within continued triplequotes and backslashes will remain unaffected.....if all goes well. This method is basically used for <%python> blocks. N(R"R°R¯RA(RR3((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR0ès cCsíd}|iƒxÃ|iD]¸}|i|dƒo|ii|ddƒqti|dƒ|d<|djo:ti d|dƒo#ti d|dƒi dƒ}n|ii|i |d|ƒdƒqWg|_|iƒdS(Nis s[^ \t]s ^([ \t]*)( RlR±R¯RÌRR»RBRµR§R½R´R¶R¼(RRÆtentry((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR³ûs  $#( cCs g|_dS(N(R®(R((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRžscCs|iƒdS(N(R³(R((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyR)s(RRRR²R"RmRºR,R¼R±RÌRtR0R³RžR)(((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyRs l +        ( R§RBR¤Rit myghty.utiltmyghty.exceptionR·RRR(((s1/usr/lib/python2.6/site-packages/myghty/objgen.pyt s 0 ÿÙ