Python defusedxml.ElementTree.ParseError() Examples

The following are 18 code examples of defusedxml.ElementTree.ParseError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module defusedxml.ElementTree , or try the search function .
Example #1
Source File: client.py    From jira with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def backup_progress(self):
        """Return status of cloud backup as a dict.

        Is there a way to get progress for Server version?
        """
        epoch_time = int(time.time() * 1000)
        if self.deploymentType == "Cloud":
            url = (
                self._options["server"] + "/rest/obm/1.0/getprogress?_=%i" % epoch_time
            )
        else:
            logging.warning("This functionality is not available in Server version")
            return None
        r = self._session.get(url, headers=self._options["headers"])
        # This is weird.  I used to get xml, but now I'm getting json
        try:
            return json.loads(r.text)
        except Exception:
            import defusedxml.ElementTree as etree

            progress = {}
            try:
                root = etree.fromstring(r.text)
            except etree.ParseError as pe:
                logging.warning(
                    "Unable to find backup info.  You probably need to initiate a new backup. %s"
                    % pe
                )
                return None
            for k in root.keys():
                progress[k] = root.get(k)
            return progress 
Example #2
Source File: vasprun.py    From pyiron with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #3
Source File: applekeychain.py    From pass-import with GNU General Public License v3.0 6 votes vote down vote up
def _decode_data(self, entry):
        """Decode data field (password or comments)."""
        key = entry.get('type', 'password')
        key = 'comments' if key == 'note' else key
        data = entry.pop('data', '')
        if isinstance(data, int):
            return key, ''

        data = self._decode(data)
        if key == 'comments':
            if data:
                try:
                    tree = ElementTree.XML(data)
                except ElementTree.ParseError:
                    return key, ''

                found = tree.find('.//string')
                if found is None:
                    return key, ''
                return key, found.text
            return key, ''
        return key, data 
Example #4
Source File: winevtx.py    From plaso with Apache License 2.0 5 votes vote down vote up
def _ParseRecords(self, parser_mediator, evtx_file):
    """Parses Windows XML EventLog (EVTX) records.

    Args:
      parser_mediator (ParserMediator): mediates interactions between parsers
          and other components, such as storage and dfvfs.
      evtx_file (pyevt.file): Windows XML EventLog (EVTX) file.
    """
    # To handle errors when parsing a Windows XML EventLog (EVTX) file in the
    # most granular way the following code iterates over every event record.
    # The call to evt_file.get_record() and access to members of evt_record
    # should be called within a try-except.

    for record_index in range(evtx_file.number_of_records):
      if parser_mediator.abort:
        break

      try:
        evtx_record = evtx_file.get_record(record_index)
        self._ParseRecord(parser_mediator, record_index, evtx_record)

      except (IOError, ElementTree.ParseError) as exception:
        parser_mediator.ProduceExtractionWarning(
            'unable to parse event record: {0:d} with error: {1!s}'.format(
                record_index, exception))

    for record_index in range(evtx_file.number_of_recovered_records):
      if parser_mediator.abort:
        break

      try:
        evtx_record = evtx_file.get_recovered_record(record_index)
        self._ParseRecord(
            parser_mediator, record_index, evtx_record, recovered=True)

      except IOError as exception:
        parser_mediator.ProduceExtractionWarning((
            'unable to parse recovered event record: {0:d} with error: '
            '{1!s}').format(record_index, exception)) 
Example #5
Source File: xml.py    From pass-import with GNU General Public License v3.0 5 votes vote down vote up
def is_format(self):
        """Return True if the file is an XML file."""
        try:
            self.dom = parse(self.file)
        except (ParseError, ExpatError, UnicodeDecodeError):
            return False
        return True 
Example #6
Source File: xml.py    From pass-import with GNU General Public License v3.0 5 votes vote down vote up
def is_format(self):
        """Return True if the file is an HTML file."""
        try:
            self.tree = ElementTree.XML(self.file.read())
            if self.tree.tag != 'html':
                return False
        except (ParseError, ExpatError):
            return False
        return True 
