Beispiel #1
0
    def _parse_error_response(self, resp):
        '''Handle error response from server

        Try to raise most-specific exception.
        '''

        # Note that even though the final server backend may guarantee to always
        # deliver an XML document body with a detailed error message, we may
        # also get errors from intermediate proxies.
        content_type = resp.headers['Content-Type']

        # If method == HEAD, server must not return response body
        # even in case of errors
        if resp.method.upper() == 'HEAD':
            assert self.conn.read(1) == b''
            raise HTTPError(resp.status, resp.reason, resp.headers)

        # If not XML, do the best we can
        if not XML_CONTENT_RE.match(content_type) or resp.length == 0:
            self.conn.discard()
            raise HTTPError(resp.status, resp.reason, resp.headers)

        # We don't stream the data into the parser because we want
        # to be able to dump a copy if the parsing fails.
        body = self.conn.readall()
        try:
            tree = ElementTree.parse(BytesIO(body)).getroot()
        except:
            log.error('Unable to parse server response as XML:\n%s',
                      self._dump_response(resp, body))
            raise

        raise get_S3Error(tree.findtext('Code'), tree.findtext('Message'), resp.headers)
Beispiel #2
0
    def _parse_xml_response(self, resp, body=None):
        '''Return element tree for XML response'''

        content_type = resp.headers['Content-Type']

        # AWS S3 sometimes "forgets" to send a Content-Type
        # when responding to a multiple delete request.
        # https://forums.aws.amazon.com/thread.jspa?threadID=134372
        if content_type is None:
            log.error('Server did not provide Content-Type, assuming XML')
        elif not XML_CONTENT_RE.match(content_type):
            log.error('Unexpected server reply: expected XML, got:\n%s',
                      self._dump_response(resp))
            raise RuntimeError('Unexpected server response')

        # We don't stream the data into the parser because we want
        # to be able to dump a copy if the parsing fails.
        if body is None:
            body = self.conn.readall()
        try:
            tree = ElementTree.parse(BytesIO(body)).getroot()
        except:
            log.error('Unable to parse server response as XML:\n%s',
                      self._dump_response(resp, body))
            raise

        return tree
Beispiel #3
0
Datei: s3c.py Projekt: mkhon/s3ql
    def _parse_error_response(self, resp):
        '''Handle error response from server

        Try to raise most-specific exception.
        '''

        # Note that even though the final server backend may guarantee to always
        # deliver an XML document body with a detailed error message, we may
        # also get errors from intermediate proxies.
        content_type = resp.headers['Content-Type']

        # If method == HEAD, server must not return response body
        # even in case of errors
        if resp.method.upper() == 'HEAD':
            assert self.conn.read(1) == b''
            raise HTTPError(resp.status, resp.reason, resp.headers)

        # If not XML, do the best we can
        if not XML_CONTENT_RE.match(content_type) or resp.length == 0:
            self.conn.discard()
            raise HTTPError(resp.status, resp.reason, resp.headers)

        # We don't stream the data into the parser because we want
        # to be able to dump a copy if the parsing fails.
        body = self.conn.readall()
        try:
            tree = ElementTree.parse(BytesIO(body)).getroot()
        except:
            log.error('Unable to parse server response as XML:\n%s',
                      self._dump_response(resp, body))
            raise

        raise get_S3Error(tree.findtext('Code'), tree.findtext('Message'),
                          resp.headers)
def storeTree(item):
    print(f'{item.name} start: {datetime.now()}')
    actXSD = xmlschema.XMLSchema(
        '/Users/john/Development/HumAI_data/Schema/iati-activities-schema.xsd')
    orgXSD = xmlschema.XMLSchema(
        '/Users/john/Development/HumAI_data/Schema/iati-organisations-schema.xsd'
    )

    db = firestore.Client()

    try:
        tree = ET.parse(item)
    except xml.etree.ElementTree.ParseError as exp:
        parse_error += 1
        print('Parse error:', exp, file=sys.stderr)
    else:
        tree_ = bf.data(tree.getroot())
        if "iati-activities" in tree_:
            print('Prune activity ', item.name)
    #        pruneTree(db, None, tree_, actXSD)
        elif "iati-organisations" in tree_:
            print('Prune organisation ', item.name)
    #        pruneTree(db, None, tree_, orgXSD)
        else:
            pass
    print(f'{item.name} end: {datetime.now()}')
