~chromium-team/chromium-browser/translations-pump

« back to all changes in this revision

Viewing changes to fileformats/utils.py

  • Committer: Chad MILLER
  • Date: 2015-10-19 18:42:18 UTC
  • Revision ID: chad.miller@canonical.com-20151019184218-benco7vjulfva1lc
.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        logging.warn("A name used in condition %r is unknown! You have to define it. I only know of %s", expression_string, local_variables.keys())
44
44
        return False
45
45
    return condition_result
 
46
 
 
47
 
 
48
from xml.etree import ElementTree as ET
 
49
from collections import OrderedDict
 
50
 
 
51
ET.Element =ET._Element_Py
 
52
 
 
53
class _L(list):
 
54
    def __init__(self, *args, **kwargs):
 
55
        super().__init__(*args, **kwargs)
 
56
        print("init _L")
 
57
 
 
58
    def sort(self):
 
59
        print("Ignoring request to sort.")
 
60
        pass
 
61
 
 
62
class _OrderedDictWithItemsNoSort(OrderedDict):
 
63
    def __init__(self, *args, **kwargs):
 
64
        super().__init__(*args, **kwargs)
 
65
 
 
66
    def items(self):
 
67
        return _L(super().items())
 
68
 
 
69
class OrderedXMLParser(ET.XMLParser):
 
70
    def __init__(self, *args, **kwargs):
 
71
        super().__init__(*args, **kwargs)
 
72
 
 
73
    def _start(self, tag, attr_list):
 
74
        # Handler for expat's StartElementHandler. Since ordered_attributes
 
75
        # is set, the attributes are reported as a list of alternating
 
76
        # attribute name,value.
 
77
        fixname = self._fixname
 
78
        tag = fixname(tag)
 
79
        attrib = _OrderedDictWithItemsNoSort()
 
80
        if attr_list:
 
81
            for i in range(0, len(attr_list), 2):
 
82
                attrib[fixname(attr_list[i])] = attr_list[i+1]
 
83
        return self.target.start(tag, attrib)
 
84