def __init__(self): choices = { "(last | past) <day>": "last day", "(next | this) <day>": "next day", "last week <day>": "last week", "next week <day>": "next week", } extras = [Day("day")] Choice.__init__(self, name=None, choices=choices, extras=extras)
def __init__(self): choices = { "<n> days ago": -1, "yesterday": -1, "today": 0, "tomorrow": +1, "in <n> days": +1, } extras = [IntegerRef("n", 1, 100)] Choice.__init__(self, name=None, choices=choices, extras=extras)
def value(self, node): value = Choice.value(self, node) n = node.get_child_by_name("n") print("November:", n) if n is not None: value = value * n.value() return date.today() + timedelta(days=value)
def value(self, node): value = Choice.value(self, node) day = node.get_child_by_name("day").value() now = date.today().weekday() print(value, day, now) if value == "last day": if day < now: day_offset = -now + day else: day_offset = -7 - now + day elif value == "next day": if day < now: day_offset = 7 - now + day else: day_offset = day - now elif value == "last week": day_offset = -now - 7 + day elif value == "next week": day_offset = -now + 7 + day return date.today() + timedelta(days=day_offset)
def get_giant_choice(name): choices = {} for word in get_500_words(): choices[word] = get_random_int() return Choice(name, choices)
def __init__(self, name): Choice.__init__(self, name=name, choices=day_names)
def __init__(self, name): Choice.__init__(self, name=name, choices=month_names)
def spec_override_from_config(mp): '''run at boot time only: changes are permanent''' if mp.time != MergeInf.BOOT_NO_MERGE: # 3 == MergeInf.BOOT return '''redundant safety check''' if DEFS is None: return for rule in [mp.rule1, mp.rule2]: if rule is not None: '''SPECS''' specs_changed = False for spec in rule.mapping_actual().keys(): action = rule.mapping_actual()[spec] pspec = PreservedSpec.preserve(spec) for original in DEFS.specs.keys(): if original in pspec.altered: new = DEFS.specs[original] pspec.altered = pspec.altered.replace(original, new) pspec.altered = PreservedSpec.restore(pspec) if spec == pspec.altered: continue del rule.mapping_actual()[spec] rule.mapping_actual()[pspec.altered] = action specs_changed = True '''EXTRAS''' extras = rule.extras_copy().values() extras_changed = False if len(extras) > 0: replacements = {} for extra in extras: if isinstance( extra, Choice ): # IntegerRefSTs will be dealt with elsewhere choices = extra._choices replace = False for s in choices.keys( ): #ex: "dunce make" is key, some int or whatever is the value for ns in DEFS.extras.keys( ): #ex: "dunce" is key, "down" is the value if ns in s: # ex: "dunce" is in "dunce make" replace = True val = choices[s] del choices[s] s = s.replace(ns, DEFS.extras[ns]) choices[s] = val if replace: new_choice = Choice(extra.name, choices) replacements[extra] = new_choice for old_choice in replacements: new_choice = replacements[old_choice] extras.remove(old_choice) extras.append(new_choice) if len(replacements) > 0: extras_changed = True '''DEFAULTS''' defaults = rule.defaults_copy() defaults_changed = False if len(defaults) > 0: replacements = {} for default_key in defaults.keys(): # value = defaults[default_key] if isinstance(value, basestring): '''only replace strings; also, only replace values, not keys: default_key should not be changed - it will never be spoken''' nvalue = value # new value replace = False for old in DEFS.defaults.keys( ): # 'old' is the target word(s) in the old 'value' new = DEFS.defaults[old] if old in nvalue: nvalue = nvalue.replace(old, new) replace = True if replace: defaults[default_key] = nvalue defaults_changed = True if specs_changed or extras_changed or defaults_changed: rule.__init__(rule._name, rule.mapping_actual(), extras, defaults, rule._exported, rule.ID, rule.composite, rule.compatible, rule._mcontext, rule._mwith)
class EclipseRule(MergeRule): pronunciation = "eclipse" mapping = { "prior tab [<n>]": R(Key("cs-f6"), rdescript="Eclipse: Previous Tab") * Repeat(extra="n"), # these two must be set up in the eclipse preferences "next tab [<n>]": R(Key("c-f6"), rdescript="Eclipse: Next Tab") * Repeat(extra="n"), "open resource": R(Key("cs-r"), rdescript="Eclipse: Open Resource"), "open type": R(Key("cs-t"), rdescript="Eclipse: Open Type"), "jump to source": R(Key("f3"), rdescript="Eclipse: Jump To Source"), "editor select": R(Key("c-e"), rdescript="Eclipse: Editor Select"), "step over [<n>]": R(Key("f6/50") * Repeat(extra="n"), rdescript="Eclipse: Step Over"), "step into": R(Key("f5"), rdescript="Eclipse: Step Into"), "step out [of]": R(Key("f7"), rdescript="Eclipse: Step Out"), "resume": R(Key("f8"), rdescript="Eclipse: Resume"), "(debug | run) last": R(Key("f11"), rdescript="Eclipse: Run Last"), "mark occurrences": R(Key("as-o"), rdescript="Eclipse: Mark Occurrences"), # "terminate" changes to the settings for this hotkey: (when: in dialogs and windows) "terminate": R(Key("c-f2"), rdescript="Eclipse: Terminate Running Program"), "refractor symbol": R(Key("sa-r"), rdescript="Eclipse: Re-Factor Symbol"), "symbol next [<n>]": R(Key("c-k"), rdescript="Eclipse: Symbol Next") * Repeat(extra="n"), "symbol prior [<n>]": R(Key("cs-k"), rdescript="Eclipse: Symbol Prior") * Repeat(extra="n"), "format code": R(Key("cs-f"), rdescript="Eclipse: Format Code"), "do imports": R(Key("cs-o"), rdescript="Eclipse: Do Imports"), "comment line": R(Key("c-slash"), rdescript="Eclipse: Comment Line"), "build it": R(Key("c-b"), rdescript="Eclipse: Build"), "split view horizontal": R(Key("cs-underscore"), rdescript="Eclipse: Split View (H)"), "split view vertical": R(Key("cs-lbrace"), rdescript="Eclipse: Split View (V)"), #Line Ops "find everywhere": R(Key("ca-g"), rdescript="Eclipse: Search Project"), "find word <text> [<back>] [<go>]": R(Key("c-f")+Function(ec_con.regex_off)+Function(ec_con.find),\ rdescript="Eclipse: Find Word"), "find regex <text> [<back>] [<go>]": R(Key("c-f")+Function(ec_con.regex_on)+Function(ec_con.find),\ rdescript="Eclipse: Find Regex"), "find <a> [<b> [<c>]] [<back>] [<go>]": R(Key("c-f")+Function(ec_con.find),\ rdescript="Eclipse: Find Alpha"), "find <punctuation> [<back>] [<go>]": R(Key("c-f")+Function(ec_con.find),\ rdescript="Eclipse: Find Character(s)"), } extras = [ Dictation("text"), Dictation("mim"), IntegerRefST("n", 1, 3000), alphanumeric.get_alphabet_choice("a"), alphanumeric.get_alphabet_choice("b"), alphanumeric.get_alphabet_choice("c"), Choice("punctuation", {"hash tag": "#"}), Boolean("back"), Boolean("go"), ] defaults = { "n": 1, "mim": "", "a": None, "b": None, "c": None, "punctuation": None, "back": False, "go": False }
def __init__(self, spec): Choice.__init__(self, spec, {spec: True})
def _spec_override_from_config(rule, definitions): '''redundant safety check''' if len(definitions) == 0: return rule '''SPECS''' mapping = rule._mapping.copy() specs_changed = False for spec in list(mapping.keys()): action = mapping[spec] extra_analyses = _analyze_extras(spec) new_spec = spec for token_to_replace in definitions.specs.keys(): if token_to_replace in new_spec: replacement = definitions.specs[token_to_replace] new_spec = new_spec.replace(token_to_replace, replacement) if spec == new_spec: continue ''' We want to protect the user from: - removing required extras from specs - accidentally renaming required extras But we still want the user to be able to: - move required extras - remove optional extras ''' violation = _detect_illegal_spec_alteration(extra_analyses, new_spec) if violation is not None: printer.out("Illegal spec modification: cannot alter required extras. " + "Cannot modify <{}> in \"{}\".".format(violation.name, spec)) continue del mapping[spec] mapping[new_spec] = action specs_changed = True '''EXTRAS''' extras_list = list(rule._extras.values()) resulting_extras_list = list(extras_list) extras_changed = False for extra in extras_list: # only choices; no need to bother with dictation or integers if isinstance(extra, Choice): choices_dict_copy = extra._choices.copy() # operate on this copy of the choices dict choices_dict_copy_keys = set(choices_dict_copy.keys()) replaced_a_choice_key = False for choices_key in choices_dict_copy_keys: # ex: "dunce make" = key, something else = value for replaceable_text in definitions.extras.keys(): # ex: "dunce" is key, "down" is the value if replaceable_text in choices_key: # ex: "dunce" is in "dunce make" replaced_a_choice_key = True value = choices_dict_copy[choices_key] del choices_dict_copy[choices_key] replacement = definitions.extras[replaceable_text] choices_key = choices_key.replace(replaceable_text, replacement) choices_dict_copy[choices_key] = value if replaced_a_choice_key: extras_changed = True new_choice = Choice(extra.name, choices_dict_copy) resulting_extras_list.remove(extra) resulting_extras_list.append(new_choice) '''DEFAULTS''' defaults = rule._defaults.copy() defaults_changed = False if len(defaults) > 0: for default_key in list(defaults.keys()): # value = defaults[default_key] if isinstance(value, six.string_types): '''only replace strings; also, only replace values, not keys: default_key should not be changed - it will never be spoken''' nvalue = value # new value replaced_a_choice_key = False for old in definitions.defaults.keys(): # 'old' is the target word(s) in the old 'value' new = definitions.defaults[old] if old in nvalue: nvalue = nvalue.replace(old, new) replaced_a_choice_key = True if replaced_a_choice_key: defaults[default_key] = nvalue defaults_changed = True if specs_changed or extras_changed or defaults_changed: # rule_class = rule.__class__ rule.__init__(name=rule.name, mapping=mapping, extras=resulting_extras_list, defaults=defaults) return rule
def _spec_override_from_config(rule, definitions): '''redundant safety check''' if len(definitions) == 0: return rule '''SPECS''' mapping = rule._mapping.copy() specs_changed = False for spec in list(mapping.keys()): action = mapping[spec] pspec = _preserve(spec) for original in definitions.specs.keys(): if original in pspec.altered: new = definitions.specs[original] pspec.altered = pspec.altered.replace(original, new) pspec.altered = _restore(pspec) if spec == pspec.altered: continue del mapping[spec] mapping[pspec.altered] = action specs_changed = True '''EXTRAS''' extras_list = list(rule._extras.values()) resulting_extras_list = list(extras_list) extras_changed = False for extra in extras_list: # only choices; no need to bother with dictation or integers if isinstance(extra, Choice): choices_dict_copy = extra._choices.copy( ) # operate on this copy of the choices dict choices_dict_copy_keys = set(choices_dict_copy.keys()) replaced_a_choice_key = False for choices_key in choices_dict_copy_keys: # ex: "dunce make" = key, something else = value for replaceable_text in definitions.extras.keys( ): # ex: "dunce" is key, "down" is the value if replaceable_text in choices_key: # ex: "dunce" is in "dunce make" replaced_a_choice_key = True value = choices_dict_copy[choices_key] del choices_dict_copy[choices_key] replacement = definitions.extras[replaceable_text] choices_key = choices_key.replace( replaceable_text, replacement) choices_dict_copy[choices_key] = value if replaced_a_choice_key: extras_changed = True new_choice = Choice(extra.name, choices_dict_copy) resulting_extras_list.remove(extra) resulting_extras_list.append(new_choice) '''DEFAULTS''' defaults = rule._defaults.copy() defaults_changed = False if len(defaults) > 0: for default_key in list(defaults.keys()): # value = defaults[default_key] if isinstance(value, six.string_types): '''only replace strings; also, only replace values, not keys: default_key should not be changed - it will never be spoken''' nvalue = value # new value replaced_a_choice_key = False for old in definitions.defaults.keys( ): # 'old' is the target word(s) in the old 'value' new = definitions.defaults[old] if old in nvalue: nvalue = nvalue.replace(old, new) replaced_a_choice_key = True if replaced_a_choice_key: defaults[default_key] = nvalue defaults_changed = True if specs_changed or extras_changed or defaults_changed: # rule_class = rule.__class__ rule.__init__(name=rule.name, mapping=mapping, extras=resulting_extras_list, defaults=defaults) return rule