def readXSD(fileName):
    try:
        xsdTree = ET.parse(fileName)
    except Exception as err:
        print('Exception parsing XSD: ', err)
        exit(0)
    return (xsdTree)
Beispiel #6
0
Datei: s3c.py Projekt: mkhon/s3ql
    def _parse_xml_response(self, resp, body=None):
        '''Return element tree for XML response'''

        content_type = resp.headers['Content-Type']

        # AWS S3 sometimes "forgets" to send a Content-Type
        # when responding to a multiple delete request.
        # https://forums.aws.amazon.com/thread.jspa?threadID=134372
        if content_type is None:
            log.error('Server did not provide Content-Type, assuming XML')
        elif not XML_CONTENT_RE.match(content_type):
            log.error('Unexpected server reply: expected XML, got:\n%s',
                      self._dump_response(resp))
            raise RuntimeError('Unexpected server response')

        # We don't stream the data into the parser because we want
        # to be able to dump a copy if the parsing fails.
        if body is None:
            body = self.conn.readall()
        try:
            tree = ElementTree.parse(BytesIO(body)).getroot()
        except:
            log.error('Unable to parse server response as XML:\n%s',
                      self._dump_response(resp, body))
            raise

        return tree
Beispiel #7
0
    def pull_item(self, index):
        """
        Returns image at index in torch tensor form (RGB),
        corresponding normalized annotation in 2d array [[xmin, ymin, xmax, ymax, label_ind],
                                                         ... ],
        height and width of image
        """
        img_id = self.ids[index]

        target = ET.parse(self._annopath % img_id).getroot()
        img = cv2.imread(self._imgpath % img_id)

        height, width, _ = img.shape

        if self.target_transform is not None:
            target = self.target_transform(target, width, height)

        if self.transform is not None:
            boxes = np.asarray([x['bbox'] for x in target])
            labels = np.asarray([x['label_idx'] for x in target])
            img, boxes, labels = self.transform(img, boxes, labels)
            if self.rgb:
                img = img[:, :, (2, 1, 0)]
            target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
        return torch.from_numpy(img).permute(2, 0, 1), target, height, width
    def concatRead(self):
        match, not_match, parse_error, count = 0, 0, 0, 0
        tag_dict = {}
        if self.tag == None:
            root = oET.Element("root")
        else:
            root = oET.Element(self.tag)
        for item in self.sourcePath.rglob('*.xml'):
            if item.is_file():
                count += 1
                try:
                    loadTree = ET.parse(item)
                except xml.etree.ElementTree.ParseError as exp:
                    parse_error += 1
                    print('Parse error:', exp, file=sys.stderr)
                else:
                    graft = loadTree.getroot()
                    if graft.tag in tag_dict:
                        tag_dict[graft.tag] = tag_dict[graft.tag] + 1
                    else:
                        tag_dict[graft.tag] = 1
#                        'iati-organisations' or 'iati-activities'
                    if self.filter == None:
                        root.append(graft)
                        match += 1
                    else:
                        if self.filter == graft.tag:
                            root.append(graft)
                            match += 1
                        else:
                            not_match += 1
        return (oET.ElementTree(root))
Beispiel #9
0
 def is_valid(self):
     """ Check if XML is valid with XSD file """
     if self.xsd_path is not None:
         xsd_file = XMLSchema(self.xsd_path)
         xml_file = ElementTree.parse(self.get_xml_path())
         return xsd_file.is_valid(xml_file)
     return False
