/
_bringme.py
197 lines (155 loc) · 6.05 KB
/
_bringme.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
#
# This file is a command-module for Dragonfly.
# (c) Copyright 2008 by Christo Butcher
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
"""
Command-module for direct opening of **user-defined targets**
============================================================================
This module offers very easy and direct opening of user-
defined targets. User define their own targets in the
configuration file, and then have instant access to them
by saying "bring me <target>". The configuration file
format is described below.
Installation
----------------------------------------------------------------------------
If you are using DNS and Natlink, simply place this file in you Natlink
macros directory. It will then be automatically loaded by Natlink when
you next toggle your microphone or restart Natlink.
Commands
----------------------------------------------------------------------------
Command: **"bring me <target>"**
Open the specified target.
The *<target>* extra in this rule can be any one
of the targets defined in this module's configuration file.
Command: **"paste me <target>"**
Paste the address of the specified target.
The *<target>* extra is the same as for the bring me command.
Customization
----------------------------------------------------------------------------
Users should customize this module by editing its configuration
file. In this file they should fill the ``targets.mapping``
with their own personal targets. This target mapping maps *what
you say* to *which target to open*.
For example:
.. code-block:: python
targets.mapping = {
"[my] favorite website": website("http://code.google.com/p/dragonfly"),
"my local folder": folder(r"C:\"),
}
(Note the format of each target: ``"...": type("..."),``
And note the use of ``r"..."`` when specifying a path
containing backslash characters; the *r* in front of the
open-quote means that backslashes can be given literally
and *don't* need to be doubled.)
Using the configuration above would allow the user to say:
- **"bring me my favorite website"** and the Dragonfly homepage
will be opened.
- **"bring me my local folder"** and a Windows Explorer
will be opened showing the local ``C:\`` folder.
"""
try:
import pkg_resources
pkg_resources.require("dragonfly >= 0.6.5beta1.dev-r76")
except ImportError:
pass
import os
import os.path
import webbrowser
import subprocess
from dragonfly import (Grammar, Choice, CompoundRule,
Paste, Config, Section, Item)
#---------------------------------------------------------------------------
# Bringable classes.
class BringableBase(object):
def __init__(self, target):
self.target = target
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.target)
__str__ = __repr__
def bring_it(self):
pass
class website(BringableBase):
def bring_it(self):
target = self.target
subprocess.Popen(["explorer", target])
# subprocess.Popen([r"C:\Program Files\Mozilla Firefox\firefox.exe", self.target])
# webbrowser.open(self.target)
class folder(BringableBase):
def bring_it(self):
target = self.target
subprocess.Popen(["explorer", target])
class open(BringableBase):
def __init__(self, target, verb="open"):
self.verb = verb
BringableBase.__init__(self, target)
def bring_it(self):
target = self.target
os.startfile(target, self.verb)
class ssh(BringableBase):
putty_path = r"C:\Program Files\PuTTY\putty"
def bring_it(self):
subprocess.Popen([self.putty_path, "-load", self.target])
#---------------------------------------------------------------------------
# Set up this module's configuration.
config = Config("bring me")
config.targets = Section("Targets section")
config.targets.mapping = Item(
default={
"my site": website("http://www.google.com"),
},
doc="Mapping of spoken targets to bringable targets.",
namespace={
"website": website,
"open": open,
"folder": folder,
"ssh": ssh,
},
)
config.lang = Section("Language section")
config.lang.bring_me = Item("bring me <target>",
doc="Command to bring a target;"
" must contain the <target> extra.")
config.lang.paste_me = Item("paste me <target>",
doc="Command to paste the location of a target;"
" must contain the <target> extra.")
config.load()
#---------------------------------------------------------------------------
# Bring rule.
class BringRule(CompoundRule):
# lang.bring_me = "bring me <target>"
spec = config.lang.bring_me
# targets.mapping = {
extras = [Choice("target", config.targets.mapping)]
def _process_recognition(self, node, extras):
target = extras["target"]
self._log.debug("%s: bringing target %s." % (self, target))
# class BringableBase(object):
# def __init__(self, target):构造函数
# class website子类(BringableBase):
# class folder子类(BringableBase):
# class open子类(BringableBase):
# class ssh子类(BringableBase):
target.bring_it()
#---------------------------------------------------------------------------
# Paste rule.
class PasteRule(CompoundRule):
# lang.paste_me = "paste me <target>"
spec = config.lang.paste_me
# targets.mapping = {
extras = [Choice("target", config.targets.mapping)]
def _process_recognition(self, node, extras):
target = extras["target"]
self._log.debug("%s: pasting target %s." % (self, target))
Paste(target.target).execute()
#---------------------------------------------------------------------------
# Create and manage this module's grammar.创建并管理这个模块的语法。
grammar = Grammar("bring me")
grammar.add_rule(BringRule())
grammar.add_rule(PasteRule())
grammar.load()
def unload():
global grammar
if grammar: grammar.unload()
grammar = None