Example #7
Source File: utils.py    From atoma with MIT License 5 votes vote down vote up
def parse_xml(xml_content):
    try:
        return defused_xml_parse(xml_content)
    except ParseError:
        raise FeedXMLError('Not a valid XML document') 
Example #8
Source File: tdvt_core.py    From connector-plugin-sdk with MIT License 5 votes vote down vote up
def run_diff(test_config, diff):
    root_directory = get_root_dir()
    allowed_test_path = os.path.join(root_directory, diff)
    test_path_base = os.path.split(allowed_test_path)[0]
    test_name = os.path.split(allowed_test_path)[1]

    actual, actual_diff, setup, expected_files, next_path = get_test_file_paths(test_path_base, test_name,
                                                                                test_config.output_dir)

    logging.debug('actual_path: ' + actual)
    diff_count_map = {}

    for f in expected_files:
        logging.debug('expected_path: ' + f)
        if os.path.isfile(f) and os.path.isfile(actual):
            logging.debug("Diffing " + actual + " and " + f)
            actual_xml = None
            expected_xml = None
            try:
                actual_xml = parse(actual).getroot()
            except ParseError as e:
                logging.debug("Exception parsing actual file: " + actual + " exception: " + str(e))
                continue
            try:
                expected_xml = parse(f).getroot()
            except ParseError as e:
                logging.debug("Exception parsing expected file: " + f + " exception: " + str(e))
                continue

            result = TestResult(test_config=test_config)
            result.add_test_results(actual_xml, actual)
            expected_output = TestResult(test_config=test_config)
            expected_output.add_test_results(expected_xml, '')
            num_diffs, diff_string = result.diff_test_results(expected_output)
            logging.debug(diff_string)
            diff_count_map[f] = sum(num_diffs)

    for t in diff_count_map:
        logging.debug(t + ' Number of differences: ' + str(diff_count_map[t]))
    return 0 
Example #9
Source File: parse_xml.py    From android-malware-analysis with GNU General Public License v3.0 5 votes vote down vote up
def main():
    all_permissions = [] # list of strings naming each permission used in the dataset
    app_permission_map = {} # mapping from android app names to lists of permissions
    app_malicious_map = {} # mapping from android app names to 1 or 0 for malware or goodware
    root_dir = os.getcwd()
    for i, directory in enumerate(['benign_apk', 'malicious_apk']):
        os.chdir(directory)
        category_root_dir = os.getcwd()
        for filename in glob.glob('*.apk'):
            print('Processing ' + filename)
            try:
                os.chdir(filename[:-4])
                with open('AndroidManifest.xml') as xml_file:
                    et = ElementTree.parse(xml_file)
            except (ElementTree.ParseError, UnicodeDecodeError, FileNotFoundError):
                print('Parsing error encountered for ' + filename)
                os.chdir(category_root_dir)
                continue
            app_name = filename
            # make a one-hot bit vector of length 2. 1st bit set if malicious, otherwise 2nd bit
            app_malicious_map[app_name] = [1,0] if i else [0,1]
            permissions = et.getroot().findall('./uses-permission')
            app_permission_map[app_name] = []
            for permission in permissions:
                try:
                    permission_name = permission.attrib['{http://schemas.android.com/apk/res/android}name'].upper()
                    if not permission_name.startswith('ANDROID.PERMISSION'): continue # ignore custom permissions
                    if permission_name not in all_permissions: all_permissions.append(permission_name)
                    app_permission_map[app_name].append(permission_name)
                except KeyError:
                    pass
            os.chdir(os.pardir)
        os.chdir(root_dir)
    all_apps = {} # mapping combining app_permission_map and app_malicious_map using bits
    for app_name in app_permission_map:
        bit_vector = [1 if p in app_permission_map[app_name] else 0 for p in all_permissions]
        all_apps[app_name] = {'vector': bit_vector, 'malicious': app_malicious_map[app_name]}
    with open('app_permission_vectors.json', 'w') as outfile:
        json.dump({'features': all_permissions, 'apps': all_apps}, outfile)
    print('Wrote data on ' + str(len(all_permissions)) + ' permissions and ' + str(len(all_apps)) + ' apps to a file.') 