Beispiel #10
0
    def loadSettings(self):
        if os.path.isfile(get_config_path("MyTerm.xml")):
            with open(get_config_path("MyTerm.xml"), 'r') as f:
                tree = safeET.parse(f)

            port = tree.findtext('GUISettings/PortConfig/port', default='')
            if port != '':
                self.cmbPort.setCurrentText(port)

            baudrate = tree.findtext('GUISettings/PortConfig/baudrate', default='38400')
            if baudrate != '':
                self.cmbBaudRate.setCurrentText(baudrate)

            databits = tree.findtext('GUISettings/PortConfig/databits', default='8')
            id = self.cmbDataBits.findText(databits)
            if id >= 0:
                self.cmbDataBits.setCurrentIndex(id)

            parity = tree.findtext('GUISettings/PortConfig/parity', default='None')
            id = self.cmbParity.findText(parity)
            if id >= 0:
                self.cmbParity.setCurrentIndex(id)

            stopbits = tree.findtext('GUISettings/PortConfig/stopbits', default='1')
            id = self.cmbStopBits.findText(stopbits)
            if id >= 0:
                self.cmbStopBits.setCurrentIndex(id)

            rtscts = tree.findtext('GUISettings/PortConfig/rtscts', default='off')
            if 'on' == rtscts:
                self.chkRTSCTS.setChecked(True)
            else:
                self.chkRTSCTS.setChecked(False)

            xonxoff = tree.findtext('GUISettings/PortConfig/xonxoff', default='off')
            if 'on' == xonxoff:
                self.chkXonXoff.setChecked(True)
            else:
                self.chkXonXoff.setChecked(False)

            LocalEcho = tree.findtext('GUISettings/View/LocalEcho', default='off')
            if 'on' == LocalEcho:
                self.actionLocal_Echo.setChecked(True)
                self._localEcho = True
            else:
                self.actionLocal_Echo.setChecked(False)
                self._localEcho = False

            ReceiveView = tree.findtext('GUISettings/View/ReceiveView', default='HEX(UPPERCASE)')
            if 'Ascii' in ReceiveView:
                self.actionAscii.setChecked(True)
                self._viewMode = VIEWMODE_ASCII
            elif 'lowercase' in ReceiveView:
                self.actionHex_lowercase.setChecked(True)
                self._viewMode = VIEWMODE_HEX_LOWERCASE
            elif 'UPPERCASE' in ReceiveView:
                self.actionHEX_UPPERCASE.setChecked(True)
                self._viewMode = VIEWMODE_HEX_UPPERCASE
            self.receiver_thread.setViewMode(self._viewMode)
Beispiel #11
0
def get_ignored_layers(model_xml, num_layers=1):
    ir_tree = ET.parse(model_xml)
    ignored_layers = []
    all_supported = [l for l in ir_tree.iter("layer") if l.get("type") == ("Convolution", "FullyConnected")]
    if num_layers > 0:
        ignored_layers += [layer.get("name") for layer in all_supported[num_layers:]]
    all_bns = [l for l in ir_tree.iter("layer") if l.get("type") == "ScaleShift"]
    ignored_layers += [bn.get("name") for bn in all_bns]
    return ignored_layers
Beispiel #12
0
    def __init__(self, sourceFile, numTubes):
        self.sourceFile = ET.parse(sourceFile)
        self.root = self.sourceFile.getroot()

        self.numTubes = numTubes

        self.nameList = None
        self.valList = None

        self._parseXML()
