def __init__(self, language, package, filetype='', valid_type='', elements=None): self.up_package = strFunctions.upper_first(package) self.valid_type = strFunctions.upper_first(valid_type) self.file_type = filetype if filetype == 'main': self.name = '{0}Validator'.format(self.up_package) elif filetype == 'constraints': self.name = '{0}{1}ConsistencyConstraints'.format(self.up_package, self.valid_type) elif filetype == 'consistency': self.name = '{0}{1}ConsistencyValidator'.format(self.up_package, self.valid_type) self.baseClass = '{0}Validator'.format(self.up_package) elif filetype == 'declared': self.name = '{0}{1}ConsistencyConstraints' \ 'Declared'.format(self.up_package, self.valid_type) self.brief_description = \ 'Definition of {0}.'.format(self.name) if filetype == 'declared': BaseCppFile.BaseCppFile.__init__(self, self.name, 'cxx', None) else: BaseCppFile.BaseCppFile.__init__(self, self.name, 'cpp', None) self.language = language self.package = package if filetype == 'consistency': self.baseClass = '{0}Validator'.format(self.up_package) self.elements = elements
def __init__(self, object_desc, spec_name, number, package, pkg_ref): # members from object self.name = object_desc['name'] self.fullname = spec_name self.number = number self.package = package.lower() self.pkg_ref = pkg_ref self.up_package = strFunctions.upper_first(self.package) # useful repeated text strings self.valid = '\\validRule{' self.start_b = '{' self.end_b = '}' self.lower_name = strFunctions.lower_first(strFunctions.remove_prefix(self.name)) self.formatted_name = '\{0}'.format(strFunctions.remove_prefix(self.name)) self.indef = strFunctions.get_indefinite(self.lower_name) self.indef_u = strFunctions.upper_first(self.indef) self.reqd_att = [] self.opt_att = [] self.reqd_elem = [] self.opt_elem = [] self.reqd_child_lo_elem = [] self.opt_child_lo_elem = [] self.parse_attributes(self, object_desc['attribs']) self.parse_elements(self, object_desc['attribs'], object_desc['root']) self.rules = [] self.tc = 'TBC'
def __init__(self, language, package, filetype='', valid_type=''): self.language = language self.cap_language = language.upper() self.up_package = strFunctions.upper_first(package) self.valid_type = strFunctions.upper_first(valid_type) self.file_type = filetype if filetype == 'main': self.name = '{0}Validator'.format(self.up_package) elif filetype == 'consistency': self.name = '{0}{1}ConsistencyValidator'.format(self.up_package, self.valid_type) self.baseClass = '{0}Validator'.format(self.up_package) if valid_type == '': self.category = 'LIB{0}_CAT_GENERAL_' \ 'CONSISTENCY'.format(self.cap_language) else: self.category = 'LIB{0}_CAT_IDENTIFIER_' \ 'CONSISTENCY'.format(self.cap_language) self.brief_description = \ 'Definition of {0}.'.format(self.name) BaseCppFile.BaseCppFile.__init__(self, self.name, 'h', None) self.package = package if filetype == 'consistency': self.baseClass = '{0}Validator'.format(self.up_package)
def __init__(self, language, class_name, is_header, elements=None, package='', std_base=''): self.language = language self.up_language = strFunctions.upper_first(language) self.cap_language = language.upper() self.class_name = class_name self.std_base = std_base self.is_header = is_header self.elements = elements self.package = package self.up_package = strFunctions.upper_first(package)
def __init__(self, name, package, elements, plugins, is_header): self.package = package.lower() self.cap_package = self.package.upper() self.up_package = strFunctions.upper_first(self.package) self.is_header = is_header if is_header: self.fileout = BaseCppFile.BaseCppFile(name, 'h', None) self.fileout.brief_description = 'SWIG header file' else: self.fileout = BaseCppFile.BaseCppFile(name, 'i', None) self.fileout.brief_description = 'SWIG local file' self.elements = elements self.plugins = plugins self.language = global_variables.language self.cap_language = global_variables.language.upper() self.base = global_variables.std_base self.ext_dir = '{0}/packages/{1}/extension/'.format(self.language, self.package) self.common_dir = '{0}/packages/{1}/common/'.format(self.language, self.package) self.base_dir = '{0}/packages/{1}/{0}/'.format(self.language, self.package)
def __init__(self, spec_name, number, package, pkg_ref, level, version, pkg_version, reqd_status): # members from object self.fullname = spec_name self.number = number self.offset = number self.package = package.lower() self.pkg_ref = pkg_ref self.up_package = strFunctions.upper_first(self.package) if reqd_status: self.reqd_status = 'true' else: self.reqd_status = 'false' self.full_pkg_command = \ '\\{0}Package'.format(strFunctions.texify(self.fullname)) # useful repeated text strings self.valid = '\\validRule{' self.start_b = '{' self.end_b = '}' self.level = level self.version = version self.pkg_version = pkg_version self.rules = [] self.tc = 'TBC'
def get_loclass_name_value(self, node, name): xml_loclass_name = '' temp = self.get_value(node, name) # we expect this to be camel case starting with lower if temp is not None: xml_loclass_name = strFunctions.upper_first(temp) return xml_loclass_name
def write_macro_for_listof(self, sbml_class): if sbml_class['hasListOf']: if 'lo_elementName' in sbml_class \ and sbml_class['lo_elementName'] != '': lo_name = strFunctions.upper_first( sbml_class['lo_elementName']) else: lo_name = strFunctions.cap_list_of_name(sbml_class['name']) self.write_line('\\newcommand{0}\\{1}{2}{0}\\defRef{0}{1}{2}' '{0}{3}{2}{2}' .format(self.start_b, lo_name, self.end_b, strFunctions.make_class(lo_name))) # hack for render if sbml_class['name'] == 'GradientBase': self.write_line('\\newcommand{0}\\{4}{2}{0}\\defRef{0}{1}{2}' '{0}{3}{2}{2}' .format(self.start_b, lo_name, self.end_b, strFunctions.make_class(lo_name), 'ListOfGradientBases')) elif sbml_class['name'] == 'RenderPoint': self.write_line('\\newcommand{0}\\{4}{2}{0}\\defRef{0}{1}{2}' '{0}{3}{2}{2}' .format(self.start_b, lo_name, self.end_b, strFunctions.make_class(lo_name), 'ListOfRenderPoints'))
def get_element_class_name(self, node): class_name = None temp = self.get_value(node, 'name') # expect camelcase with upper first if temp is not None: class_name = strFunctions.upper_first(temp) return class_name
def write_class_comments(self, extension, plugin, validator): fullname = global_variables.package_full_name up_package = strFunctions.upper_first(self.package) validator_class_comment = 'The {0} class extends the ' \ 'Validator class from core libSBML to ' \ 'apply validation to the constructs ' \ 'introduced by the SBML Level 3 ' \ '{1} package. This class then acts as a ' \ 'base class for any validators that ' \ 'apply rules to the “{2}” ' \ 'package specification constructs or to ' \ 'entire models that use the “{2}' \ '” package, and may therefore be ' \ 'subject to other global restrictions ' \ 'introduced.'.format(self.name, fullname, self.package.lower()) self.write_blank_comment_line() self.write_comment_line('@class {0}'.format(self.class_name)) if extension: self.write_comment_line('@sbmlbrief{0}{1}{2} Base extension class' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@class {0}PkgNamespaces' ''.format(up_package)) self.write_comment_line('@sbmlbrief{0}{1}{2} SBMLNamespaces ' 'extension.' ''.format(self.open_br, self.package.lower(), self.close_br)) elif plugin: self.write_comment_line('@sbmlbrief{0}{1}{2} Extension of ' '{3}.'.format(self.open_br, self.package.lower(), self.close_br, self.class_object['sbase'])) elif validator: self.write_comment_line('@sbmlbrief{0}{1}{2} Entry point for ' '“{1}&rdquo package validation' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') self.write_blank_comment_line() self.write_comment_line('@copydetails doc_common_intro_' 'package_validators') self.write_blank_comment_line() self.write_comment_line('{0}'.format(validator_class_comment)) self.write_blank_comment_line() self.write_comment_line('@copydetails doc_section_package_' 'validators_general_info') else: self.write_comment_line('@sbmlbrief{0}{1}{2} TODO:' '{3}'.format(self.open_br, self.package.lower(), self.close_br, self.brief_description))
def __init__(self, package): self.up_package = strFunctions.upper_first(package['name']) self.name = '{0}Extension'.format(self.up_package) self.brief_description = \ 'Implementation of {0}.'.format(self.name) BaseCppFile.BaseCppFile.__init__(self, self.name, 'cpp', None) # members from object self.package = package['name'] self.cap_package = package['name'].upper() self.baseClass = '{0}Extension'.format(self.cap_language) self.elements = package['elements'] self.number = package['number'] self.enums = package['enums'] self.plugins = package['plugins'] self.offset = package['offset'] self.num_versions = 1 if 'num_versions' in package: self.num_versions = package['num_versions'] # create a class object so we can just reuse code self.class_object['package'] = self.package self.class_object['name'] = self.name self.class_object['concretes'] = [] self.class_object['baseClass'] = self.baseClass self.class_object['attribs'] = [] self.class_object['has_children'] = False self.class_object['child_elements'] = [] self.class_object['overwrites_children'] = False
def create_class_description(self, num): if num >= len(self.package['plugins']): class_object = self.create_package_info_plugin_desc() else: class_object = self.package['plugins'][num] up_package = strFunctions.upper_first(self.package['name']) class_object['name'] = '{0}{1}Plugin'.format(up_package, class_object['sbase']) class_object['is_plugin'] = True class_object['is_list_of'] = False class_object['hasListOf'] = False class_object['package'] = self.package['name'] class_object['typecode'] = '' # class_object['baseClass'] = 'SBasePlugin' class_object['baseClass'] = 'AbstractSBasePlugin' class_object['sid_refs'] = [] class_object['unit_sid_refs'] = [] class_object['hasMath'] = False for i in range(0, len(class_object['extension'])): class_object['attribs'].append(self.get_attrib_descrip (class_object['extension'][i])) for elem in class_object['lo_extension']: class_object['attribs'].append(self.get_attrib_descrip(elem)) return class_object
def __init__(self, name, extension, object_desc): BaseFile.BaseFile.__init__(self, name, extension) # change the comment delimiter and line length self.comment = '%' self.line_length = 72 self.package = object_desc['name'] self.fullname = object_desc['fullname'] self.sbml_classes = object_desc['baseElements'] self.offset = object_desc['offset'] self.plugins = object_desc['plugins'] self.enums = object_desc['enums'] self.level = object_desc['base_level'] self.version = object_desc['base_version'] self.pkg_version = object_desc['pkg_version'] if object_desc['required']: self.reqd_status = 'true' else: self.reqd_status = 'false' self.prim_class = [] self.start_b = '{' self.end_b = '}' # expand the information for the classes self.fulltexname = strFunctions.texify(self.fullname) self.upper_package = strFunctions.upper_first(self.package) self.sort_class_names(self.sbml_classes) self.sort_attribute_names(self.sbml_classes) self.sort_enum_names(self.enums) self.full_pkg_command = '\\{0}Package'.format(self.fulltexname) self.brief_pkg_command = '\\{0}'.format(self.upper_package)
def write_body_for_lo_class(self, sbml_class, nested=False): lo_name = 'default' if sbml_class['hasListOf']: if 'lo_elementName' in sbml_class \ and sbml_class['lo_elementName'] != '': lo_name = strFunctions.upper_first( sbml_class['lo_elementName']) else: lo_name = strFunctions.cap_list_of_name(sbml_class['name']) # do not write if already written if lo_name in self.classes_written: return self.classes_written.append(lo_name) if 'texname' in sbml_class: classname = sbml_class['texname'] else: classname = sbml_class['name'] self.write_comment_line('---------------------------------------------' '------------') self.write_line('\subsection{0}The \class{0}{1}{2} class{2}' .format(self.start_b, lo_name, self.end_b)) self.write_line('\label{0}{1}-class{2}' .format(self.start_b, lo_name.lower(), self.end_b)) self.skip_line() self.write_to_do('explain {0}'.format(lo_name)) if 'min_lo_children' not in sbml_class: number = 'one' elif sbml_class['min_lo_children'] == 1: number = 'one' else: number = strFunctions.replace_digits(str( sbml_class['min_lo_children'])).lower() self.write_line('The \\{0} object derives from the \\class{1}SBase{2}' ' and inherits the core attributes and subobjects ' 'from that class. It contains ' '{4} or more objects of type \\{3}.' .format(lo_name, self.start_b, self.end_b, classname, number)) self.skip_line() written = False for i in range(0, len(sbml_class['lo_attribs'])): if not written: self.write_line('In addition the \\{0} object has the ' 'following attributes.' .format(lo_name)) self.skip_line() written = True att = sbml_class['lo_attribs'][i] self.write_attibute_paragraph(att, lo_name) if not nested: self.write_body_for_class(self.get_class(sbml_class['name']))
def adjust_line(line): lowerlibname = global_variables.library_name.lower() nonlibname = lowerlibname if lowerlibname.startswith('lib'): nonlibname = lowerlibname[3:] line = re.sub('nonlibsbml', nonlibname, line) line = re.sub('SBase', global_variables.std_base, line) line = re.sub('LIBSBML', global_variables.library_name.upper(), line) line = re.sub('LibSBML', strFunctions.upper_first(global_variables.library_name), line) line = re.sub('libSBML', strFunctions.lower_first(global_variables.library_name), line) line = re.sub('libsbml', lowerlibname, line) if lowerlibname.startswith('lib'): line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname[3:]), line) else: line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname), line) doctype = '{0}_DOCUMENT'.format(global_variables.language.upper()) if global_variables.document_class != 'SedDocument': doctype = 'LIB_{0}_{1}'.format(strFunctions.get_library_suffix(global_variables.library_name).upper(), strFunctions.remove_prefix(global_variables.document_class).upper()) line = re.sub('SBML_DOCUMENT', doctype, line) line = re.sub('SBMLDocument', global_variables.document_class, line) line = re.sub('toplevelname', strFunctions.lower_first(global_variables.top_level_element_name), line) line = re.sub('CAT_SBML', 'CAT_{0}'.format(global_variables.language.upper()), line) line = re.sub('SBML_Lang', '{0}'.format(global_variables.language.upper()), line) line = re.sub('SBML_', '{0}_'.format(global_variables.language.upper()), line) # hack for SedML if global_variables.prefix == 'Sed': line = re.sub('readSBML', 'read{0}ML'.format(global_variables.prefix), line) line = re.sub('writeSBML', 'write{0}ML'.format(global_variables.prefix), line) else: line = re.sub('readSBML', 'read{0}'.format(global_variables.language.upper()), line) line = re.sub('writeSBML', 'write{0}'.format(global_variables.language.upper()), line) line = re.sub('sbml', global_variables.language, line) line = re.sub('SBMLSBML', '{0}{1}'.format(strFunctions.upper_first(global_variables.language), global_variables.prefix), line) line = re.sub('SBML', global_variables.prefix, line) line = re.sub('ListOf', '{0}ListOf'.format(global_variables.prefix), line) line = re.sub('SPEC_NAMESPACE', '\"{0}\"'.format(global_variables.namespaces[0]['namespace']), line) line = re.sub('language_', '{0}'.format(global_variables.language.lower()), line) line = re.sub('LANGUAGE', '{0}'.format(global_variables.language.upper()), line) return line
def __init__(self, language, package, elements, offset, num_versions=1): self.language = language self.package = package self.elements = elements self.offset = offset self.num_versions = num_versions # derived members self.up_package = strFunctions.upper_first(self.package) self.cap_language = language.upper() self.struct_name = '{0}Extension'.format(self.up_package)
def get_enum_name(self, node): enum_name = '' temp = self.get_value(node, 'name') # expect camelcase with upper first if temp is not None: last = len(temp) # strip _t if it is there; since we add it later if temp.endswith('_t'): last -= 2 enum_name = strFunctions.upper_first(temp[0:last]) return enum_name
def __init__(self, language, package, base, enums, plugins): self.language = language self.package = package self.std_base = base self.enums = enums self.plugins = plugins # derived members self.up_package = strFunctions.upper_first(self.package) self.cap_language = language.upper() self.struct_name = '{0}Extension'.format(self.up_package)
def write_parser_code(self, class_descrip): class_descrip['original_name'] = class_descrip['name'] class_descrip['name'] = strFunctions.upper_first(class_descrip['name']) + 'Parser' class_descrip.update({'is_parser': True}) class_descrip.update({'is_plugin': False}) class_descrip.update({'is_constantFile': False}) class_descrip.update({'is_classFile': False}) fileout = JavaCodeFile.JavaCodeFile(class_descrip) if self.verbose: print('Writing file {0}'.format(fileout.filename)) fileout.write_parser_file() fileout.close_file()
def __init__(self, enum_package, original_package, custom_file=None): self.up_enum_package = strFunctions.upper_first(enum_package['name']) # Get enums data self.enums_data = enum_package['values'] self.original_enum_package = original_package self.custom_file = custom_file if self.custom_file is None: self.filename = '{0}'.format(self.up_enum_package) self.name = '{0}'.format(self.up_enum_package) self.brief_description = \ 'Implementation of {0} Enum.'.format(self.name) BaseJavaFile.BaseJavaFile.__init__(self, self.name, 'java', None) # members from object self.package = original_package['name'] self.cap_package = strFunctions.upper_first(original_package['name'])
def __init__(self, package, custom_file=None): self.up_package = strFunctions.upper_first(package['name']) # TODO GSOC 2016 self.original_package = package self.custom_file = custom_file if self.custom_file is not None: self.filename = '{0}{1}'.format(self.up_package, self.custom_file) self.name = '{0}{1}'.format(self.up_package, self.custom_file) else: self.name = '{0}Extension'.format(self.up_package) self.brief_description = \ 'Implementation of {0}.'.format(self.name) BaseJavaFile.BaseJavaFile.__init__(self, self.name, 'java', None) # members from object self.package = package['name'] self.cap_package = package['name'].upper() self.elements = package['elements'] self.number = package['number'] self.enums = package['enums'] self.plugins = package['plugins'] self.offset = package['offset'] self.num_versions = 1 if 'num_versions' in package: self.num_versions = package['num_versions'] # create a class object so we can just reuse code self.class_object['package'] = self.package self.class_object['name'] = self.name self.class_object['concretes'] = [] self.class_object['baseClass'] = self.baseClass self.class_object['attribs'] = [] self.class_object['has_children'] = False self.class_object['child_elements'] = [] self.class_object['overwrites_children'] = False # TODO in case when class is abstract it has to be an interface,so this part needs to be changed self.is_class_abstract = False # Expand java import statement self.pack = self.package self.expand_import_modules(self.original_package) self.expand_jsbml_methods()
def get_lo_children(self): elements = [] listed_elements = [] for i in range(0, len(self.attributes)): attribute = self.attributes[i] att_type = attribute['attType'] name = attribute['name'] if att_type == 'lo_element' and name not in listed_elements: # check for concrete instances if attribute['type'] == 'inline_lo_element': capname = strFunctions.upper_first(name) attrib_class = query.get_class(capname, attribute['root']) if attrib_class and 'concrete' in attrib_class: attribute['concrete'] = attrib_class['concrete'] elements.append(attribute) listed_elements.append(name) return elements
def write_type_code_enum_header(self, package): up_package = strFunctions.upper_first(package) self.open_comment() self.write_comment_line('@enum {0}{1}' 'TypeCode_t'.format(self.cap_language, up_package)) self.write_comment_line('@brief {0}{1}TypeCode_t Enumeration ' 'of possible types in the lib{0} ' '“{2}” package ' 'implementation.'.format(self.cap_language, up_package, package)) self.write_blank_comment_line() self.write_comment_line('@copydetails doc_what_are_typecodes') self.write_blank_comment_line() self.write_comment_line('@copydetails doc_additional_typecode_details') self.close_comment()
def __init__(self, name, package, binding): self.binding = binding self.package = package.lower() self.cap_package = self.package.upper() self.up_package = strFunctions.upper_first(self.package) if binding == 'csharp' or binding == 'java': self.fileout = BaseInterfaceFile.BaseInterfaceFile(name) self.fileout.brief_description = 'Casting to most specific ' \ 'extension object for ' \ '{0}'.format(binding) else: self.fileout = BaseCppFile.BaseCppFile(name, 'cpp', None) self.fileout.brief_description = 'Casting to most specific ' \ 'extension object for ' \ '{0}'.format(binding)
def __init__(self, name, package, binding, plugins): self.binding = binding self.package = package.lower() self.cap_package = self.package.upper() self.up_package = strFunctions.upper_first(self.package) if binding == 'csharp' or binding == 'java': self.fileout = None else: self.fileout = BaseCppFile.BaseCppFile(name, 'cpp', None) self.fileout.brief_description = 'Casting to most specific ' \ 'plugins object for ' \ '{0}'.format(binding) self.cap_language = global_variables.language.upper() self.plugins = plugins self.base = global_variables.std_base
def write_uri_constructor(self): ob_name = self.object_name package = self.package.lower() up_package = strFunctions.upper_first(self.package) # create doc string header title_line = 'Creates a new {0} using the given uri, prefix and ' \ 'package namespace.'.format(ob_name) params = ['@param uri a string, representing the uri of the package.', '@param prefix a string, the prefix to be used.', '@param {0}ns a pointer to the {1}PkgNamespaces object to ' 'be used.'.format(package, up_package)] return_lines = [] additional = '' # create the function declaration function = self.class_name return_type = '' arguments = ['const std::string& uri', 'const std::string& prefix', '{0}PkgNamespaces* ' '{1}ns'.format(up_package, package)] ns = '{0}ns'.format(package) constructor_args = self.write_constructor_args(ns) # create the function implementation if self.is_doc_plugin or not self.has_children: code = [] else: code = [dict({'code_type': 'line', 'code': ['connectToChild()']})] return dict({'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': self.object_name, 'implementation': code, 'constructor_args': constructor_args})
def __init__(self, package, filetype=''): self.up_package = strFunctions.upper_first(package['name']) if filetype == '': self.name = '{0}Extension'.format(self.up_package) self.class_name = self.name elif filetype == 'types': self.name = '{0}ExtensionTypes'.format(self.up_package) elif filetype == 'fwd': self.name = '{0}fwd'.format(package['name']) self.brief_description = \ 'Definition of {0}.'.format(self.name) BaseCppFile.BaseCppFile.__init__(self, self.name, 'h', None) # members from object self.package = package['name'] #Looks like it's the whole package self.cap_package = package['name'].upper() self.baseClass = '{0}Extension'.format(self.cap_language) self.class_name = self.name self.elements = package['baseElements'] self.saved_elements = self.elements self.number = package['number'] self.enums = package['enums'] self.offset = package['offset'] self.plugins = package['plugins'] self.num_versions = 1 if 'num_versions' in package: self.num_versions = package['num_versions'] # create a class object so we can just reuse code self.class_object['package'] = self.package self.class_object['name'] = self.name self.class_object['concretes'] = [] self.class_object['baseClass'] = self.baseClass self.class_object['attribs'] = [] self.class_object['has_children'] = False self.class_object['child_elements'] = [] self.class_object['overwrites_children'] = False self.class_object['name'] = self.name
def write_extension_instance(self): up_package = strFunctions.upper_first(self.package) self.open_comment() self.write_blank_comment_line() self.write_comment_line('Required typedef definitions') self.write_blank_comment_line() self.write_comment_line('{0}PkgNamespace is derived from {1}Namespaces ' 'class and used when creating an object of {2} ' 'derived classes defined in the {3} ' 'package'.format(up_package, self.cap_language, self.std_base, self.package)) self.write_blank_comment_line() self.write_comment_line('{0}ExtensionNamespaces<{1}Extension> must be ' 'instantiated in {1}Extension.cpp for ' 'DLL'.format(self.cap_language, up_package)) self.write_blank_comment_line() self.close_comment() self.write_line('typedef {0}ExtensionNamespaces<{1}Extension> ' '{1}PkgNamespaces;'.format(self.cap_language, up_package))
def create_document_plugin_desc(self): up_package = strFunctions.upper_first(self.package['name']) up_language = self.language.upper() doc_plug = dict({'attribs': [], 'extension': [], 'lo_extension': [], 'sbase': '{0}Document'.format(up_language), 'name': '{0}{1}DocumentPlugin'.format(up_package, up_language), 'is_plugin': True, 'is_list_of': False, 'hasListOf': False, 'package': self.package['name'], 'typecode': '', 'baseClass': '{0}DocumentPlugin'.format(up_language), 'sid_refs': [], 'unit_sid_refs': [], 'hasMath': False, 'is_doc_plugin': True, 'reqd': self.package['required']}) return doc_plug
def write_assignment_args(self): if self.base_class: constructor_args = ['{0}::operator=(rhs)'.format(self.base_class)] else: constructor_args = [] for attrib in self.attributes: if attrib['isArray']: member = attrib['memberName'] length = strFunctions.upper_first(attrib['name']) constructor_args.append('{0} = NULL'.format(member)) constructor_args.append('set{0}(rhs.{1}, ' 'rhs.{1}Length)'.format(length, member)) elif attrib['type'] != 'element': constructor_args.append('{0} = rhs.{0}' .format(attrib['memberName'])) if attrib['isNumber'] or attrib['attType'] == 'boolean': constructor_args.append('mIsSet{0} = rhs.mIsSet{0}' .format(attrib['capAttName'])) if self.overwrites_children: constructor_args.append('mElementName = rhs.mElementName') return constructor_args
def __init__(self, lib_object, verbose=False): # members from object self.lib_object = lib_object self.language = global_variables.language self.cap_language = self.language.upper() self.package = lib_object['name'] self.up_package = strFunctions.upper_first(self.package) self.cap_lib = 'LIB{0}'.format(self.cap_language) self.offset = lib_object['offset'] self.sbml_classes = lib_object['baseElements'] self.enums = lib_object['enums'] self.plugins = lib_object['plugins'] self.fullname = lib_object['fullname'] self.level = lib_object['base_level'] self.version = lib_object['base_version'] self.pkg_version = lib_object['pkg_version'] self.pkg_ref = '{0} Level~{1} Package specification for {2}, ' \ 'Version~{3}'.format(self.cap_language, self.level, self.fullname, self.pkg_version) self.reqd_status = lib_object['required'] self.error_file = None self.class_rules = [] if len(global_variables.error_list) == 0: self.create_rule_structure() global_variables.class_rules = self.class_rules else: self.class_rules = global_variables.class_rules self.open_br = '{' self.close_br = '}' self.verbose = verbose
def __init__(self, package): self.up_package = strFunctions.upper_first(package['name']) self.name = '{0}Extension'.format(self.up_package) self.brief_description = \ 'Implementation of {0}.'.format(self.name) BaseCppFile.BaseCppFile.__init__(self, self.name, 'cpp', None) # members from object self.package = package['name'] self.cap_package = package['name'].upper() self.baseClass = '{0}Extension'.format(self.cap_language) self.elements = package['elements'] self.number = package['number'] self.enums = package['enums'] self.plugins = package['plugins'] self.offset = package['offset'] self.num_versions = 1 if 'num_versions' in package: self.num_versions = package['num_versions'] self.core_level = package['base_level'] self.core_version = package['base_version'] self.lv_info = package['lv_info'] # create a class object so we can just reuse code self.class_object['package'] = self.package self.class_object['name'] = self.name self.class_object['concretes'] = [] self.class_object['baseClass'] = self.baseClass self.class_object['attribs'] = [] self.class_object['has_children'] = False self.class_object['child_elements'] = [] self.class_object['overwrites_children'] = False
def write_extension_instance(self): # this is a bit different from the header file up_package = strFunctions.upper_first(self.package) self.open_comment() self.write_blank_comment_line() self.write_comment_line('Adds this {0}Extension to the {1}' 'ExtensionRegistry ' 'class'.format(up_package, self.cap_language)) self.write_blank_comment_line() self.close_comment() self.write_line('static {0}ExtensionRegister<{1}Extension> ' '{2}ExtensionRegistry;'.format(self.cap_language, up_package, self.package)) self.skip_line() self.write_line('static') self.write_line('const char* {0}_{1}_TYPECODE_STRINGS[] =' ''.format(self.cap_language, self.cap_package)) self.write_line('{') self.up_indent() self.write_line(' \"{0}\"'.format(self.elements[0]['name'])) for i in range(1, len(self.elements)): self.write_line(' , \"{0}\"'.format(self.elements[i]['name'])) self.down_indent() self.write_line('};') self.skip_line(2) self.open_comment() self.write_blank_comment_line() self.write_comment_line('Instantiate {0}ExtensionNamespaces<{1}' 'Extension> for DLL'.format( self.cap_language, up_package)) self.write_blank_comment_line() self.close_comment() self.write_line('template class LIB{0}_EXTERN {0}ExtensionNamespaces<' '{1}Extension>;'.format(self.cap_language, up_package)) self.skip_line()
def write_create_object(self): if not self.is_cpp_api: return elif self.is_list_of: return if len(self.elements) == 0: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Creates and returns an new "elementName" object in this {0}.' \ .format(self.class_name) params.append('@param elementName, the name of the element to create.') return_lines.append('@return pointer to the element created.') # create the function declaration function = 'createChildObject' # if self.is_plugin: return_type = '{0}*'.format(global_variables.baseClass) arguments = ['const std::string& elementName'] code = [] # create the function implementation if self.is_plugin: first_line = ['{0}* obj = NULL'.format(global_variables.baseClass)] else: first_line = ['{0}* obj = NULL'.format(self.base_class)] last_line = ['return obj'] if not self.has_elements_with_same_xml_name(): first = True block = [] if_block = [] for elem in self.elements: if elem['concrete']: for conc in elem['concrete']: if not first: block.append('else if') else: first = False concname = conc['name'] concelem = conc['name'] if conc['name'].lower() != conc['element'].lower(): concelem = strFunctions.lower_first( strFunctions.remove_prefix(conc['element'])) # hack for render if self.package == 'Render' or self.package == 'render': remove_prefix = True prefix_to_remove = strFunctions.upper_first( self.package) concelem = strFunctions.remove_prefix( conc['element'], False, remove_prefix, prefix_to_remove) block.append('elementName == \"{0}\"'.format(concname)) block.append('return create{0}()'.format( strFunctions.upper_first(concelem))) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) else: if not first: block.append('else if') else: first = False block.append('elementName == \"{0}\"'.format(elem['name'])) [elem_name, unused] = strFunctions.remove_hyphens(elem['name']) block.append('return create{0}()'.format( strFunctions.upper_first(elem_name))) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [ self.create_code_block('line', first_line), if_block, self.create_code_block('line', last_line) ] else: code = [ self.create_code_block('line', first_line), self.create_code_block('comment', ['TO DO']), self.create_code_block('line', last_line) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def __init__(self, name, filetype): self.class_prefix = SF.upper_first(name) self.filetype = filetype
def adjust_line(line): lowerlibname = global_variables.library_name.lower() nonlibname = lowerlibname if lowerlibname.startswith('lib'): nonlibname = lowerlibname[3:] line = re.sub('nonlibsbml', nonlibname, line) line = re.sub('SBase', global_variables.std_base, line) line = re.sub('LIBSBML', global_variables.library_name.upper(), line) line = re.sub('LibSBML', strFunctions.upper_first(global_variables.library_name), line) line = re.sub('libSBML', strFunctions.lower_first(global_variables.library_name), line) line = re.sub('libsbml', lowerlibname, line) if lowerlibname.startswith('lib'): line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname[3:]), line) else: line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname), line) doctype = '{0}_DOCUMENT'.format(global_variables.language.upper()) if global_variables.document_class != 'SedDocument': doctype = 'LIB_{0}_{1}'.format( strFunctions.get_library_suffix( global_variables.library_name).upper(), strFunctions.remove_prefix( global_variables.document_class).upper()) line = re.sub('SBML_DOCUMENT', doctype, line) line = re.sub('SBMLDocument', global_variables.document_class, line) line = re.sub( 'toplevelname', strFunctions.lower_first(global_variables.top_level_element_name), line) line = re.sub('CAT_SBML', 'CAT_{0}'.format(global_variables.language.upper()), line) line = re.sub('SBML_Lang', '{0}'.format(global_variables.language.upper()), line) line = re.sub('SBML_', '{0}_'.format(global_variables.language.upper()), line) # hack for SedML if global_variables.prefix == 'Sed': line = re.sub('readSBML', 'read{0}ML'.format(global_variables.prefix), line) line = re.sub('writeSBML', 'write{0}ML'.format(global_variables.prefix), line) else: line = re.sub('readSBML', 'read{0}'.format(global_variables.language.upper()), line) line = re.sub('writeSBML', 'write{0}'.format(global_variables.language.upper()), line) line = re.sub('sbml', global_variables.language, line) line = re.sub( 'SBMLSBML', '{0}{1}'.format( strFunctions.upper_first(global_variables.language), global_variables.prefix), line) line = re.sub('SBML', global_variables.prefix, line) line = re.sub('ListOf', '{0}ListOf'.format(global_variables.prefix), line) line = re.sub( 'SPEC_NAMESPACE', '\"{0}\"'.format(global_variables.namespaces[0]['namespace']), line) line = re.sub('language_', '{0}'.format(global_variables.language.lower()), line) line = re.sub('LANGUAGE', '{0}'.format(global_variables.language.upper()), line) return line
def expand_attributes(self, attributes): for i in range(0, len(attributes)): capname = strFunctions.upper_first(attributes[i]['name']) attributes[i]['name'] = strFunctions.lower_first(capname) attributes[i]['capAttName'] = capname attributes[i]['memberName'] = 'm' + capname attributes[i]['pluralName'] = \ strFunctions.plural(attributes[i]['name']) attributes[i]['isEnum'] = False attributes[i]['isArray'] = False attributes[i]['isVector'] = False attributes[i]['children_overwrite'] = False att_type = attributes[i]['type'] if att_type == 'SId' or att_type == 'SIdRef' or att_type == 'IDREF': attributes[i]['attType'] = 'string' attributes[i]['attTypeCode'] = 'std::string&' attributes[i]['CType'] = 'const char *' attributes[i]['isNumber'] = False attributes[i]['default'] = '""' elif att_type == 'UnitSId' or att_type == 'UnitSIdRef': attributes[i]['attType'] = 'string' attributes[i]['attTypeCode'] = 'std::string&' attributes[i]['CType'] = 'const char *' attributes[i]['isNumber'] = False attributes[i]['default'] = '""' elif att_type == 'string': attributes[i]['attType'] = 'string' attributes[i]['attTypeCode'] = 'std::string&' attributes[i]['CType'] = 'const char *' attributes[i]['isNumber'] = False attributes[i]['default'] = '""' elif att_type == 'double': attributes[i]['attType'] = 'double' attributes[i]['attTypeCode'] = 'double' attributes[i]['CType'] = 'double' attributes[i]['isNumber'] = True attributes[i]['default'] = 'util_NaN()' elif att_type == 'int': attributes[i]['attType'] = 'integer' attributes[i]['attTypeCode'] = 'int' attributes[i]['CType'] = 'int' attributes[i]['isNumber'] = True attributes[i]['default'] = '{0}_INT_' \ 'MAX'.format(self.cap_language) elif att_type == 'uint': attributes[i]['attType'] = 'unsigned integer' attributes[i]['attTypeCode'] = 'unsigned int' attributes[i]['CType'] = 'unsigned int' attributes[i]['isNumber'] = True attributes[i]['default'] = '{0}_INT_' \ 'MAX'.format(self.cap_language) elif att_type == 'bool' or att_type == 'boolean': attributes[i]['attType'] = 'boolean' attributes[i]['attTypeCode'] = 'bool' attributes[i]['CType'] = 'int' attributes[i]['isNumber'] = False attributes[i]['default'] = 'false' elif att_type == 'enum': attributes[i]['isEnum'] = True attributes[i]['attType'] = 'enum' attributes[i]['attTypeCode'] = attributes[i]['element'] + '_t' attributes[i]['CType'] = attributes[i]['element'] + '_t' attributes[i]['isNumber'] = False attributes[i]['default'] = \ query.get_default_enum_value(attributes[i]) elif att_type == 'element': el_name = attributes[i]['element'] at_name = attributes[i]['name'] attributes[i]['attType'] = 'element' if attributes[i]['name'] == 'math': if global_variables.is_package: attributes[i]['attTypeCode'] = 'ASTNode*' attributes[i]['CType'] = 'ASTNode_t*' else: attributes[i]['attTypeCode'] = 'LIBSBML_CPP_NAMESPACE_QUALIFIER ASTNode*' attributes[i]['CType'] = 'LIBSBML_CPP_NAMESPACE_QUALIFIER ASTNode_t*' else: attributes[i]['attTypeCode'] = attributes[i]['element']+'*' attributes[i]['CType'] = attributes[i]['element']+'_t*' if attributes[i]['attTypeCode'] == 'XMLNode*' and not global_variables.is_package: attributes[i]['attTypeCode'] = 'LIBSBML_CPP_NAMESPACE_QUALIFIER {0}*'.format(attributes[i]['element']) attributes[i]['CType'] = 'LIBSBML_CPP_NAMESPACE_QUALIFIER {0}_t*'.format(attributes[i]['element']) attributes[i]['isNumber'] = False attributes[i]['default'] = 'NULL' if strFunctions.compare_no_case(strFunctions.remove_prefix(el_name), at_name): attributes[i]['children_overwrite'] = False else: attributes[i]['children_overwrite'] = True elif att_type == 'lo_element' or att_type == 'inline_lo_element': name = strFunctions.list_of_name(attributes[i]['element']) plural = strFunctions.plural_no_prefix(attributes[i]['element']) attributes[i]['attType'] = 'lo_element' attributes[i]['attTypeCode'] = name attributes[i]['CType'] = 'ListOf_t' attributes[i]['memberName'] = 'm' + plural attributes[i]['isNumber'] = False attributes[i]['default'] = 'NULL' elif att_type == 'array': attributes[i]['isArray'] = True attributes[i]['element'] = \ strFunctions.lower_first(attributes[i]['element']) attributes[i]['attType'] = 'array' attributes[i]['attTypeCode'] = attributes[i]['element'] + '*' attributes[i]['CType'] = attributes[i]['attTypeCode'] attributes[i]['isNumber'] = False attributes[i]['default'] = 'NULL' elif att_type == 'vector': attributes[i]['isVector'] = True attributes[i]['element'] = \ strFunctions.lower_first(attributes[i]['element']) attributes[i]['attType'] = 'vector' attributes[i]['attTypeCode'] = 'std::vector<{0}>'.format(attributes[i]['element']) attributes[i]['CType'] = attributes[i]['attTypeCode'] attributes[i]['isNumber'] = False attributes[i]['default'] = 'NULL' else: global_variables.code_returned \ = global_variables.return_codes['unknown type used'] attributes[i]['attType'] = 'FIXME_{0}'.format(att_type) attributes[i]['attTypeCode'] = 'FIXME_{0}'.format(att_type) attributes[i]['CType'] = 'FIXME_{0}'.format(att_type) attributes[i]['isNumber'] = False attributes[i]['default'] = 'FIXME_{0}'.format(att_type) return attributes
def write_enum_from_string_function(self, index, values=None, str_name=''): enum = self.enums[index] name = enum['name'] classname = self.get_class_name(enum['values'][0]['name']) # create comment parts title_line = 'Returns the #{2}_t enumeration corresponding to the given string or @sbmlconstant{0}{1}, {2}_t{3} ' \ 'if there is no such match.'.format(self.open_br, values[-1], name, self.close_br) params = [ '@param code the string to convert to a #{0}_t.'.format(name) ] return_lines = [ '@return the corresponding #{2}_t or @sbmlconstant{0}{1}, {2}_t{3} ' 'if no match is found.'.format(self.open_br, values[-1], name, self.close_br) ] additional = [ '@note The matching is case-sensitive: \"{4}\" will return @sbmlconstant{0}{1}, {2}_t{3}, ' 'but \"{5}\" will return @sbmlconstant{0}{6}, {2}_t{3}.'.format( self.open_br, enum['values'][0]['name'], name, self.close_br, enum['values'][0]['value'], strFunctions.upper_first(enum['values'][0]['value']), values[-1]), ' ', '@if conly', '@memberof {0}_t'.format(classname), '@endif' ] # create the function declaration arguments = ['const char* code'] function = '{0}_fromString'.format(name) return_type = '{0}_t'.format(name) # create the function implementation if values: implementation = [ 'static int size = sizeof({0})/sizeof' '({0}[0])'.format(str_name), 'std::string type(code)' ] code = [dict({'code_type': 'line', 'code': implementation})] if_code = self.create_code_block('if', [ 'type == {0}' '[i]'.format(str_name), 'return ({0}_t)' '(i)'.format(name) ]) code.append( self.create_code_block( 'for', ['int i = 0; i < size; ' 'i++', if_code])) code.append( self.create_code_block('line', ['return {0}'.format(values[-1])])) else: code = [] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': False, 'object_name': name, 'implementation': code })
def write_get_object(self): if not self.is_cpp_api: return elif self.is_list_of: return if len(self.elements) == 0: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Returns the nth object of \"objectName" in this {0}.' \ .format(self.class_name) params.append( '@param elementName, the name of the element to get number of.') params.append( '@param index, unsigned int the index of the object to retrieve.') return_lines.append('@return pointer to the object.') # create the function declaration function = 'getObject' return_type = '{0}*'.format(global_variables.baseClass) arguments = ['const std::string& elementName', 'unsigned int index'] code = [] if not self.has_elements_with_same_xml_name(): # create the function implementation first_line = ['{0}* obj = NULL'.format(global_variables.baseClass)] last_line = ['return obj'] first = True block = [] if_block = [] for elem in self.elements: if not first: block.append('else if') else: first = False block.append('elementName == \"{0}\"'.format(elem['name'])) [elem_name, unused] = strFunctions.remove_hyphens(elem['name']) if elem in self.single_elements: block.append('return get{0}()'.format( strFunctions.upper_first(elem_name))) else: block.append('return get{0}(index)'.format( strFunctions.upper_first(elem_name))) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [ self.create_code_block('line', first_line), if_block, self.create_code_block('line', last_line) ] else: code = [ self.create_code_block('comment', ['TO DO']), self.create_code_block('line', ['return NULL']) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def write_remove_object(self): if not self.is_cpp_api: return elif self.is_list_of: return if len(self.elements) == 0: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Removes and returns the new "elementName" object with the given id in this {0}.' \ .format(self.class_name) params.append('@param elementName, the name of the element to remove.') params.append('@param id, the id of the element to remove.') return_lines.append('@return pointer to the element removed.') # create the function declaration function = 'removeChildObject' return_type = '{0}*'.format(global_variables.baseClass) arguments = ['const std::string& elementName', 'const std::string& id'] code = [] if not self.has_elements_with_same_xml_name(): # create the function implementation last_line = ['return NULL'] first = True block = [] if_block = [] for elem in self.elements: if elem['concrete']: for conc in elem['concrete']: if not first: block.append('else if') else: first = False elemName = elem['name'] if not elem['element'] or elem['element'] == elemName: elemElem = elemName else: elemElem = elem['element'] block.append('elementName == \"{0}\"'.format( conc['name'])) single = True if elem not in self.single_elements: thisClass = query.get_class( elemElem, self.classroot) single = False hasid = False for att in thisClass['attribs']: if att['name'] == 'id': hasid = True if hasid: block.append('return remove{0}(id)'.format( strFunctions.upper_first(elemName))) else: nested_if = self.create_code_block( 'if', [ 'get{0}(i)->getId() == id'.format( strFunctions.upper_first(elemName) ), 'return remove{0}(i)'.format( strFunctions.upper_first(elemName)) ]) nested_for = self.create_code_block( 'for', [ 'unsigned int i = 0; i < getNum{0}(); i++' ''.format( strFunctions.plural( strFunctions.upper_first( elemName))), nested_if ]) block.append(nested_for) else: [elem_name, unused] = strFunctions.remove_hyphens(elemName) block.append('{0} * obj = get{1}()'.format( elemElem, strFunctions.upper_first(elem_name))) block.append( 'if (unset{0}() == LIBSBML_OPERATION_SUCCESS) return obj' .format(strFunctions.upper_first(elem_name))) else: if not first: block.append('else if') else: first = False elemName = elem['name'] if not elem['element'] or elem['element'] == elemName: elemElem = elemName else: elemElem = elem['element'] block.append('elementName == \"{0}\"'.format(elem['name'])) single = True if elem not in self.single_elements: thisClass = query.get_class(elemElem, self.classroot) single = False hasid = False for att in thisClass['attribs']: if att['name'] == 'id': hasid = True if hasid: block.append('return remove{0}(id)'.format( strFunctions.upper_first(elemName))) else: nested_if = self.create_code_block( 'if', [ 'get{0}(i)->getId() == id'.format( strFunctions.upper_first(elemName)), 'return remove{0}(i)'.format( strFunctions.upper_first(elemName)) ]) nested_for = self.create_code_block( 'for', [ 'unsigned int i = 0; i < getNum{0}(); i++' ''.format( strFunctions.plural( strFunctions.upper_first( elemName))), nested_if ]) block.append(nested_for) else: [elem_name, unused] = strFunctions.remove_hyphens(elemName) block.append('{0} * obj = get{1}()'.format( elemElem, strFunctions.upper_first(elem_name))) block.append( 'if (unset{0}() == LIBSBML_OPERATION_SUCCESS) return obj' .format(strFunctions.upper_first(elem_name))) if single: if len(block) > 3: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) else: if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [if_block, self.create_code_block('line', last_line)] else: code = [ self.create_code_block('comment', ['TO DO']), self.create_code_block('line', ['return NULL']) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def set_globals(lang, base, doc, prfix, lib, is_pack, pkg_prefix, specifications=[], depend=[], library_vers=[], my_copyright='', annot_elem='', notes_elem='', use_id_1=True, use_name_1=True, ast=False, xml=False, top_name=''): global language language = lang global namespaces global has_level_version namespaces = [] if len(specifications) > 0: namespaces = specifications if 'level' not in namespaces[0] or (not namespaces[0]['level'] and not namespaces[0]['version']): has_level_version = False else: namespaces.append(dict({'namespace': 'not defined'})) global dependency dependency = [] if len(depend) > 0: dependency = depend global library_version library_version = [] if library_vers: library_version = library_vers global custom_copyright custom_copyright = my_copyright if base: global baseClass baseClass = base global std_base std_base = base if prfix: global prefix prefix = prfix if doc: global document_class global top_level_element_name document_class = doc # this is a bit of a hack as sedml and combine archive vary if doc.endswith('Document'): top_level_element_name = language else: top_level_element_name = doc if not doc.startswith(prefix): document_class = '{0}{1}'.format(prefix, doc) if top_name and top_name != '': top_level_element_name = top_name global package_prefix if pkg_prefix: package_prefix = pkg_prefix elif pkg_prefix == '': package_prefix = '' global annot_element if annot_elem and annot_elem != '': annot_element = annot_elem else: annot_element = 'Annotation' global notes_element if notes_elem and notes_elem != '': notes_element = notes_elem else: notes_element = 'Notes' global use_id use_id = True if not use_id_1: use_id = False global use_name use_name = True if not use_name_1: use_name = False global uses_ASTNode uses_ASTNode = False if ast: uses_ASTNode = True global uses_XMLNode uses_XMLNode = False if xml: uses_XMLNode = True global library_name if lib: library_name = lib else: library_name = 'Lib' + language global up_full_lib if not library_name.upper().startswith('LIB'): up_full_lib = 'LIB{0}'.format(library_name.upper()) else: up_full_lib = library_name.upper() global is_package is_package = is_pack global ret_success ret_success = '{0}_OPERATION_SUCCESS'.format(library_name.upper()) global ret_failed ret_failed = '{0}_OPERATION_FAILED'.format(library_name.upper()) global ret_invalid_obj ret_invalid_obj = '{0}_INVALID_OBJECT'.format(library_name.upper()) global ret_invalid_att ret_invalid_att = \ '{0}_INVALID_ATTRIBUTE_VALUE'.format(library_name.upper()) global ret_level_mis ret_level_mis = '{0}_LEVEL_MISMATCH'.format(library_name.upper()) global ret_vers_mis ret_vers_mis = '{0}_VERSION_MISMATCH'.format(library_name.upper()) global ret_pkgv_mis ret_pkgv_mis = '{0}_PKG_VERSION_MISMATCH'.format(library_name.upper()) global ret_ns_mis ret_ns_mis = '{0}_NAMESPACES_MISMATCH'.format(library_name.upper()) global ret_dup_id ret_dup_id = '{0}_DUPLICATE_OBJECT_ID'.format(library_name.upper()) global ret_att_unex ret_att_unex = '{0}_UNEXPECTED_ATTRIBUTE'.format(library_name.upper()) global unknown_error if is_package: if pkg_prefix != '': unknown_error = '{0}Unknown'.format(pkg_prefix) else: from util import strFunctions unknown_error = '{0}Unknown'.format(strFunctions.upper_first(lang)) else: unknown_error = '{0}Unknown'.format(prfix)
def set_unknown_error(pkg): global unknown_error from util import strFunctions unknown_error = '{0}Unknown'.format(strFunctions.upper_first(pkg))
def __init__(self, name, filetype): self.class_prefix = strFunctions.upper_first(name) self.filetype = filetype
def write_unset(self, attributes): if not self.is_cpp_api: return elif self.is_list_of: return # create comment parts params = [] return_lines = [] additional = [] title_line = ' Unsets the value of the "attributeName" attribute of this {0}.'.format( self.class_name) params.append( '@param attributeName, the name of the attribute to query.') return_lines.append('@copydetails doc_returns_success_code') return_lines.append('@li @{0}constant{1}{2}, ' ' OperationReturnValues_' 't{3}'.format(self.language, self.open_br, self.success, self.close_br)) return_lines.append('@li @{0}constant{1}{2},' ' OperationReturnValues_' 't{3}'.format(self.language, self.open_br, self.failed, self.close_br)) # create the function declaration function = 'unsetAttribute' return_type = 'int' arguments = ['const std::string& attributeName'] # create the function implementation first_line = [ 'int value = {0}::unsetAttribute(attributeName)'.format( self.base_class) ] last_line = ['return value'] first = True block = [] for attrib in attributes: name = attrib['name'] if 'xml_name' in attrib and attrib['xml_name'] != '': name = attrib['xml_name'] if not first: block.append('else if') else: first = False block.append('attributeName == \"{0}\"'.format(name)) if attrib['attType'] == 'vector': block.append('value = clear{0}()'.format( strFunctions.upper_first(attrib['pluralName']))) else: block.append('value = unset{0}()'.format(attrib['capAttName'])) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [] if len(attributes) == 0: code = [ self.create_code_block('line', first_line), self.create_code_block('line', last_line) ] else: code = [ self.create_code_block('line', first_line), if_block, self.create_code_block('line', last_line) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def adjust_line(line): """ Replace certain items in a line of code. :param line: the line before replacement(s) :return: the line after replacement(s) """ lowerlibname = gv.library_name.lower() nonlibname = lowerlibname if lowerlibname.startswith('lib'): nonlibname = lowerlibname[3:] # Remove leading "lib" line = re.sub('nonlibsbml', nonlibname, line) line = re.sub('SBase', gv.std_base, line) line = re.sub('LIBSBML', gv.library_name.upper(), line) line = re.sub('LibSBML', SF.upper_first(gv.library_name), line) line = re.sub('libSBML', SF.lower_first(gv.library_name), line) line = re.sub('libsbml', lowerlibname, line) if lowerlibname.startswith('lib'): line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname[3:]), line) else: line = re.sub('sbmlfwd', '{0}fwd'.format(lowerlibname), line) doctype = '{0}_DOCUMENT'.format(gv.language.upper()) # if gv.document_class != 'SedDocument': # doctype = 'LIB_{0}_{1}'. # format(SF.get_library_suffix(gv.library_name).upper(), # SF.remove_prefix(gv.document_class).upper()) line = re.sub('SBML_DOCUMENT', doctype, line) line = re.sub('SBMLDocument', gv.document_class, line) line = re.sub('toplevelname', gv.top_level_element_name, line) line = re.sub('CAT_SBML', 'CAT_{0}'.format(gv.language.upper()), line) line = re.sub('SBML_Lang', '{0}'.format(gv.language.upper()), line) line = re.sub('SBML_', '{0}_'.format(gv.language.upper()), line) # hack for SedML if gv.prefix == 'Sed': line = re.sub('readSBML', 'read{0}ML'.format(gv.prefix), line) line = re.sub('writeSBML', 'write{0}ML'.format(gv.prefix), line) else: line = re.sub('readSBML', 'read{0}'.format(gv.language.upper()), line) line = re.sub('writeSBML', 'write{0}'.format(gv.language.upper()), line) line = re.sub('sbml', gv.language, line) line = re.sub('SBMLSBML', '{0}{1}'.format(SF.upper_first(gv.language), gv.prefix), line) line = re.sub('SBML', gv.prefix, line) line = re.sub('ListOf', '{0}ListOf'.format(gv.prefix), line) line = re.sub('<SPEC_NAMESPACE>', '\"{0}\"'.format(gv.namespaces[-1]['namespace']), line) line = re.sub('language_', '{0}'.format(gv.language.lower()), line) line = re.sub('LANGUAGE', '{0}'.format(gv.language.upper()), line) if gv.namespaces is not None and len(gv.namespaces) > 0 \ and 'level' in gv.namespaces[-1]: line = re.sub('<SPEC_LEVEL>', '{0}'.format(gv.namespaces[-1]['level']), line) line = re.sub('<SPEC_VERSION>', '{0}'.format(gv.namespaces[-1]['version']), line) if gv.annot_element is not None: line = re.sub('<Annotation>', gv.annot_element, line) line = re.sub('<annotation_variable>', '\"{0}\"'.format(SF.lower_first(gv.annot_element)), line) if gv.notes_element is not None: line = re.sub('<Notes>', gv.notes_element, line) line = re.sub('<notes_variable>', '\"{0}\"'.format(SF.lower_first(gv.notes_element)), line) line = re.sub('<NS>', 'LIBSBML_CPP_NAMESPACE_QUALIFIER ', line) return line
def write_add_object(self): if not self.is_cpp_api: return elif self.is_list_of: return if len(self.elements) == 0: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Adds a new "elementName" object to this {0}.' \ .format(self.class_name) params.append('@param elementName, the name of the element to create.') params.append('@param element, pointer to the element to be added.') return_lines.append('@copydetails doc_returns_success_code') return_lines.append('@li @{0}constant{1}{2}, ' ' OperationReturnValues_' 't{3}'.format(self.language, self.open_br, self.success, self.close_br)) return_lines.append('@li @{0}constant{1}{2},' ' OperationReturnValues_' 't{3}'.format(self.language, self.open_br, self.failed, self.close_br)) # create the function declaration function = 'addChildObject' return_type = 'int' arguments = [ 'const std::string& elementName', 'const {0}* element'.format(global_variables.baseClass) ] code = [] if not self.has_elements_with_same_xml_name(): # create the function implementation last_line = ['return LIBSBML_OPERATION_FAILED'] first = True block = [] if_block = [] for elem in self.elements: if elem['concrete']: for conc in elem['concrete']: if not first: block.append('else if') else: first = False elemName = elem['name'] if not elem['element'] or elem['element'] == elemName: elemElem = elemName else: elemElem = elem['element'] block.append( 'elementName == \"{0}\" && element->getTypeCode() == {1}' .format(conc['name'], conc['typecode'])) if elem not in self.single_elements: block.append( 'return add{0}((const {1}*)(element))'.format( strFunctions.upper_first(elemName), elemElem)) else: block.append( 'return set{0}((const {1}*)(element))'.format( strFunctions.upper_first(elemName), elemElem)) else: if not first: block.append('else if') else: first = False elemName = elem['name'] if not elem['element'] or elem['element'] == elemName: elemElem = elemName else: elemElem = elem['element'] [elem_name, unused] = strFunctions.remove_hyphens(elemName) block.append( 'elementName == \"{0}\" && element->getTypeCode() == {1}' .format(elemName, elem['typecode'])) if elem not in self.single_elements: block.append( 'return add{0}((const {1}*)(element))'.format( strFunctions.upper_first(elem_name), elemElem)) else: block.append( 'return set{0}((const {1}*)(element))'.format( strFunctions.upper_first(elem_name), elemElem)) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [if_block, self.create_code_block('line', last_line)] else: code = [ self.create_code_block('comment', ['TO DO']), self.create_code_block('line', ['return -1']) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def create_element_list_enum(self): up_package = strFunctions.upper_first(self.package['name']) name = up_package + 'List' list_info = dict({'name': name, 'values': self.package['elements']}) return list_info
def write_is_set(self, attributes): if not self.is_cpp_api: return elif self.is_list_of: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Predicate returning {0} if ' \ 'this {1}\'s attribute \"attributeName\" is set.' \ .format(self.true, self.class_name) params.append( '@param attributeName, the name of the attribute to query.') return_lines.append( '@return {0} if this {1}\'s attribute \"attributeName\" has been ' 'set, otherwise {2} is returned.'.format(self.true, self.class_name, self.false)) # create the function declaration function = 'isSetAttribute' return_type = 'bool' arguments = ['const std::string& attributeName'] # create the function implementation first_line = [ 'bool value = {0}::isSetAttribute(attributeName)'.format( self.base_class) ] last_line = ['return value'] first = True block = [] for attrib in attributes: name = attrib['name'] if 'xml_name' in attrib and attrib['xml_name'] != '': name = attrib['xml_name'] if not first: block.append('else if') else: first = False block.append('attributeName == \"{0}\"'.format(name)) if 'isVector' in attrib and attrib['isVector']: block.append('value = has{0}()'.format( strFunctions.upper_first(attrib['pluralName']))) else: block.append('value = isSet{0}()'.format(attrib['capAttName'])) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [] if len(attributes) == 0: code = [ self.create_code_block('line', first_line), self.create_code_block('line', last_line) ] else: code = [ self.create_code_block('line', first_line), if_block, self.create_code_block('line', last_line) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': True, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def write_get_num_objects(self): if not self.is_cpp_api: return elif self.is_list_of: return if len(self.elements) == 0: return # create comment parts params = [] return_lines = [] additional = [] title_line = 'Returns the number of \"elementName" in this {0}.' \ .format(self.class_name) params.append( '@param elementName, the name of the element to get number of.') return_lines.append('@return unsigned int number of elements.') # create the function declaration function = 'getNumObjects' return_type = 'unsigned int' arguments = ['const std::string& elementName'] code = [] if not self.has_elements_with_same_xml_name(): # create the function implementation first_line = ['unsigned int n = 0'] last_line = ['return n'] first = True block = [] if_block = [] for elem in self.elements: if not first: block.append('else if') else: first = False block.append('elementName == \"{0}\"'.format(elem['name'])) if elem in self.lo_elements: name = strFunctions.plural( strFunctions.upper_first(elem['name'])) block.append('return getNum{0}()'.format(name)) else: [elem_name, unused] = strFunctions.remove_hyphens(elem['name']) nested_if = [ 'isSet{0}()'.format( strFunctions.upper_first(elem_name)), 'return 1' ] nested_if_block = self.create_code_block('if', nested_if) block.append(nested_if_block) if len(block) > 2: if_block = self.create_code_block('else_if', block) else: if_block = self.create_code_block('if', block) code = [ self.create_code_block('line', first_line), if_block, self.create_code_block('line', last_line) ] else: code = [ self.create_code_block('comment', ['TO DO']), self.create_code_block('line', ['return 0']) ] # return the parts return dict({ 'title_line': title_line, 'params': params, 'return_lines': return_lines, 'additional': additional, 'function': function, 'return_type': return_type, 'arguments': arguments, 'constant': False, 'virtual': True, 'object_name': self.struct_name, 'implementation': code })
def write_attribute_type_rule(self, attribute, lo=None): if lo: formatted_name = lo['formatted_name'] refname = lo['name'] abbrev = strFunctions.abbrev_lo_name(refname) else: formatted_name = self.formatted_name refname = self.name abbrev = self.name att_type = attribute['type'] att_name = strFunctions.upper_first(attribute['name']) name = strFunctions.wrap_token(attribute['texname'], self.package) rule_type = 'String' if att_type == 'SId': return elif att_type == 'SIdRef': [ref_name, ref_type] = \ strFunctions.get_sid_refs(attribute['element']) # hack for render if ref_name == 'StartHead' or ref_name == 'EndHead': ref_name = 'LineEnding' if ref_name == 'SBase': text = 'The value of the attribute {0} of {1} {2} object must be ' \ 'the identifier of an existing object derived from the \SBase class and defined in the ' \ 'enclosing \Model object.'\ .format(name, self.indef, self.formatted_name, ref_name) else: text = 'The value of the attribute {0} of {1} {2} object must be ' \ 'the identifier of an existing \{3} object defined in the ' \ 'enclosing \Model object.'\ .format(name, self.indef, self.formatted_name, ref_name) rule_type = ref_type elif att_type == 'string': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('string')) elif att_type == 'ID': text = 'The attribute {0} on {1} {2} must have a value of XML data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('ID')) elif att_type == 'IDREF': text = 'The value of the attribute {0} of {1} {2} object must be ' \ 'the \'metaid\' of an existing \SBase object defined in the ' \ 'enclosing \Model object.'\ .format(name, self.indef, formatted_name) rule_type = 'SBase' elif att_type == 'int' or att_type == 'uint': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}{4}'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('integer'), '.' if att_type == 'int' else ', and must be non negative.') rule_type = 'Integer' if att_type == 'int' else 'NonNegativeInteger' elif att_type == 'double': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('double')) rule_type = 'Double' elif att_type == 'bool' or att_type == 'boolean': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('boolean')) rule_type = 'Boolean' elif att_type == 'UnitSId' or att_type == 'UnitSIdRef': text = 'The value of the attribute {0} on {1} {2} must have a ' \ 'taken from the following: the identifier of a ' \ '\\UnitDefinition object in the enclosing \Model, or one ' \ 'of the base units in SBML.'.format(name, self.indef, formatted_name) rule_type = 'UnitSId' elif att_type == 'enum': enum_name = attribute['element'] enums = attribute['parent']['root']['enums'] enum_values = self.parse_enum_values(enum_name, enums) text = 'The value of the attribute {0} of {1} {2} object must ' \ 'conform to the syntax of SBML data type {3} and ' \ 'may only take on the allowed values of {3} defined ' \ 'in SBML; that is, the value must be one of the ' \ 'following {4}.'.format(name, self.indef, self.formatted_name, strFunctions.wrap_enum(enum_name), enum_values) rule_type = '{0}Enum'.format(attribute['element']) elif att_type == 'array': array_type = strFunctions.lower_first(attribute['element']) text = 'The value of the attribute {0} of {1} {2} object must ' \ 'be an array of values of type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token(array_type)) else: text = 'FIX ME: Encountered an unknown attribute type {0} in ' \ 'ValidationRulesForClass:write_attribute_type_rule'\ .format(att_type) add_extended = True if self.package == 'render': add_extended = False ref = '{0}, {1}.'\ .format(self.pkg_ref, strFunctions.wrap_section(self.name, True, add_extended)) sev = 'ERROR' lib_sev = '{0}_SEV_ERROR'.format(global_variables.up_full_lib) short = '{0} attribute must be {1}.'.format(att_name, rule_type) lib_ref = 'L3V1 {0} V1 Section'.format(self.up_package) tc = '{0}{1}{2}MustBe{3}'.format(self.up_package, self.name, att_name, rule_type) return dict({'number': self.number, 'text': text, 'reference': ref, 'severity': sev, 'typecode': tc, 'plugin': True, 'object': self.name, 'lo': False, 'lib_sev': lib_sev, 'short': short, 'lib_ref': lib_ref, 'attrib': attribute['name'], 'attrib_type': att_type})
def get_class_name(self, value): # value will be CLASS_ENUMTYPE_VALUE parts = value.split('_') classname = strFunctions.upper_first(parts[0].lower()) return classname
def write_jsbml_constants(self): self.up_indent() base_level = self.original_package['base_level'] base_version = self.original_package['base_version'] package_version = self.original_package['pkg_version'] package_name = self.original_package['name'] title_line = 'The namespace URI of this parser for SBML level {0}, version {1} \ and package version {2}.'.format(base_level, base_version, package_version) self.write_brief_header(title_line) self.namespace_uri = 'namespaceURI_L{0}V{1}V{2}'.format(base_level, base_version, package_version) line = 'public static final String {0} = "http://www.sbml.org/sbml/level{1}/version{2}/{4}/version{3}"'.\ format(self.namespace_uri, base_level, base_version, package_version, package_name) self.write_jsbml_line_verbatim(line) title_line = 'The latest namespace URI of this parser, this value can change between releases.' self.write_brief_header(title_line) line = 'public static final String namespaceURI = {0}'.format(self.namespace_uri) self.write_jsbml_line_verbatim(line) # This part is for write ResourceBundle variable # self.write_variable_comment() # line = 'public static final ResourceBundle bundle = ResourceManager' \ # '.getBundle("org.sbml.jsbml.ext.{0}.Messages")'.format(package_name) # # self.write_line(line) # self.write_jsbml_line_verbatim(line) self.write_variable_comment() line = 'public static final String shortLabel = "{0}"'.format(package_name) # self.write_line(line) self.write_jsbml_line_verbatim(line) self.write_variable_comment() line = 'public static final int MIN_SBML_LEVEL = {0}'.format(base_level) # self.write_line(line) self.write_jsbml_line_verbatim(line) self.write_variable_comment() line = 'public static final int MIN_SBML_VERSION = {0}'.format(base_version) # self.write_line(line) self.write_jsbml_line_verbatim(line) self.write_variable_comment() line = 'public static final int PACKAGE_VERSION = {0}'.format(package_version) # self.write_line(line) self.write_jsbml_line_verbatim(line) self.write_variable_comment() line = 'public static final List<String> namespaces' self.write_jsbml_line_verbatim(line) self.write_variable_comment() full_name = self.original_package['fullname'] line = 'public static final String packageName = "{0}"'.format(full_name) self.write_jsbml_line_verbatim(line) #Write static self.write_static(self.namespace_uri) # Attributes part self.write_serial_version_comment() # TODO need to change serialVersionUID line = 'private static final long serialVersionUID = {0}L'.format(self.serialVersionUID) self.write_jsbml_line_verbatim(line) # write attributes, but make sure that there are no duplicates attribs_to_write = [] attribs_name_to_write = [] base_elements = self.original_package['baseElements'] for element in base_elements: attributes = element['attribs'] for attribute in attributes: # print(attribute['memberName']) name = attribute['name'] if str(name) != 'id' and str(name) != 'name': if name not in attribs_name_to_write: attribs_to_write.append(attribute) attribs_name_to_write.append(name) elements = self.original_package['elements'] for element in elements: # print(attribute['memberName']) name = strFunctions.lower_first(element['name']) if str(name) != 'id' and str(name) != 'name': if name not in attribs_name_to_write: attribs_to_write.append(element) attribs_name_to_write.append(name) plugin_elements = self.original_package['plugins'] for element in plugin_elements: attributes = element['attribs'] for attribute in attributes: # print(attribute['memberName']) name = attribute['name'] if str(name) != 'id' and str(name) != 'name': if name not in attribs_name_to_write: attribs_to_write.append(attribute) attribs_name_to_write.append(name) for attribute in attribs_to_write: self.write_variable_comment() name = attribute['name'] write_name = strFunctions.lower_first(name) line = 'public static final String {0} = "{1}"'.format(write_name, write_name) self.write_jsbml_line_verbatim(line) if 'type' in list(attribute.keys()): if attribute['type'] == 'lo_element': self.write_variable_comment() write_name = strFunctions.upper_first(name) line = 'public static final String listOf{0}s = "listOf{0}s"'.format(write_name) self.write_jsbml_line_verbatim(line) self.down_indent() get_namespace_uri_func = self.get_namespace_uri() self.write_function_implementation(get_namespace_uri_func) self.up_indent() self.down_indent()
def write_attribute_type_rule(self, attribute): att_type = attribute['type'] att_name = strFunctions.upper_first(attribute['name']) name = strFunctions.wrap_token(attribute['name'], self.package) rule_type = 'String' if att_type == 'SId': return elif att_type == 'SIdRef': ref_name = strFunctions.upper_first(attribute['name']) # hack for render if ref_name == 'StartHead' or ref_name == 'EndHead': ref_name = 'LineEnding' text = 'The value of the attribute {0} of {1} {2} object must be ' \ 'the identifier of an existing \{3} object defined in the ' \ 'enclosing \Model object.'\ .format(name, self.indef, self.formatted_name, ref_name) rule_type = ref_name elif att_type == 'string': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('string')) elif att_type == 'int' or att_type == 'uint': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('integer')) rule_type = 'Integer' if att_type == 'int' else 'UnInteger' elif att_type == 'double': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('double')) rule_type = 'Double' elif att_type == 'bool' or att_type == 'boolean': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token('boolean')) rule_type = 'Boolean' elif att_type == 'enum': enum_name = attribute['element'] enums = attribute['parent']['root']['enums'] enum_values = self.parse_enum_values(enum_name, enums) text = 'The value of the attribute {0} of {1} {2} object must ' \ 'conform to the syntax of SBML data type {3} and ' \ 'may only take on the allowed values of {3} defined ' \ 'in SBML; that is, the value must be one of the ' \ 'following {4}.'.format(name, self.indef, self.formatted_name, strFunctions.wrap_enum(enum_name), enum_values) rule_type = '{0}Enum'.format(attribute['element']) elif att_type == 'array': text = 'The value of the attribute {0} of {1} {2} object must ' \ 'be an array of values of type {3}.'\ .format(name, self.indef, self.formatted_name, strFunctions.wrap_token(attribute['element'])) else: text = 'FIX ME: Encountered an unknown attribute type {0} in ' \ 'ValidationRulesForClass:write_attribute_type_rule'\ .format(att_type) ref = '{0}, {1}.'\ .format(self.pkg_ref, strFunctions.wrap_section(self.name)) sev = 'ERROR' lib_sev = '{0}_SEV_ERROR'.format(global_variables.up_full_lib) short = 'Attribute {0} values allowed on <{1}>.' \ ''.format(attribute['name'], self.lower_name) lib_ref = 'L3V1 {0} V1 Section'.format(self.up_package) tc = '{0}{1}{2}MustBe{3}'.format(self.up_package, self.name, att_name, rule_type) return dict({'number': self.number, 'text': text, 'reference': ref, 'severity': sev, 'typecode': tc, 'lib_sev': lib_sev, 'short': short, 'lib_ref': lib_ref})
def expand_class(self, class_object): self.class_object = class_object self.is_list_of = class_object['is_list_of'] self.has_parent_list_of = class_object['hasListOf'] self.name = class_object['name'] self.class_name = class_object['name'] self.package = class_object['package'] self.typecode = class_object['typecode'] if class_object['is_list_of']: self.list_of_name = class_object['list_of_name'] self.list_of_child = class_object['lo_child'] else: self.list_of_name = '' self.list_of_child = '' # check case of things where we assume upper/lower if self.package[0].islower(): self.package = strFunctions.upper_first(class_object['package']) # are we a plugin if 'is_plugin' in class_object: self.is_plugin = class_object['is_plugin'] if 'is_doc_plugin' in class_object: self.is_doc_plugin = class_object['is_doc_plugin'] # information about the base class self.baseClass = class_object['baseClass'] if self.language != 'sbml': if not self.is_list_of: base = '{0}Base'.format(global_variables.prefix) else: base = '{0}ListOf'.format(global_variables.prefix) if base != self.baseClass: self.has_std_base = False elif not self.is_list_of and not self.is_plugin \ and self.baseClass != 'SBase': self.has_std_base = False elif self.is_list_of and not self.is_plugin \ and self.baseClass != 'ListOf': self.has_std_base = False elif self.is_plugin and not self.is_doc_plugin \ and self.baseClass != 'SBasePlugin': self.has_std_base = False elif self.is_doc_plugin: self.has_std_base = True self.std_base = '{0}DocumentPlugin'.format(self.cap_language) self.class_object['has_std_base'] = self.has_std_base self.class_object['std_base'] = self.std_base # references self.sid_refs = class_object['sid_refs'] self.unit_sid_refs = class_object['unit_sid_refs'] if 'addDecls' in class_object: self.add_decls = class_object['addDecls'] if 'addDefs' in class_object: self.add_impl = class_object['addDefs'] # if 'childrenOverwriteElementName' in class_object: # self.overwrites_children = \ # class_object['childrenOverwriteElementName'] if 'root' in class_object: self.overwrites_children = \ query.overwrites_name(class_object['root'], class_object['name']) self.class_object['overwrites_children'] = self.overwrites_children # child elements self.has_math = class_object['hasMath'] self.has_children = query.has_children(class_object['attribs']) if self.has_math and \ not query.has_children_not_math(class_object['attribs']): self.has_only_math = True # mark child elements as ML nodes for i in range(0, len(self.child_elements)): element = self.child_elements[i] if element['element'].endswith('Node') \ and not element['element'].endswith('CSGNode'): self.child_elements[i]['is_ml'] = True self.has_non_std_children = True self.num_non_std_children += 1 else: self.child_elements[i]['is_ml'] = False if 'concrete' in class_object: self.concretes = query.get_concretes(class_object['root'], class_object['concrete']) self.class_attributes = query.separate_attributes(self.attributes) # document class for other libraries self.document = False if 'document' in class_object: self.document = class_object['document'] # add info back to the class_object so we can pass it on self.class_object['package'] = self.package self.class_object['class_attributes'] = self.class_attributes self.class_object['child_lo_elements'] = self.child_lo_elements self.class_object['child_elements'] = self.child_elements self.class_object['concretes'] = self.concretes self.class_object['has_array'] = query.has_array(self.class_attributes) self.class_object['has_vector'] = query.has_vector(self.class_attributes) self.class_object['has_math'] = self.has_math self.class_object['has_children'] = self.has_children self.class_object['has_only_math'] = self.has_only_math self.class_object['has_parent_list_of'] = self.has_parent_list_of self.class_object['num_children'] = self.num_children self.class_object['has_non_std_chilren'] = self.has_non_std_children self.class_object['num_non_std_children'] = self.num_non_std_children self.class_object['is_header'] = self.is_header self.class_object['document'] = self.document
def write_attribute_type_rule(self, attribute, lo=None): if lo: formatted_name = lo['formatted_name'] refname = lo['name'] abbrev = strFunctions.abbrev_lo_name(refname) else: formatted_name = self.formatted_name refname = self.name abbrev = self.name att_type = attribute['type'] att_name = strFunctions.upper_first(attribute['name']) name = strFunctions.wrap_token(attribute['texname'], self.package) rule_type = 'String' if att_type == 'SId': return elif att_type == 'SIdRef': [ref_name, ref_type] = \ strFunctions.get_sid_refs(attribute['element']) # hack for render if ref_name == 'StartHead' or ref_name == 'EndHead': ref_name = 'LineEnding' text = 'The value of the attribute {0} of {1} {2} object must be ' \ 'the identifier of an existing \{3} object defined in the ' \ 'enclosing \Model object.'\ .format(name, self.indef, formatted_name, ref_name) rule_type = ref_type elif att_type == 'string' or att_type == 'IDREF': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, formatted_name, strFunctions.wrap_token('string')) elif att_type == 'int' or att_type == 'uint': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, formatted_name, strFunctions.wrap_token('integer')) rule_type = 'Integer' if att_type == 'int' else 'UnInteger' elif att_type == 'double': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, formatted_name, strFunctions.wrap_token('double')) rule_type = 'Double' elif att_type == 'boolean' or att_type == 'bool': text = 'The attribute {0} on {1} {2} must have a value of data ' \ 'type {3}.'\ .format(name, self.indef, formatted_name, strFunctions.wrap_token('boolean')) rule_type = 'Boolean' elif att_type == 'UnitSId' or att_type == 'UnitSIdRef': text = 'The value of the attribute {0} on {1} {2} must have a ' \ 'taken from the following: the identifier of a ' \ '\\UnitDefinition object in the enclosing \Model, or one ' \ 'of the base units in SBML.'.format(name, self.indef, formatted_name) rule_type = 'Unit' elif att_type == 'enum': enum_name = strFunctions.texify(attribute['element']) enums = attribute['parent']['root']['enums'] enum_values = self.parse_enum_values(attribute['element'], enums) text = 'The value of the attribute {0} of {1} {2} object must ' \ 'conform to the syntax of SBML data type {3} and ' \ 'may only take on the allowed values of {3} defined ' \ 'in SBML; that is, the value must be one of the ' \ 'following: {4}.'.format(name, self.indef, formatted_name, strFunctions.wrap_enum(enum_name), enum_values) rule_type = '{0}Enum'.format(attribute['element']) elif att_type == 'array': text = 'The value of the attribute {0} of {1} {2} object must ' \ 'be an array of values of type {3}.'\ .format(name, self.indef, formatted_name, strFunctions.wrap_token(attribute['element'])) elif att_type == 'element' and attribute['element'] == 'RelAbsVector': text = 'The value of the attribute {0} of {1} {2} object must ' \ 'conform to the syntax of SBML data type \\RelAbsVector ' \ 'i.e. a string encoding optionally an absolute number ' \ 'followed by an optional relative number followed ' \ 'by a \\% sign. Adding spaces between the ' \ 'coordinates is encouraged, but not required.'\ .format(name, self.indef, formatted_name) else: text = 'FIXME: Encountered an unknown attribute type {0} in ' \ 'ValidationRulesForClass'\ .format(att_type) # global_variables.code_returned = \ # global_variables.return_codes['unknown type used'] ref = '{0}, {1}.'\ .format(self.pkg_ref, strFunctions.wrap_section(refname)) sev = 'ERROR' lib_sev = '{0}_SEV_ERROR'.format(global_variables.up_full_lib) short = '{0} attribute must be {1}.'.format(att_name, rule_type) lib_ref = 'L3V1 {0} V1 Section'.format(self.up_package) tc = '{0}{1}{2}MustBe{3}'.format(self.up_package, abbrev, att_name, rule_type) return dict({'number': self.number, 'text': text, 'reference': ref, 'severity': sev, 'typecode': tc, 'lib_sev': lib_sev, 'short': short, 'lib_ref': lib_ref, 'object': refname, 'attrib': att_name, 'attrib_type': att_type})
def write_class_comments(self, extension, plugin, validator): """ Write a comment block about the C++ class. :param extension: `True` if this is an extension :param plugin: `True` if this is a plugin :param validator: `True` if a validator Only one of these three cases may be `True`. All may be `False`. """ # Sanity check: options = [extension, plugin, validator] if sum(options) > 1: # raise an error and...Not sure what Deviser usually does mystr = "Error in write_class_comments - too many true values: " for opt in options: if opt: mystr += "{0} ".format(opt) raise ValueError(mystr) fullname = gv.package_full_name up_package = SF.upper_first(self.package) validator_class_comment = 'The {0} class extends the ' \ 'Validator class from core libSBML to ' \ 'apply validation to the constructs ' \ 'introduced by the SBML Level 3 ' \ '{1} package. This class then acts as a ' \ 'base class for any validators that ' \ 'apply rules to the “{2}” ' \ 'package specification constructs or to ' \ 'entire models that use the “{2}' \ '” package, and may therefore be ' \ 'subject to other global restrictions ' \ 'introduced.'.format(self.name, fullname, self.package.lower()) self.write_blank_comment_line() self.write_comment_line('@class {0}'.format(self.name)) if extension: self.write_comment_line('@sbmlbrief{0}{1}{2} Base extension' ' class for the package' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') self.write_blank_comment_line() self.write_comment_line('This is the {0} package extension of the' ' SBMLExtension class that is used to ' 'facilitate libSBML plug-ins in the ' 'implementation of an SBMLLevel 3 ' 'package.'.format( SF.upper_first(self.package))) self.write_blank_comment_line() self.write_comment_line('@class {0}PkgNamespaces' ''.format(up_package)) self.write_comment_line('@sbmlbrief{0}{1}{2} SBMLNamespaces ' 'extension.' ''.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') elif plugin: self.write_comment_line('@sbmlbrief{0}{1}{2} Extension of ' '{3}.'.format(self.open_br, self.package.lower(), self.close_br, self.class_object['sbase'])) elif validator: self.write_comment_line('@sbmlbrief{0}{1}{2} Entry point for ' '“{1}” package validation' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') self.write_blank_comment_line() self.write_comment_line('@copydetails doc_common_intro_' 'package_validators') self.write_blank_comment_line() self.write_comment_line('{0}'.format(validator_class_comment)) self.write_blank_comment_line() self.write_comment_line('@copydetails doc_section_package_' 'validators_general_info') else: self.write_comment_line('@sbmlbrief{0}{1}{2} TODO:' '{3}'.format(self.open_br, self.package.lower(), self.close_br, self.brief_description))
def write_class_comments(self, extension, plugin, validator): fullname = global_variables.package_full_name up_package = strFunctions.upper_first(self.package) validator_class_comment = 'The {0} class extends the ' \ 'Validator class from core libSBML to ' \ 'apply validation to the constructs ' \ 'introduced by the SBML Level 3 ' \ '{1} package. This class then acts as a ' \ 'base class for any validators that ' \ 'apply rules to the “{2}” ' \ 'package specification constructs or to ' \ 'entire models that use the “{2}' \ '” package, and may therefore be ' \ 'subject to other global restrictions ' \ 'introduced.'.format(self.name, fullname, self.package.lower()) self.write_blank_comment_line() self.write_comment_line('@class {0}'.format(self.name)) if extension: self.write_comment_line( '@sbmlbrief{0}{1}{2} Base extension class for the package' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') self.write_blank_comment_line() self.write_comment_line( 'This is the {0} package extension of the SBMLExtension class ' 'that is used to facilitate libSBML plug-ins in the implementation of an SBML' 'Level 3 package.'.format( strFunctions.upper_first(self.package))) self.write_blank_comment_line() self.write_comment_line('@class {0}PkgNamespaces' ''.format(up_package)) self.write_comment_line('@sbmlbrief{0}{1}{2} SBMLNamespaces ' 'extension.' ''.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') elif plugin: self.write_comment_line('@sbmlbrief{0}{1}{2} Extension of ' '{3}.'.format(self.open_br, self.package.lower(), self.close_br, self.class_object['sbase'])) elif validator: self.write_comment_line('@sbmlbrief{0}{1}{2} Entry point for ' '“{1}” package validation' '.'.format(self.open_br, self.package.lower(), self.close_br)) self.write_blank_comment_line() self.write_comment_line('@htmlinclude not-sbml-warning.html') self.write_blank_comment_line() self.write_comment_line('@copydetails doc_common_intro_' 'package_validators') self.write_blank_comment_line() self.write_comment_line('{0}'.format(validator_class_comment)) self.write_blank_comment_line() self.write_comment_line('@copydetails doc_section_package_' 'validators_general_info') else: self.write_comment_line('@sbmlbrief{0}{1}{2} TODO:' '{3}'.format(self.open_br, self.package.lower(), self.close_br, self.brief_description))
def parse_deviser_xml(filename): """ Parses the given filename and returns a dictionary with the definition contained in it """ sbml_elements = [] elements = [] plugins = [] enums = [] dom = parse(filename) temp = get_value(dom.documentElement, 'name') # we expect this to be lower case package_name = temp.lower() number = to_int(get_value(dom.documentElement, 'number')) offset = to_int(get_value(dom.documentElement, 'offset')) fullname = get_value(dom.documentElement, 'fullname') required = to_bool(get_value(dom.documentElement, 'required')) # get package information (assume we want the first only) sbml_level = 3 sbml_version = 1 pkg_version = 1 for node in dom.getElementsByTagName('pkgVersion'): sbml_level = to_int(get_value(node, 'level')) sbml_version = to_int(get_value(node, 'version')) pkg_version = to_int(get_value(node, 'pkg_version')) break concrete_dict = dict({}) # read concrete versions of abstract classes and fill dictionary for node in dom.getElementsByTagName('element'): element_name = get_value(node, 'name') concrete_list = [] for concrete in node.getElementsByTagName('concrete'): concrete_list.append( dict({ 'name': get_value(concrete, "name"), 'element': get_value(concrete, "element") })) concrete_dict[element_name] = concrete_list # read element for node in dom.getElementsByTagName('element'): element_name = get_value(node, 'name') base_class = get_value(node, 'baseClass') type_code = get_value(node, 'typeCode') has_math = to_bool(get_value(node, 'hasMath')) has_children = to_bool(get_value(node, 'hasChildren')) has_list_of = to_bool(get_value(node, 'hasListOf')) abstract = to_bool(get_value(node, 'abstract')) children_overwrite_element_name = to_bool( get_value(node, 'childrenOverwriteElementName')) temp = get_value(node, 'elementName') # we expect this to be camel case starting with lower if temp is not None: xml_element_name = strFunctions.lower_first(temp) else: xml_element_name = '' temp = get_value(node, 'listOfName') # we expect this to be camel case starting with lower if temp is not None: xml_lo_element_name = strFunctions.lower_first(temp) else: xml_lo_element_name = '' temp = get_value(node, 'listOfClassName') # we expect this to be camel case starting with upper if temp is not None: lo_class_name = strFunctions.upper_first(temp) else: lo_class_name = '' add_decls = get_value(node, 'additionalDecls') add_defs = get_value(node, 'additionalDefs') attributes = [] # add attributes for attr in node.getElementsByTagName('attribute'): attr_name = get_value(attr, 'name') required = to_bool(get_value(attr, 'required')) attr_type = standardize_types(get_value(attr, 'type')) attr_abstract = to_bool(get_value(attr, 'abstract')) temp = get_value(attr, 'element') # expect this to be uppercase if temp is not None: attr_element = strFunctions.standard_element_name(temp) else: attr_element = '' attribute_dict = dict({ 'type': attr_type, 'reqd': required, 'name': attr_name, 'element': attr_element, 'abstract': attr_abstract, }) if attr_abstract: attribute_dict['concrete'] = concrete_dict[attr_element] attributes.append(attribute_dict) lo_attributes = [] # add attributes for attr in node.getElementsByTagName('listOfAttribute'): attr_name = get_value(attr, 'name') required = to_bool(get_value(attr, 'required')) attr_type = get_value(attr, 'type') attr_abstract = to_bool(get_value(attr, 'abstract')) attr_element = get_value(attr, 'element') attribute_dict = dict({ 'type': attr_type, 'reqd': required, 'name': attr_name, 'element': attr_element, 'abstract': attr_abstract }) if attr_abstract: attribute_dict['concrete'] = concrete_dict[attr_element] lo_attributes.append(attribute_dict) # construct element element = dict({ 'name': element_name, 'package': package_name, 'typecode': type_code, 'hasListOf': has_list_of, 'attribs': attributes, 'lo_attribs': lo_attributes, 'hasChildren': has_children, 'hasMath': has_math, 'childrenOverwriteElementName': children_overwrite_element_name, 'baseClass': base_class, 'abstract': abstract }) if xml_element_name is not None: element['elementName'] = xml_element_name if xml_lo_element_name is not None: element['lo_elementName'] = xml_lo_element_name if lo_class_name is not None: element['lo_class_name'] = lo_class_name if add_decls is not None: temp = os.path.dirname(filename) + '//' + add_decls if os.path.exists(temp): add_decls = os.path.abspath(temp) element['addDecls'] = add_decls if add_defs is not None: if os.path.exists(os.path.dirname(filename) + '/' + add_defs): add_defs += os.path.abspath(filename) + '/' element['addDefs'] = add_defs if abstract: element['concrete'] = concrete_dict[element_name] elements.append( dict({ 'name': element_name, 'typecode': type_code, 'isListOf': has_list_of, 'listOfName': xml_lo_element_name if xml_lo_element_name is not None else '', 'listOfClassName': lo_class_name if lo_class_name is not None else '' })) sbml_elements.append(element) for node in dom.getElementsByTagName('plugin'): plug_elements = [] plug_lo_elements = [] ext_point = get_value(node, 'extensionPoint') add_decls = get_value(node, 'additionalDecls') add_defs = get_value(node, 'additionalDefs') # read references to elements for reference in node.getElementsByTagName('reference'): temp = find_element(elements, get_value(reference, 'name')) if temp is not None: plug_elements.append(temp) # look for references to ListOf elements for reference in node.getElementsByTagName('reference'): temp = find_lo_element(elements, get_value(reference, 'name')) if temp is not None: plug_lo_elements.append(temp) attributes = [] # read additional attributes for attr in node.getElementsByTagName('attribute'): attr_name = get_value(attr, 'name') required = to_bool(get_value(attr, 'required')) attr_type = get_value(attr, 'type') attr_abstract = to_bool(get_value(attr, 'abstract')) attr_element = get_value(attr, 'element') attribute_dict = dict({ 'type': attr_type, 'reqd': required, 'name': attr_name, 'element': attr_element, 'abstract': attr_abstract }) if attr_abstract: attribute_dict['concrete'] = concrete_dict[attr_element] attributes.append(attribute_dict) plugin_dict = dict({ 'sbase': ext_point, 'extension': plug_elements, 'attribs': attributes, 'lo_extension': plug_lo_elements }) if add_decls is not None: if os.path.exists(os.path.dirname(filename) + '/' + add_decls): add_decls += os.path.dirname(filename) + '/' plugin_dict['addDecls'] = add_decls if add_defs is not None: if os.path.exists(os.path.dirname(filename) + '/' + add_defs): add_defs += os.path.dirname(filename) + '/' plugin_dict['addDefs'] = add_defs plugins.append(plugin_dict) for node in dom.getElementsByTagName('enum'): values = [] enum_name = get_value(node, 'name') for val in node.getElementsByTagName('enumValue'): values.append( dict({ 'name': get_value(val, 'name'), 'value': get_value(val, 'value') })) enums.append(dict({'name': enum_name, 'values': values})) package = dict({ 'name': package_name, 'elements': elements, 'plugins': plugins, 'number': number, 'sbmlElements': sbml_elements, 'enums': enums, 'offset': offset, 'fullname': fullname, 'sbml_level': sbml_level, 'sbml_version': sbml_version, 'pkg_version': pkg_version, 'required': required }) # link elements for elem in package['elements']: elem['root'] = package if 'attribs' in elem: for attr in elem['attribs']: attr['parent'] = elem attr['root'] = package for elem in package['sbmlElements']: elem['root'] = package if 'attribs' in elem: for attr in elem['attribs']: attr['parent'] = elem attr['root'] = package if 'concrete' in elem: for attr in elem['concrete']: attr['parent'] = elem attr['root'] = package return package