#NEWLINE# def create_js_hasevents_class( cls , cls_name , base_class = 'HasEvents.prototype' ) : #NEWLINE# #INDENT# ' Create the JS equivalent of a subclass of the HasEvents class.\n    \n    Given a Python class with handlers, properties and emitters, this\n    creates the code for the JS version of this class. It also supports\n    class constants that are int/float/str, or a tuple/list thereof.\n    ' #NEWLINE# assert ( cls_name != 'HasEvents' ) #NEWLINE# handlers = [ ] #NEWLINE# emitters = [ ] #NEWLINE# properties = [ ] #NEWLINE# total_code = [ ] #NEWLINE# funcs_code = [ ] #NEWLINE# const_code = [ ] #NEWLINE# err = 'Objects on JS HasEvents classes can only be int, float, str, or a list/tuple thereof. Not %s -> %r.' #NEWLINE# total_code . extend ( get_class_definition ( cls_name , base_class ) ) #NEWLINE# prefix = ( '' if cls_name . count ( '.' ) else 'var ' ) #NEWLINE# total_code [ 0 ] = ( prefix + total_code [ 0 ] ) #NEWLINE# for ( name , val ) in sorted ( cls . __dict__ . items ( ) ) : #NEWLINE# #INDENT# name = name . replace ( '_JS__' , ( '_%s__' % cls_name . split ( '.' ) [ ( - 1 ) ] ) ) #NEWLINE# funcname = ( ( '_' + name ) + '_func' ) #NEWLINE# if isinstance ( val , BaseEmitter ) : #NEWLINE# #INDENT# if isinstance ( val , Property ) : #NEWLINE# #INDENT# properties . append ( name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# emitters . append ( name ) #NEWLINE# #UNINDENT# code = py2js ( val . _func , ( ( cls_name + '.prototype.' ) + funcname ) ) #NEWLINE# code = code . replace ( 'super()' , base_class ) #NEWLINE# funcs_code . append ( code . rstrip ( ) ) #NEWLINE# t = '%s.prototype.%s.nobind = true;' #NEWLINE# funcs_code . append ( ( t % ( cls_name , funcname ) ) ) #NEWLINE# t = '%s.prototype.%s._emitter_type = %s;' #NEWLINE# emitter_type = val . __class__ . __name__ #NEWLINE# funcs_code . append ( ( t % ( cls_name , funcname , reprs ( emitter_type ) ) ) ) #NEWLINE# funcs_code . append ( '' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( val , HandlerDescriptor ) : #NEWLINE# #INDENT# handlers . append ( name ) #NEWLINE# code = py2js ( val . _func , ( ( cls_name + '.prototype.' ) + funcname ) ) #NEWLINE# code = code . replace ( 'super()' , base_class ) #NEWLINE# funcs_code . append ( code . rstrip ( ) ) #NEWLINE# t = '%s.prototype.%s.nobind = true;' #NEWLINE# funcs_code . append ( ( t % ( cls_name , funcname ) ) ) #NEWLINE# t = '%s.prototype.%s._connection_strings = %s;' #NEWLINE# funcs_code . append ( ( t % ( cls_name , funcname , reprs ( val . _connection_strings ) ) ) ) #NEWLINE# funcs_code . append ( '' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if callable ( val ) : #NEWLINE# #INDENT# code = py2js ( val , ( ( cls_name + '.prototype.' ) + name ) ) #NEWLINE# code = code . replace ( 'super()' , base_class ) #NEWLINE# funcs_code . append ( code . rstrip ( ) ) #NEWLINE# funcs_code . append ( '' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if name . startswith ( '__' ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# serialized = json . dumps ( val ) #NEWLINE# #UNINDENT# except Exception as err : #NEWLINE# #INDENT# raise ValueError ( ( 'Attributes on JS HasEvents class must be JSON compatible.\n%s' % str ( err ) ) ) #NEWLINE# #UNINDENT# const_code . append ( ( '%s.prototype.%s = JSON.parse(%s)' % ( cls_name , name , reprs ( serialized ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( base_class in ( 'Object' , 'HasEvents.prototype' ) ) : #NEWLINE# #INDENT# t = '%s.prototype.__emitters__ = %s;' #NEWLINE# total_code . append ( ( t % ( cls_name , reprs ( list ( sorted ( emitters ) ) ) ) ) ) #NEWLINE# t = '%s.prototype.__properties__ = %s;' #NEWLINE# total_code . append ( ( t % ( cls_name , reprs ( list ( sorted ( properties ) ) ) ) ) ) #NEWLINE# t = '%s.prototype.__handlers__ = %s;' #NEWLINE# total_code . append ( ( t % ( cls_name , reprs ( list ( sorted ( handlers ) ) ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# t = '%s.prototype.__emitters__ = %s.__emitters__.concat(%s).sort();' #NEWLINE# total_code . append ( ( t % ( cls_name , base_class , reprs ( emitters ) ) ) ) #NEWLINE# t = '%s.prototype.__properties__ = %s.__properties__.concat(%s).sort();' #NEWLINE# total_code . append ( ( t % ( cls_name , base_class , reprs ( properties ) ) ) ) #NEWLINE# t = '%s.prototype.__handlers__ = %s.__handlers__.concat(%s).sort();' #NEWLINE# total_code . append ( ( t % ( cls_name , base_class , reprs ( handlers ) ) ) ) #NEWLINE# #UNINDENT# total_code . append ( '' ) #NEWLINE# total_code . extend ( const_code ) #NEWLINE# total_code . append ( '' ) #NEWLINE# total_code . extend ( funcs_code ) #NEWLINE# return '\n' . join ( total_code )