Beispiel #13
0
    def LoadSettings(self):
        if os.path.isfile(get_config_path("settings.xml")):
            with open(get_config_path("settings.xml"), 'r') as f:
                tree = safeET.parse(f)

            port = tree.findtext('GUISettings/PortConfig/port', default='')
            if port != '':
                self.cmbPort.setCurrentText(port)

            baudrate = tree.findtext('GUISettings/PortConfig/baudrate', default='38400')
            if baudrate != '':
                self.cmbBaudRate.setCurrentText(baudrate)

            databits = tree.findtext('GUISettings/PortConfig/databits', default='8')
            id = self.cmbDataBits.findText(databits)
            if id >= 0:
                self.cmbDataBits.setCurrentIndex(id)

            parity = tree.findtext('GUISettings/PortConfig/parity', default='None')
            id = self.cmbParity.findText(parity)
            if id >= 0:
                self.cmbParity.setCurrentIndex(id)

            stopbits = tree.findtext('GUISettings/PortConfig/stopbits', default='1')
            id = self.cmbStopBits.findText(stopbits)
            if id >= 0:
                self.cmbStopBits.setCurrentIndex(id)

            rtscts = tree.findtext('GUISettings/PortConfig/rtscts', default='off')
            if 'on' == rtscts:
                self.chkRTSCTS.setChecked(True)
            else:
                self.chkRTSCTS.setChecked(False)

            xonxoff = tree.findtext('GUISettings/PortConfig/xonxoff', default='off')
            if 'on' == xonxoff:
                self.chkXonXoff.setChecked(True)
            else:
                self.chkXonXoff.setChecked(False)

            LocalEcho = tree.findtext('GUISettings/View/LocalEcho', default='off')
            if 'on' == LocalEcho:
                self.actionLocal_Echo.setChecked(True)
                self._localEcho = True
            else:
                self.actionLocal_Echo.setChecked(False)
                self._localEcho = False

            ReceiveView = tree.findtext('GUISettings/View/ReceiveView', default='HEX(UPPERCASE)')
            if 'Ascii' in ReceiveView:
                self.actionAscii.setChecked(True)
            elif 'lowercase' in ReceiveView:
                self.actionHex_lowercase.setChecked(True)
            elif 'UPPERCASE' in ReceiveView:
                self.actionHEX_UPPERCASE.setChecked(True)
Beispiel #14
0
def get_coverage_from_cobertura_xml(filepath):
    """
    Reads just the coverage percentage from the Cobertura Coverage XML file.
    """
    doc = ET.parse(filepath,
                   forbid_dtd=True,
                   forbid_entities=True,
                   forbid_external=True)
    for coverage in doc.iter("coverage"):
        coverage_percent = 100.0 * float(coverage.get("line-rate", "0"))
        return coverage_percent
    raise Exception("Failed to process Cobertura Coverage XML")
Beispiel #15
0
def scan(location: ScanLocation, **mode) -> Generator[Detection, None, None]:
    """
    Attempt to parse the XML using defused XML and the mode options to turn protections on/off
    When an exception is raised for supported XML problems a detection is yielded back to the analyzer
    """
    try:
        cElementTree.parse(location.str_location, **mode)
    except EntitiesForbidden:
        yield Detection(detection_type="MalformedXML",
                        message="Malformed or malicious XML",
                        score=get_score_or_default("malformed-xml-entities",
                                                   100),
                        extra={"type": "entities"},
                        location=location.location,
                        signature=f"malformed_xml#entities#{str(location)}",
                        tags={"malformed_xml", "xml_entities"})
    except DTDForbidden:
        yield Detection(detection_type="MalformedXML",
                        message="Malformed or malicious XML",
                        score=get_score_or_default("malformed-xml-dtd", 20),
                        extra={"type": "dtd"},
                        location=location.location,
                        signature=f"malformed_xml#dtd#{str(location)}",
                        tags={"malformed_xml", "xml_dtd"})
    except ExternalReferenceForbidden:
        yield Detection(
            detection_type="MalformedXML",
            message="Malformed or malicious XML",
            score=get_score_or_default("malformed-xml-external-reference",
                                       100),
            extra={"type": "external_reference"},
            location=location.location,
            signature=f"malformed_xml#external_reference#{str(location)}",
            tags={"malformed_xml", "xml_external_reference"})
    except NotSupportedError:
        pass
    except ParseError:
        pass
    except Exception:
        pass
    def _RetrievePlacemarks(self, xml_data):
        """Retrieve placemarks from xml data.

    Args:
      xml_data: Query results from the Google Places database.
    Returns:
      xmlstr: XML with placemarks.
      total_placemarks: Total no. of placemarks.
    """

        xmlstr = ""
        total_results = 0
        # Perform XML parsing using cElementTree.
        root = ET.parse(xml_data).getroot()

        for element in root:
            if element.tag == "result":
                # Rename "result" tags as "Placemark" as per KML(XML) requirements.
                element.tag = "Placemark"

                for subelement in element[:]:
                    # For the sake of simplicity, we presently look for
                    # "name", "geometry", "icon", "vicinity" tags only in the
                    # response and ignore the others.
                    if subelement.tag not in self._reqd_tags:
                        element.remove(subelement)
                        continue

                    if subelement.tag == "geometry":
                        # Extract latitude and longitude coordinates.
                        lat = subelement.find("location").find("lat").text
                        lng = subelement.find("location").find("lng").text

                        # Add "Point" and "coordinates" tags to element.
                        point = ET.SubElement(element, "Point")
                        coords = ET.SubElement(point, "coordinates")
                        coords.text = "%s, %s" % (lng, lat)
                        element.remove(subelement)

                    # Rename "vicinity" and "icon" tags to
                    # "snippet" and "description" as per naming convention
                    # being followed in existing Search Services.
                    elif subelement.tag == "vicinity":
                        subelement.tag = "snippet"
                    elif subelement.tag == "icon":
                        subelement.tag = "description"

                xmlstr += ET.tostring(element, method="xml")
                total_results += 1

        return (xmlstr, total_results)
