当前位置: 首页>>代码示例>>Python>>正文


Python ElementTree.parse方法代码示例

本文整理汇总了Python中defusedxml.ElementTree.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.parse方法的具体用法?Python ElementTree.parse怎么用?Python ElementTree.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在defusedxml.ElementTree的用法示例。


在下文中一共展示了ElementTree.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ParseFileObject

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def ParseFileObject(self, parser_mediator, file_object):
    """Parses an Opera global history file-like object.

    Args:
      parser_mediator (ParserMediator): mediates interactions between parsers
          and other components, such as storage and dfvfs.
      file_object (dfvfs.FileIO): file-like object.

    Raises:
      UnableToParseFile: when the file cannot be parsed.
    """
    encoding = self._ENCODING or parser_mediator.codepage
    text_file_object = text_file.TextFile(file_object, encoding=encoding)
    if not self._ParseAndValidateRecord(parser_mediator, text_file_object):
      raise errors.UnableToParseFile(
          'Unable to parse as Opera global_history.dat.')

    while self._ParseRecord(parser_mediator, text_file_object):
      pass 
开发者ID:log2timeline,项目名称:plaso,代码行数:21,代码来源:opera.py

示例2: parse_human_readable_size

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def parse_human_readable_size(size):
    units = {
        'b': 1,
        'kb': 1000, 'mb': 1000**2, 'gb': 1000**3, 'tb': 1000**4,
        'kib': 1024, 'mib': 1024**2, 'gib': 1024**3, 'tib': 1024**4,
    }
    try:
        return int(float(size))
    except (ValueError, TypeError):
        if type(size) != str:
            raise ValueError(_('Could not parse size "{size}", wrong type "{type}"')
                             .format(size=size, type=type(size)))
        s = size.lower().replace(' ', '')
        m = re.match(r'^(?P<value>[0-9][0-9.]+) *(?P<unit>' + r'|'.join(units.keys()) + r')$', s)
        if not m:
            raise ValueError(_('Not a valid size definition: "{}"').format(size))
        return int(float(m.group("value")) * units[m.group("unit")]) 
开发者ID:f-droid,项目名称:fdroidserver,代码行数:19,代码来源:common.py

示例3: load_hosts

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def load_hosts(self, file):

        root = ElementTree.parse(file).getroot()
        for child in root.findall('host'):
            host = self._generate_host(child)

            yield host 
开发者ID:delvelabs,项目名称:batea,代码行数:9,代码来源:nmap_parser.py

示例4: get_dom

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def get_dom():
        # Load the XML on first use and keep it in memory in a global
        # variable. This is perhaps not the best design.
        global XML_DOM
        if XML_DOM is None:
            XML_DOM = ET.parse(XML_FILE)
        return XML_DOM 
开发者ID:GovReady,项目名称:compliancelib-python,代码行数:9,代码来源:nist800_53.py

示例5: read_xml_file

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def read_xml_file(filename):
    tree = None
    try:
        tree = ET.parse(filename).getroot()
    except Exception as e:
        log.error('Xml parsing error: ' + str(e))
    return tree 
开发者ID:rdmorganiser,项目名称:rdmo,代码行数:9,代码来源:imports.py

示例6: parse_xml

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def parse_xml(xml_content):
    try:
        return defused_xml_parse(xml_content)
    except ParseError:
        raise FeedXMLError('Not a valid XML document') 
开发者ID:NicolasLM,项目名称:atoma,代码行数:7,代码来源:utils.py

示例7: run_diff

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
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 
开发者ID:tableau,项目名称:connector-plugin-sdk,代码行数:42,代码来源:tdvt_core.py

