Example #1
0
    def __init__(self, cap_file):
        """!Initialize xml.etree.ElementTree
        """
        try:
            etree.ElementTree.__init__(self, file = cap_file)
        except etree.ParseError:
            raise etree.ParseError(_("Unable to parse XML file"))
        except IOError as error:
            raise etree.ParseError(_("Unabble to open XML file '%s'.\n%s\n" % (cap_file, error)))

        if self.getroot() is None:
            raise etree.ParseError(_("Root node was not found.")) 
Example #2
0
    def __init__(self, conffile=None, force_reload=False):
        '''
    Typically the Config class is instantiated without arguments. The
    conffile and force_reload options are used to control the class
    singleton behaviour during testing.
    '''
        if force_reload:
            self.__dict__.clear()
            self._is_initialised(False)

        if self._is_initialised():
            return

        if conffile is None:
            confname = 'osqpipe_config.xml'
            LOGGER.debug("Looking for config file %s...", confname)
            for loc in os.curdir, os.path.expanduser("~"), "/etc", \
                  os.environ.get("OSQPIPE_CONFDIR"):
                if loc is not None:
                    source = os.path.join(loc, confname)
                    if os.path.exists(source):
                        conffile = source
                        LOGGER.info("Found config file at %s", conffile)
                        break
            if not conffile:
                thisdir = os.path.dirname(os.path.realpath(__file__))
                conffile = os.path.join(thisdir, 'config', confname)


#        LOGGER.warning("Site configuration file not found."
#                       + " Falling back to package config %s.", conffile)
        if not os.path.exists(conffile):
            LOGGER.error("Configuration file not found (%s).", conffile)

        config = ET.parse(conffile)

        # Note that currently we assume no name collisions between
        # sections, to ease transition from the database cs_config table
        # to a config file. This may change in future.
        for section in config.getroot().findall('./section'):
            for option in section.findall('./option'):
                if 'name' not in option.attrib:
                    raise ET.ParseError("Option tag has no name attribute.")
                key = option.attrib['name']
                if key in self.__dict__.keys():
                    raise ET.ParseError(
                        "Duplicate option name in config file: %s" % (key, ))
                self.__dict__[key] = self._parse_value_elem(option)

        self._config(config)
        self._conffile(conffile)
        self._is_initialised(True)
Example #3
0
    def __init__(self, file_path):
        """
            Args:
                file_path: The path of the file from which to retrieve song information
        """
        self.file_path = file_path
        pe = ElementTree.ParseError("File is not in proper OpenSong format")

        #The file should be in XML format and the root tag should be "song"
        try:
            dt = ElementTree.parse(self.file_path)
        except ParseError:
            raise pe

        r = dt.getroot()
        if r.tag.lower() != 'song':
            raise pe

        #Extract the song data from the file
        self.lyrics = getattr(dt.find('lyrics'), 'text', '') or ''
        self.title = getattr(dt.find('title'), 'text', '') or ''
        self.author = getattr(dt.find('author'), 'text', '') or ''
        self.copyright = getattr(dt.find('copyright'), 'text', '') or ''
        self.ccli = getattr(dt.find('ccli'), 'text', '') or ''
        self.presentation = getattr(dt.find('presentation'), 'text', '') or ''

        self.capo = ''
        self.capo_print = False
        capo_data = dt.find('capo')

        if capo_data is not None:
            self.capo = capo_data.text
            self.capo_print = capo_data.attrib.get('print', '') == 'true'

        self.key = getattr(dt.find('key'), 'text', '') or ''
def frame_xml_parse(xml_path: str) -> dict:
    """
    Parse a xml file of a frame
    :param xml_path: the path to the xml file
    :return: a dict containing the filename and objects info of the frame
    """
    xml_path = pt.normpath(xml_path)
    frame_info = dict()
    xy_seq = ('xmax', 'xmin', 'ymax', 'ymin')
    try:
        root = Et.parse(xml_path).getroot()
        frame_info['filename'] = root.find('filename').text

        # parse each object of this frame
        obj = []
        for child_obj in root.iter('object'):
            c_obj = dict()
            c_obj['name'] = arg_set.CLASS_IDS[child_obj.find('name').text]
            c_obj['trackid'] = int(child_obj.find('trackid').text)
            c_obj['occluded'] = int(child_obj.find('occluded').text)
            c_obj['generated'] = int(child_obj.find('generated').text)
            box = child_obj.find('bndbox')
            c_obj['bndbox'] = [int(box.find(a).text) for a in xy_seq]
            obj.append(c_obj)
        frame_info['object'] = obj
    except Exception:
        raise Et.ParseError('Fail:\n%s\n' % xml_path)
    return frame_info
