-
Notifications
You must be signed in to change notification settings - Fork 3
/
_text_manipulation.py
91 lines (61 loc) · 2.52 KB
/
_text_manipulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# This file is a command-module for Dragonfly.
# (c) Copyright 2016 by Hawkeye Parker
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
"""
Command-module for manipulating text
=====================================================
This command-module offers commands for manipulating text.
Commands
--------
The following voice commands are available:
Command: **"upper that"**
Converst the selected text to upper case
Usage examples
--------------
- Say **"upper that"** to convert the selected text to upper case.
"""
# import logging
from dragonfly.windows.clipboard import Clipboard
from dragonfly import Config, Section, Item, Grammar, CompoundRule, Key
from supporting import utils
# rule_log = logging.getLogger("rule")
# ---------------------------------------------------------------------------
# Set up this module's configuration.
config = Config("Text Manipulation")
config.tweak = Section("Tweak section")
config.tweak.upper = Item("upper that", doc="Command to convert the selected text to uppercase.")
config.tweak.lower = Item("lower that", doc="Command to convert the selected text to lower case.")
# config.generate_config_file()
config.load()
# ===========================================================================
# Create this module's main grammar object.
text_manipulation_grammar = Grammar("text manipulation")
class UpperRule(CompoundRule):
spec = config.tweak.upper
def _process_recognition(self, node, extras):
copy_modify_paste(lambda s: s.upper())
text_manipulation_grammar.add_rule(UpperRule())
class LowerRule(CompoundRule):
spec = config.tweak.lower
def _process_recognition(self, node, extras):
copy_modify_paste(lambda s: s.lower())
text_manipulation_grammar.add_rule(LowerRule())
# ---------------------------------------------------------------------------
def copy_modify_paste(modifying_function):
selected_text = utils.getSelectedText()
if not selected_text:
print "No selected text?"
return
modified_text = modifying_function(str(selected_text))
# tried to use the original clipboard here, but couldn't get it to "clear" -- some apps would
# somehow get the original, unmodified text when the paste happened
new_clipboard = Clipboard()
new_clipboard.set_text(modified_text)
new_clipboard.copy_to_system()
Key("c-v").execute()
text_manipulation_grammar.load()
def unload():
global text_manipulation_grammar
text_manipulation_grammar = utils.unloadHelper(text_manipulation_grammar, __name__)