Package xmlschema_acue :: Module compat

Source Code for Module xmlschema_acue.compat

 1  # -*- coding: utf-8 -*- 
 2  # 
 3  # Copyright (c), 2016-2019, SISSA (International School for Advanced Studies). 
 4  # All rights reserved. 
 5  # This file is distributed under the terms of the MIT License. 
 6  # See the file 'LICENSE' in the root directory of the present 
 7  # distribution, or http://opensource.org/licenses/MIT. 
 8  # 
 9  # @author Davide Brunato <brunato@sissa.it> 
10  # 
11  """ 
12  This module contains imports and definitions for Python 2 and 3 compatibility. 
13  """ 
14  import sys 
15  from collections import OrderedDict 
16   
17  PY3 = sys.version_info[0] == 3 
18   
19  if PY3: 
20      # Python 3 imports 
21      from urllib.request import urlopen, urljoin, urlsplit, pathname2url 
22      from urllib.parse import uses_relative, urlparse, urlunsplit 
23      from urllib.error import URLError 
24      from io import StringIO, BytesIO 
25      from collections.abc import Iterable, MutableSet, Sequence, MutableSequence, Mapping, MutableMapping 
26      from functools import lru_cache 
27   
28      long_type = int 
29      string_base_type = str 
30      unicode_type = str 
31      unicode_chr = chr 
32   
33  else: 
34      # Python 2.7 imports 
35      from urllib import pathname2url 
36      from urllib2 import urlopen, URLError 
37      from urlparse import urlsplit, urljoin, uses_relative, urlparse, urlunsplit 
38      from StringIO import StringIO  # the io.StringIO accepts only unicode type 
39      from io import BytesIO 
40      from collections import Iterable, MutableSet, Sequence, MutableSequence, Mapping, MutableMapping 
41      from functools import wraps 
42   
43      long_type = long 
44      string_base_type = basestring 
45      unicode_type = unicode 
46      unicode_chr = unichr 
47 48 - def lru_cache(maxsize=128, typed=False):
49 """ 50 A fake lru_cache decorator function for Python 2.7 compatibility until support ends. 51 """ 52 def lru_cache_decorator(f): 53 @wraps(f) 54 def wrapper(*args, **kwargs): 55 return f(*args, **kwargs)
56 return wrapper 57 return lru_cache_decorator 58 59 60 ordered_dict_class = dict if sys.version_info >= (3, 6) else OrderedDict
61 62 63 -def add_metaclass(metaclass):
64 """ 65 Class decorator for creating a class with a metaclass. 66 From `six` package source code: https://bitbucket.org/gutworth/six/overview. 67 """ 68 def wrapper(cls): 69 orig_vars = cls.__dict__.copy() 70 slots = orig_vars.get('__slots__') 71 if slots is not None: 72 if isinstance(slots, str): 73 slots = [slots] 74 for slots_var in slots: 75 orig_vars.pop(slots_var) 76 orig_vars.pop('__dict__', None) 77 orig_vars.pop('__weakref__', None) 78 return metaclass(cls.__name__, cls.__bases__, orig_vars)
79 return wrapper 80