<s> import time <EOL> import os . path <EOL> import inflector <EOL> from json_to_model . parser import NodeType <EOL> from jinja2 import Environment , PackageLoader <EOL> type_map = { <EOL> NodeType . TYPE_ARRAY : '<STR_LIT>' , <EOL> NodeType . TYPE_BOOL : '<STR_LIT>' , <EOL> NodeType . TYPE_INT : '<STR_LIT>' , <EOL> NodeType . TYPE_FLOAT : '<STR_LIT>' , <EOL> NodeType . TYPE_STRING : '<STR_LIT>' , <EOL> } <EOL> retain_map = { <EOL> NodeType . TYPE_OBJECT : '<STR_LIT>' , <EOL> NodeType . TYPE_ARRAY : '<STR_LIT>' , <EOL> NodeType . TYPE_BOOL : '<STR_LIT>' , <EOL> NodeType . TYPE_INT : '<STR_LIT>' , <EOL> NodeType . TYPE_FLOAT : '<STR_LIT>' , <EOL> NodeType . TYPE_STRING : '<STR_LIT>' , <EOL> } <EOL> english = inflector . English ( ) <EOL> name_table = { <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> } <EOL> def get_property_type ( context , node ) : <EOL> result = type_map . get ( node . type , None ) <EOL> if result is None : <EOL> return '<STR_LIT>' % english . classify ( node . class_name ) <EOL> return result <EOL> def get_property_name ( context , node ) : <EOL> name = english . variablize ( node . name ) <EOL> if name in name_table : <EOL> return name_table [ name ] <EOL> return name <EOL> def get_type_of_first_obj_in_array ( context , node ) : <EOL> if node . type == NodeType . TYPE_ARRAY : <EOL> if len ( node . children ) > <NUM_LIT:0> : <EOL> return get_property_type ( context , node . children [ <NUM_LIT:0> ] ) <EOL> return None <EOL> def property_is_inherited ( context , node , property ) : <EOL> c = node . super_class <EOL> found = False <EOL> while c is not None : <EOL> found = property in c . children <EOL> c = c . super_class <EOL> return found <EOL> def gen_code ( pathname , context ) : <EOL> env = Environment ( loader = PackageLoader ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> header_template = env . get_template ( '<STR_LIT>' ) <EOL> source_template = env . get_template ( '<STR_LIT>' ) <EOL> for class_name , clazz in context . classes . iteritems ( ) : <EOL> original_name = class_name <EOL> class_name = english . classify ( original_name ) <EOL> properties = [ ] <EOL> includes = set ( ) <EOL> for node in clazz . children : <EOL> if property_is_inherited ( context , clazz , node ) : <EOL> continue <EOL> properties . append ( { <EOL> '<STR_LIT:type>' : get_property_type ( context , node ) , <EOL> '<STR_LIT:name>' : get_property_name ( context , node ) , <EOL> '<STR_LIT>' : node . name , <EOL> '<STR_LIT>' : retain_map [ node . type ] , <EOL> '<STR_LIT>' : get_type_of_first_obj_in_array ( context , node ) <EOL> } ) <EOL> if node . class_name : <EOL> includes . add ( get_property_type ( context , node ) ) <EOL> if node . children : <EOL> if node . children [ <NUM_LIT:0> ] . class_name : <EOL> includes . add ( get_property_type ( context , node . children [ <NUM_LIT:0> ] ) ) <EOL> super_name = english . classify ( clazz . super_class . class_name ) if clazz . super_class else '<STR_LIT>' <EOL> if super_name != '<STR_LIT>' : <EOL> includes . add ( super_name ) <EOL> with open ( os . path . join ( pathname , '<STR_LIT>' % class_name ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( header_template . render ( time = time . ctime ( ) , <EOL> class_name = class_name , <EOL> super_name = super_name , <EOL> properties = properties , <EOL> includes = includes ) ) <EOL> with open ( os . path . join ( pathname , '<STR_LIT>' % class_name ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( source_template . render ( time = time . ctime ( ) , <EOL> class_name = class_name , <EOL> super_name = super_name , <EOL> properties = properties ) ) </s>