Beispiel #17
0
def _xml_file_import(filename):
    ''' Parse and import a single XML file '''
    logging.info("Importing %s ... " % filename)
    try:
        tree = ET.parse(filename)
        xml_root = tree.getroot()
        levels = get_child_by_tag(xml_root, "gamelevels")
        create_levels(levels)
        corporations = get_child_by_tag(xml_root, "corporations")
        create_corps(corporations)
        logging.info("Imported %s successfully" % filename)
        return True
    except:
        logging.exception("Exception raised while parsing %s" % filename)
        return False
Beispiel #18
0
    def pull_anno(self, index):
        """Returns the original annotation of image at index

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.


        Args:
            index (int): index of img to get annotation of
        Returns:
            list:  img_id, [[bbox coords, label_idx],...]]
                eg: ['001718', [[xmin, ymin, xmax, ymax, label_ind], ... ]]
        """
        img_name = self.ids[index]
        anno = ET.parse(self._annopath % img_name).getroot()
        gt = self.target_transform(anno, 1, 1)
        return img_name[1], gt
Beispiel #19
0
def _xml_file_import(filename):
    ''' Parse and import a single XML file '''
    logging.debug("Processing: %s" % filename)
    try:
        tree = ET.parse(filename)
        xml_root = tree.getroot()
        levels = get_child_by_tag(xml_root, "gamelevels")
        create_levels(levels)
        corporations = get_child_by_tag(xml_root, "corporations")
        create_corps(corporations)
        logging.debug("Done processing: %s" % filename)
        dbsession.commit()
        return True
    except:
        dbsession.rollback()
        logging.exception("Exception raised while parsing %s, rolling back changes" % filename)
        return False
Beispiel #20
0
    def load_conf_file(filename, tablistwidget):
        tree = ETRead.parse(filename)
        filterTree = tablistwidget.filterTree
        columnList = tablistwidget.columnList
        root = tree.getroot()

        # POPULATING FILTERS
        for filter_tag in (filters for filters in root
                           if filters.tag == "filter"):
            for item in filter_tag.findall('filter_item'):
                column = item.find("column").text
                show = item.find("mode").text
                if show.lower() == "show":
                    show = True
                else:
                    show = False
                filterstring = item.find("filter").text
                # HOTFIX FOR EMPTY STRINGS
                if filterstring is None:
                    filterstring = ""
                strict = item.find("strict").text
                if strict.lower() == "true":
                    strict = True
                else:
                    strict = False

                filter_detail = FilterDetails(column, strict, show,
                                              filterstring)
                tree_item = TabWidget.createtreeitem(filter_detail)
                filterTree.addTopLevelItem(tree_item)
                tablistwidget.control.addfilter(filter_detail)

        # POPULATING COLUMNS TO DELETE
        for column in (columns for columns in root
                       if columns.tag == "columndelete"):
            for column_item in column:
                column = column_item.get('name')
                # CREATING NEW LIST ITEM
                new_item = QtGui.QListWidgetItem()
                new_item.setText(column)
                # ADDING IT TO THE LIST
                columnList.addItem(new_item)
                main_window.tabList.currentWidget().control.removecolumn(
                    CellDetail(column))

        return
