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

Unified Diff: grit/shortcuts.py

Issue 7994004: Initial source commit to grit-i18n project. (Closed) Base URL: http://grit-i18n.googlecode.com/svn/trunk/
Patch Set: Created 9 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « grit/pseudo_unittest.py ('k') | grit/shortcuts_unittests.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: grit/shortcuts.py
===================================================================
--- grit/shortcuts.py (revision 0)
+++ grit/shortcuts.py (revision 0)
@@ -0,0 +1,95 @@
+#!/usr/bin/python2.4
+# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''Stuff to prevent conflicting shortcuts.
+'''
+
+import re
+
+from grit import util
+
+
+class ShortcutGroup(object):
+ '''Manages a list of cliques that belong together in a single shortcut
+ group. Knows how to detect conflicting shortcut keys.
+ '''
+
+ # Matches shortcut keys, e.g. &J
+ SHORTCUT_RE = re.compile('([^&]|^)(&[A-Za-z])')
+
+ def __init__(self, name):
+ self.name = name
+ # Map of language codes to shortcut keys used (which is a map of
+ # shortcut keys to counts).
+ self.keys_by_lang = {}
+ # List of cliques in this group
+ self.cliques = []
+
+ def AddClique(self, c):
+ for existing_clique in self.cliques:
+ if existing_clique.GetId() == c.GetId():
+ # This happens e.g. when we have e.g.
+ # <if expr1><structure 1></if> <if expr2><structure 2></if>
+ # where only one will really be included in the output.
+ return
+
+ self.cliques.append(c)
+ for (lang, msg) in c.clique.items():
+ if lang not in self.keys_by_lang:
+ self.keys_by_lang[lang] = {}
+ keymap = self.keys_by_lang[lang]
+
+ content = msg.GetRealContent()
+ keys = [groups[1] for groups in self.SHORTCUT_RE.findall(content)]
+ for key in keys:
+ key = key.upper()
+ if key in keymap:
+ keymap[key] += 1
+ else:
+ keymap[key] = 1
+
+ def GenerateWarnings(self, tc_project):
+ # For any language that has more than one occurrence of any shortcut,
+ # make a list of the conflicting shortcuts.
+ problem_langs = {}
+ for (lang, keys) in self.keys_by_lang.items():
+ for (key, count) in keys.items():
+ if count > 1:
+ if lang not in problem_langs:
+ problem_langs[lang] = []
+ problem_langs[lang].append(key)
+
+ warnings = []
+ if len(problem_langs):
+ warnings.append("WARNING - duplicate keys exist in shortcut group %s" %
+ self.name)
+ for (lang,keys) in problem_langs.items():
+ warnings.append(" %6s duplicates: %s" % (lang, ', '.join(keys)))
+ return warnings
+
+
+def GenerateDuplicateShortcutsWarnings(uberclique, tc_project):
+ '''Given an UberClique and a project name, will print out helpful warnings
+ if there are conflicting shortcuts within shortcut groups in the provided
+ UberClique.
+
+ Args:
+ uberclique: clique.UberClique()
+ tc_project: 'MyProjectNameInTheTranslationConsole'
+
+ Returns:
+ ['warning line 1', 'warning line 2', ...]
+ '''
+ warnings = []
+ groups = {}
+ for c in uberclique.AllCliques():
+ for group in c.shortcut_groups:
+ if group not in groups:
+ groups[group] = ShortcutGroup(group)
+ groups[group].AddClique(c)
+ for group in groups.values():
+ warnings += group.GenerateWarnings(tc_project)
+ return warnings
+
Property changes on: grit/shortcuts.py
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « grit/pseudo_unittest.py ('k') | grit/shortcuts_unittests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698