Mercurial > babel > old > babel-test
comparison babel/util.py @ 167:06af84d082a3 stable-0.8.x
Ported [165], [167], and [169] to 0.8.x branch.
author | cmlenz |
---|---|
date | Fri, 22 Jun 2007 11:13:10 +0000 |
parents | 60565dc8495d |
children |
comparison
equal
deleted
inserted
replaced
159:d8b5fb8af7ad | 167:06af84d082a3 |
---|---|
11 # individuals. For the exact contribution history, see the revision | 11 # individuals. For the exact contribution history, see the revision |
12 # history and logs, available at http://babel.edgewall.org/log/. | 12 # history and logs, available at http://babel.edgewall.org/log/. |
13 | 13 |
14 """Various utility classes and functions.""" | 14 """Various utility classes and functions.""" |
15 | 15 |
16 import codecs | |
16 from datetime import timedelta, tzinfo | 17 from datetime import timedelta, tzinfo |
17 import os | 18 import os |
19 import parser | |
18 import re | 20 import re |
19 import time | 21 import time |
20 | 22 |
21 __all__ = ['pathmatch', 'relpath', 'UTC', 'LOCALTZ'] | 23 __all__ = ['pathmatch', 'relpath', 'UTC', 'LOCALTZ'] |
22 __docformat__ = 'restructuredtext en' | 24 __docformat__ = 'restructuredtext en' |
25 | |
26 # Regexp to match python magic encoding line | |
27 PYTHON_MAGIC_COMMENT_re = re.compile( | |
28 r'[ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)', re.VERBOSE) | |
29 def parse_encoding(fp): | |
30 """Deduce the encoding of a source file from magic comment. | |
31 | |
32 It does this in the same way as the `Python interpreter`__ | |
33 | |
34 .. __: http://docs.python.org/ref/encodings.html | |
35 | |
36 The ``fp`` argument should be a seekable file object. | |
37 | |
38 (From Jeff Dairiki) | |
39 """ | |
40 pos = fp.tell() | |
41 fp.seek(0) | |
42 try: | |
43 line1 = fp.readline() | |
44 has_bom = line1.startswith(codecs.BOM_UTF8) | |
45 if has_bom: | |
46 line1 = line1[len(codecs.BOM_UTF8):] | |
47 | |
48 m = PYTHON_MAGIC_COMMENT_re.match(line1) | |
49 if not m: | |
50 try: | |
51 parser.suite(line1) | |
52 except SyntaxError: | |
53 # Either it's a real syntax error, in which case the source is | |
54 # not valid python source, or line2 is a continuation of line1, | |
55 # in which case we don't want to scan line2 for a magic | |
56 # comment. | |
57 pass | |
58 else: | |
59 line2 = fp.readline() | |
60 m = PYTHON_MAGIC_COMMENT_re.match(line2) | |
61 | |
62 if has_bom: | |
63 if m: | |
64 raise SyntaxError( | |
65 "python refuses to compile code with both a UTF8 " | |
66 "byte-order-mark and a magic encoding comment") | |
67 return 'utf_8' | |
68 elif m: | |
69 return m.group(1) | |
70 else: | |
71 return None | |
72 finally: | |
73 fp.seek(pos) | |
23 | 74 |
24 def pathmatch(pattern, filename): | 75 def pathmatch(pattern, filename): |
25 """Extended pathname pattern matching. | 76 """Extended pathname pattern matching. |
26 | 77 |
27 This function is similar to what is provided by the ``fnmatch`` module in | 78 This function is similar to what is provided by the ``fnmatch`` module in |