Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: grit/util.py

Issue 9924009: Add c_format as a format option. (Closed) Base URL: https://grit-i18n.googlecode.com/svn/trunk
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « grit/tool/build.py ('k') | grit/util_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 sys 9 import sys
10 import os.path 10 import os.path
11 import codecs 11 import codecs
12 import htmlentitydefs 12 import htmlentitydefs
13 import re 13 import re
14 import time 14 import time
15 from xml.sax import saxutils 15 from xml.sax import saxutils
16 16
17 from grit import lazy_re 17 from grit import lazy_re
18 18
19 _root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..')) 19 _root_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
20 20
21 21
22 # Matches all different types of linebreaks.
23 LINEBREAKS = re.compile('\r\n|\n|\r')
24
25 def MakeRelativePath(base_path, path_to_make_relative):
26 """Returns a relative path such from the base_path to
27 the path_to_make_relative.
28
29 In other words, os.join(base_path,
30 MakeRelativePath(base_path, path_to_make_relative))
31 is the same location as path_to_make_relative.
32
33 Args:
34 base_path: the root path
35 path_to_make_relative: an absolute path that is on the same drive
36 as base_path
37 """
38
39 def _GetPathAfterPrefix(prefix_path, path_with_prefix):
40 """Gets the subpath within in prefix_path for the path_with_prefix
41 with no beginning or trailing path separators.
42
43 Args:
44 prefix_path: the base path
45 path_with_prefix: a path that starts with prefix_path
46 """
47 assert path_with_prefix.startswith(prefix_path)
48 path_without_prefix = path_with_prefix[len(prefix_path):]
49 normalized_path = os.path.normpath(path_without_prefix.strip(os.path.sep))
50 if normalized_path == '.':
51 normalized_path = ''
52 return normalized_path
53
54 def _GetCommonBaseDirectory(*args):
55 """Returns the common prefix directory for the given paths
56
57 Args:
58 The list of paths (at least one of which should be a directory)
59 """
60 prefix = os.path.commonprefix(args)
61 # prefix is a character-by-character prefix (i.e. it does not end
62 # on a directory bound, so this code fixes that)
63
64 # if the prefix ends with the separator, then it is prefect.
65 if len(prefix) > 0 and prefix[-1] == os.path.sep:
66 return prefix
67
68 # We need to loop through all paths or else we can get
69 # tripped up by "c:\a" and "c:\abc". The common prefix
70 # is "c:\a" which is a directory and looks good with
71 # respect to the first directory but it is clear that
72 # isn't a common directory when the second path is
73 # examined.
74 for path in args:
75 assert len(path) >= len(prefix)
76 # If the prefix the same length as the path,
77 # then the prefix must be a directory (since one
78 # of the arguements should be a directory).
79 if path == prefix:
80 continue
81 # if the character after the prefix in the path
82 # is the separator, then the prefix appears to be a
83 # valid a directory as well for the given path
84 if path[len(prefix)] == os.path.sep:
85 continue
86 # Otherwise, the prefix is not a directory, so it needs
87 # to be shortened to be one
88 index_sep = prefix.rfind(os.path.sep)
89 # The use "index_sep + 1" because it includes the final sep
90 # and it handles the case when the index_sep is -1 as well
91 prefix = prefix[:index_sep + 1]
92 # At this point we backed up to a directory bound which is
93 # common to all paths, so we can quit going through all of
94 # the paths.
95 break
96 return prefix
97
98 prefix = _GetCommonBaseDirectory(base_path, path_to_make_relative)
99 # If the paths had no commonality at all, then return the absolute path
100 # because it is the best that can be done. If the path had to be relative
101 # then eventually this absolute path will be discovered (when a build breaks)
102 # and an appropriate fix can be made, but having this allows for the best
103 # backward compatibility with the absolute path behavior in the past.
104 if len(prefix) <= 0:
105 return path_to_make_relative
106 # Build a path from the base dir to the common prefix
107 remaining_base_path = _GetPathAfterPrefix(prefix, base_path)
108
109 # The follow handles two case: "" and "foo\\bar"
110 path_pieces = remaining_base_path.split(os.path.sep)
111 base_depth_from_prefix = len([d for d in path_pieces if len(d)])
112 base_to_prefix = (".." + os.path.sep) * base_depth_from_prefix
113
114 # Put add in the path from the prefix to the path_to_make_relative
115 remaining_other_path = _GetPathAfterPrefix(prefix, path_to_make_relative)
116 return base_to_prefix + remaining_other_path
117
118
22 # Matches all of the resource IDs predefined by Windows. 119 # Matches all of the resource IDs predefined by Windows.
23 # The '\b' before and after each word makes sure these match only whole words an d 120 # The '\b' before and after each word makes sure these match only whole words an d
24 # not the beginning of any word.. eg. ID_FILE_NEW will not match ID_FILE_NEW_PRO JECT 121 # not the beginning of any word.. eg. ID_FILE_NEW will not match ID_FILE_NEW_PRO JECT
25 # see http://www.amk.ca/python/howto/regex/ (search for "\bclass\b" inside the h tml page) 122 # see http://www.amk.ca/python/howto/regex/ (search for "\bclass\b" inside the h tml page)
26 SYSTEM_IDENTIFIERS = lazy_re.compile( 123 SYSTEM_IDENTIFIERS = lazy_re.compile(
27 r'''\bIDOK\b | \bIDCANCEL\b | \bIDC_STATIC\b | \bIDYES\b | \bIDNO\b | 124 r'''\bIDOK\b | \bIDCANCEL\b | \bIDC_STATIC\b | \bIDYES\b | \bIDNO\b |
28 \bID_FILE_NEW\b | \bID_FILE_OPEN\b | \bID_FILE_CLOSE\b | \bID_FILE_SAVE\b | 125 \bID_FILE_NEW\b | \bID_FILE_OPEN\b | \bID_FILE_CLOSE\b | \bID_FILE_SAVE\b |
29 \bID_FILE_SAVE_AS\b | \bID_FILE_PAGE_SETUP\b | \bID_FILE_PRINT_SETUP\b | 126 \bID_FILE_SAVE_AS\b | \bID_FILE_PAGE_SETUP\b | \bID_FILE_PRINT_SETUP\b |
30 \bID_FILE_PRINT\b | \bID_FILE_PRINT_DIRECT\b | \bID_FILE_PRINT_PREVIEW\b | 127 \bID_FILE_PRINT\b | \bID_FILE_PRINT_DIRECT\b | \bID_FILE_PRINT_PREVIEW\b |
31 \bID_FILE_UPDATE\b | \bID_FILE_SAVE_COPY_AS\b | \bID_FILE_SEND_MAIL\b | 128 \bID_FILE_UPDATE\b | \bID_FILE_SAVE_COPY_AS\b | \bID_FILE_SEND_MAIL\b |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 def IsVerbose(): 401 def IsVerbose():
305 return verbose 402 return verbose
306 403
307 def IsExtraVerbose(): 404 def IsExtraVerbose():
308 return extra_verbose 405 return extra_verbose
309 406
310 def GetCurrentYear(): 407 def GetCurrentYear():
311 '''Returns the current 4-digit year as an integer.''' 408 '''Returns the current 4-digit year as an integer.'''
312 return time.localtime()[0] 409 return time.localtime()[0]
313 410
OLDNEW
« no previous file with comments | « grit/tool/build.py ('k') | grit/util_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698