Example #5
0
    def _parse_value_elem(self, value):
        '''
    A recursive method used to extract a config value (dict, list, or
    scalar) of abritrary depth.
    '''
        # First, if the value elem has children, it must be list or dict.
        children = list(value)
        if len(children) > 0:

            # list or dict parsing; recursion necessary.
            if all('name' in e.attrib for e in children):

                # dict
                return dict((e.attrib['name'], self._parse_value_elem(e))
                            for e in children)

            elif all('name' not in e.attrib for e in children):

                # list
                return [self._parse_value_elem(e) for e in children]

            else:
                raise ET.ParseError(
                    "Value is ambiguous; neither list nor dict. Values must" +
                    " all have 'name' attribute, or must all lack it.")

        # If no children, it must be scalar
        else:
            return value.text
Example #6
0
 def fn_replace(context, txt, regexp, repl) :
     txt = asstr(txt)
     repl = asstr(repl)
     try :
         res = re.sub(regexp, repl, txt)
     except Exception as e :
         raise et.ParseError(e.message + ": txt = {}, regexp = {}, repl = {}".format(txt, regexp, repl))
     return res
def _parse(fin):
    """Parse an XML file. Improves the error message by saying which file throws an error."""
    try:
        tree = ET.parse(fin)
    except ET.ParseError as e:
        raise ET.ParseError(f"Error parsing {fin}: {str(e)}")

    return tree
Example #8
0
 def __init__(self, file_path: str) -> None:
     tree = ElementTree.parse(file_path, )
     self._root = tree.getroot()
     header = self._root.find('header')
     if header is None:
         raise ElementTree.ParseError(
             'DAT file does not contain a valid "header"')
     self._header = Header.from_xml(header)
Example #9
0
 def _parse_response(self, relpath, attr=None):
     """
     returns text node of element or attribute of element
     of the first add-on from xml response.
     """
     try:
         el = self.xml_response.getroot().find('./addon/%s' % relpath)
         return attr and el.attrib[attr] or el.text
     except (AttributeError, KeyError):
         raise ET.ParseError(self._error_message(relpath, attr))
Example #10
0
 def unparsed_entity_declaration(
         self,
         entity_name,
         base,
         system_id,  # type: ignore
         public_id,
         notation_name):
     raise PyElementTree.ParseError(
         "Unparsed entities are forbidden (entity_name={!r})".format(
             entity_name))
Example #11
0
 def count_args(actual, needed, opcode):
     """
     Counts if instruction has valid arguments number
     :param actual: Actual instruction's number of arguments
     :param needed: Needed instruction's number of arguments
     :param opcode: Instruction code
     """
     if actual != needed:
         raise ET.ParseError(
             "too many or missing arguments for %s instruction" % opcode)
Example #12
0
    def __init__(self, cap_file, force_version=None):
        """!Parses WMS capabilities file.
            If the capabilities file cannot be parsed if it raises xml.etree.ElementTree.ParseError.

        The class manges inheritance in 'Layer' elements. Inherited elements 
        are added to 'Layer' element.
        The class also removes elements which are in invalid form and are needed 
        by wxGUI capabilities dialog.

        @param cap_file - capabilities file        
        @param force_version - force capabilities file version (1.1.1, 1.3.0)
        """
        BaseCapabilitiesTree.__init__(self, cap_file)
        self.xml_ns = WMSXMLNsHandler(self)

        grass.debug('Checking WMS capabilities tree.', 4)

        if not "version" in self.getroot().attrib:
            raise etree.ParseError(
                _("Missing version attribute root node "
                  "in Capabilities XML file"))
        else:
            wms_version = self.getroot().attrib["version"]

        if wms_version == "1.3.0":
            self.proj_tag = "CRS"
        else:
            self.proj_tag = "SRS"

        if force_version is not None:
            if wms_version != force_version:
                raise etree.ParseError(
                    _("WMS server does not support '%s' version.") %
                    wms_version)

        capability = self._find(self.getroot(), "Capability")
        root_layer = self._find(capability, "Layer")

        self._checkFormats(capability)
        self._checkLayerTree(root_layer)

        grass.debug('Check of WMS capabilities tree was finished.', 4)
