try to support python 2.7 for iteration with ElementTree

svn: r20477
This commit is contained in:
Jérôme Rapinat 2012-10-01 09:51:47 +00:00
parent bc06218642
commit adac46441d

View File

@ -94,15 +94,6 @@ def TipsParse(filename, mark):
tree = ElementTree.parse(filename) tree = ElementTree.parse(filename)
root = tree.getroot() root = tree.getroot()
python_v = sys.version_info
#if python_v[1] != 6:
# python 2.7
# iter() is the new name for getiterator;
# in ET 1.3, it is implemented as a generator method,
# but is otherwise identical
''' '''
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<tips> <tips>
@ -133,6 +124,12 @@ def TipsParse(filename, mark):
tips = open('../gramps/data/tips.xml.in.h', 'w') tips = open('../gramps/data/tips.xml.in.h', 'w')
# python 2.7
# iter() is the new name for getiterator;
# in ET 1.3, it is implemented as a generator method,
# but is otherwise identical
# it works with python 2.6 only !
for key in root.getiterator(mark): for key in root.getiterator(mark):
tip = ElementTree.tostring(key, encoding="UTF-8") tip = ElementTree.tostring(key, encoding="UTF-8")
tip = tip.replace("<?xml version='1.0' encoding='UTF-8'?>", "") tip = tip.replace("<?xml version='1.0' encoding='UTF-8'?>", "")
@ -161,13 +158,18 @@ def HolidaysParse(filename, mark):
python_v = sys.version_info python_v = sys.version_info
#if python_v[1] != 6:
# python 2.7 # python 2.7
# iter() is the new name for getiterator; # iter() is the new name for getiterator;
# in ET 1.3, it is implemented as a generator method, # in ET 1.3, it is implemented as a generator method,
# but is otherwise identical # but is otherwise identical
# getiterator() for python 2.6
ITERATION = root.getiterator()
# iter() for python 2.7 and greater versions
if sys.version_info[1] ==7:
ITERATION = root.iter()
''' '''
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
calendar> calendar>
@ -188,7 +190,7 @@ def HolidaysParse(filename, mark):
holidays = open('../gramps/plugins/lib/holidays.xml.in.h', 'w') holidays = open('../gramps/plugins/lib/holidays.xml.in.h', 'w')
for key in root.getiterator(): for key in ITERATION:
if key.attrib.get(mark): if key.attrib.get(mark):
line = key.attrib line = key.attrib
string = line.items string = line.items