def write_application(package, report_class): """ Write java class which extends android application. I will handle shared information as the report Returns a dictionary with the information to add to Manifest file. """ # Get the application class file. output_directory = get_filepath(ACTIVITIES) if (not exists(output_directory)): makedirs(output_directory) application_template_filename = get_property(TEMPLATES_SECTION, APPLICATION_FILE) application_filename = join(output_directory, (Template(application_template_filename). safe_substitute(CLASS_NAME=report_class))) # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, APPLICATION) template = environment.get_template(template_name) model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL) # Open and write the manifest file app_file = open(application_filename, 'w') app_file.write(template.render(package=package, model_package=model_package, report_class=report_class)) app_file.close() return {'name':report_class+'_Application', 'launcher':False}
def write_application(package, report_class): """ Write java class which extends android application. I will handle shared information as the report Returns a dictionary with the information to add to Manifest file. """ # Get the application class file. output_directory = get_filepath(ACTIVITIES) if (not exists(output_directory)): makedirs(output_directory) application_template_filename = get_property(TEMPLATES_SECTION, APPLICATION_FILE) application_filename = join( output_directory, (Template(application_template_filename).safe_substitute( CLASS_NAME=report_class))) # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, APPLICATION) template = environment.get_template(template_name) model_package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL) # Open and write the manifest file app_file = open(application_filename, 'w') app_file.write( template.render(package=package, model_package=model_package, report_class=report_class)) app_file.close() return {'name': report_class + '_Application', 'launcher': False}
def write_template(template, languages, xml_files, report=None): """ Write a piece of localized template Keyword Arguments: template -- template name for being localized and written. language -- Language code, following i18n, to write. xml_file -- file open for write where the template should be written. (default None) report -- dicom report where information is taken. """ # Set the section of the template name for the strings. if (report is not None): if (template in TEMPLATE_BY_ID): section = report.get_ontology() + " " + template elif (template in TEMPLATE_BY_REPORT): section = template else: section = template # Get the template path and its filename template_path, template_filename = get_template_filename(template) # The template is STRING type. # This handler only get string templates. So this check is redundant # if (template in STRING_TEMPLATES): env = set_environment(STRING_TEMPLATES_PATH) # Localize the template for this language # Get localized strings from Report Data if (template in TEMPLATE_BY_REPORT): localized_strings = get_localized_report(env, template_filename, languages, template, report) #Templates localized by Report Ontology ID or default strings. else: localized_strings = substitute_words(env, section, template_filename, languages, template) #Write the localized string for language, localized_string in localized_strings.iteritems(): xml_files[language].write( u'{0}'.format(localized_string).encode('utf-8'))
def write_one_column_layout_one_level(layout_filename, container, children, children_layout): """ Write a layout with one columns where there are only children Keyword Arguments: report_level -- level of the report to write xml_filename -- file name where the layout should be written dict_level -- DICOM report where information is to write in the xml layout """ #If the concept have already generate a layout don't do it again. if (not isfile(layout_filename)): #print(" * {0}".format(container)) layout_file = open(layout_filename, 'w') # Set the Environment for the jinja2 templates. environment = set_environment(LAYOUT_TEMPLATES_PATH) # There are ONLY CHILDREN in this layout if (len(children) > 0): # Get the template template_name = get_property(LAYOUT_TEMPLATES_SECTION, ONE_COLUMN) template = environment.get_template(template_name) # Store previous concept id previous_item = "code_{0}".format(container.get_code()).lower() items, current_item = get_children(environment, container.get_concept(), previous_item, children_layout) # Render layout template with correct values. layout_file.write(template.render(level_code=container.get_code().lower(), content=items) .encode('utf-8')) layout_file.close() # TODO: There are ONLY ATTRIBUTES in this layout if (len(container.attributes) > 0): pass else: print "Layout {0} already created".format(layout_filename)
def write_manifest(package_name, activities,app_name): """ Write the Android Manifest file """ # Get the AndroidManifest output_directory = get_filepath(MANIFEST) if (not exists(output_directory)): makedirs(output_directory) manifest_filename = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST) manifest_path = join(output_directory, manifest_filename) # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST) template = environment.get_template(template_name) # Open and write the manifest file manifest_file = open(manifest_path, 'w') manifest_file.write(template.render(package_name=package_name, app_name=app_name, activities=activities)) manifest_file.close()
def write_manifest(package_name, activities, app_name): """ Write the Android Manifest file """ # Get the AndroidManifest output_directory = get_filepath(MANIFEST) if (not exists(output_directory)): makedirs(output_directory) manifest_filename = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST) manifest_path = join(output_directory, manifest_filename) # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) template_name = get_property(ACTIVITIES_TEMPLATES_SECTION, MANIFEST) template = environment.get_template(template_name) # Open and write the manifest file manifest_file = open(manifest_path, 'w') manifest_file.write( template.render(package_name=package_name, app_name=app_name, activities=activities)) manifest_file.close()
def write_activities(activities_filenames, report): flat = report.get_flat_data() #Get the ontology id of the report ontology_id = report.get_ontology() # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) # Get report root container. This will be the launcher activity. report_root = report.get_root().get_schema_code() # Variables to store data activities = [] launcher_activity = "" package = get_property(ANDROID_PACKAGES, BASE_MODEL) # Get containers position. It must match strings-array position. position = {} cardinality = {} report.get_data_from_report(CHILDREN_ARRAYS, position=position, cardinality=cardinality) report_class = report_root.lower().capitalize() app_classname = report_class+'_Application' old_container = None aux_dict = {} #Write layout for every file for container, children in report.report.depthFirstChildren(): actual_level = container.get_level() #for container, children in flat.iteritems(): parent_container = None if len(children): aux_dict[container.get_level()] = container activity = {} # Get this container's parent parent = None if actual_level is not 1: parent = aux_dict[actual_level - 1] if parent: parent_code = parent.get_code() parent_schema = parent.get_schema() else: parent_code, parent_schema = None, None activity_filename = get_activity_filename(activities_filenames, flat, container, parent_code, parent_schema) activity_name = get_activity_name(activity_filename) # Store info to write the Android Manifest # Check if this activity is the launcher # TODO : (on liner) activity['launcher'] = (container.get_schema_code() == report_root) if (container.get_schema_code() == report_root): activity['launcher'] = True else: activity['launcher'] = False activity['name'] = activity_name activities.append(activity) # Log purpose info #print "[Level {0}] {1}".format(container.tree_level, children_layout) #print activity_filename, package, a_name write_activity_file(environment, ontology_id, package, activities_filenames, activity_filename, activity_name, container, children, position, cardinality, parent, report_class, app_classname) old_container = container #print type(old_container) activity = write_application(package, report_class) activities.append(activity) write_manifest(package, activities, '.'+app_classname)
def write_model(java_filenames, report, language_code): template_model_file = get_template_model_file() # Set the Environment for the jinja2 templates and get the template environment = set_environment(MODEL_TEMPLATES_PATH) package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL) # Store an ids list of expandable. expandables = [] flat_tree = {} written = [] #Write model for every container. for container, children in report.report.depthFirstChildren(): imports = [] #Get parent code, parent schema and grandparent class parent_code, parent_schema, gparent_class = get_parent_class(flat_tree, container) # Build this container java filename using its parent codes and its own class_name = get_class_name(container.get_schema(), container.get_code(), parent_schema, parent_code).replace('-', '_') # Build a parent/children codes hash table. # We will use it for parent_code and schema. if (children): add_tree_hierarchy(flat_tree, container, children, class_name) model_filename = get_model_file(template_model_file, class_name) # Write model if (not isfile(model_filename)): model_file = open(model_filename, 'w') # Get the attributes. attributes, methods = get_attributes(environment, container.attributes, imports) # Get children attributes and its methods parent_class = (container.get_schema().lower().capitalize() + '_' + container.get_code().lower()) c_attributes, c_methods = get_children(environment, children, imports, parent_class, class_name, expandables, template_model_file, package) attributes.extend(c_attributes) methods.extend(c_methods) #Write the model # If this class will be expandable in activity. # Child class should extend its parent interface. template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE) template = environment.get_template(template_name) # If this class has expandables it has to implement # the children interface class has_multiple_children = container.properties.max_cardinality == -1 if (class_name in expandables and has_multiple_children): implement_class = Template(IMPLEMENTS).safe_substitute( PARENT_CLASS=gparent_class + CHILD_CLASS) model_file.write( template.render(package=package, class_type=CLASS, class_name=class_name, attributes=attributes, imports=imports, implements_class=implement_class, methods=methods)) else: model_file.write( template.render(package=package, class_type=CLASS, class_name=class_name, attributes=attributes, imports=imports, methods=methods)) model_file.close() else: print "Java class {0} already created".format(model_filename)
def write_two_columns_layout(layout_filename, container, children, children_layout, language_code): #print container.concept, children #If the concept have already generate a layout don't replicate. if (not isfile(layout_filename)): layout_file = open(layout_filename, 'w') # print(" * {0}".format(container)) # Set the Environment for the jinja2 templates. environment = set_environment(LAYOUT_TEMPLATES_PATH) # Get templates # Layout template template_name = get_property(LAYOUT_TEMPLATES_SECTION, TWO_COLUMNS) template = environment.get_template(template_name) #Atrtibutes template attributes_template = environment.get_template( get_property(LAYOUT_TEMPLATES_SECTION, ATTRIBUTES)) # Store previous concept id layout_prev_item = "code_{0}".format(container.get_code().lower()) left_content = "" right_content = "" # There are ONLY ATTRIBUTES in this layout if (len(container.attributes) > 0 and len(children) == 0): # Split attributes in two columns num_attributes = len(container.attributes) left_attributes = container.attributes[0:(num_attributes / 2)] right_attributes = container.attributes[(num_attributes / 2):] # Get the attributes list left_items, previous_item = get_attributes_list(environment, left_attributes, layout_prev_item, language_code) left_content = attributes_template.render( previous_item=layout_prev_item, items=left_items) right_items, previous_item = get_attributes_list( environment, right_attributes, previous_item, language_code) right_content = attributes_template.render( previous_item='right_layout', items=right_items) # There are ATTRIBUTES and CHILDREN in this layout elif((len(container.attributes) > 0) and (len(children) > 0)): attributes = container.attributes concept = container.get_concept() # Get the attributes list left_items, previous_item = get_attributes_list(environment, attributes, layout_prev_item, language_code) left_content = attributes_template.render( previous_item=layout_prev_item, items=left_items) right_content, previous_item = get_children(environment, concept, previous_item, children_layout) try: # Render layout template with correct values. layout_file.write(template.render(level_code=container.get_code().lower(), left_content=left_content, right_content=right_content) .encode('utf-8')) layout_file.close() except NameError: layout_file.close() print "Error generating layout items for layout {0}".format( layout_filename) else: print "Layout {0} already created".format(layout_filename)
def write_activities(activities_filenames, report): flat = report.get_flat_data() #Get the ontology id of the report ontology_id = report.get_ontology() # Set the Environment for the jinja2 templates and get the template environment = set_environment(ACTIVITIES_TEMPLATES_PATH) # Get report root container. This will be the launcher activity. report_root = report.get_root().get_schema_code() # Variables to store data activities = [] launcher_activity = "" package = get_property(ANDROID_PACKAGES, BASE_MODEL) # Get containers position. It must match strings-array position. position = {} cardinality = {} report.get_data_from_report(CHILDREN_ARRAYS, position=position, cardinality=cardinality) report_class = report_root.lower().capitalize() app_classname = report_class + '_Application' old_container = None aux_dict = {} #Write layout for every file for container, children in report.report.depthFirstChildren(): actual_level = container.get_level() #for container, children in flat.iteritems(): parent_container = None if len(children): aux_dict[container.get_level()] = container activity = {} # Get this container's parent parent = None if actual_level is not 1: parent = aux_dict[actual_level - 1] if parent: parent_code = parent.get_code() parent_schema = parent.get_schema() else: parent_code, parent_schema = None, None activity_filename = get_activity_filename(activities_filenames, flat, container, parent_code, parent_schema) activity_name = get_activity_name(activity_filename) # Store info to write the Android Manifest # Check if this activity is the launcher # TODO : (on liner) activity['launcher'] = (container.get_schema_code() == report_root) if (container.get_schema_code() == report_root): activity['launcher'] = True else: activity['launcher'] = False activity['name'] = activity_name activities.append(activity) # Log purpose info #print "[Level {0}] {1}".format(container.tree_level, children_layout) #print activity_filename, package, a_name write_activity_file(environment, ontology_id, package, activities_filenames, activity_filename, activity_name, container, children, position, cardinality, parent, report_class, app_classname) old_container = container #print type(old_container) activity = write_application(package, report_class) activities.append(activity) write_manifest(package, activities, '.' + app_classname)
def write_model(java_filenames, report, language_code): template_model_file = get_template_model_file() # Set the Environment for the jinja2 templates and get the template environment = set_environment(MODEL_TEMPLATES_PATH) package = get_property(ANDROID_PACKAGES, PACKAGE_MODEL) # Store an ids list of expandable. expandables = [] flat_tree = {} written = [] #Write model for every container. for container, children in report.report.depthFirstChildren(): imports = [] #Get parent code, parent schema and grandparent class parent_code, parent_schema, gparent_class = get_parent_class( flat_tree, container) # Build this container java filename using its parent codes and its own class_name = get_class_name(container.get_schema(), container.get_code(), parent_schema, parent_code).replace('-', '_') # Build a parent/children codes hash table. # We will use it for parent_code and schema. if (children): add_tree_hierarchy(flat_tree, container, children, class_name) model_filename = get_model_file(template_model_file, class_name) # Write model if (not isfile(model_filename)): model_file = open(model_filename, 'w') # Get the attributes. attributes, methods = get_attributes(environment, container.attributes, imports) # Get children attributes and its methods parent_class = (container.get_schema().lower().capitalize() + '_' + container.get_code().lower()) c_attributes, c_methods = get_children(environment, children, imports, parent_class, class_name, expandables, template_model_file, package) attributes.extend(c_attributes) methods.extend(c_methods) #Write the model # If this class will be expandable in activity. # Child class should extend its parent interface. template_name = get_property(MODEL_TEMPLATES_SECTION, CLASS_TEMPLATE) template = environment.get_template(template_name) # If this class has expandables it has to implement # the children interface class has_multiple_children = container.properties.max_cardinality == -1 if (class_name in expandables and has_multiple_children): implement_class = Template(IMPLEMENTS).safe_substitute( PARENT_CLASS=gparent_class + CHILD_CLASS) model_file.write( template.render(package=package, class_type=CLASS, class_name=class_name, attributes=attributes, imports=imports, implements_class=implement_class, methods=methods)) else: model_file.write( template.render(package=package, class_type=CLASS, class_name=class_name, attributes=attributes, imports=imports, methods=methods)) model_file.close() else: print "Java class {0} already created".format(model_filename)