Example #10
Source File: scores.py    From sports.py with MIT License 5 votes vote down vote up
def _request_xml(sport):
    """
    Request XML data from scorespro.com

    :param sport: sport being played
    :type sport: string
    :return: XML data
    :rtype: string
    """
    url = 'http://www.scorespro.com/rss2/live-{}.xml'.format(sport)
    try:
        r = requests.get(url)
        return _load_xml(r.content)
    except ET.ParseError:
        raise errors.SportError(sport) 
Example #11
Source File: putservice.py    From PEBA with GNU General Public License v3.0 5 votes vote down vote up
def checkPostData(postrequest):
    """check if postdata is XML"""
    postdata = postrequest.decode('utf-8')
    try:
        return ETdefused.fromstring(postdata)
    except ETdefused.ParseError:
        app.logger.error('Invalid XML in post request')
        return False 
Example #12
Source File: xibcontext.py    From xiblint with Apache License 2.0 5 votes vote down vote up
def __init__(self, path):
        self.path = path
        self.success = True
        self.errors = []
        try:
            self.tree = parse_xml(path)
        except ElementTree.ParseError as ex:
            self.tree = None
            self.errors.append({
                'file': self.path,
                'line': ex.position[0],
                'error': ex.msg,
            })
        self.rule_name = None 
Example #13
Source File: __init__.py    From python-connect-box with MIT License 5 votes vote down vote up
def async_get_ipv6_filtering(self) -> None:
        """Get the current ipv6 filter (and filters time) rules."""
        if self.token is None:
            await self.async_initialize_token()

        self.ipv6_filters.clear()
        self._ipv6_filters_time = None
        raw = await self._async_ws_get_function(CMD_GET_IPV6_FILTER_RULE)

        try:
            xml_root = element_tree.fromstring(raw)
            for instance in xml_root.iter("instance"):
                self.ipv6_filters.append(
                    Ipv6FilterInstance(
                        int(instance.find("idd").text),
                        instance.find("src_addr").text,
                        int(instance.find("src_prefix").text),
                        instance.find("dst_addr").text,
                        int(instance.find("dst_prefix").text),
                        int(instance.find("src_sport").text),
                        int(instance.find("src_eport").text),
                        int(instance.find("dst_sport").text),
                        int(instance.find("dst_eport").text),
                        int(instance.find("protocol").text),
                        int(instance.find("allow").text),
                        int(instance.find("enabled").text),
                    )
                )

            self._ipv6_filters_time = FiltersTimeMode(
                int(xml_root.find("time_mode").text),
                _parse_general_time(xml_root),
                _parse_daily_time(xml_root)
            )

        except (element_tree.ParseError, TypeError):
            _LOGGER.warning("Can't read IPv6 filter rules from %s", self.host)
            self.token = None
            raise exceptions.ConnectBoxNoDataAvailable() from None 
Example #14
Source File: __init__.py    From python-connect-box with MIT License 5 votes vote down vote up
def async_get_downstream(self):
        """Get the current downstream cable modem state."""
        if self.token is None:
            await self.async_initialize_token()

        self.ds_channels.clear()
        raw = await self._async_ws_get_function(CMD_DOWNSTREAM)

        try:
            xml_root = element_tree.fromstring(raw)
            for downstream in xml_root.iter("downstream"):
                self.ds_channels.append(
                    DownstreamChannel(
                        int(downstream.find("freq").text),
                        int(downstream.find("pow").text),
                        downstream.find("mod").text,
                        downstream.find("chid").text,
                        float(downstream.find("RxMER").text),
                        int(downstream.find("PreRs").text),
                        int(downstream.find("PostRs").text),
                        downstream.find("IsQamLocked").text == "1",
                        downstream.find("IsFECLocked").text == "1",
                        downstream.find("IsMpegLocked").text == "1",
                    )
                )
        except (element_tree.ParseError, TypeError):
            _LOGGER.warning("Can't read downstream channels from %s", self.host)
            self.token = None
            raise exceptions.ConnectBoxNoDataAvailable() from None 