Example #13
0
    def _find(self, etreeElement, tag):
        """!Find child element.
            If the element is not found it raises xml.etree.ElementTree.ParseError.  
        """
        res = etreeElement.find(tag)

        if res is None:
            raise  etree.ParseError(_("Unable to parse tile service file. \n\
                                       Tag <%s> was not found.") % tag)

        return res
Example #14
0
    def _findall(self, etreeElement, tag):
        """!Find all children element.
            If no element is found it raises xml.etree.ElementTree.ParseError.  
        """
        res = etreeElement.findall(self.xml_ns.Ns(tag))

        if not res:
            raise etree.ParseError(_("Unable to parse capabilities file. \n\
                                      Tag <%s> was not found.") % tag)

        return res
Example #15
0
	def loadXML(self):
		content = None
		try :
			with open(self.filePath, 'r') as file:
				xmlFile = ET.parse(file)
				rootObj = xmlFile.getroot()
				content = rootObj
		except:
			raise ET.ParseError(f"Unexpected error while loading the file:\n{self.fullPath}\n{exc_info()}")

		return content
Example #16
0
 def test_elementtree_parse_file_error(self, mock_parse):
     err = ET.ParseError()
     err.msg = "it broke"
     err.lineno = 1
     mock_parse.side_effect = err
     try:
         utils.elementtree_parse_file("test_file")
     except ET.ParseError as err:
         assert str(err) == "it broke (test_file, line 1)"
     else:
         assert False  # Expected ParseError
Example #17
0
 def test_elementtree_parse_file_error(self, mock_parse):
     err = ET.ParseError()
     err.msg = "it broke"
     err.lineno = 1
     mock_parse.side_effect = err
     try:
         utils.elementtree_parse_file("test_file")
     except ET.ParseError as err:
         self.assertEqual(str(err), "it broke (test_file, line 1)")
     else:
         self.fail("Expected ParseError")
Example #18
0
 def entity_declaration(
         self,
         entity_name,
         is_parameter_entity,
         value,
         base,  # type: ignore
         system_id,
         public_id,
         notation_name):
     raise PyElementTree.ParseError(
         "Entities are forbidden (entity_name={!r})".format(entity_name))
Example #19
0
    def __init__(self, caps):
        """!Handle XML namespaces according to WMS version of capabilities.
        """
        self.namespace = "{http://www.opengis.net/wms}"

        if caps.getroot().find("Service") is not None:
            self.use_ns = False
        elif caps.getroot().find(self.namespace + "Service") is not None:
            self.use_ns = True
        else:
            raise etree.ParseError(_("Unable to parse capabilities file.\n\
                                      Tag <%s> was not found.") % "Service")
Example #20
0
 def test_duplicate_grammar_exception_xml(self):
     element = ET.fromstring("<template />")
     xml_exception = ET.ParseError()
     xml_exception.position = []
     xml_exception.position.append(1)
     xml_exception.position.append(2)
     exception = ParserException("message", filename="test.xml", xml_exception=xml_exception, xml_element=element)
     self.assertEqual("message", exception.message)
     self.assertEqual("test.xml", exception.filename)
     self.assertEqual(xml_exception, exception.xml_exception)
     self.assertEqual(element, exception._xml_element)
     self.assertEqual("message in [test.xml] at [line(1), column(2)]", exception.format_message())
Example #21
0
    def _findall(self, etreeElement, tag, ns = None):
        """!Find all children element.
            If no element is found it raises xml.etree.ElementTree.ParseError.  
        """
        if not ns:
            res = etreeElement.findall(tag)
        else:
            res = etreeElement.findall(ns(tag))

        if not res:
            raise etree.ParseError(_("Unable to parse capabilities file. \n\
                                      Tag '%s' was not found.") % tag)

        return res
