diff options
author | dol-sen <brian.dolbec@gmail.com> | 2011-09-18 23:22:15 -0700 |
---|---|---|
committer | dol-sen <brian.dolbec@gmail.com> | 2011-09-22 20:12:00 -0700 |
commit | f3e1976da8eeef942a95019669633255fa3b6c50 (patch) | |
tree | fd5abe96dd7a25684a81fd6508e7c5c86204f683 /layman/compatibility.py | |
parent | 2413c2f4d2c5eb09063f477bcd158244ab8d32ff (diff) | |
download | layman-f3e1976da8eeef942a95019669633255fa3b6c50.tar.gz layman-f3e1976da8eeef942a95019669633255fa3b6c50.tar.bz2 layman-f3e1976da8eeef942a95019669633255fa3b6c50.zip |
py2, py3 compatability changes so 2to3 will work correctly.
Diffstat (limited to 'layman/compatibility.py')
-rw-r--r-- | layman/compatibility.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/layman/compatibility.py b/layman/compatibility.py new file mode 100644 index 0000000..b71a8af --- /dev/null +++ b/layman/compatibility.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +""" Copyright 2005 - 2008 Gunnar Wrobel + 2011 - Brian Dolbec + Distributed under the terms of the GNU General Public License v2 +""" + +import sys, types + + +def encode(text, enc="UTF-8"): + """py2, py3 compatibility function""" + if hasattr(text, 'decode'): + try: + return text.decode(enc) + except UnicodeEncodeError: + return unicode(text) + return str(text) + + +def fileopen(path, mode='r', enc="UTF-8"): + """py2, py3 compatibility function""" + try: + f = open(path, mode, encoding=enc) + except TypeError: + f = open(path, mode) + return f + |