<s> import types <EOL> import instanceproperties <EOL> import utils <EOL> def fake ( traverser , ** kw ) : <EOL> return JSObject ( traverser = traverser , ** kw ) <EOL> BASE_MEMBERS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT:data>" , "<STR_LIT>" ] <EOL> class JSObject ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = BASE_MEMBERS <EOL> def __init__ ( self , data = None , traverser = None , callable_ = False , const = False ) : <EOL> self . const = False <EOL> self . traverser = traverser <EOL> self . type_ = "<STR_LIT:object>" <EOL> self . TYPEOF = "<STR_LIT:object>" <EOL> self . data = { } <EOL> if data : <EOL> self . data . update ( data ) <EOL> self . callable = callable_ <EOL> self . const = const <EOL> self . recursing = False <EOL> def get ( self , traverser , name , instantiate = False ) : <EOL> "<STR_LIT>" <EOL> name = unicode ( name ) <EOL> output = None <EOL> if name in self . data : <EOL> output = self . data [ name ] <EOL> if callable ( output ) : <EOL> output = output ( ) <EOL> elif instantiate or name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> output = fake ( traverser ) <EOL> self . set ( name , output , traverser = traverser ) <EOL> if traverser : <EOL> modifier = instanceproperties . get_operation ( "<STR_LIT>" , name ) <EOL> if modifier : <EOL> modifier ( traverser ) <EOL> if output is None : <EOL> return fake ( traverser ) <EOL> return output <EOL> def get_literal_value ( self , traverser = None ) : <EOL> return u"<STR_LIT>" <EOL> def set ( self , name , value , traverser = None , ignore_setters = False ) : <EOL> traverser = self . traverser or traverser <EOL> if traverser and not ignore_setters : <EOL> modifier = instanceproperties . get_operation ( "<STR_LIT>" , name ) <EOL> if modifier : <EOL> modified_value = modifier ( value , traverser ) <EOL> if modified_value is not None : <EOL> value = modified_value <EOL> if traverser : <EOL> if ( self . has_var ( name , traverser ) and <EOL> self . get ( traverser , name ) . const ) : <EOL> traverser . err . warning ( <EOL> err_id = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> warning = "<STR_LIT>" , <EOL> description = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> filename = traverser . filename , <EOL> line = traverser . line , <EOL> column = traverser . position , <EOL> context = traverser . context ) <EOL> return <EOL> self . data [ unicode ( name ) ] = value <EOL> def has_var ( self , name , traverser = None ) : <EOL> return unicode ( name ) in self . data <EOL> def output ( self ) : <EOL> if self . recursing : <EOL> return u"<STR_LIT>" <EOL> self . recursing = True <EOL> output_dict = { } <EOL> for key in self . data . keys ( ) : <EOL> if callable ( self . data [ key ] ) : <EOL> continue <EOL> elif self . data [ key ] == self : <EOL> output_dict [ key ] = u"<STR_LIT>" <EOL> elif self . data [ key ] is None : <EOL> output_dict [ key ] = u"<STR_LIT>" <EOL> else : <EOL> output_dict [ key ] = self . data [ key ] . output ( ) <EOL> self . recursing = False <EOL> return unicode ( output_dict ) <EOL> def __str__ ( self ) : <EOL> return self . output ( ) <EOL> def delete ( self , member ) : <EOL> if member not in self . data : <EOL> return <EOL> del self . data [ member ] <EOL> class JSGlobal ( JSObject ) : <EOL> __slots__ = BASE_MEMBERS + [ "<STR_LIT:name>" , "<STR_LIT>" ] <EOL> def __init__ ( self , global_data , traverser = None , ** kw ) : <EOL> self . global_data = utils . evaluate_lambdas ( traverser , global_data ) <EOL> super ( JSGlobal , self ) . __init__ ( traverser = traverser , ** kw ) <EOL> if "<STR_LIT>" in self . global_data : <EOL> self . TYPEOF = self . global_data [ "<STR_LIT>" ] <EOL> def _get_contents ( self , traverser ) : <EOL> if "<STR_LIT:value>" not in self . global_data : <EOL> return None <EOL> directory = utils . evaluate_lambdas ( <EOL> traverser , self . global_data [ "<STR_LIT:value>" ] ) <EOL> if directory and callable ( self . global_data [ "<STR_LIT:value>" ] ) : <EOL> self . global_data [ "<STR_LIT:value>" ] = directory <EOL> return directory <EOL> def get ( self , traverser , name , instantiate = False ) : <EOL> if name in self . data or instantiate : <EOL> traverser . _debug ( "<STR_LIT>" % name ) <EOL> return super ( JSGlobal , self ) . get ( <EOL> traverser , name , instantiate = instantiate ) <EOL> directory = self . _get_contents ( traverser ) <EOL> if directory and isinstance ( directory , dict ) and name in directory : <EOL> traverser . _debug ( "<STR_LIT>" % name ) <EOL> value = utils . evaluate_lambdas ( traverser , directory [ name ] ) <EOL> if "<STR_LIT>" in value : <EOL> lit = utils . evaluate_lambdas ( traverser , value [ "<STR_LIT>" ] ) <EOL> return JSLiteral ( lit , traverser = traverser ) <EOL> return traverser . _build_global ( name = name , entity = value ) <EOL> traverser . _debug ( "<STR_LIT>" % <EOL> ( name , directory ) ) <EOL> return super ( JSGlobal , self ) . get ( traverser , name ) <EOL> def set ( self , name , value , traverser = None ) : <EOL> directory = self . _get_contents ( traverser ) <EOL> if directory and isinstance ( directory , dict ) and name in directory : <EOL> traverser . _debug ( "<STR_LIT>" % name ) <EOL> obj = self . get ( traverser , name ) <EOL> if not isinstance ( obj , JSLiteral ) : <EOL> obj . _set_to ( value , traverser = traverser ) <EOL> else : <EOL> modified_global = self . global_data . get ( "<STR_LIT:name>" , "<STR_LIT>" ) <EOL> traverser . warning ( <EOL> err_id = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> warning = "<STR_LIT>" , <EOL> description = [ <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( modified_global , name ) ] ) <EOL> return super ( JSGlobal , self ) . set ( name , value , traverser = traverser ) <EOL> def _set_to ( self , value , traverser = None ) : <EOL> "<STR_LIT>" <EOL> traverser . _debug ( "<STR_LIT>" ) <EOL> self . _get_contents ( traverser ) <EOL> if self . global_data . get ( "<STR_LIT>" ) : <EOL> traverser . err . notice ( <EOL> err_id = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> notice = "<STR_LIT>" , <EOL> description = [ "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % <EOL> self . global_data . get ( "<STR_LIT:name>" , "<STR_LIT>" ) ] , <EOL> filename = traverser . filename , <EOL> line = traverser . line , <EOL> column = traverser . position , <EOL> context = traverser . context ) <EOL> def has_var ( self , name , traverser = None ) : <EOL> directory = self . _get_contents ( traverser ) <EOL> if directory and name in directory : <EOL> return True <EOL> return super ( JSGlobal , self ) . has_var ( name , traverser = traverser ) <EOL> def get_literal_value ( self , traverser = None ) : <EOL> traverser = traverser or self . traverser <EOL> if "<STR_LIT>" in self . global_data : <EOL> return utils . evaluate_lambdas ( <EOL> traverser , self . global_data [ "<STR_LIT>" ] ) <EOL> directory = self . _get_contents ( traverser ) <EOL> if directory and not isinstance ( directory , dict ) : <EOL> return directory . get_literal_value ( traverser = traverser ) <EOL> return super ( JSGlobal , self ) . get_literal_value ( traverser ) <EOL> def output ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> getattr ( self , "<STR_LIT:name>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:U+002C>" . join ( "<STR_LIT>" % ( repr ( k ) , repr ( v ) ) for <EOL> k , v in self . global_data . items ( ) ) , <EOL> super ( JSGlobal , self ) . output ( ) ) <EOL> class JSContext ( JSObject ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = BASE_MEMBERS <EOL> def __init__ ( self , context_type = "<STR_LIT:default>" , traverser = None , ** kw ) : <EOL> super ( JSContext , self ) . __init__ ( traverser = traverser , ** kw ) <EOL> self . type_ = context_type <EOL> self . data = kw . get ( "<STR_LIT:data>" , { } ) <EOL> def get ( self , traverser , name , instantiate = False ) : <EOL> "<STR_LIT>" <EOL> name = unicode ( name ) <EOL> output = None <EOL> if name in self . data : <EOL> output = self . data [ name ] <EOL> if callable ( output ) : <EOL> output = output ( ) <EOL> if output is None : <EOL> return fake ( traverser ) <EOL> return output <EOL> LITERAL_TYPEOF = { <EOL> int : "<STR_LIT>" , <EOL> float : "<STR_LIT>" , <EOL> long : "<STR_LIT>" , <EOL> str : "<STR_LIT:string>" , <EOL> unicode : "<STR_LIT:string>" , <EOL> bool : "<STR_LIT>" , <EOL> } <EOL> class JSLiteral ( JSObject ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = BASE_MEMBERS + [ "<STR_LIT:value>" ] <EOL> def __init__ ( self , value = None , traverser = None , ** kw ) : <EOL> super ( JSLiteral , self ) . __init__ ( traverser = traverser , ** kw ) <EOL> if isinstance ( value , JSLiteral ) : <EOL> self . value = value . value <EOL> else : <EOL> self . value = value <EOL> self . TYPEOF = LITERAL_TYPEOF . get ( type ( value ) , "<STR_LIT:object>" ) <EOL> def __str__ ( self ) : <EOL> if isinstance ( self . value , bool ) : <EOL> return unicode ( self . output ( ) ) . lower ( ) <EOL> return unicode ( self . output ( ) ) <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT>' % self . value <EOL> def output ( self ) : <EOL> return self . value <EOL> def get_literal_value ( self , traverser = None ) : <EOL> "<STR_LIT>" <EOL> return self . value <EOL> def has_var ( self , name , traverser = None ) : <EOL> return False <EOL> def delete ( self , member ) : <EOL> pass <EOL> class JSArray ( JSObject ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = BASE_MEMBERS + [ "<STR_LIT>" ] <EOL> def __init__ ( self , elements = None , traverser = None , ** kw ) : <EOL> super ( JSArray , self ) . __init__ ( traverser = traverser , ** kw ) <EOL> self . elements = elements or [ ] <EOL> def get ( self , traverser , index , instantiate = False ) : <EOL> if index == "<STR_LIT>" : <EOL> return JSLiteral ( len ( self . elements ) , traverser = traverser ) <EOL> try : <EOL> el = self . elements [ int ( index . strip ( ) . split ( ) [ <NUM_LIT:0> ] ) ] <EOL> if el is None : <EOL> el = JSLiteral ( None , traverser = traverser ) <EOL> return el <EOL> except ( ValueError , IndexError , KeyError ) : <EOL> return super ( JSArray , self ) . get ( traverser , index , instantiate ) <EOL> def has_var ( self , name , traverser = None ) : <EOL> index = None <EOL> if isinstance ( name , types . StringTypes ) and name . isdigit ( ) : <EOL> index = utils . get_as_num ( name ) <EOL> elif isinstance ( name , int ) : <EOL> index = name <EOL> if index is not None and len ( self . elements ) > index >= <NUM_LIT:0> : <EOL> return True <EOL> return super ( JSArray , self ) . has_var ( name , traverser = traverser ) <EOL> def get_literal_value ( self , traverser = None ) : <EOL> """<STR_LIT>""" <EOL> if self . recursing : <EOL> return u"<STR_LIT>" <EOL> self . recursing = True <EOL> output = u"<STR_LIT:U+002C>" . join ( <EOL> unicode ( w . get_literal_value ( traverser = traverser or <EOL> self . traverser ) if <EOL> w is not None and w is not self else u"<STR_LIT>" ) for <EOL> w in self . elements ) <EOL> self . recursing = False <EOL> return output <EOL> def set ( self , index , value , traverser = None ) : <EOL> if index . isdigit ( ) : <EOL> try : <EOL> i_index = min ( int ( index ) , <NUM_LIT> ) <EOL> if i_index != float ( index ) or i_index < <NUM_LIT:0> : <EOL> return super ( JSArray , self ) . set ( index , value , traverser ) <EOL> if len ( self . elements ) <= i_index : <EOL> for i in xrange ( i_index - len ( self . elements ) + <NUM_LIT:1> ) : <EOL> self . elements . append ( None ) <EOL> self . elements [ i_index ] = value <EOL> return value <EOL> except ValueError : <EOL> return super ( JSArray , self ) . set ( index , value , traverser ) <EOL> else : <EOL> return super ( JSArray , self ) . set ( index , value , traverser ) <EOL> def delete ( self , member ) : <EOL> if member . isdigit ( ) and self . has_var ( member ) : <EOL> index = int ( member ) <EOL> if index == len ( self . elements ) - <NUM_LIT:1> : <EOL> self . elements . pop ( ) <EOL> else : <EOL> self . elements [ member ] = None <EOL> else : <EOL> super ( JSArray , self ) . delete ( member ) <EOL> def output ( self ) : <EOL> return u"<STR_LIT>" % self . get_literal_value ( ) </s>
