main
Last change
on this file was
42bd667,
checked in by David Fuertes <dfuertes@…>, 4 years ago
|
Historial Limpio
|
-
Property mode set to
100755
|
File size:
1.7 KB
|
Line | |
---|
1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | "Special module to handle differences between Python 2 and 3 versions" |
---|
5 | |
---|
6 | import sys |
---|
7 | |
---|
8 | PY3K = sys.version_info >= (3, 0) |
---|
9 | |
---|
10 | try: |
---|
11 | import cPickle as pickle |
---|
12 | except ImportError: |
---|
13 | import pickle |
---|
14 | |
---|
15 | try: |
---|
16 | from urllib import urlopen |
---|
17 | except ImportError: |
---|
18 | from urllib.request import urlopen |
---|
19 | |
---|
20 | try: |
---|
21 | from io import BytesIO |
---|
22 | except ImportError: |
---|
23 | try: |
---|
24 | from cStringIO import StringIO as BytesIO |
---|
25 | except ImportError: |
---|
26 | from StringIO import StringIO as BytesIO |
---|
27 | |
---|
28 | try: |
---|
29 | from hashlib import md5 |
---|
30 | except ImportError: |
---|
31 | try: |
---|
32 | from md5 import md5 |
---|
33 | except ImportError: |
---|
34 | md5 = None |
---|
35 | def hashpath(fn): |
---|
36 | h = md5() |
---|
37 | if PY3K: |
---|
38 | h.update(fn.encode("UTF-8")) |
---|
39 | else: |
---|
40 | h.update(fn) |
---|
41 | return h.hexdigest() |
---|
42 | |
---|
43 | # Check if PIL is available (tries importing both pypi version and corrected or manually installed versions). |
---|
44 | # Necessary for JPEG and GIF support. |
---|
45 | # TODO: Pillow support |
---|
46 | try: |
---|
47 | from PIL import Image |
---|
48 | except ImportError: |
---|
49 | try: |
---|
50 | import Image |
---|
51 | except ImportError: |
---|
52 | Image = None |
---|
53 | |
---|
54 | try: |
---|
55 | from HTMLParser import HTMLParser |
---|
56 | except ImportError: |
---|
57 | from html.parser import HTMLParser |
---|
58 | |
---|
59 | if PY3K: |
---|
60 | basestring = str |
---|
61 | unicode = str |
---|
62 | ord = lambda x: x |
---|
63 | else: |
---|
64 | basestring = basestring |
---|
65 | unicode = unicode |
---|
66 | ord = ord |
---|
67 | |
---|
68 | # shortcut to bytes conversion (b prefix) |
---|
69 | def b(s): |
---|
70 | if isinstance(s, basestring): |
---|
71 | return s.encode("latin1") |
---|
72 | elif isinstance(s, int): |
---|
73 | if PY3K: |
---|
74 | return bytes([s]) # http://bugs.python.org/issue4588 |
---|
75 | else: |
---|
76 | return chr(s) |
---|
77 | |
---|
78 | def exception(): |
---|
79 | "Return the current the exception instance currently being handled" |
---|
80 | # this is needed to support Python 2.5 that lacks "as" syntax |
---|
81 | return sys.exc_info()[1] |
---|
82 | |
---|
83 | |
---|
Note: See
TracBrowser
for help on using the repository browser.