Ejemplo n.º 1
0
 def __init__(self, name, params=None):
     self._name = name
     if params is None:
         self._params = OrderedDict()
     else:
         self._params = params
     self._sub_sections = OrderedDict()
Ejemplo n.º 2
0
    def load(self):
        import libxml2
        d = libxml2.parseFile(self._file_name)
        root = d.getRootElement()

        params = root.xpathEval(XMLAdapter.XPATH_ALL_ATTRS)
        params_list = OrderedDict()
        for p in params:
            params_list[p.name] = p.content

        root_section = Section(root.name, params_list)

        # Recursively load all child nodes (and its params)
        # and interpret them as sections
        def load_sections(sec, root_node):
            for child_node in root_node.xpathEval(XMLAdapter.XPATH_ALL_CHILDS):
                params = child_node.xpathEval(XMLAdapter.XPATH_ALL_ATTRS)
                params_list = {}
                for p in params:
                    params_list[p.name] = p.content

                chld_sec = sec.add_sub_section(Section(child_node.name, params_list))
                load_sections(chld_sec, child_node)

        load_sections(root_section, root)
        d.freeDoc()
        return root_section
Ejemplo n.º 3
0
def read_xml(file, warnfunc=dummy_warn):
    """Load all resource names from an Android strings.xml resource file.

    The result is a dict of ``name => value``, `with ``value`` being
    either a string (a single string tag), a list (a string-array tag) or
    a dict (a plurals tag).
    """
    result = OrderedDict()
    comment = []

    try:
        doc = etree.parse(file)
    except etree.XMLSyntaxError, e:
        raise InvalidResourceError(e)
Ejemplo n.º 4
0
def get_keys(iterable):
    if not iterable:
        return []
    return OrderedDict(iterable).keys()
Ejemplo n.º 5
0
def get_items(iterable):
    if not iterable:
        return []
    return OrderedDict(iterable).items()
Ejemplo n.º 6
0
 def __init__(self, *args):
     self.arguments = OrderedDict((arg.name, arg) for arg in args)
Ejemplo n.º 7
0
        self._adapter.save(self._root_section)

    def load(self):
        self._root_section = self._adapter.load()


if __name__ == '__main__':
    #       Saving XML

    # Vytvoreni patricneho adapteru
    ad = XMLAdapter('file.xml')

    # Vytvoreni jakehosi "dokumentu" a jeho 'root' sekce
    doc = Document(ad)
    info_args = OrderedDict([("kernel", '3.10.0-244.el7.x86_64'),
                            ("system_release", 'Red Hat Enterprise Linux Server release 7.1 (Maipo)'),
                            ("hostname", 'ibm-x3650m3-02.lab.eng.brq.redhat.com')
                             ])
    r_section = Section('host', info_args)
    doc.create_root_section(r_section)
    doc.add_sub_section(Section("SanityTest"))
    subsecNetperf = doc.add_sub_section(Section("NetperfTCPStream"))

    # Vytvoreni dvou do sebe vnorenych podsekci
    subsection_1 = OrderedDict([("dstip", '172.18.10.20'),
                                ("AF", 'INET4'),
                                ("dstname", 'bnx2_1'),
                                ("srcname", 'bnx2_1'),
                                ("srcip", '172.18.10.10')
                                ])
    secPath = subsecNetperf.add_sub_section(Section('path', subsection_1))
Ejemplo n.º 8
0
def po2xml(catalog, with_untranslated=False, filter=None, warnfunc=dummy_warn):
    """Convert the gettext catalog in ``catalog`` to an XML DOM.

    This currently relies entirely in the fact that we can use the context
    of each message to specify the Android resource name (which we need
    to do to handle duplicates, but this is a nice by-product). However
    that also means we cannot handle arbitrary catalogs.

    The latter would in theory be possible by using the original,
    untranslated XML to match up a messages id to a resource name, but
    right now we don't support this (and it's not clear it would be
    necessary, even).

    If ``with_untranslated`` is given, then strings in the catalog
    that have no translation are written out with the original id. In
    the case of a string-array, if ``with_untranslated`` is NOT
    specified, then only strings that DO have a translation are written
    out, potentially causing the array to be incomplete.
    TODO: This should not be the case: Arrays should always contain
    all elements, whether translated or not (using an empty string
    instead). When writing tests for this, make sure we generally test
    the with_untranslated mode, i.e. also the behavior for normal strings.
    """
    # First, process the catalog into a Python sort-of-tree structure.
    # We can't write directly to the XML output, since stuff like
    # string-array items are not guaranteed to appear in the correct
    # order in the calalog. We "xml tree" pulls these things together.
    # It is quite similar to the structure returned by read_xml().
    xml_tree = OrderedDict()
    for message in catalog:
        if not message.id:
            # This is the header
            continue

        if not message.string and not with_untranslated:
            # Untranslated.
            continue

        if not message.context:
            warnfunc(('Ignoring message "%s": has no context; somebody other '+
                      'than android2po seems to have added to this '+
                      'catalog.') % message.id, 'error')
            continue

        if filter and filter(message):
            continue

        value = message.string or message.id

        if ':' in message.context:
            # A colon indicates a string array; collect all the
            # strings of this array with their indices, so when
            # we're done processing the whole catalog, we can
            # sort by index and restore the proper array order.
            name, index = message.context.split(':', 2)
            xml_tree.setdefault(name, {})
            if index in xml_tree[name]:
                warnfunc(('Duplicate index %s in array "%s"; ignoring '+
                          'the message. The catalog has possibly been '+
                          'corrupted.') % (index, name), 'error')
            xml_tree[name][index] = value
        else:
            xml_tree[message.context] = value

    # Convert the xml tree we've built into an actual Android XML DOM.
    root_tags = []
    namespaces_used = {}
    for name, value in xml_tree.iteritems():
        if isinstance(value, dict):
            # string-array - first, sort by index
            array_el = etree.Element('string-array')
            array_el.attrib['name'] = name
            for k in sorted(value):
                item_el = write_to_dom('item', value[k], message, namespaces_used, warnfunc)
                array_el.append(item_el)
            root_tags.append(array_el)
        else:
            # standard string
            string_el = write_to_dom('string', value, message, namespaces_used, warnfunc)
            string_el.attrib['name'] = name
            root_tags.append(string_el)

    # Generate the root element, define the namespaces that have been
    # used across all of our child elements.
    root_el = etree.Element('resources', nsmap=namespaces_used)
    for e in root_tags:
        root_el.append(e)
    return root_el