Beispiel #21
0
    def from_file(self, filename="vasprun.xml"):
        """
        Parsing vasprun.xml from the working directory

        Args:
            filename (str): Path to the vasprun file
        """
        if not (os.path.isfile(filename)):
            raise AssertionError()
        try:
            self.root = ETree.parse(filename).getroot()
        except ParseError:
            raise VasprunError(
                "The vasprun.xml file is either corrupted or the simulation has failed"
            )

        self.parse_root_to_dict()
    def concat(self):
        match, not_match, parse_error, count = 0, 0, 0, 0
        tag_dict = {}
        if self.tag == None:
            root = oET.Element("root")
        else:
            root = oET.Element(self.tag)
        for item in self.sourcePath.rglob('*.xml'):
            if item.is_file():
                count += 1
                try:
                    loadTree = ET.parse(item)
                except xml.etree.ElementTree.ParseError as exp:
                    parse_error += 1
                    print('Parse error:', exp)
                else:
                    graft = loadTree.getroot()
                    if graft.tag in tag_dict:
                        tag_dict[graft.tag] = tag_dict[graft.tag] + 1
                    else:
                        tag_dict[graft.tag] = 1


#                        'iati-organisations' or 'iati-activities'
                    if self.filter == None:
                        root.append(graft)
                        match += 1
                    else:
                        if self.filter == graft.tag:
                            root.append(graft)
                            match += 1
                        else:
                            not_match += 1
        tree = oET.ElementTree(root)
        if self.outFileName == None:
            tree.write(sys.stdout, encoding='unicode', xml_declaration=True)
        else:
            tree.write(self.outFileName,
                       encoding='UTF-8',
                       xml_declaration=True)
            print(
                f'Match = {match} Not match {not_match} Parse failed {parse_error} Total {count}'
            )
            for x in tag_dict:
                print('{:-6}     | {}'.format(tag_dict[x], x))
Beispiel #23
0
def find_all_parameters(buffer, model_xml):
    ir_tree = ET.parse(model_xml)
    all_parameters = OrderedDict()
    for layer in ir_tree.iter("layer"):
        if layer.get("type") not in {
                "Convolution", "FullyConnected", "ScaleShift"
        }:
            continue

        get_weight_shape_fn = get_conv_weight_shape
        if layer.get("type") == "FullyConnected":
            get_weight_shape_fn = get_fc_weight_shape
        if layer.get("type") == "ScaleShift":
            get_weight_shape_fn = get_ss_weight_shape

        extract_params(buffer, all_parameters, layer, get_weight_shape_fn)

    return all_parameters
Beispiel #24
0
def process_junit_xml(filepath):
    """
    Converts a JUnit XML file into a Markdown Report.
    """
    doc = ET.parse(filepath,
                   forbid_dtd=True,
                   forbid_entities=True,
                   forbid_external=True)
    for suite in doc.iter("testsuite"):
        cases = _collect_cases(suite)
        errors = int(suite.attrib.get("errors", "0"))
        failures = int(suite.attrib.get("failures", "0"))
        skips = int(suite.attrib.get("skips", "0"))
        collected = int(suite.attrib.get("tests", "0"))
        timetxt = "{0} seconds".format(suite.attrib.get("time", ""))
        suiteobj = TestSuite(cases, errors, failures, skips, collected,
                             timetxt)
        return _process_junit_cases(suiteobj)
    raise Exception("Failed to process JUnit XML")
