summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authormazda@chromium.org <mazda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-30 15:45:03 +0000
committermazda@chromium.org <mazda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-06-30 15:45:03 +0000
commitadac276d454da01034416da7a2653e764abd5fed (patch)
tree7059204d8f3d922bbafa9b48c9f5d89bc44195bc /tools
parent81aa9e1c829dfc8058b665d7c13106b5b1bd8fcf (diff)
downloadchromium_src-adac276d454da01034416da7a2653e764abd5fed.zip
chromium_src-adac276d454da01034416da7a2653e764abd5fed.tar.gz
chromium_src-adac276d454da01034416da7a2653e764abd5fed.tar.bz2
Modify gen_keyboard_overlay_data.py to rewrite the related files.
BUG=none TEST=Ran the script and confirmed that it worked properly. Review URL: http://codereview.chromium.org/7282004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@91138 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rwxr-xr-xtools/gen_keyboard_overlay_data/gen_keyboard_overlay_data.py127
1 files changed, 88 insertions, 39 deletions
diff --git a/tools/gen_keyboard_overlay_data/gen_keyboard_overlay_data.py b/tools/gen_keyboard_overlay_data/gen_keyboard_overlay_data.py
index 457f493..91c3a14 100755
--- a/tools/gen_keyboard_overlay_data/gen_keyboard_overlay_data.py
+++ b/tools/gen_keyboard_overlay_data/gen_keyboard_overlay_data.py
@@ -8,30 +8,30 @@
This script fetches data from the keyboard layout and hotkey data spreadsheet,
and output the data depending on the option.
- --cc: Generates C++ code snippet to be inserted into
+ --cc: Rewrites a part of C++ code in
chrome/browser/chromeos/webui/keyboard_overlay_ui.cc
- --grd: Generates grd message snippet to be inserted into
+ --grd: Rewrites a part of grd messages in
chrome/app/generated_resources.grd
- --js: Generates a JavaScript file used as
+ --js: Rewrites the entire JavaScript code in
chrome/browser/resources/keyboard_overlay/keyboard_overlay_data.js
- --altgr: Generates a list of layouts to be inserted into
+ --altgr: Rewrites a list of layouts in
chrome/browser/chromeos/input_method/xkeyboard.cc
-
-These options can be specified at the same time, and the files are generated
-with corresponding extensions appended at the end of the name specified by
---out.
+These options can be specified at the same time.
e.g.
-python gen_keyboard_overlay_data.py --cc --grd --js --out=keyboard_overlay_data
+python gen_keyboard_overlay_data.py --cc --grd --js
+
+The output directory of the generated files can be changed with --outdir.
-The command above generates keyboard_overlay_data.cc,
-keyboard_overlay_data.grd, and keyboard_overlay_data.js.
+e.g. (This will generate tmp/xkeyboard.cc)
+python gen_keyboard_overlay_data.py --outdir=tmp --altgr
"""
+import cStringIO
import datetime
import gdata.spreadsheet.service
import getpass
@@ -47,6 +47,21 @@ MODIFIER_ALT = 1 << 2
KEYBOARD_GLYPH_SPREADSHEET_KEY = '0Ao3KldW9piwEdExLbGR6TmZ2RU9aUjFCMmVxWkVqVmc'
HOTKEY_SPREADSHEET_KEY = '0AqzoqbAMLyEPdE1RQXdodk1qVkFyTWtQbUxROVM1cXc'
+CC_OUTDIR = 'chrome/browser/ui/webui/chromeos'
+CC_FILENAME = 'keyboard_overlay_ui.cc'
+GRD_OUTDIR = 'chrome/app'
+GRD_FILENAME = 'generated_resources.grd'
+JS_OUTDIR = 'chrome/browser/resources'
+JS_FILENAME = 'keyboard_overlay_data.js'
+ALTGR_OUTDIR = 'chrome/browser/chromeos/input_method'
+ALTGR_FILENAME = 'xkeyboard.cc'
+CC_START = r'IDS_KEYBOARD_OVERLAY_INSTRUCTIONS_HIDE },'
+CC_END = r'};'
+GRD_START = """Escape to hide
+ </message>"""
+GRD_END = r' </if>'
+ALTGR_START = r"arrays are generated by 'gen_keyboard_overlay_data.py --altgr'"
+ALTGR_END = r'bool KeepRightAlt(const std::string& layout_name) {'
LABEL_MAP = {
'glyph_arrow_down': 'down',
@@ -103,6 +118,7 @@ const char* kCapsLockRemapped[] = {
%s
};
+
"""
def SplitBehavior(behavior):
@@ -181,8 +197,8 @@ def ParseOptions():
help='Output cc file.')
parser.add_option('--altgr', dest='altgr', default=False, action='store_true',
help='Output altgr file.')
- parser.add_option('--out', dest='out', default='keyboard_overlay_data',
- help='The output file name without extension.')
+ parser.add_option('--outdir', dest='outdir', default=None,
+ help='Specify the directory files are generated.')
(options, unused_args) = parser.parse_args()
if not options.username.endswith('google.com'):
@@ -345,9 +361,13 @@ def UniqueBehaviors(hotkey_data):
cmp=lambda x, y: cmp(ToMessageName(x), ToMessageName(y)))
-def OutputJson(keyboard_glyph_data, hotkey_data, layouts, var_name, outfile):
+def GetPath(path_from_src):
+ """Returns the absolute path of the specified path."""
+ return os.path.join(os.path.dirname(__file__), '../..', path_from_src)
+
+
+def OutputJson(keyboard_glyph_data, hotkey_data, layouts, var_name, outdir):
"""Outputs the keyboard overlay data as a JSON file."""
- print 'Generating: %s' % outfile
action_to_id = {}
for (behavior, action) in hotkey_data:
i18nContent = Toi18nContent(behavior)
@@ -355,40 +375,69 @@ def OutputJson(keyboard_glyph_data, hotkey_data, layouts, var_name, outfile):
data = {'keyboardGlyph': keyboard_glyph_data,
'shortcut': action_to_id,
'layouts': layouts}
- out = file(outfile, 'w')
+ if outdir:
+ outpath = os.path.join(outdir, JS_FILENAME)
+ else:
+ outpath = GetPath(os.path.join(JS_OUTDIR, JS_FILENAME))
+ out = file(outpath, 'w')
out.write(GenerateCopyrightHeader() + '\n')
json_data = json.dumps(data, sort_keys=True, indent=2)
# Remove redundant spaces after ','
json_data = json_data.replace(', \n', ',\n')
out.write('var %s = %s;\n' % (var_name, json_data))
-
-
-def OutputGrd(hotkey_data, outfile):
- """Outputs a snippet used for a grd file."""
- print 'Generating: %s' % outfile
+ print 'Output ' + os.path.normpath(outpath)
+
+
+def RewriteFile(start, end, original_dir, original_filename, snippet,
+ outdir=None):
+ """Replaces a part of the specified file with snippet and outputs it."""
+ original_path = GetPath(os.path.join(original_dir, original_filename))
+ original = file(original_path, 'r')
+ original_content = original.read()
+ original.close()
+ if outdir:
+ outpath = os.path.join(outdir, original_filename)
+ else:
+ outpath = original_path
+ out = file(outpath, 'w')
+ rx = re.compile(r'%s\n.*?%s\n' % (re.escape(start), re.escape(end)),
+ re.DOTALL)
+ new_content = re.sub(rx, '%s\n%s%s\n' % (start, snippet, end),
+ original_content)
+ out.write(new_content)
+ out.close()
+ print 'Output ' + os.path.normpath(outpath)
+
+
+def OutputGrd(hotkey_data, outdir):
+ """Outputs a part of messages in the grd file."""
desc = 'The text in the keyboard overlay to explain the shortcut.'
- out = file(outfile, 'w')
+ snippet = cStringIO.StringIO()
for behavior in UniqueBehaviors(hotkey_data):
- out.write(GRD_SNIPPET_TEMPLATE % (ToMessageName(behavior), desc, behavior))
+ snippet.write(GRD_SNIPPET_TEMPLATE %
+ (ToMessageName(behavior), desc, behavior))
+
+ RewriteFile(GRD_START, GRD_END, GRD_OUTDIR, GRD_FILENAME, snippet.getvalue(),
+ outdir)
-def OutputCC(hotkey_data, outfile):
- """Outputs a snippet used for C++ file."""
- print 'Generating: %s' % outfile
- out = file(outfile, 'w')
+def OutputCC(hotkey_data, outdir):
+ """Outputs a part of code in the C++ file."""
+ snippet = cStringIO.StringIO()
for behavior in UniqueBehaviors(hotkey_data):
message_name = ToMessageName(behavior)
output = CC_SNIPPET_TEMPLATE % (Toi18nContent(behavior), message_name)
# Break the line if the line is longer than 80 characters
if len(output) > 80:
output = output.replace(' ' + message_name, '\n %s' % message_name)
- out.write(output)
+ snippet.write(output)
+ RewriteFile(CC_START, CC_END, CC_OUTDIR, CC_FILENAME, snippet.getvalue(),
+ outdir)
-def OutputAltGr(keyboard_glyph_data, outfile):
- """Outputs the keyboard overlay data as a JSON file."""
- print 'Generating: %s' % outfile
+def OutputAltGr(keyboard_glyph_data, outdir):
+ """Outputs the keyboard overlay data as a JSON file."""
altgr_output = []
capslock_output = []
@@ -409,11 +458,11 @@ def OutputAltGr(keyboard_glyph_data, outfile):
capslock_output.append(' "%s",' % layout)
except KeyError:
pass
+ snippet = ALTGR_TEMPLATE % ("\n".join(altgr_output),
+ "\n".join(capslock_output))
- out = file(outfile, 'w')
- out.write(ALTGR_TEMPLATE % (
- "\n".join(altgr_output), "\n".join(capslock_output)))
-
+ RewriteFile(ALTGR_START, ALTGR_END, ALTGR_OUTDIR, ALTGR_FILENAME, snippet,
+ outdir)
def main():
@@ -427,13 +476,13 @@ def main():
if options.js:
layouts = FetchLayoutsData(client)
OutputJson(keyboard_glyph_data, hotkey_data, layouts, 'keyboardOverlayData',
- options.out + '.js')
+ options.outdir)
if options.grd:
- OutputGrd(hotkey_data, options.out + '.grd')
+ OutputGrd(hotkey_data, options.outdir)
if options.cc:
- OutputCC(hotkey_data, options.out + '.cc')
+ OutputCC(hotkey_data, options.outdir)
if options.altgr:
- OutputAltGr(keyboard_glyph_data, options.out + '.altgr')
+ OutputAltGr(keyboard_glyph_data, options.outdir)
if __name__ == '__main__':