<s> from zope . interface import implements <EOL> from twisted . internet import defer <EOL> from twisted . python import log <EOL> from nevow import inevow , rend , loaders , static , url , tags , util <EOL> from nevow . flat import flatten <EOL> from nevow . stan import Proto , Tag <EOL> from itertools import count <EOL> cn = count ( ) . next <EOL> cookie = lambda : str ( cn ( ) ) <EOL> _hookup = { } <EOL> _canvasCGIService = None <EOL> m = Proto ( '<STR_LIT:m>' ) <EOL> a = Proto ( '<STR_LIT:a>' ) <EOL> l = Proto ( '<STR_LIT:l>' ) <EOL> d = Proto ( '<STR_LIT:d>' ) <EOL> i = Proto ( '<STR_LIT:i>' ) <EOL> def squish ( it ) : <EOL> if isinstance ( it , Tag ) : <EOL> return a [ it ] <EOL> return a ( v = it ) <EOL> class _Remoted ( object ) : <EOL> def __init__ ( self , cookie , canvas ) : <EOL> self . cookie = cookie <EOL> self . canvas = canvas <EOL> class Text ( _Remoted ) : <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> def change ( self , text ) : <EOL> self . text = text <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , text ) <EOL> def move ( self , x , y ) : <EOL> self . x = x <EOL> self . y = y <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , x , y ) <EOL> def listFonts ( self ) : <EOL> if hasattr ( self . canvas , '<STR_LIT>' ) : <EOL> return defer . succeed ( self . canvas . _fontList ) <EOL> cook = cookie ( ) <EOL> self . canvas . deferreds [ cook ] = d = defer . Deferred ( ) <EOL> self . canvas . call ( '<STR_LIT>' , cook ) <EOL> def _cb ( l ) : <EOL> L = l . split ( '<STR_LIT:U+002C>' ) <EOL> self . canvas . _fontList = L <EOL> return L <EOL> return d . addCallback ( _cb ) <EOL> def font ( self , font ) : <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , font ) <EOL> def size ( self , size ) : <EOL> self . canvas . call ( '<STR_LIT:size>' , self . cookie , size ) <EOL> class Image ( _Remoted ) : <EOL> def move ( self , x , y ) : <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , x , y ) <EOL> def scale ( self , x , y ) : <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , x , y ) <EOL> def alpha ( self , alpha ) : <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , alpha ) <EOL> def rotate ( self , angle ) : <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , angle ) <EOL> class Sound ( _Remoted ) : <EOL> def play ( self , offset = <NUM_LIT:0> , timesLoop = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . canvas . call ( '<STR_LIT>' , self . cookie , offset , timesLoop ) <EOL> class GroupBase ( object ) : <EOL> def call ( self , method , * args ) : <EOL> """<STR_LIT>""" <EOL> flatcall = flatten ( <EOL> m ( n = method , t = self . groupName ) [ [ <EOL> squish ( x ) for x in args if x is not None ] ] ) <EOL> self . socket . write ( flatcall + '<STR_LIT>' ) <EOL> groupx = <NUM_LIT:0> <EOL> groupy = <NUM_LIT:0> <EOL> def reposition ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . groupx = x <EOL> self . groupy = y <EOL> self . call ( '<STR_LIT>' , x , y ) <EOL> def rotate ( self , angle ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , angle ) <EOL> _alpha = <NUM_LIT:100> <EOL> def alpha ( self , percent ) : <EOL> """<STR_LIT>""" <EOL> self . _alpha = percent <EOL> self . call ( '<STR_LIT>' , percent ) <EOL> def line ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , x , y ) <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> def move ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . x = x <EOL> self . y = y <EOL> self . call ( '<STR_LIT>' , x , y ) <EOL> def pen ( self , width = None , rgb = None , alpha = None ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , width , rgb , alpha ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' ) <EOL> def fill ( self , rgb , alpha = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , rgb , alpha ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' ) <EOL> def curve ( self , controlX , controlY , anchorX , anchorY ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , controlX , controlY , anchorX , anchorY ) <EOL> def gradient ( self , type , colors , alphas , ratios , matrix ) : <EOL> """<STR_LIT>""" <EOL> self . call ( '<STR_LIT>' , type , <EOL> l [ [ a ( v = x ) for x in colors ] ] , <EOL> l [ [ a ( v = x ) for x in alphas ] ] , <EOL> l [ [ a ( v = x ) for x in ratios ] ] , <EOL> d [ [ i ( k = k , v = v ) for ( k , v ) in matrix . items ( ) ] ] ) <EOL> def text ( self , text , x , y , height , width ) : <EOL> """<STR_LIT>""" <EOL> cook = cookie ( ) <EOL> t = Text ( cook , self ) <EOL> t . text = text <EOL> self . call ( '<STR_LIT:text>' , cook , text , x , y , height , width ) <EOL> return t <EOL> def image ( self , where ) : <EOL> """<STR_LIT>""" <EOL> cook = cookie ( ) <EOL> I = Image ( cook , self ) <EOL> self . call ( '<STR_LIT:image>' , cook , where ) <EOL> print "<STR_LIT>" , where <EOL> return I <EOL> def sound ( self , where , stream = True ) : <EOL> """<STR_LIT>""" <EOL> cook = cookie ( ) <EOL> S = Sound ( cook , self ) <EOL> self . call ( '<STR_LIT>' , cook , where , stream and <NUM_LIT:1> or <NUM_LIT:0> ) <EOL> return S <EOL> def group ( self ) : <EOL> """<STR_LIT>""" <EOL> cook = cookie ( ) <EOL> G = Group ( '<STR_LIT>' % ( self . groupName , cook ) , self . socket , self ) <EOL> self . call ( '<STR_LIT>' , cook ) <EOL> return G <EOL> class Group ( GroupBase ) : <EOL> def __init__ ( self , groupName , socket , canvas ) : <EOL> self . groupName = groupName <EOL> self . socket = socket <EOL> self . canvas = canvas <EOL> self . deferreds = canvas . deferreds <EOL> closed = property ( lambda self : self . canvas . closed ) <EOL> def setMask ( self , other = None ) : <EOL> """<STR_LIT>""" <EOL> if other is None : <EOL> self . call ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> self . call ( '<STR_LIT>' , other . groupName ) <EOL> def setVisible ( self , visible ) : <EOL> self . call ( '<STR_LIT>' , str ( bool ( visible ) ) ) <EOL> xscale = <NUM_LIT:100> <EOL> yscale = <NUM_LIT:100> <EOL> def scale ( self , x , y ) : <EOL> self . call ( '<STR_LIT>' , x , y ) <EOL> def swapDepth ( self , intOrGroup ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( intOrGroup , Group ) : <EOL> self . call ( '<STR_LIT>' , intOrGroup . groupName ) <EOL> else : <EOL> self . call ( '<STR_LIT>' , intOrGroup ) <EOL> def depth ( self ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:0> <EOL> class CanvasSocket ( GroupBase ) : <EOL> """<STR_LIT>""" <EOL> implements ( inevow . IResource ) <EOL> groupName = '<STR_LIT>' <EOL> closed = False <EOL> def __init__ ( self ) : <EOL> self . canvas = self <EOL> self . d = defer . Deferred ( ) . addErrback ( log . err ) <EOL> def locateChild ( self , ctx , segs ) : <EOL> self . cookie = segs [ <NUM_LIT:0> ] <EOL> return ( self , ( ) ) <EOL> def renderHTTP ( self , ctx ) : <EOL> try : <EOL> self . deferreds = { } <EOL> self . buffer = '<STR_LIT>' <EOL> self . socket = inevow . IRequest ( ctx ) . transport <EOL> self . socket . protocol = self <EOL> self . delegate = _hookup [ self . cookie ] <EOL> self . delegate . onload ( self ) <EOL> del _hookup [ self . cookie ] <EOL> except : <EOL> log . err ( ) <EOL> return self . d <EOL> def dataReceived ( self , data ) : <EOL> self . buffer += data <EOL> while '<STR_LIT>' in self . buffer : <EOL> I = self . buffer . index ( '<STR_LIT>' ) <EOL> message = self . buffer [ : I ] <EOL> self . buffer = self . buffer [ I + <NUM_LIT:1> : ] <EOL> self . gotMessage ( message ) <EOL> def gotMessage ( self , message ) : <EOL> I = message . index ( '<STR_LIT:U+0020>' ) <EOL> handler = getattr ( self , '<STR_LIT>' % ( message [ : I ] , ) , None ) <EOL> if handler is not None : <EOL> handler ( message [ I + <NUM_LIT:1> : ] ) <EOL> else : <EOL> self . deferreds [ message [ : I ] ] . callback ( message [ I + <NUM_LIT:1> : ] ) <EOL> del self . deferreds [ message [ : I ] ] <EOL> def connectionLost ( self , reason ) : <EOL> self . closed = True <EOL> del self . socket <EOL> def done ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closed = True <EOL> self . d . callback ( '<STR_LIT>' ) <EOL> def handle_onKeyDown ( self , info ) : <EOL> if self . delegate . onKeyDown : <EOL> self . delegate . onKeyDown ( self , chr ( int ( info ) ) ) <EOL> def handle_onKeyUp ( self , info ) : <EOL> if self . delegate . onKeyUp : <EOL> self . delegate . onKeyUp ( self , chr ( int ( info ) ) ) <EOL> def handle_onMouseUp ( self , info ) : <EOL> if self . delegate . onMouseUp : <EOL> self . delegate . onMouseUp ( self , * map ( int , map ( float , info . split ( ) ) ) ) <EOL> def handle_onMouseDown ( self , info ) : <EOL> if self . delegate . onMouseDown : <EOL> self . delegate . onMouseDown ( self , * map ( int , map ( float , info . split ( ) ) ) ) <EOL> def handle_onMouseMove ( self , info ) : <EOL> if self . delegate . onMouseMove : <EOL> self . delegate . onMouseMove ( self , * map ( int , map ( float , info . split ( ) ) ) ) <EOL> def handle_diagnostic ( self , info ) : <EOL> print "<STR_LIT>" , info <EOL> canvasServerMessage = loaders . stan ( tags . html [ "<STR_LIT>" ] ) <EOL> def canvas ( width , height , delegate , useCGI = False ) : <EOL> C = cookie ( ) <EOL> if useCGI : <EOL> global _canvasCGIService <EOL> if _canvasCGIService is None : <EOL> from nevow import appserver <EOL> from twisted . internet import reactor <EOL> _canvasCGIService = reactor . listenTCP ( <NUM_LIT:0> , appserver . NevowSite ( Canvas ( docFactory = canvasServerMessage ) ) ) <EOL> _canvasCGIService . dispatchMap = { } <EOL> port = _canvasCGIService . getHost ( ) . port <EOL> prefix = '<STR_LIT:/>' <EOL> movie_url = url . here . click ( '<STR_LIT:/>' ) . secure ( False , port ) <EOL> else : <EOL> movie_url = url . here <EOL> port = lambda c , d : inevow . IRequest ( c ) . transport . server . port <EOL> def prefix ( c , d ) : <EOL> pre = inevow . IRequest ( c ) . path <EOL> if pre . endswith ( '<STR_LIT:/>' ) : <EOL> return pre <EOL> return pre + '<STR_LIT:/>' <EOL> _hookup [ C ] = delegate <EOL> handlerInfo = [ ] <EOL> for handlerName in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if getattr ( delegate , handlerName , None ) is not None : <EOL> handlerInfo . append ( ( handlerName , <NUM_LIT:1> ) ) <EOL> movie_url = movie_url . child ( '<STR_LIT>' ) . add ( '<STR_LIT>' , C ) . add ( '<STR_LIT:port>' , port ) . add ( '<STR_LIT>' , prefix ) <EOL> for ( k , v ) in handlerInfo : <EOL> movie_url = movie_url . add ( k , v ) <EOL> return tags . _object ( classid = "<STR_LIT>" , <EOL> codebase = "<STR_LIT>" , <EOL> width = width , height = height , id = ( "<STR_LIT>" , C ) , align = "<STR_LIT>" ) [ <EOL> tags . param ( name = "<STR_LIT>" , value = "<STR_LIT>" ) , <EOL> tags . param ( name = "<STR_LIT>" , value = movie_url ) , <EOL> tags . param ( name = "<STR_LIT>" , value = "<STR_LIT>" ) , <EOL> tags . param ( name = "<STR_LIT>" , value = "<STR_LIT>" ) , <EOL> tags . param ( name = "<STR_LIT>" , value = "<STR_LIT>" ) , <EOL> Tag ( '<STR_LIT>' ) ( <EOL> src = movie_url , <EOL> quality = "<STR_LIT>" , <EOL> scale = "<STR_LIT>" , <EOL> bgcolor = "<STR_LIT>" , <EOL> width = width , <EOL> height = height , <EOL> name = ( "<STR_LIT>" , C ) , <EOL> align = "<STR_LIT>" , <EOL> allowScriptAccess = "<STR_LIT>" , <EOL> type = "<STR_LIT>" , <EOL> pluginspage = "<STR_LIT>" ) ] <EOL> class Canvas ( rend . Page ) : <EOL> """<STR_LIT>""" <EOL> addSlash = True <EOL> def __init__ ( self , original = None , width = None , height = None , onload = None , <EOL> onMouseMove = None , onMouseDown = None , onMouseUp = None , <EOL> onKeyDown = None , onKeyUp = None , ** kw ) : <EOL> rend . Page . __init__ ( self , original , ** kw ) <EOL> if width : self . width = width <EOL> if height : self . height = height <EOL> if onload : self . onload = onload <EOL> if onMouseMove : self . onMouseMove = onMouseMove <EOL> if onMouseDown : self . onMouseDown = onMouseDown <EOL> if onMouseUp : self . onMouseUp = onMouseUp <EOL> if onKeyDown : self . onKeyDown = onKeyDown <EOL> if onKeyUp : self . onKeyUp = onKeyUp <EOL> def child_canvas_socket ( self , ctx ) : <EOL> return CanvasSocket ( ) <EOL> width = <NUM_LIT:1000> <EOL> height = <NUM_LIT> <EOL> onload = None <EOL> onMouseDown = None <EOL> onMouseUp = None <EOL> onMouseMove = None <EOL> onKeyUp = None <EOL> onKeyDown = None <EOL> def render_canvas ( self , ctx , data ) : <EOL> return canvas ( <EOL> self . width , self . height , self ) <EOL> docFactory = loaders . stan ( tags . html [ render_canvas ] ) <EOL> setattr ( Canvas , '<STR_LIT>' , static . File ( <EOL> util . resource_filename ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ) ) </s>
