Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / geopy / compat.py @ 545

History | View | Annotate | Download (3.11 KB)

1
"""
2
Compatibility...
3
"""
4

    
5
import sys
6

    
7
py3k = sys.version_info >= (3, 0)
8

    
9
if py3k: # pragma: no cover
10
    string_compare = str
11
else: # pragma: no cover
12
    string_compare = (str, unicode)
13

    
14
# Unicode compatibility, borrowed from 'six'
15
if py3k: # pragma: no cover
16
    def u(s):
17
        """
18
        Convert to Unicode with py3k
19
        """
20
        return s
21
else: # pragma: no cover
22
    def u(s):
23
        """
24
        Convert to Unicode with unicode escaping
25
        """
26
        return unicode(s.replace(r'\\', r'\\\\'), 'unicode_escape')
27

    
28
if py3k: # pragma: no cover
29
    from urllib.parse import urlencode, quote # pylint: disable=W0611,F0401,W0611,E0611
30
    from urllib.request import (Request, urlopen, # pylint: disable=W0611,F0401,W0611,E0611
31
                                build_opener, ProxyHandler,
32
                                URLError, install_opener,
33
                                HTTPPasswordMgrWithDefaultRealm,
34
                                HTTPBasicAuthHandler)
35
    from urllib.error import HTTPError # pylint: disable=W0611,F0401,W0611,E0611
36

    
37
    def itervalues(d):
38
        """
39
        Function for iterating on values due to methods
40
        renaming between Python 2 and 3 versions
41
        For Python2
42
        """
43
        return iter(d.values())
44
    def iteritems(d):
45
        """
46
        Function for iterating on items due to methods
47
        renaming between Python 2 and 3 versions
48
        For Python2
49
        """
50
        return iter(d.items())
51

    
52
else: # pragma: no cover
53
    from urllib import urlencode as original_urlencode, quote # pylint: disable=W0611,F0401,W0611,E0611
54
    from urllib2 import (Request, HTTPError,   # pylint: disable=W0611,F0401,W0611,E0611
55
                         ProxyHandler, URLError, urlopen,
56
                         build_opener, install_opener,
57
                         HTTPPasswordMgrWithDefaultRealm,
58
                         HTTPBasicAuthHandler)
59

    
60
    def force_str(str_or_unicode):
61
        """
62
        Python2-only, ensures that a string is encoding to a str.
63
        """
64
        if isinstance(str_or_unicode, unicode):
65
            return str_or_unicode.encode('utf-8')
66
        else:
67
            return str_or_unicode
68

    
69
    def urlencode(query, doseq=0):
70
        """
71
        A version of Python's urllib.urlencode() function that can operate on
72
        unicode strings. The parameters are first cast to UTF-8 encoded strings
73
        and then encoded as per normal.
74

75
        Based on the urlencode from django.utils.http
76
        """
77
        if hasattr(query, 'items'):
78
            query = query.items()
79
        return original_urlencode(
80
            [(force_str(k),
81
              [force_str(i) for i in v]
82
              if isinstance(v, (list, tuple)) else force_str(v))
83
             for k, v in query],
84
            doseq)
85

    
86
    def itervalues(d):
87
        """
88
        Function for iterating on values due to methods
89
        renaming between Python 2 and 3 versions
90
        For Python3
91
        """
92
        return d.itervalues()
93
    def iteritems(d):
94
        """
95
        Function for iterating on items due to methods
96
        renaming between Python 2 and 3 versions
97
        For Python3
98
        """
99
        return d.iteritems()