Example #22
0
    def symb(self, arg):
        """
        Parse and validate symbols
        :param arg: Symbol argument
        """
        if arg[0] == "var":
            self.var(arg)
        elif arg[0] == "bool":
            if arg[1] != "true" and arg[1] != "false":
                raise IPPcodeParseError("unknown bool value")
        elif arg[0] == "int":
            try:
                arg[1] = int(arg[1])
            except (ValueError, TypeError):
                raise IPPcodeParseError("wrong int literal")
        elif arg[0] == "float":
            try:
                arg[1] = float.fromhex(arg[1])
            except (ValueError, TypeError):
                raise IPPcodeParseError("wrong float literal")
        elif arg[0] == "string":
            if arg[1] is None:
                arg[1] = ""

            if " " in arg[1]:
                raise IPPcodeParseError("found whitespace in string literal")
            elif "#" in arg[1]:
                raise IPPcodeParseError("found # char in string literal")

            arg[1] = unescape(arg[1])

            i = 0
            for char in arg[1]:
                if char == "\\":
                    try:
                        xyz = int(arg[1][i + 1] + arg[1][i + 2] +
                                  arg[1][i + 3])
                        arg[1] = arg[1][:i] + chr(xyz) + arg[1][i + 4:]
                        i -= 3
                    except (ValueError, IndexError):
                        raise IPPcodeParseError("wrong string escape sequence")
                i += 1
        else:
            raise ET.ParseError(
                "symbol can only be var, int, float, bool or string")
Example #23
0
 def test_remove_xml_element_parse_error(self, mock_parse):
     err = ET.ParseError()
     err.msg = "it broke"
     err.lineno = 1
     mock_parse.side_effect = err
     with utils.temporary_dir() as d:
         path = os.path.join(d, "test.xml")
         with open(path, "w") as f:
             f.write(
                 '<?xml version="1.0" ?>'
                 '<root xmlns="http://soap.sforce.com/2006/04/metadata">'
                 "<tag>text</tag></root>")
         try:
             utils.remove_xml_element_directory("tag", d, "*")
         except ET.ParseError as err:
             assert str(err) == "it broke (test.xml, line 1)"
         else:
             assert False  # Expected ParseError
Example #24
0
    def _parse(self):
        """Parse reference.info.xml in reference folder."""
        fileName = real_ppath(self.fileName)
        if isExist(fileName):
            try:
                tree = ET.parse(fileName)
                root = tree.getroot()
                ref = root.find("reference")
                refFile = ref.find("file")
                refFormat = refFile.get("format")
                if refFormat.lower().find("text/fasta") != -1:
                    self.refFastaFile = real_upath(
                        op.join(self.dirname, op.relpath(refFile.text)))
                else:
                    errMsg = "Could not find the reference fasta " + \
                             "file in reference.info.xml."
                    raise IOError(errMsg)

                for node in ref.getchildren():
                    if node.tag == "description":
                        self.desc = node.text
                    if node.tag == "index_file" and \
                       node.get("type").lower() == "sawriter":
                        self.refSawriterFile = real_upath(
                            op.join(self.dirname, op.relpath(node.text)))

                # Get the adapter annotation GFF file
                annotations = root.findall("annotations/annotation")
                for annotation in annotations:
                    if annotation.get("type") == "adapter":
                        self.adapterGffFile = real_upath(
                            op.join(self.dirname,
                                    annotation.find("file").text))
                        break
            except IOError as e:
                raise IOError(str(e))
            except ET.ParseError as e:
                errMsg = "Failed to parse {f}".format(f=fileName)
                raise ET.ParseError(errMsg)
        else:
            errMsg = "{fn} is not a valid reference info file."\
                .format(fn=fileName)
            raise IOError(errMsg)
