Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def unmarshal( self, marshaller ):
for i in xrange( len( self ) ):
%(value_read)s
return self
""" % locals()
class PyStringType(StringType, PyPrimitiveType):
def getBoxedTypeName( self ): return "str"
def getCast( self, var_name ): return """isinstance( %(var_name)s, str ) and %(var_name)s or str( %(var_name)s )""" % locals()
def getDefaultValue( self ): return "\"\""
def getConstantValue( self, value ): return "\"" + value + "\""
class PyStructType(StructType, PyCompoundType):
def __repr__( self ):
name = self.getName()
parent_type_names = ", ".join( self.getParentTypeNames() )
other_methods = lpad( "\n\n", indent( self.getOtherMethods(), INDENT_SPACES ) )
if len( self.getMembers() ) > 0:
init_vars = ", ".join( [member.getIdentifier() + "=" + member.getType().getDefaultValue() for member in self.getMembers()] )
init_statements = ( "\n" + INDENT_SPACES * 2 ).join( ["self." + member.getIdentifier() + " = " + member.getType().getCast( member.getIdentifier() ) for member in self.getMembers()] )
marshal_statements = ( "\n" + INDENT_SPACES * 2 ).join( [member.getIdentifier() + " = " + member.getType().getCast( "self." + member.getIdentifier() ) + "; " + member.getType().getMarshalCall( member.getIdentifier(), member.getIdentifier() ) for member in self.getMembers()] )
unmarshal_statements = ( "\n" + INDENT_SPACES * 2 ).join( [member.getType().getUnmarshalCall( member.getIdentifier(), "self." + member.getIdentifier() ) for member in self.getMembers()] )
return """\
class %(name)s(%(parent_type_names)s):
def __init__( self, %(init_vars)s ):
%(init_statements)s
def marshal( self, marshaller ):
class PyInclude(Include):
def __repr__( self ):
return ""
class PyInterface(Interface):
def __repr__( self ):
name = self.getName()
operations = "\n".join( [INDENT_SPACES + repr( operation ) for operation in self.getOperations()] )
return """\
class %(name)s(object):
%(operations)s
""" % locals()
class PyMapType(MapType, PyCompoundType):
def __repr__( self ):
name = self.getName()
return """
class %(name)s(dict): pass
""" % locals()
class PyNumericType(NumericType, PyPrimitiveType):
def getBoxedTypeName( self ):
if self.getName() == "float" or self.getName() == "double": return "float"
elif self.getName().endswith( "int64_t" ): return "long"
else: return "int"
def getCast( self, var_name ):
if self.getName() == "float" or self.getName() == "double": return "type( %(var_name)s ) == float and %(var_name)s or float( %(var_name)s )" % locals()
else: return "( type( %(var_name)s ) == int or type( %(var_name)s ) == long ) and %(var_name)s or long( %(var_name)s )" % locals()
name = self.getName()
param_decls = ["self"]
for param in self.getParameters():
assert not param.isOutbound()
param_decls.append( param.getIdentifier() )
param_decls = pad( " ", ", ".join( param_decls ), " " )
return "def %(name)s(%(param_decls)s): raise NotImplementedError" % locals()
class PyPointerType(PointerType, PyPrimitiveType):
def getBoxedTypeName( self ): return "buffer"
def getCast( self, var_name ): return var_name
def getDefaultValue( self ): return "\"\""
class PySequenceType(SequenceType, PyCompoundType):
def __repr__( self ):
name = self.getName()
value_new = self.getValueType().getBoxedTypeName()
value_cast = self.getValueType().getCast( "value" )
value_write = self.getValueType().getMarshalCall( "( *this )[i]", "value" )
value_read = self.getValueType().getUnmarshalCall( "( *this )[i]", "self[i]" )
return """
class %(name)s(list):
def __init__( self, init_list=[] ):
list.__init__( self )
self.extend( init_list )
def resize( self, to_len ): list.extend( self, [%(value_new)s() for i in xrange( to_len )] )
def extend( self, values ): list.extend( self, [%(value_cast)s for value in values] )
def append( self, value ): list.append( self, %(value_cast)s )
def __setvalue__( self, key, value ): list.__setvalue__( self, key, %(value_cast)s )