<s> """<STR_LIT>""" <EOL> from time import time as now <EOL> from cStringIO import StringIO <EOL> import random <EOL> import warnings <EOL> from zope . interface import implements , providedBy <EOL> import twisted . python . components as tpc <EOL> from twisted . python . reflect import qual , accumulateClassList <EOL> from nevow . context import WovenContext , NodeNotFound , PageContext <EOL> from nevow import inevow , tags , flat , util , url <EOL> from nevow . util import log <EOL> import formless <EOL> from formless import iformless , annotate <EOL> def _getPreprocessors ( inst ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in vars ( inst ) : <EOL> return inst . preprocessors <EOL> preprocessors = [ ] <EOL> accumulateClassList ( <EOL> inst . __class__ , <EOL> '<STR_LIT>' , <EOL> preprocessors ) <EOL> return preprocessors <EOL> class RenderFactory ( object ) : <EOL> implements ( inevow . IRendererFactory ) <EOL> def renderer ( self , context , name ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> if name . find ( '<STR_LIT:U+0020>' ) != - <NUM_LIT:1> : <EOL> name , args = name . split ( None , <NUM_LIT:1> ) <EOL> args = [ arg . strip ( ) for arg in args . split ( '<STR_LIT:U+002C>' ) ] <EOL> callable = getattr ( self , '<STR_LIT>' % name , None ) <EOL> if callable is None : <EOL> warnings . warn ( <EOL> "<STR_LIT>" % ( <EOL> name , qual ( type ( self ) ) ) , <EOL> category = DeprecationWarning , <EOL> stacklevel = <NUM_LIT:1> ) <EOL> callable = lambda * a , ** kw : context . tag [ <EOL> "<STR_LIT>" % ( name , self ) ] <EOL> if args : <EOL> return callable ( * args ) <EOL> return callable <EOL> render_sequence = lambda self , context , data : sequence ( context , data ) <EOL> render_mapping = lambda self , context , data : mapping ( context , data ) <EOL> render_string = lambda self , context , data : string ( context , data ) <EOL> render_xml = lambda self , context , data : context . tag . clear ( ) [ tags . xml ( data ) ] <EOL> render_data = lambda self , context , data_ : data ( context , data_ ) <EOL> class MacroFactory ( object ) : <EOL> implements ( inevow . IMacroFactory ) <EOL> def macro ( self , ctx , name ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> if name . find ( '<STR_LIT:U+0020>' ) != - <NUM_LIT:1> : <EOL> name , args = name . split ( None , <NUM_LIT:1> ) <EOL> args = [ arg . strip ( ) for arg in args . split ( '<STR_LIT:U+002C>' ) ] <EOL> callable = getattr ( self , '<STR_LIT>' % name , None ) <EOL> if callable is None : <EOL> callable = lambda ctx , * args : ctx . tag [ <EOL> "<STR_LIT>" % ( name , self ) ] <EOL> if args : <EOL> return lambda ctx : callable ( ctx , * args ) <EOL> return callable <EOL> class DataNotFoundError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class DataFactory ( object ) : <EOL> implements ( inevow . IContainer ) <EOL> def child ( self , context , n ) : <EOL> args = [ ] <EOL> if n . find ( '<STR_LIT:U+0020>' ) != - <NUM_LIT:1> : <EOL> name , args = n . split ( None , <NUM_LIT:1> ) <EOL> args = [ arg . strip ( ) for arg in args . split ( '<STR_LIT:U+002C>' ) ] <EOL> else : <EOL> name = n <EOL> callable = getattr ( self , '<STR_LIT>' % name , None ) <EOL> if callable is None : <EOL> container = inevow . IContainer ( self . original , None ) <EOL> if container is None : <EOL> raise DataNotFoundError ( "<STR_LIT>" % ( name , self ) ) <EOL> else : <EOL> return container . child ( context , n ) <EOL> if args : <EOL> return callable ( * args ) <EOL> return callable <EOL> class FreeformChildMixin : <EOL> """<STR_LIT>""" <EOL> def locateChild ( self , ctx , segments ) : <EOL> request = inevow . IRequest ( ctx ) <EOL> bindingName = None <EOL> name = segments [ <NUM_LIT:0> ] <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> configurableName , bindingName = name . split ( '<STR_LIT:!>' ) [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> elif name . startswith ( '<STR_LIT>' ) : <EOL> configurableName , request . args [ '<STR_LIT>' ] = name . split ( '<STR_LIT:!>' ) [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> bindingName = request . args [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if bindingName : <EOL> ctx . remember ( self , inevow . IResource ) <EOL> ctx . remember ( request , inevow . IRequest ) <EOL> cf = iformless . IConfigurableFactory ( self ) <EOL> def checkC ( c ) : <EOL> if c is not None : <EOL> return self . webFormPost ( request , self , c , ctx , bindingName , request . args ) <EOL> return util . maybeDeferred ( cf . locateConfigurable , ctx , configurableName ) . addCallback ( checkC ) <EOL> return NotFound <EOL> def child_freeform_hand ( self , ctx ) : <EOL> carryoverHand = inevow . IHand ( ctx , None ) <EOL> if carryoverHand is not None : <EOL> inevow . ISession ( ctx ) . setComponent ( inevow . IHand , carryoverHand ) <EOL> return carryoverHand <EOL> return inevow . IHand ( inevow . ISession ( ctx ) , None ) <EOL> class ConfigurableMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( iformless . IConfigurable ) <EOL> def getBindingNames ( self , ctx ) : <EOL> """<STR_LIT>""" <EOL> for name in dir ( self ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> yield name [ len ( '<STR_LIT>' ) : ] <EOL> def getBinding ( self , ctx , name ) : <EOL> """<STR_LIT>""" <EOL> def _get_binding ( binding ) : <EOL> if callable ( binding ) : <EOL> binding = util . maybeDeferred ( binding , ctx ) <EOL> return binding <EOL> def _convert_list ( binding ) : <EOL> if isinstance ( binding , list ) : <EOL> binding = annotate . MethodBinding ( <EOL> name , annotate . Method ( arguments = [ <EOL> annotate . Argument ( n , v , v . id ) <EOL> for ( n , v ) in binding ] ) ) <EOL> return binding <EOL> binding = util . maybeDeferred ( getattr , self , '<STR_LIT>' % name ) <EOL> return binding . addCallback ( _get_binding ) . addCallback ( _convert_list ) <EOL> def getDefault ( self , forBinding ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( forBinding , annotate . Argument ) : <EOL> if hasattr ( self , forBinding . name ) : <EOL> return getattr ( self , forBinding . name ) <EOL> return forBinding . default <EOL> def postForm ( self , ctx , bindingName , args ) : <EOL> """<STR_LIT>""" <EOL> def _callback ( binding ) : <EOL> ctx . remember ( binding , iformless . IBinding ) <EOL> ctx . remember ( self , iformless . IConfigurable ) <EOL> rv = iformless . IInputProcessor ( binding ) . process ( ctx , self , args ) <EOL> ctx . remember ( rv , inevow . IHand ) <EOL> ctx . remember ( '<STR_LIT>' % bindingName , inevow . IStatusMessage ) <EOL> return rv <EOL> return util . maybeDeferred ( self . getBinding , ctx , <EOL> bindingName ) . addCallback ( _callback ) <EOL> class ConfigurableFactory : <EOL> """<STR_LIT>""" <EOL> implements ( iformless . IConfigurableFactory ) <EOL> def locateConfigurable ( self , context , name ) : <EOL> """<STR_LIT>""" <EOL> return util . maybeDeferred ( getattr ( self , '<STR_LIT>' % name ) , <EOL> context ) . addCallback ( iformless . IConfigurable ) <EOL> def configurable_ ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if filter ( lambda x : issubclass ( x , annotate . TypedInterface ) , providedBy ( self ) ) : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning ) <EOL> from formless import configurable <EOL> return configurable . TypedInterfaceConfigurable ( self ) <EOL> return self <EOL> def configurable_original ( self , ctx ) : <EOL> """<STR_LIT>""" <EOL> return self . original <EOL> _CARRYOVER = { } <EOL> def defaultsFactory ( ctx ) : <EOL> co = _CARRYOVER . get ( <EOL> ctx . tag . args . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] , None ) <EOL> from formless import webform <EOL> defaults = webform . FormDefaults ( ) <EOL> if co is not None : <EOL> e = iformless . IFormErrors ( co , { } ) <EOL> for k , v in e . items ( ) : <EOL> defaults . getAllDefaults ( k ) . update ( v . partialForm ) <EOL> return defaults <EOL> def errorsFactory ( ctx ) : <EOL> co = _CARRYOVER . get ( <EOL> ctx . tag . args . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] , None ) <EOL> from formless import webform <EOL> errs = webform . FormErrors ( ) <EOL> if co is not None : <EOL> e = iformless . IFormErrors ( co , { } ) <EOL> for k , v in e . items ( ) : <EOL> errs . updateErrors ( k , v . errors ) <EOL> errs . setError ( k , v . formErrorMessage ) <EOL> return errs <EOL> def handFactory ( ctx ) : <EOL> co = _CARRYOVER . get ( <EOL> ctx . tag . args . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] , None ) <EOL> return inevow . IHand ( co , None ) <EOL> def statusFactory ( ctx ) : <EOL> co = _CARRYOVER . get ( <EOL> ctx . tag . args . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] , None ) <EOL> return inevow . IStatusMessage ( co , None ) <EOL> def originalFactory ( ctx ) : <EOL> return ctx . tag <EOL> class Fragment ( DataFactory , RenderFactory , MacroFactory , ConfigurableMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( inevow . IRenderer , inevow . IGettable ) <EOL> docFactory = None <EOL> original = None <EOL> def __init__ ( self , original = None , docFactory = None ) : <EOL> if original is not None : <EOL> self . original = original <EOL> self . toremember = [ ] <EOL> if docFactory is not None : <EOL> self . docFactory = docFactory <EOL> def get ( self , context ) : <EOL> return self . original <EOL> def rend ( self , context , data ) : <EOL> context = WovenContext ( parent = context ) <EOL> self . rememberStuff ( context ) <EOL> preprocessors = _getPreprocessors ( self ) <EOL> old = self . docFactory . pattern <EOL> self . docFactory . pattern = '<STR_LIT:content>' <EOL> self . docFactory . precompiledDoc = None <EOL> try : <EOL> try : <EOL> doc = self . docFactory . load ( context , preprocessors ) <EOL> finally : <EOL> self . docFactory . pattern = old <EOL> self . docFactory . precompiledDoc = None <EOL> except TypeError , e : <EOL> if '<STR_LIT>' not in str ( e ) : <EOL> raise <EOL> doc = self . docFactory . load ( context , preprocessors ) <EOL> except NodeNotFound : <EOL> doc = self . docFactory . load ( context , preprocessors ) <EOL> else : <EOL> if old == '<STR_LIT:content>' : <EOL> warnings . warn ( <EOL> """<STR_LIT>""" , <EOL> DeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> context . tag = tags . invisible [ doc ] <EOL> return context <EOL> def remember ( self , obj , inter = None ) : <EOL> """<STR_LIT>""" <EOL> self . toremember . append ( ( obj , inter ) ) <EOL> def rememberStuff ( self , ctx ) : <EOL> ctx . remember ( self , inevow . IRenderer ) <EOL> ctx . remember ( self , inevow . IRendererFactory ) <EOL> ctx . remember ( self , inevow . IMacroFactory ) <EOL> ctx . remember ( self , inevow . IData ) <EOL> class ChildLookupMixin ( FreeformChildMixin ) : <EOL> children = None <EOL> def locateChild ( self , ctx , segments ) : <EOL> """<STR_LIT>""" <EOL> if self . children is not None : <EOL> r = self . children . get ( segments [ <NUM_LIT:0> ] , None ) <EOL> if r is not None : <EOL> return r , segments [ <NUM_LIT:1> : ] <EOL> w = getattr ( self , '<STR_LIT>' % segments [ <NUM_LIT:0> ] , None ) <EOL> if w is not None : <EOL> if inevow . IResource ( w , None ) is not None : <EOL> return w , segments [ <NUM_LIT:1> : ] <EOL> r = w ( ctx ) <EOL> if r is not None : <EOL> return r , segments [ <NUM_LIT:1> : ] <EOL> r = self . childFactory ( ctx , segments [ <NUM_LIT:0> ] ) <EOL> if r is not None : <EOL> return r , segments [ <NUM_LIT:1> : ] <EOL> return FreeformChildMixin . locateChild ( self , ctx , segments ) <EOL> def childFactory ( self , ctx , name ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def putChild ( self , name , child ) : <EOL> if self . children is None : <EOL> self . children = { } <EOL> self . children [ name ] = child <EOL> class Page ( Fragment , ConfigurableFactory , ChildLookupMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( inevow . IResource ) <EOL> buffered = False <EOL> beforeRender = None <EOL> afterRender = None <EOL> addSlash = None <EOL> flattenFactory = lambda self , * args : flat . flattenFactory ( * args ) <EOL> def renderHTTP ( self , ctx ) : <EOL> if self . beforeRender is not None : <EOL> return util . maybeDeferred ( self . beforeRender , ctx ) . addCallback ( <EOL> lambda result , ctx : self . _renderHTTP ( ctx ) , ctx ) <EOL> return self . _renderHTTP ( ctx ) <EOL> def _renderHTTP ( self , ctx ) : <EOL> request = inevow . IRequest ( ctx ) <EOL> if self . addSlash and inevow . ICurrentSegments ( ctx ) [ - <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> request . redirect ( str ( request . URLPath ( ) . child ( '<STR_LIT>' ) ) ) <EOL> return '<STR_LIT>' <EOL> log . msg ( http_render = None , uri = request . uri ) <EOL> self . rememberStuff ( ctx ) <EOL> def finishRequest ( ) : <EOL> carryover = request . args . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] <EOL> if carryover is not None and _CARRYOVER . has_key ( carryover ) : <EOL> del _CARRYOVER [ carryover ] <EOL> if self . afterRender is not None : <EOL> return util . maybeDeferred ( self . afterRender , ctx ) <EOL> if self . buffered : <EOL> io = StringIO ( ) <EOL> writer = io . write <EOL> def finisher ( result ) : <EOL> request . write ( io . getvalue ( ) ) <EOL> return util . maybeDeferred ( finishRequest ) . addCallback ( lambda r : result ) <EOL> else : <EOL> writer = request . write <EOL> def finisher ( result ) : <EOL> return util . maybeDeferred ( finishRequest ) . addCallback ( lambda r : result ) <EOL> preprocessors = _getPreprocessors ( self ) <EOL> doc = self . docFactory . load ( ctx , preprocessors ) <EOL> ctx = WovenContext ( ctx , tags . invisible [ doc ] ) <EOL> return self . flattenFactory ( doc , ctx , writer , finisher ) <EOL> def rememberStuff ( self , ctx ) : <EOL> Fragment . rememberStuff ( self , ctx ) <EOL> ctx . remember ( self , inevow . IResource ) <EOL> def renderString ( self , ctx = None ) : <EOL> """<STR_LIT>""" <EOL> io = StringIO ( ) <EOL> writer = io . write <EOL> def finisher ( result ) : <EOL> return io . getvalue ( ) <EOL> ctx = PageContext ( parent = ctx , tag = self ) <EOL> self . rememberStuff ( ctx ) <EOL> doc = self . docFactory . load ( ctx ) <EOL> ctx = WovenContext ( ctx , tags . invisible [ doc ] ) <EOL> return self . flattenFactory ( doc , ctx , writer , finisher ) <EOL> def renderSynchronously ( self , ctx = None ) : <EOL> """<STR_LIT>""" <EOL> io = StringIO ( ) <EOL> ctx = PageContext ( parent = ctx , tag = self ) <EOL> self . rememberStuff ( ctx ) <EOL> doc = self . docFactory . load ( ctx ) <EOL> ctx = WovenContext ( ctx , tags . invisible [ doc ] ) <EOL> def raiseAlways ( item ) : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( item , ) ) <EOL> list ( flat . iterflatten ( doc , ctx , io . write , raiseAlways ) ) <EOL> return io . getvalue ( ) <EOL> def child_ ( self , ctx ) : <EOL> """<STR_LIT>""" <EOL> if self . addSlash and len ( inevow . IRemainingSegments ( ctx ) ) == <NUM_LIT:1> : <EOL> return self <EOL> return None <EOL> def webFormPost ( self , request , res , configurable , ctx , bindingName , args ) : <EOL> """<STR_LIT>""" <EOL> def redirectAfterPost ( aspects ) : <EOL> hand = aspects . get ( inevow . IHand ) <EOL> refpath = None <EOL> if hand is not None : <EOL> if isinstance ( hand , Page ) : <EOL> refpath = url . here <EOL> if '<STR_LIT>' not in inevow . IRequest ( ctx ) . prepath : <EOL> refpath = refpath . child ( '<STR_LIT>' ) <EOL> if isinstance ( hand , ( url . URL , url . URLOverlay ) ) : <EOL> refpath , hand = hand , None <EOL> if refpath is None : <EOL> redirectAfterPost = request . getComponent ( iformless . IRedirectAfterPost , None ) <EOL> if redirectAfterPost is None : <EOL> ref = request . getHeader ( '<STR_LIT>' ) <EOL> if ref : <EOL> refpath = url . URL . fromString ( ref ) <EOL> else : <EOL> refpath = url . here <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" , DeprecationWarning , <NUM_LIT:2> ) <EOL> ref = str ( redirectAfterPost ) <EOL> refpath = url . URL . fromString ( ref ) <EOL> if hand is not None or aspects . get ( iformless . IFormErrors ) is not None : <EOL> magicCookie = '<STR_LIT>' % ( now ( ) , request . getClientIP ( ) , random . random ( ) ) <EOL> refpath = refpath . replace ( '<STR_LIT>' , magicCookie ) <EOL> _CARRYOVER [ magicCookie ] = C = tpc . Componentized ( ) <EOL> for k , v in aspects . iteritems ( ) : <EOL> C . setComponent ( k , v ) <EOL> destination = flat . flatten ( refpath , ctx ) <EOL> request . redirect ( destination ) <EOL> from nevow import static <EOL> return static . Data ( '<STR_LIT>' % bindingName , '<STR_LIT>' ) , ( ) <EOL> return util . maybeDeferred ( <EOL> configurable . postForm , ctx , bindingName , args <EOL> ) . addCallback ( <EOL> self . onPostSuccess , request , ctx , bindingName , redirectAfterPost <EOL> ) . addErrback ( <EOL> self . onPostFailure , request , ctx , bindingName , redirectAfterPost <EOL> ) <EOL> def onPostSuccess ( self , result , request , ctx , bindingName , redirectAfterPost ) : <EOL> if result is None : <EOL> message = "<STR_LIT>" % formless . nameToLabel ( bindingName ) <EOL> else : <EOL> message = result <EOL> return redirectAfterPost ( { inevow . IHand : result , inevow . IStatusMessage : message } ) <EOL> def onPostFailure ( self , reason , request , ctx , bindingName , redirectAfterPost ) : <EOL> reason . trap ( formless . ValidateError ) <EOL> return redirectAfterPost ( { iformless . IFormErrors : { bindingName : reason . value } } ) <EOL> def sequence ( context , data ) : <EOL> """<STR_LIT>""" <EOL> tag = context . tag <EOL> headers = tag . allPatterns ( '<STR_LIT>' ) <EOL> pattern = tag . patternGenerator ( '<STR_LIT>' ) <EOL> divider = tag . patternGenerator ( '<STR_LIT>' , default = tags . invisible ) <EOL> content = [ ( pattern ( data = element ) , divider ( data = element ) ) for element in data ] <EOL> if not content : <EOL> content = tag . allPatterns ( '<STR_LIT>' ) <EOL> else : <EOL> content [ - <NUM_LIT:1> ] = content [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> footers = tag . allPatterns ( '<STR_LIT>' ) <EOL> return tag . clear ( ) [ headers , content , footers ] <EOL> def mapping ( context , data ) : <EOL> """<STR_LIT>""" <EOL> for k , v in data . items ( ) : <EOL> context . fillSlots ( k , v ) <EOL> return context . tag <EOL> def string ( context , data ) : <EOL> return context . tag . clear ( ) [ str ( data ) ] <EOL> def data ( context , data ) : <EOL> """<STR_LIT>""" <EOL> return context . tag . clear ( ) [ data ] <EOL> class FourOhFour : <EOL> """<STR_LIT>""" <EOL> implements ( inevow . IResource ) <EOL> notFound = "<STR_LIT>" <EOL> original = None <EOL> def locateChild ( self , ctx , segments ) : <EOL> return NotFound <EOL> def renderHTTP ( self , ctx ) : <EOL> inevow . IRequest ( ctx ) . setResponseCode ( <NUM_LIT> ) <EOL> try : <EOL> notFoundHandler = ctx . locate ( inevow . ICanHandleNotFound ) <EOL> except KeyError , e : <EOL> return self . notFound <EOL> try : <EOL> return notFoundHandler . renderHTTP_notFound ( PageContext ( parent = ctx , tag = notFoundHandler ) ) <EOL> except : <EOL> log . err ( ) <EOL> return self . notFound <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> NotFound = None , ( ) </s>