Example #15
Source File: __init__.py    From python-connect-box with MIT License 5 votes vote down vote up
def async_get_upstream(self):
        """Get the current upstream cable modem state."""
        if self.token is None:
            await self.async_initialize_token()

        self.us_channels.clear()
        raw = await self._async_ws_get_function(CMD_UPSTREAM)

        try:
            xml_root = element_tree.fromstring(raw)
            for upstream in xml_root.iter("upstream"):
                self.us_channels.append(
                    UpstreamChannel(
                        int(upstream.find("freq").text),
                        int(upstream.find("power").text),
                        upstream.find("srate").text,
                        upstream.find("usid").text,
                        upstream.find("mod").text,
                        upstream.find("ustype").text,
                        int(upstream.find("t1Timeouts").text),
                        int(upstream.find("t2Timeouts").text),
                        int(upstream.find("t3Timeouts").text),
                        int(upstream.find("t4Timeouts").text),
                        upstream.find("channeltype").text,
                        int(upstream.find("messageType").text),
                    )
                )
        except (element_tree.ParseError, TypeError):
            _LOGGER.warning("Can't read upstream channels from %s", self.host)
            self.token = None
            raise exceptions.ConnectBoxNoDataAvailable() from None 
Example #16
Source File: __init__.py    From python-connect-box with MIT License 4 votes vote down vote up
def async_get_cmstatus_and_service_flows(self):
        """Get various status information."""
        if self.token is None:
            await self.async_initialize_token()

        self.cmstatus = None
        self.downstream_service_flows = []
        self.upstream_service_flows = []
        raw = await self._async_ws_get_function(CMD_CMSTATUS)

        try:
            xml_root = element_tree.fromstring(raw)
            self.cmstatus = CmStatus(
                provisioningStatus=xml_root.find("provisioning_st").text,
                cmComment=xml_root.find("cm_comment").text,
                cmDocsisMode=xml_root.find("cm_docsis_mode").text,
                cmNetworkAccess=xml_root.find("cm_network_access").text,
                numberOfCpes=int(xml_root.find("NumberOfCpes").text),
                firmwareFilename=xml_root.find("FileName").text,
                dMaxCpes=int(xml_root.find("dMaxCpes").text),
                bpiEnable=int(xml_root.find("bpiEnable").text),
            )
            for elmt_service_flow in xml_root.iter("serviceflow"):
                service_flow = ServiceFlow(
                    id=int(elmt_service_flow.find("Sfid").text),
                    pMaxTrafficRate=int(elmt_service_flow.find("pMaxTrafficRate").text),
                    pMaxTrafficBurst=int(
                        elmt_service_flow.find("pMaxTrafficBurst").text
                    ),
                    pMinReservedRate=int(
                        elmt_service_flow.find("pMinReservedRate").text
                    ),
                    pMaxConcatBurst=int(elmt_service_flow.find("pMaxConcatBurst").text),
                    pSchedulingType=int(elmt_service_flow.find("pSchedulingType").text),
                )
                direction = int(elmt_service_flow.find("direction").text)
                if direction == 1:
                    self.downstream_service_flows.append(service_flow)
                elif direction == 2:
                    self.upstream_service_flows.append(service_flow)
                else:
                    raise element_tree.ParseError(
                        "Unknown service flow direction '{}'".format(direction)
                    )
        except (element_tree.ParseError, TypeError):
            _LOGGER.warning("Can't read cmstatus from %s", self.host)
            self.token = None
            raise exceptions.ConnectBoxNoDataAvailable() from None 
