commit: ea812ff32370c426fb9c266b8824ba9338264c7c Author: Devan Franchini <twitch153 <AT> gentoo <DOT> org> AuthorDate: Wed May 14 21:10:50 2014 +0000 Commit: Devan Franchini <twitch153 <AT> gentoo <DOT> org> CommitDate: Wed May 14 21:10:50 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=ea812ff3
layman/dbbase.py: Adds py3 unicode compatibility to ElementTree When writing the database to installed.xml, ElementTree needs to have an encoding type specified or run time errors will occur. To ensure compatibility between py2/py3 a check has been made to see which python version is running and to set the _unicode variable accordingly. --- layman/dbbase.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index ff99965..3632a57 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -37,6 +37,14 @@ from layman.utils import indent from layman.compatibility import fileopen from layman.overlays.overlay import Overlay +#py3.2 +if sys.hexversion >= 0x30200f0: + _unicode = 'unicode' +else: + _unicode = 'UTF-8' + + + #=============================================================================== # # Class UnknownOverlayException @@ -220,16 +228,13 @@ class DbBase(object): >>> os.rmdir(tmpdir) ''' - tree = ET.Element('repositories', version="1.0") + tree = ET.Element('repositories', version="1.0", encoding="unicode") tree[:] = [e.to_xml() for e in list(self.overlays.values())] indent(tree) tree = ET.ElementTree(tree) try: f = fileopen(path, 'w') - f.write("""\ -<?xml version="1.0" encoding="UTF-8"?> -""") - tree.write(f, encoding='utf-8') + tree.write(f, encoding=_unicode) f.close() except Exception as error: raise Exception('Failed to write to local overlays file: '