示例8: validate_file_specific_rules

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def validate_file_specific_rules(file_to_test: ConnectorFile, path_to_file: Path, xml_violations_buffer: List[str]) -> bool:

    if file_to_test.file_type == 'connection-fields':
        xml_tree = parse(str(path_to_file))
        root = xml_tree.getroot()

        for child in root.iter('field'):
            if 'name' in child.attrib:
                field_name = child.attrib['name']
                if not (field_name in PLATFORM_FIELD_NAMES or field_name.startswith(VENDOR_FIELD_NAME_PREFIX)):
                    xml_violations_buffer.append("Element 'field', attribute 'name'='" + field_name +
                                                 "' not an allowed value. See 'Connection Field Platform Integration' section of documentation for allowed values.")
                    return False

    return True 
开发者ID:tableau,项目名称:connector-plugin-sdk,代码行数:17,代码来源:xsd_validator.py

示例9: main

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
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.') 
开发者ID:mwleeds,项目名称:android-malware-analysis,代码行数:42,代码来源:parse_xml.py

示例10: _load_data

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def _load_data(self, env, data_source, encoding):
        rel_filename, filename = env.relfn2path(data_source)
        if data_source.endswith('.yaml'):
            return self._load_yaml(filename, encoding)
        elif data_source.endswith('.json'):
            return self._load_json(filename, encoding)
        elif data_source.endswith('.csv'):
            return self._load_csv(filename, encoding)
        elif "xml" in mimetypes.guess_type(data_source)[0]:
            # there are many XML based formats
            return ET.parse(filename).getroot()
        else:
            raise NotImplementedError('cannot load file type of %s' %
                                      data_source) 
开发者ID:sphinx-contrib,项目名称:datatemplates,代码行数:16,代码来源:directive.py

示例11: load_xml

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def load_xml(source, absolute_resolved_path, **options):
    yield ET.parse(absolute_resolved_path).getroot() 
开发者ID:sphinx-contrib,项目名称:datatemplates,代码行数:4,代码来源:loaders.py

示例12: checkrepo

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def checkrepo(self):
        """If the local directory exists, but is somehow not a git repository,
        git will traverse up the directory tree until it finds one
        that is (i.e.  fdroidserver) and then we'll proceed to destroy
        it!  This is called as a safety check.

        """

        p = FDroidPopen(['git', 'rev-parse', '--show-toplevel'], cwd=self.local, output=False)
        result = p.output.rstrip()
        if not result.endswith(self.local):
            raise VCSException('Repository mismatch') 
开发者ID:f-droid,项目名称:fdroidserver,代码行数:14,代码来源:common.py

示例13: parse_srclib_spec

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def parse_srclib_spec(spec):

    if type(spec) != str:
        raise MetaDataException(_("can not parse scrlib spec "
                                  "(not a string): '{}'")
                                .format(spec))

    tokens = spec.split('@')
    if len(tokens) > 2:
        raise MetaDataException(_("could not parse srclib spec "
                                  "(too many '@' signs): '{}'")
                                .format(spec))
    elif len(tokens) < 2:
        raise MetaDataException(_("could not parse srclib spec "
                                  "(no ref specified): '{}'")
                                .format(spec))

    name = tokens[0]
    ref = tokens[1]
    number = None
    subdir = None

    if ':' in name:
        number, name = name.split(':', 1)
    if '/' in name:
        name, subdir = name.split('/', 1)

    return (name, ref, number, subdir) 
开发者ID:f-droid,项目名称:fdroidserver,代码行数:30,代码来源:common.py

示例14: calculate_math_string

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def calculate_math_string(expr):
    ops = {
        ast.Add: operator.add,
        ast.Mult: operator.mul,
        ast.Sub: operator.sub,
        ast.USub: operator.neg,
    }

    def execute_ast(node):
        if isinstance(node, ast.Num):  # <number>
            return node.n
        elif isinstance(node, ast.BinOp):  # <left> <operator> <right>
            return ops[type(node.op)](execute_ast(node.left),
                                      execute_ast(node.right))
        elif isinstance(node, ast.UnaryOp):  # <operator> <operand> e.g., -1
            return ops[type(node.op)](ast.literal_eval(node.operand))
        else:
            raise SyntaxError(node)

    try:
        if '#' in expr:
            raise SyntaxError('no comments allowed')
        return execute_ast(ast.parse(expr, mode='eval').body)
    except SyntaxError:
        raise SyntaxError("could not parse expression '{expr}', "
                          "only basic math operations are allowed (+, -, *)"
                          .format(expr=expr)) 