Example #17
Source File: __init__.py    From python-connect-box with MIT License 4 votes vote down vote up
def async_get_devices(self):
        """Scan for new devices and return a list with found device IDs."""
        if self.token is None:
            await self.async_initialize_token()

        self.devices.clear()
        raw = await self._async_ws_get_function(CMD_DEVICES)

        try:
            xml_root = element_tree.fromstring(raw)
            mac_adresses: List[str] = [mac.text for mac in xml_root.iter("MACAddr")]
            hostnames: List[str] = [mac.text for mac in xml_root.iter("hostname")]
            ip_addresses: List[str] = [mac.text for mac in xml_root.iter("IPv4Addr")]
            interfaces: List[str] = [mac.text for mac in xml_root.iter("interface")]
            speeds: List[str] = [mac.text for mac in xml_root.iter("speed")]
            interface_ids: List[str] = [
                mac.text for mac in xml_root.iter("interfaceid")
            ]
            methods: List[str] = [mac.text for mac in xml_root.iter("method")]
            lease_times: List[str] = [mac.text for mac in xml_root.iter("leaseTime")]

            for (
                mac_address,
                hostname,
                ip_address,
                interface,
                speed,
                interface_id,
                method,
                lease_time,
            ) in zip(
                mac_adresses,
                hostnames,
                ip_addresses,
                interfaces,
                speeds,
                interface_ids,
                methods,
                lease_times,
            ):
                self.devices.append(
                    Device(
                        mac_address,
                        hostname,
                        ip_address.partition("/")[0],
                        interface,
                        speed,
                        interface_id,
                        method,
                        lease_time,
                    )
                )
        except (element_tree.ParseError, TypeError):
            _LOGGER.warning("Can't read device from %s", self.host)
            self.token = None
            raise exceptions.ConnectBoxNoDataAvailable() from None 
Example #18
Source File: importotp.py    From privacyidea with GNU Affero General Public License v3.0 4 votes vote down vote up
def parseSafeNetXML(xml):
    """
    This function parses XML data of a Aladdin/SafeNet XML
    file for eToken PASS

    It returns a dictionary of
        serial : { otpkey , counter, type }
    """

    TOKENS = {}
    try:
        elem_tokencontainer = etree.fromstring(xml)
    except etree.ParseError as e:
        log.debug(traceback.format_exc())
        raise ImportException('Could not parse XML data: {0!s}'.format(e))

    if getTagName(elem_tokencontainer) != "Tokens":
        raise ImportException("No toplevel element Tokens")

    for elem_token in list(elem_tokencontainer):
        SERIAL = None
        COUNTER = None
        HMAC = None
        DESCRIPTION = None
        if getTagName(elem_token) == "Token":
            SERIAL = elem_token.get("serial")
            log.debug("Found token with serial {0!s}".format(SERIAL))
            for elem_tdata in list(elem_token):
                tag = getTagName(elem_tdata)
                if "ProductName" == tag:
                    DESCRIPTION = elem_tdata.text
                    log.debug("The Token with the serial %s has the "
                              "productname %s" % (SERIAL, DESCRIPTION))
                if "Applications" == tag:
                    for elem_apps in elem_tdata:
                        if getTagName(elem_apps) == "Application":
                            for elem_app in elem_apps:
                                tag = getTagName(elem_app)
                                if "Seed" == tag:
                                    HMAC = elem_app.text
                                if "MovingFactor" == tag:
                                    COUNTER = elem_app.text
            if not SERIAL:
                log.error("Found token without a serial")
            else:
                if HMAC:
                    hashlib = "sha1"
                    if len(HMAC) == 64:
                        hashlib = "sha256"

                    TOKENS[SERIAL] = {'otpkey': HMAC,
                                      'counter': COUNTER,
                                      'type': 'hotp',
                                      'hashlib': hashlib
                                      }
                else:
                    log.error("Found token {0!s} without a element 'Seed'".format(
                              SERIAL))

    return TOKENS