diff options
author | Anthon van der Neut <anthon@mnt.org> | 2015-08-28 08:03:59 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2015-08-28 08:03:59 +0200 |
commit | 386029ed647ca7fd0209506285f02365c347eef9 (patch) | |
tree | 78263b913d9dbe9bf7c8a3c04379f508bff56a23 /compat.py | |
parent | 04e651b6b062edbdf66271847d3dde06550adbb4 (diff) | |
download | ruamel.yaml-386029ed647ca7fd0209506285f02365c347eef9.tar.gz |
- main problem in moving stuff from yaml/py to yaml was that
parser.py clashes with built-in parser module (CPython, C-module)
which is inlucded from pkg_resources/__init__.py
- no C compile yet
Diffstat (limited to 'compat.py')
-rw-r--r-- | compat.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/compat.py b/compat.py new file mode 100644 index 0000000..dc0c51c --- /dev/null +++ b/compat.py @@ -0,0 +1,106 @@ + +from __future__ import print_function + +# partially from package six by Benjamin Peterson + +import sys +import os +import types + +try: + from ruamel.ordereddict import ordereddict +except: + try: + from collections import OrderedDict + except ImportError: + from orderddict import OrderedDict + # to get the right name import ... as ordereddict doesn't do that + + class ordereddict(OrderedDict): + pass + +PY2 = sys.version_info[0] == 2 +PY3 = sys.version_info[0] == 3 + +if PY3: + def utf8(s): + return s + + def to_str(s): + return s + + def to_unicode(s): + return s + +else: + def utf8(s): + return s.encode('utf-8') + + def to_str(s): + return str(s) + + def to_unicode(s): + return unicode(s) + +if PY3: + string_types = str, + integer_types = int, + class_types = type, + text_type = str + binary_type = bytes + + MAXSIZE = sys.maxsize + unichr = chr + import io + StringIO = io.StringIO + BytesIO = io.BytesIO + +else: + string_types = basestring, + integer_types = (int, long) + class_types = (type, types.ClassType) + text_type = unicode + binary_type = str + + unichr = unichr # to allow importing + import StringIO + StringIO = StringIO.StringIO + import cStringIO + BytesIO = cStringIO.StringIO + +if PY3: + builtins_module = 'builtins' +else: + builtins_module = '__builtin__' + + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + return meta("NewBase", bases, {}) + +DBG_TOKEN = 1 +DBG_EVENT = 2 +DBG_NODE = 4 + + +_debug = None + + +# used from yaml util when testing +def dbg(val=None): + global _debug + if _debug is None: + # set to true or false + _debug = os.environ.get('YAMLDEBUG') + if _debug is None: + _debug = 0 + else: + _debug = int(_debug) + if val is None: + return _debug + return _debug & val + + +def nprint(*args, **kw): + if dbg: + print(*args, **kw) |