开发者ID:f-droid,项目名称:fdroidserver,代码行数:29,代码来源:common.py

示例15: intellij_read_sdks

# 需要导入模块: from defusedxml import ElementTree [as 别名]
# 或者: from defusedxml.ElementTree import parse [as 别名]
def intellij_read_sdks():
    sdks = dict()
    if mx.is_linux() or mx.is_openbsd() or mx.is_sunos() or mx.is_windows():
        xmlSdks = glob.glob(os.path.expanduser("~/.IdeaIC*/config/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/.IntelliJIdea*/config/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/.config/JetBrains/IdeaIC*/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/.config/JetBrains/IntelliJIdea*/options/jdk.table.xml"))
    elif mx.is_darwin():
        xmlSdks = \
          glob.glob(os.path.expanduser("~/Library/Application Support/JetBrains/IdeaIC*/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/Library/Application Support/JetBrains/IntelliJIdea*/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/Library/Preferences/IdeaIC*/options/jdk.table.xml")) + \
          glob.glob(os.path.expanduser("~/Library/Preferences/IntelliJIdea*/options/jdk.table.xml"))
    else:
        mx.warn("Location of IntelliJ SDK definitions on {} is unknown".format(mx.get_os()))
        return sdks
    if len(xmlSdks) == 0:
        mx.warn("IntelliJ SDK definitions not found")
        return sdks

    verRE = re.compile(r'^.*[/\\]\.?(IntelliJIdea|IdeaIC)([^/\\]+)[/\\].*$')
    def verSort(path):
        match = verRE.match(path)
        return match.group(2) + (".a" if match.group(1) == "IntellijIC" else ".b")

    xmlSdks.sort(key=verSort)
    xmlSdk = xmlSdks[-1]  # Pick the most recent IntelliJ version, preferring Ultimate over Community edition.
    mx.log("Using SDK definitions from {}".format(xmlSdk))

    versionRegexes = {}
    versionRegexes[intellij_java_sdk_type] = re.compile(r'^java\s+version\s+"([^"]+)"$|^([\d._]+)$')
    versionRegexes[intellij_python_sdk_type] = re.compile(r'^Python\s+(.+)$')
    # Examples:
    #   truffleruby 19.2.0-dev-2b2a7f81, like ruby 2.6.2, Interpreted JVM [x86_64-linux]
    #   ver.2.2.4p230 ( revision 53155) p230
    versionRegexes[intellij_ruby_sdk_type] = re.compile(r'^\D*(\d[^ ,]+)')

    for sdk in etreeParse(xmlSdk).getroot().findall("component[@name='ProjectJdkTable']/jdk[@version='2']"):
        name = sdk.find("name").get("value")
        kind = sdk.find("type").get("value")
        home = realpath(os.path.expanduser(sdk.find("homePath").get("value").replace('$USER_HOME$', '~')))
        if home.find('$APPLICATION_HOME_DIR$') != -1:
            # Don't know how to convert this into a real path so ignore it
            continue
        versionRE = versionRegexes.get(kind)
        if not versionRE or sdk.find("version") is None:
            # ignore unknown kinds
            continue

        match = versionRE.match(sdk.find("version").get("value"))
        if match:
            version = match.group(1)
            sdks[home] = {'name': name, 'type': kind, 'version': version}
            mx.logv("Found SDK {} with values {}".format(home, sdks[home]))
        else:
            mx.warn("Couldn't understand Java version specification \"{}\" for {} in {}".format(sdk.find("version").get("value"), home, xmlSdk))
    return sdks 
开发者ID:graalvm,项目名称:mx,代码行数:59,代码来源:mx_ide_intellij.py


注:本文中的defusedxml.ElementTree.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。