Example #25
0
File: ssv.py Project: ahd985/ssv
    def from_json(json):
        try:
            ssv = SSV.create_vis(json['x_series'], json['x_series_unit'],
                                 ET.fromstring(json['tree']), json['title'],
                                 json['font_size'])
            for element_data in json['elements']:
                condition_data = element_data.pop('conditions')
                element = ssv.add_element(element_data.pop('type'),
                                          element_data.pop('ids'),
                                          element_data.pop('description'),
                                          **element_data)
                for condition in condition_data:
                    element.add_condition(condition.pop('type'), **condition)

        except KeyError as e:
            raise KeyError("Missing attribute from json input: ", e)
        except ET.ParseError as e:
            raise ET.ParseError('Invalid SVG string: ', e)

        return ssv
Example #26
0
 def parse_data(data):
     """Parse the XML data which is a flat non-hierarchical record and return 
     a two-level dictionary hierarchy where each key is the field group and 
     associated with that, a dictionary with the fields and values associated 
     with that group."""
     import re
     pkt_grp = dict()
     # If closing tag is truncated or has excess junk like null bytes, 
     # fix before parsing.
     if data.startswith('<oriondata') and not data.endswith('</oriondata>'):
         data = re.sub('</ori(ondata)?.*$','</oriondata>', data)
         logerr("parse_data(): attempted to correct truncated data: %s" % data[-19:-1])
     try:
         elements = ElementTree.fromstring(data)
         if elements.tag == 'oriondata' and elements[0].tag == 'meas':
             for child in elements:
                 name = child.attrib['name']
                 # Ensure the correct tag and attribute is one to be recorded
                 if child.tag == 'meas' and name in ColumbiaMicroServerStation.XML_INPUT_ELEMENTS:
                     # Get the associated packet group for this field
                     pkt_type = ColumbiaMicroServerStation.XML_INPUT_ELEMENTS[name]
                     # Initialize a new dictionary for a packet group
                     if not pkt_type in pkt_grp:
                         pkt_grp[pkt_type] = dict()
                     # If the field is in the list to use for the unit type,
                     # save the unit type as the field 'base_units'.
                     if name in ColumbiaMicroServerStation.XML_INPUT_UNIT_ELEMENTS:
                         # If the packet type is 'generic' then it's a unit type that's
                         # neither US or metric such as degrees for wind direction.
                         if pkt_type == 'generic':
                             pkt_grp[pkt_type]['base_units'] = 'generic'
                         else:  
                             pkt_grp[pkt_type]['base_units'] = child.attrib['unit']
                     # Store the field and value in the dictionary for the associated packet type.
                     pkt_grp[pkt_type][name] = float(child.text)
         else:
             raise ElementTree.ParseError("invalid XML file. Missing <oriondata> and/or <meas/> tags detected.")
     except ElementTree.ParseError as e:
         logerr("ElementTree ParseError: %s for data: %s" % (e, data))
         raise weewx.WeeWxIOError(e)
     return pkt_grp
Example #27
0
 def xmlToDF(self, root: ET.Element, tags_to_traverse: list, tags_to_avoid: list=list(), tags_to_skip: list=list(),
             specific_tags: list=list(), skip_all_child_nodes: bool=False):
     self.tags_to_avoid = tags_to_avoid
     self.tags_to_skip = tags_to_skip
     self.specific_tags = specific_tags
     self.skip_all_child_nodes = skip_all_child_nodes
     try:
         node_list = list()
         nodes = self.findNodesByTag(root, tags_to_traverse)
         if not nodes is None:
             for node in nodes:
                 node_list.append(self.convertNodeToList(node, vals=OrderedDict([])))
             node_list = list(itertools.chain(*node_list))
             # print('Node Results: ')
             # pprint.pprint(nodeList[0], indent=3)
             #print("NodeList: ", nodeList)
             return pd.DataFrame.from_records(node_list)
         else:
             raise ET.ParseError("Darn! No nodes found matching the search criteria!")
     except IndexError as e:
         return None
