OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 '''Stuff to prevent conflicting shortcuts. |
| 7 ''' |
| 8 |
| 9 import re |
| 10 |
| 11 from grit import util |
| 12 |
| 13 |
| 14 class ShortcutGroup(object): |
| 15 '''Manages a list of cliques that belong together in a single shortcut |
| 16 group. Knows how to detect conflicting shortcut keys. |
| 17 ''' |
| 18 |
| 19 # Matches shortcut keys, e.g. &J |
| 20 SHORTCUT_RE = re.compile('([^&]|^)(&[A-Za-z])') |
| 21 |
| 22 def __init__(self, name): |
| 23 self.name = name |
| 24 # Map of language codes to shortcut keys used (which is a map of |
| 25 # shortcut keys to counts). |
| 26 self.keys_by_lang = {} |
| 27 # List of cliques in this group |
| 28 self.cliques = [] |
| 29 |
| 30 def AddClique(self, c): |
| 31 for existing_clique in self.cliques: |
| 32 if existing_clique.GetId() == c.GetId(): |
| 33 # This happens e.g. when we have e.g. |
| 34 # <if expr1><structure 1></if> <if expr2><structure 2></if> |
| 35 # where only one will really be included in the output. |
| 36 return |
| 37 |
| 38 self.cliques.append(c) |
| 39 for (lang, msg) in c.clique.items(): |
| 40 if lang not in self.keys_by_lang: |
| 41 self.keys_by_lang[lang] = {} |
| 42 keymap = self.keys_by_lang[lang] |
| 43 |
| 44 content = msg.GetRealContent() |
| 45 keys = [groups[1] for groups in self.SHORTCUT_RE.findall(content)] |
| 46 for key in keys: |
| 47 key = key.upper() |
| 48 if key in keymap: |
| 49 keymap[key] += 1 |
| 50 else: |
| 51 keymap[key] = 1 |
| 52 |
| 53 def GenerateWarnings(self, tc_project): |
| 54 # For any language that has more than one occurrence of any shortcut, |
| 55 # make a list of the conflicting shortcuts. |
| 56 problem_langs = {} |
| 57 for (lang, keys) in self.keys_by_lang.items(): |
| 58 for (key, count) in keys.items(): |
| 59 if count > 1: |
| 60 if lang not in problem_langs: |
| 61 problem_langs[lang] = [] |
| 62 problem_langs[lang].append(key) |
| 63 |
| 64 warnings = [] |
| 65 if len(problem_langs): |
| 66 warnings.append("WARNING - duplicate keys exist in shortcut group %s" % |
| 67 self.name) |
| 68 for (lang,keys) in problem_langs.items(): |
| 69 warnings.append(" %6s duplicates: %s" % (lang, ', '.join(keys))) |
| 70 return warnings |
| 71 |
| 72 |
| 73 def GenerateDuplicateShortcutsWarnings(uberclique, tc_project): |
| 74 '''Given an UberClique and a project name, will print out helpful warnings |
| 75 if there are conflicting shortcuts within shortcut groups in the provided |
| 76 UberClique. |
| 77 |
| 78 Args: |
| 79 uberclique: clique.UberClique() |
| 80 tc_project: 'MyProjectNameInTheTranslationConsole' |
| 81 |
| 82 Returns: |
| 83 ['warning line 1', 'warning line 2', ...] |
| 84 ''' |
| 85 warnings = [] |
| 86 groups = {} |
| 87 for c in uberclique.AllCliques(): |
| 88 for group in c.shortcut_groups: |
| 89 if group not in groups: |
| 90 groups[group] = ShortcutGroup(group) |
| 91 groups[group].AddClique(c) |
| 92 for group in groups.values(): |
| 93 warnings += group.GenerateWarnings(tc_project) |
| 94 return warnings |
| 95 |
OLD | NEW |