def _parse_function_id_type(self, function_name, attrib): """Parse function id and message type according to XML format. This implementation extracts attribute "FunctionID" as function id and messagetype as message type and searches them in enums "FunctionID" and "messageType". If at least one of them (or the entire enum) is missing it raises an error. Returns function id and message type as an instances of EnumElement. """ if "functionID" not in attrib: raise RPCBase.ParseError("No functionID specified for function '" + function_name + "'") if "messagetype" not in attrib: raise RPCBase.ParseError( "No messagetype specified for function '" + function_name + "'") function_id = self._get_enum_element_for_function( "FunctionID", self._extract_attrib(attrib, "functionID")) message_type = self._get_enum_element_for_function( "messageType", self._extract_attrib(attrib, "messagetype")) return function_id, message_type
def _provide_enum_element_for_function(self, enum_name, element_name): """Provide enum element for functions. This implementation replaces the underscore separating interface and function name with dot and sets it as name of enum element leaving the name with underscore as internal_name. For enums other than FunctionID the base implementation is called. Returns EnumElement. """ name = element_name internal_name = None if "FunctionID" == enum_name: prefix_length = len(self._interface_name) + 1 if element_name[:prefix_length] != self._interface_name + '_': raise RPCBase.ParseError( "Unexpected prefix for function id '" + element_name + "'") name = self._interface_name + "." + element_name[prefix_length:] internal_name = element_name element = super(Parser, self)._provide_enum_element_for_function( enum_name, name) if internal_name is not None: element.internal_name = internal_name return element
def check_version_format(version): """Checks correctness of format of version """ p = re.compile('\d+\\.\d+\\.\d+') result = p.match(version) if result == None or (result.end() != len(version)): raise RPCBase.ParseError("Incorrect format of version please check MOBILE_API.xml. " "Need format of version major_version.minor_version.patch_version")
def generate_msg_version(file_name, path_to_storage): """Parses MOBILE_API.xml in order to receive major_version, minor_version, and patch_version """ tree = xml.etree.ElementTree.parse(file_name) root = tree.getroot() if (root.tag == "interface" and "version" in root.attrib): check_version_format(root.attrib["version"]) array = (root.attrib["version"]).split(".") major_version = array[0] minor_version = array[1] patch_version = array[2] if (major_version.isdigit() and minor_version.isdigit() and patch_version.isdigit): data_for_storage = prepare_data_for_storage(major_version, minor_version, patch_version) store_data_to_file(path_to_storage, data_for_storage) else: raise RPCBase.ParseError("Attribute version has incorect value in MOBILE_API.xml") else: raise RPCBase.ParseError("Check MOBILE_API.xml file, parser can not find first element " " with tag interface or atribute version")
def _check_function_param_name(self, function_param_name): """Check function param name. This method is called to check whether the newly parsed function parameter name conflicts with some predefined name. """ if function_param_name in ['method', 'code']: raise RPCBase.ParseError( "'" + function_param_name + "' is a predefined name and can't be used" + " as a function parameter name")
def _get_enum_element_for_function(self, enum_name, element_name): """Get enum element with given name from given enumeration. Returns an instance of generator.Model.EnumElement. """ if enum_name not in self._types: raise RPCBase.ParseError("Enumeration '" + enum_name + "' must be declared before any function") enum = self._types[enum_name] if type(enum) is not Model.Enum: raise RPCBase.ParseError("'" + enum_name + "' is not an enumeration") if element_name not in enum.elements: raise RPCBase.ParseError("'" + element_name + "' is not a member of enum '" + enum_name + "'") return enum.elements[element_name]
def _parse_root(self, root): """Parse root XML element. This implementation parses root as interfaces element with multiple interfaces in it. Keyword arguments: root -- root element. """ self._params = root.attrib self._interface_name = None for element in root: if element.tag != "interface": raise RPCBase.ParseError("Subelement '" + element.tag + "' is unexpected in interfaces") if "name" not in element.attrib: raise RPCBase.ParseError("Name is not specified for interface") self._interface_name = element.attrib["name"] self._parse_interface(element, self._interface_name + "_")