def test_clang_format_tool_plugin_scan_element_tree_parse_error(
        mock_fromstring):
    """
    Test what happens when an ElementTree.ParseError is raised (usually means clang-format hit an error).

    Expected result: issues is empty
    """
    mock_fromstring.side_effect = ElementTree.ParseError(1)
    cfp = ClangFormatXMLParser()
    files = []
    files.append(
        os.path.join(os.path.dirname(__file__), "valid_package", "indents.c"))
    output = "valid_package/indents.c\n\
<?xml version='1.0'?>\n\
<replacements xml:space='preserve' incomplete_format='false'>\n\
<replacement offset='12' length='1'>&#10;  </replacement>\n\
</replacements>"

    issues = cfp.parse_xml_output(output, files)

    assert not issues
Example #29
0
def parse_xml(file):

    data = None
    """
    root[0] - <tx>
                </tx>
    """
    try:
        xmlparse = Xet.parse(file)
        root = xmlparse.getroot()
        data = {
            'id_type': root[0][0].find('type').text,
            'id_value': root[0][0].find('value').text,
            'name': root[0][1].text,
            'lastname': root[0][2].text
        }
    except:
        raise Xet.ParseError('Failed to parse xml!')

    if not validate_integer(data['id_value']):
        raise ValueError('Id value is not integer and cannot be none!')

    return data
Example #30
0
    def _parse_xml(self, xml: str) -> None:

        try:
            it = ET.iterparse(StringIO(xml))
            for _, el in it:
                _, _, el.tag = el.tag.rpartition("}")
            root: ET.Element = it.root  # type: ignore
        except Exception as e:
            raise ET.ParseError(f"Could not parse XML. {e}")

        def parse_tileset(element: ET.Element) -> TileSet:
            href = find_attr(element, "href")
            units_per_pixel = float(find_attr(element, "units-per-pixel"))
            order = int(find_attr(element, "order"))
            zoom_level = int(href.rsplit("/", maxsplit=1)[-1])
            return TileSet(href=href,
                           units_per_pixel=units_per_pixel,
                           order=order,
                           zoom_level=zoom_level)

        def parse_tileformat(element: ET.Element) -> TileFormat:
            width = int(find_attr(element, "width"))
            height = int(find_attr(element, "height"))
            mime_type = find_attr(element, "mime-type")
            extension = find_attr(element, "extension")
            return TileFormat(width=width,
                              height=height,
                              mime_type=mime_type,
                              extension=extension)

        if root.tag == "TileMap":
            tilemap_element = root
        else:
            tilemap_element = root.find("./TileMap")

        if tilemap_element is not None:

            title = find_text(tilemap_element, "Title")
            abstract = find_text_optional(tilemap_element, "Abstract")
            crs = find_text(tilemap_element, "SRS").upper()
            # Normalize EPSG:3785 alias to EPSG:3785
            if crs in epsg_3857_alias:
                crs = "EPSG:3785"

            tileformat_element = tilemap_element.find("TileFormat")
            if tileformat_element is None:
                raise RuntimeError("No TileFormat found.")
            tile_format = parse_tileformat(tileformat_element)

            bbox = None
            bbox84 = None
            boundingbox_element = tilemap_element.find("BoundingBox")
            if boundingbox_element is not None:
                bbox = BoundingBox(
                    west=float(find_attr(boundingbox_element, "minx")),
                    east=float(find_attr(boundingbox_element, "maxx")),
                    south=float(find_attr(boundingbox_element, "minx")),
                    north=float(find_attr(boundingbox_element, "maxy")),
                )

                # Calc bounding box in EPSG:4326
                if crs == "EPSG:4326":
                    bbox84 = bbox
                elif crs in valid_epsgs:
                    crs_from = CRS.from_string(crs)
                    crs_to = CRS.from_string("EPSG:4326")
                    transformer = get_transformer(crs_from, crs_to)
                    tl = transformer.transform(bbox.west, bbox.north)
                    br = transformer.transform(bbox.east, bbox.south)
                    bbox84 = BoundingBox(west=tl[0],
                                         east=br[0],
                                         south=br[1],
                                         north=tl[1])

            tilesets = [
                parse_tileset(tileset_element)
                for tileset_element in tilemap_element.findall(".//TileSet")
            ]
            tilemap = TileMap(
                title=title,
                abstract=abstract,
                crs=crs,
                bbox=bbox,
                bbox84=bbox84,
                tile_format=tile_format,
                tilesets=tilesets,
            )
            self.tile_map = tilemap