def print_document_errors(self, fileout): root = {'baseElements': self.elements} doc = query.get_class(global_variables.document_class, root) if not doc: docname = 'Document' else: docname = strFunctions.prefix_name(doc['name']) libname = strFunctions.get_library_suffix(global_variables.library_name) fileout.copy_line_verbatim(' if ( errorId == {0}{1}Allowed' 'CoreAttributes\n'.format(libname, docname)) level = False version = False if doc and 'attribs' in doc: for a in doc['attribs']: if a['name'] == 'level': level = True elif a['name'] == 'version': version = True if level: fileout.copy_line_verbatim(' || errorId == {0}{1}' 'LevelMustBeInteger\n' ''.format(libname, docname)) if version: fileout.copy_line_verbatim(' || errorId == {0}{1}' 'VersionMustBeInteger\n' ''.format(libname, docname)) fileout.copy_line_verbatim(' || errorId == InvalidNamespace' 'On{0})\n'.format(global_variables.prefix))
def parse_elements(self, attributes, elements): for i in range(0, len(attributes)): if self.is_element(attributes[i]['type']): # hack for render if attributes[i]['element'] == 'RelAbsVector': if attributes[i]['reqd'] is True: self.reqd_att.append(attributes[i]) else: self.opt_att.append(attributes[i]) else: element = query.get_class(attributes[i]['element'], elements) if element: attributes[i]['min_lo_children'] \ = element['min_lo_children'] attributes[i]['listOfClassName'] = element['lo_class_name'] attributes[i]['isListOf'] = element['hasListOf'] if attributes[i]['reqd'] is True: if attributes[i]['type'] != 'element': self.reqd_child_lo_elem.append(attributes[i]) self.reqd_elem.append(attributes[i]) else: self.opt_elem.append(attributes[i]) if attributes[i]['type'] != 'element': self.opt_child_lo_elem.append(attributes[i])
def has_elements_with_same_xml_name(self): for element in self.elements: if 'concrete' in element and element['concrete']: names_used = [] for conc in element['concrete']: conc_class = query.get_class(conc['element'], conc['root']) if conc_class: if 'elementName' in conc_class and conc_class[ 'elementName'] != '': if conc_class['elementName'] in names_used: return True else: names_used.append(conc_class['elementName']) return False
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 print_document_errors(self, fileout): """ Write out the document errors to the file. TODO Sorry, I don't know what these are :param fileout: object representing output file we are writing. """ root = {'baseElements': self.elements} doc = query.get_class(gv.document_class, root) if not doc: docname = 'Document' else: docname = SF.remove_prefix(doc['name'], remove_package=False, remove_doc_prefix=True) libname = SF.get_library_suffix(gv.library_name) if not libname.endswith('ml'): libname = libname + 'ml' fileout.copy_line_verbatim((10 * '') + 'if ( errorId == {0}{1}Allowed' 'CoreAttributes\n'.format(libname, docname)) level = False version = False if doc and 'attribs' in doc: for a in doc['attribs']: if a['name'] == 'level': level = True elif a['name'] == 'version': version = True if level: fileout.copy_line_verbatim((12 * ' ') + '|| errorId == {0}{1}' 'LevelMustBeNonNegativeInteger\n' ''.format(libname, docname)) if version: fileout.copy_line_verbatim((12 * ' ') + '|| errorId == {0}{1}' 'VersionMustBeNonNegativeInteger\n' ''.format(libname, docname)) fileout.copy_line_verbatim(' || errorId == InvalidNamespace' 'On{0})\n'.format(gv.prefix))
def write_lochild_attribute_rule(self, child, lo_info): child_class = query.get_class(child['element'], child['root']) if not child_class or len(child_class['lo_attribs']) == 0: return # if these are all elements we dont need this num = len(child_class['lo_attribs']) count = 0 for attrib in child_class['lo_attribs']: if self.is_element(attrib['type']): count += 1 if count == num: return attributes = [] if len(child_class['lo_class_name']) == 0: child_class['lo_class_name'] = strFunctions.list_of_name(child_class['name']) formatted_name = '\\' + child_class['lo_class_name'] name = child_class['name'] child_reqd = [] child_opt = [] for attrib in child_class['lo_attribs']: attributes.append(attrib) if attrib['reqd']: child_reqd.append(attrib) else: child_opt.append(attrib) lo_info.append(dict({'formatted_name': formatted_name, 'name': child_class['lo_class_name'], 'attributes': attributes})) reqd = self.parse_required(self, child_reqd) opt = self.parse_optional(self, child_opt) no_other_statement = 'No other attributes from the SBML Level 3 {0} ' \ 'namespaces are permitted on {1} {2} object. '\ .format(self.fullname, self.indef, formatted_name) if len(opt) == 0 and len(reqd) > 0: text = '{0} {1} object must have {2}. {3}'\ .format(self.indef_u, formatted_name, reqd, no_other_statement) elif len(reqd) == 0 and len(opt) > 0: text = '{0} {1} object may have {2}. {3}'\ .format(self.indef_u, formatted_name, opt, no_other_statement) else: text = '{0} {1} object must have {2}, and may have {3}. {4}'\ .format(self.indef_u, formatted_name, reqd, opt, no_other_statement) ref = '{0}, {1}.'\ .format(self.pkg_ref, strFunctions.wrap_section(child_class['lo_class_name'])) sev = 'ERROR' lib_sev = '{0}_SEV_ERROR'.format(global_variables.up_full_lib) short = 'Attributes allowed on <{0}>.'.format(strFunctions.lower_first(child_class['lo_class_name'])) lib_ref = 'L3V1 {0} V1 Section'.format(self.up_package) tc = '{0}{1}LO{2}AllowedAttributes'.format(self.up_package, self.name, strFunctions.plural(name)) return dict({'number': self.number, 'text': text, 'reference': ref, 'severity': sev, 'typecode': tc, 'lib_sev': lib_sev, 'short': short, 'lib_ref': lib_ref, 'plugin': False, 'object': self.name, 'lo': True, 'reqd': child_reqd, 'opt': child_opt, 'lo_object': lo_info[0]['name']})
def write_get_by_sid(self): # only write for elements with base derived children in cpp if not self.is_cpp_api or self.num_children == 0: return elif self.num_children == self.num_non_std_children: return # create comment parts title_line = 'Returns the first child element that has the given ' \ '@p id in the model-wide SId namespace, or @c NULL if ' \ 'no such object is found.' params = ['@param id a string representing the id attribute ' 'of the object to retrieve.'] return_lines = ['@return a pointer to the {0} element with the ' 'given @p id.'.format(self.std_base)] additional = [] # create the function declaration function = 'getElementBySId' return_type = '{0}*'.format(self.std_base) arguments = ['const std::string& id'] code = [] if not self.is_header: implementation = ['id.empty()', 'return NULL'] code = [self.create_code_block('if', implementation), self.create_code_block('line', ['{0}* obj = ' 'NULL'.format(self.std_base)])] if_block = ['obj != NULL', 'return obj'] if_code = self.create_code_block('if', if_block) for i in range(0, len(self.child_elements)): element_type = self.child_elements[i]['element'] if element_type == 'ASTNode' or element_type == 'XMLNode': continue name = self.child_elements[i]['memberName'] middle_if = self.create_code_block('if', ['{0}->getId() == ' 'id'.format(name), 'return {0}'.format(name)]) code.append(self.create_code_block('if', ['{0} != NULL'.format(name), middle_if, 'obj = {0}->getElementBy' 'SId(id)'.format(name), if_code])) for child in self.child_lo_elements: if query.has_lo_attribute(query.get_class(child['name'], child['root']), 'id'): line = ['{0}.getId() == id'.format(child['memberName']), 'return &{0}'.format(child['memberName'])] code.append(self.create_code_block('if', line)) line = [' obj = {0}.getElementBySId(' 'id)'.format(child['memberName'])] code.append(self.create_code_block('line', line)) code.append(if_code) if self.is_list_of: code.append(self.create_code_block('line', ['return ListOf::' 'getElementBySId(id)'])) else: code.append(self.create_code_block('line', ['return obj'])) # 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_get_by_sid(self): # only write for elements with base derived children in cpp if not self.is_cpp_api or self.num_children == 0: return elif self.num_children == self.num_non_std_children: return # create comment parts title_line = 'Returns the first child element that has the given ' \ '@p id in the model-wide SId namespace, or @c NULL if ' \ 'no such object is found.' params = [ '@param id a string representing the id attribute ' 'of the object to retrieve.' ] return_lines = [ '@return a pointer to the {0} element with the ' 'given @p id. If no such object is found, this method returns @c NULL.' .format(self.std_base) ] additional = [] # create the function declaration function = 'getElementBySId' return_type = '{0}*'.format(self.std_base) arguments = ['const std::string& id'] code = [] if not self.is_header: implementation = ['id.empty()', 'return NULL'] code = [ self.create_code_block('if', implementation), self.create_code_block( 'line', ['{0}* obj = ' 'NULL'.format(self.std_base)]) ] if_block = ['obj != NULL', 'return obj'] if_code = self.create_code_block('if', if_block) for i in range(0, len(self.child_elements)): element_type = self.child_elements[i]['element'] if element_type == 'ASTNode' or element_type == 'XMLNode': continue name = self.child_elements[i]['memberName'] middle_if = self.create_code_block('if', [ '{0}->getId() == ' 'id'.format(name), 'return {0}'.format(name) ]) code.append( self.create_code_block('if', [ '{0} != NULL'.format(name), middle_if, 'obj = {0}->getElementBy' 'SId(id)'.format(name), if_code ])) for child in self.child_lo_elements: if query.has_lo_attribute( query.get_class(child['name'], child['root']), 'id'): line = [ '{0}.getId() == id'.format(child['memberName']), 'return &{0}'.format(child['memberName']) ] code.append(self.create_code_block('if', line)) line = [ ' obj = {0}.getElementBySId(' 'id)'.format(child['memberName']) ] code.append(self.create_code_block('line', line)) code.append(if_code) if self.is_list_of: code.append( self.create_code_block( 'line', ['return ListOf::' 'getElementBySId(id)'])) else: code.append(self.create_code_block('line', ['return obj'])) # 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 expand_attributes(self, attributes): for i in range(0, len(attributes)): [attrib_name, had_hyphen] = strFunctions.remove_hyphens(attributes[i]['name']) capname = strFunctions.upper_first(attrib_name) if had_hyphen: orig_name = attributes[i]['name'] attributes[i]['name'] = strFunctions.lower_first(orig_name) else: attributes[i]['name'] = strFunctions.lower_first(capname) attributes[i]['capAttName'] = capname attributes[i]['memberName'] = 'm' + capname attributes[i]['pluralName'] = \ strFunctions.plural(attrib_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' or att_type == 'ID': 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 = attrib_name attributes[i]['attType'] = 'element' if attrib_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': childclass = query.get_class(attributes[i]['element'], attributes[i]['root']) if 'lo_class_name' in childclass and childclass[ 'lo_class_name'] != '': name = childclass['lo_class_name'] else: 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' if 'xml_name' in attributes[ i] and attributes[i]['xml_name'] != '': attributes[i]['used_child_name'] = strFunctions.singular( attributes[i]['xml_name']) elif att_type == 'array': attributes[i]['isArray'] = True if attributes[i]['element'] == 'Integer' or attributes[i][ 'element'] == 'integer': attributes[i]['element'] = 'int' else: 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 if attributes[i]['element'] == 'Integer' or attributes[i][ 'element'] == 'integer': attributes[i]['element'] = 'int' else: 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_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 })