Beispiel #25
0
    def load_conf_file(filename, tablistwidget):
        tree = ETRead.parse(filename)
        filterTree = tablistwidget.filterTree
        columnList = tablistwidget.columnList
        root = tree.getroot()

        # POPULATING FILTERS
        for filter_tag in (filters for filters in root if filters.tag == "filter"):
            for item in filter_tag.findall('filter_item'):
                column = item.find("column").text
                show = item.find("mode").text
                if show.lower() == "show":
                    show = True
                else:
                    show = False
                filterstring = item.find("filter").text
                # HOTFIX FOR EMPTY STRINGS
                if filterstring is None:
                    filterstring = ""
                strict = item.find("strict").text
                if strict.lower() == "true":
                    strict = True
                else:
                    strict = False

                filter_detail = FilterDetails(column, strict, show, filterstring)
                tree_item = TabWidget.createtreeitem(filter_detail)
                filterTree.addTopLevelItem(tree_item)
                tablistwidget.control.addfilter(filter_detail)


        # POPULATING COLUMNS TO DELETE
        for column in (columns for columns in root if columns.tag == "columndelete"):
            for column_item in column:
                column = column_item.get('name')
                # CREATING NEW LIST ITEM
                new_item = QtGui.QListWidgetItem()
                new_item.setText(column)
                # ADDING IT TO THE LIST
                columnList.addItem(new_item)
                main_window.tabList.currentWidget().control.removecolumn(CellDetail(column))

        return
Beispiel #26
0
def _xml_file_import(filename):
    ''' Parse and import a single XML file '''
    logging.debug("Processing: %s" % filename)
    try:
        tree = ET.parse(filename)
        xml_root = tree.getroot()
        levels = get_child_by_tag(xml_root, "gamelevels")
        create_levels(levels)
        corporations = get_child_by_tag(xml_root, "corporations")
        create_corps(corporations)
        logging.debug("Done processing: %s" % filename)
        dbsession.commit()
        return True
    except:
        dbsession.rollback()
        logging.exception(
            "Exception raised while parsing %s, rolling back changes" %
            filename)
        return False
 def genTree(self):
     match, not_match, parse_error, count = 0, 0, 0, 0
     for item in self.sourcePath.rglob('*.xml'):
         if item.is_file():
             count += 1
             try:
                 loadTree = ET.parse(item)
             except xml.etree.ElementTree.ParseError as exp:
                 parse_error += 1
                 print('Parse error:', exp, file=sys.stderr)
             else:
                 treeRoot = loadTree.getroot()
                 if self.filter == None:
                     match += 1
                     yield (loadTree)
                 else:
                     if self.filter == treeRoot.tag:
                         match += 1
                         yield (loadTree)
                     else:
                         not_match += 1
Beispiel #28
0
def process_cobertura_xml(filepath, minimum_coverage=None):
    """
    Converts a Cobertura Coverage XML file into a Markdown Report.
    """
    doc = ET.parse(filepath,
                   forbid_dtd=True,
                   forbid_entities=True,
                   forbid_external=True)
    result = []
    for coverage in doc.iter("coverage"):
        coverage_percent = 100.0 * float(coverage.get("line-rate", "0"))
        lines_covered = coverage.get("lines-covered", "0")
        lines_valid = coverage.get("lines-valid", "0")
        coverage_minimum_statement = ""
        if minimum_coverage is not None:
            coverage_minimum_statement = get_coverage_minimum_statement(
                coverage_percent, minimum_coverage)
        result.append("Coverage is %.2f%%%s (%s lines of %s total)." %
                      (coverage_percent, coverage_minimum_statement,
                       lines_covered, lines_valid))
        return "\n\n".join(result)
    raise Exception("Failed to process Cobertura Coverage XML")
