OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Utilities used by GRIT. | 6 '''Utilities used by GRIT. |
7 ''' | 7 ''' |
8 | 8 |
9 import codecs | 9 import codecs |
10 import htmlentitydefs | 10 import htmlentitydefs |
11 import os | 11 import os |
12 import re | 12 import re |
13 import shutil | 13 import shutil |
14 import sys | 14 import sys |
15 import tempfile | 15 import tempfile |
16 import time | 16 import time |
17 import types | 17 import types |
18 from xml.sax import saxutils | 18 from xml.sax import saxutils |
19 | 19 |
20 from grit import lazy_re | 20 from grit import lazy_re |
21 | 21 |
22 _root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | 22 _root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
23 | 23 |
| 24 _replacement_paths = {} |
| 25 |
24 | 26 |
25 # Unique constants for use by ReadFile(). | 27 # Unique constants for use by ReadFile(). |
26 BINARY, RAW_TEXT = range(2) | 28 BINARY, RAW_TEXT = range(2) |
27 | 29 |
28 | 30 |
29 # Unique constants representing data pack encodings. | 31 # Unique constants representing data pack encodings. |
30 _, UTF8, UTF16 = range(3) | 32 _, UTF8, UTF16 = range(3) |
31 | 33 |
32 | 34 |
33 def Encode(message, encoding): | 35 def Encode(message, encoding): |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 | 198 |
197 def ReadFile(filename, encoding): | 199 def ReadFile(filename, encoding): |
198 '''Reads and returns the entire contents of the given file. | 200 '''Reads and returns the entire contents of the given file. |
199 | 201 |
200 Args: | 202 Args: |
201 filename: The path to the file. | 203 filename: The path to the file. |
202 encoding: A Python codec name or one of two special values: BINARY to read | 204 encoding: A Python codec name or one of two special values: BINARY to read |
203 the file in binary mode, or RAW_TEXT to read it with newline | 205 the file in binary mode, or RAW_TEXT to read it with newline |
204 conversion but without decoding to Unicode. | 206 conversion but without decoding to Unicode. |
205 ''' | 207 ''' |
| 208 path = os.path.abspath(filename) |
| 209 replacement_path = _replacement_paths.get(path, path) |
206 mode = 'rb' if encoding == BINARY else 'rU' | 210 mode = 'rb' if encoding == BINARY else 'rU' |
207 with open(filename, mode) as f: | 211 with open(replacement_path, mode) as f: |
208 data = f.read() | 212 data = f.read() |
209 if encoding not in (BINARY, RAW_TEXT): | 213 if encoding not in (BINARY, RAW_TEXT): |
210 data = data.decode(encoding) | 214 data = data.decode(encoding) |
211 return data | 215 return data |
212 | 216 |
213 | 217 |
214 def WrapOutputStream(stream, encoding = 'utf-8'): | 218 def WrapOutputStream(stream, encoding = 'utf-8'): |
215 '''Returns a stream that wraps the provided stream, making it write | 219 '''Returns a stream that wraps the provided stream, making it write |
216 characters using the specified encoding.''' | 220 characters using the specified encoding.''' |
217 return codecs.getwriter(encoding)(stream) | 221 return codecs.getwriter(encoding)(stream) |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 return self._AsCurrentDirClass(self.GetPath()) | 657 return self._AsCurrentDirClass(self.GetPath()) |
654 | 658 |
655 class _AsCurrentDirClass(object): | 659 class _AsCurrentDirClass(object): |
656 def __init__(self, path): | 660 def __init__(self, path): |
657 self.path = path | 661 self.path = path |
658 def __enter__(self): | 662 def __enter__(self): |
659 self.oldpath = os.getcwd() | 663 self.oldpath = os.getcwd() |
660 os.chdir(self.path) | 664 os.chdir(self.path) |
661 def __exit__(self, *exc_info): | 665 def __exit__(self, *exc_info): |
662 os.chdir(self.oldpath) | 666 os.chdir(self.oldpath) |
| 667 |
| 668 def SetReplacementPaths(replacement_paths): |
| 669 global _replacement_paths |
| 670 _replacement_paths = replacement_paths |
OLD | NEW |