Beispiel #29
0
 def __enter__(self):
     try:
         self.tree = defused_etree.parse(self.path)
     except IOError:
         # Document is blank or missing
         if self.force_create is False:
             LOG.debug('%s does not seem to exist; not creating', self.path)
             # This will abort __enter__
             self.__exit__(IOError('File not found'), None, None)
         # Create topmost xml entry
         self.tree = etree.ElementTree(etree.Element(self.top_element))
         self.write_xml = True
     except ParseError:
         LOG.error('Error parsing %s', self.path)
         # "Kodi cannot parse {0}. PKC will not function correctly. Please
         # visit {1} and correct your file!"
         messageDialog(lang(29999), lang(39716).format(
             self.filename,
             'http://kodi.wiki'))
         self.__exit__(ParseError('Error parsing XML'), None, None)
     self.root = self.tree.getroot()
     return self
    def report(self):
        match, not_match, count = 0, 0, 0
        start_set = set([])
        start_dict = {}
        for item in self.sourcePath.rglob('*.xml'):
            with open(str(item), 'rb') as f:
                b = f.read(16)
                start_set.add(b)
                if b in start_dict:
                    start_dict[b]['count'] = start_dict[b]['count'] + 1
                else:
                    start_dict[b] = {'count': 1, 'roots': {}}
            try:
                tree = ET.parse(item)
                root = tree.getroot()
                if root.tag in start_dict[b]['roots']:
                    start_dict[b]['roots'][
                        root.tag] = start_dict[b]['roots'][root.tag] + 1
                else:
                    start_dict[b]['roots'][root.tag] = 1
                match += 1
            except xml.etree.ElementTree.ParseError as exp:
                print('Parse error:', exp)
                not_match += 1
            except Exception as exp:
                print('Hit Except clause', type(exp))
                raise

        print('File count | File starts')
        for x in start_dict:
            print('{:-6}     | {}  Roots: {}'.format(
                start_dict[x]['count'], x, len(start_dict[x]['roots'])))
            for y in start_dict[x]['roots']:
                print('------ {:-6}  {} '.format(start_dict[x]['roots'][y], y))

        print(f'\nGood {match}, Bad {not_match}, Total {match + not_match}')
 def list(self):
     count = 0
     self.listLine('', '', False, '', '', header=True)
     for item in self.sourcePath.rglob('*.xml'):
         if item.is_file():
             parse_error = False
             count += 1
             #                if count > 20:
             #                    break
             try:
                 loadTree = ET.parse(item)
             except xml.etree.ElementTree.ParseError as exp:
                 parse_error = True
                 self.listLine(item.parent, item.name, False, '', '')
             else:
                 graft = loadTree.getroot()
                 #                        'iati-organisations' or 'iati-activities'
                 if self.filter == None or self.filter == graft.tag:
                     if 'generated-datetime' in graft.attrib:
                         dateTime = graft.attrib['generated-datetime']
                     else:
                         dateTime = ''
                     self.listLine(item.parent, item.name, True, graft.tag,
                                   dateTime)
Beispiel #32
0
import xml.etree.cElementTree as badET
import defusedxml.cElementTree as goodET

xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>"

# unsafe
tree = badET.fromstring(xmlString)
print(tree)
badET.parse('filethatdoesntexist.xml')
badET.iterparse('filethatdoesntexist.xml')
a = badET.XMLParser()

# safe
tree = goodET.fromstring(xmlString)
print(tree)
goodET.parse('filethatdoesntexist.xml')
goodET.iterparse('filethatdoesntexist.xml')
a = goodET.XMLParser()
# -*- coding: utf-8 -*-
import xml.etree.cElementTree as badET
import defusedxml.cElementTree as goodET

xmlString = "<note>\n<to>Tove</to>\n<from>Jani</from>\n<heading>Reminder</heading>\n<body>Don't forget me this weekend!</body>\n</note>"

# unsafe
tree = badET.fromstring(xmlString)
print(tree)
badET.parse("filethatdoesntexist.xml")
badET.iterparse("filethatdoesntexist.xml")
a = badET.XMLParser()

# safe
tree = goodET.fromstring(xmlString)
print(tree)
goodET.parse("filethatdoesntexist.xml")
goodET.iterparse("filethatdoesntexist.xml")
a = goodET.XMLParser()
Beispiel #34
0
def parse_xml_file(filename):
    return _parse_xml(ET.parse(filename))
Beispiel #35
0
 def __init__(self, filename):
     self.tree = ET.parse(filename)
     self.configuration = Configuration(self.tree.getroot())
Beispiel #36
0
def get_root_element(file):
    return cElementTree.parse(file).getroot()