-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorerXXX.py
More file actions
423 lines (335 loc) · 14.3 KB
/
explorerXXX.py
File metadata and controls
423 lines (335 loc) · 14.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Windows Explorer macros with intelligent context aware actions.
# * open folder by (partial) name
# * invoke open/edit/print verbs on files by (partial) name
# * copy/move files and folders into subfolders by (partial) name
#
# (c) 2004--2008 Daniel J. Rocco
# Licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
# http://creativecommons.org/licenses/by-nc-sa/3.0/us/
#
# Last modified: $Date: 2009-04-23 10:05:04 -0400 (Thu, 23 Apr 2009) $
#
# 01.14.2005 18:36: split "camel case" file and folder names into words
#
# 09.06.2005 21:26: added command for extracting zip files to a single folder
#
# 01.17.2006 21:26: ignore network paths ('\\' prefix) due to
# performance/timeout problems
#
# 01.19.2006 23:00: undid 01.17 change: inelegant and unreliable since Windows
# substitutes the "My Network Places" alias for some network paths. Instead,
# can set a limit to the number of folder items, after which voice commands are
# disabled
#
# $Id: explorer.py 454 2009-04-23 14:05:04Z drocco $
#
# to do:
# * doesn't work in "Desktop" folder
# * problem with open grammar in folders with no subfolders?
# * selection commands
# * invert selection
# * select by file words
# * select by extension/file type
# * refine file/folder words
# * split at numbers, handle @
# * zip file extraction uses old filename for renamed files
# * exclusion
# NatLink imports
import natlink
from natlinkutils import *
# Windows imports
import win32com.client, win32api, win32con
# Python Library imports
import string, re
FOLDER_SIZE_LIMIT = 75
class ThisGrammar(GrammarBase):
gramSpec = """
<folder> exported = {folders}
;
<file> exported = (Open | Edit | Print) {files}
;
<text> exported = 'text edit' {files}
;
<copy> exported = (copy | move) to {folders}
;
<zip> exported = extract zip files | extract and flatten zip files
;
"""
# List definitions
listDefinition= {
'folders' : [],
'files' : [],
}
def initialize(self):
if not self.load(self.gramSpec):
return None
self.currentModule = ("","",0)
# gotBegin initialization code modeled after Vocola initialization
def gotBegin(self, moduleInfo):
# Return if wrong application
window = matchWindow(moduleInfo,'explorer','')
if not window:
return None
# Return if same window and title as before
if moduleInfo == self.currentModule:
return None
self.currentModule = moduleInfo
self.deactivateAll()
shellWindow = getShellWindowByHandle(moduleInfo[2])
if shellWindow != None:
self.folderName = shellWindow.Document.Folder.Items().Item().Path
self.folders, self.files =getFolderFileNames(shellWindow,FOLDER_SIZE_LIMIT)
# 03.11.2005 17:18: remove "CVS" from the folder list
# FIXME: make this modular
if ".svn" in self.folders:
del(self.folders[".svn"])
# print self.folders
self.folderWordList=createFileWordList(self.folders)
# print self.folderWordList
self.fileWordList=createFileWordList(self.files)
# print self.fileWordList
self.folderAction= ""
# FIXED: see getFolderNames below (FIXME: Unicode encoded folder names don't work)
# done (09.02.2004 djr): break names into "words"
if len (self.folders) >= 1:
self.listDefinition["folders"] = self.folderWordList.keys()
self.setList ('folders', self.folderWordList.keys())
self.activate('folder', window)
self.activate ("copy", window)
# (09.02.2004 djr): add logic for invoking verbs on files
if len (self.files) >= 1:
self.listDefinition["files"] = self.fileWordList.keys()
self.setList("files", self.listDefinition["files"])
self.activate("file", window)
self.activate ("text", window)
self.activate ("zip", window)
def gotResults_folder(self,words,fullResults):
# print "recognized: "
# print words
# (11.17.2004 djr): accept folder names after first word
folder = None
for word in words:
if word in self.folderWordList.keys():
folder = word
break
if folder != None:
self.openFolder(folder)
def openFolder(self,folderWords):
# retrieve the appropriate FolderItem name from the word list
if folderWords != None and len(folderWords) >0:
folderItemName = self.folderWordList[folderWords]
folderItem= self.folders [folderItemName]
folderItem.InvokeVerb ()
def gotResults_zip (self, words, fullResults):
"""
extract zip files to use the name of the file without the zip extension
"""
flatten = False
if words [2] == "flatten":
flatten = True
# extract all the files
for filename in self.files.keys():
# skip files without ZIP extension
if filename [-3:].lower() != "zip":
continue
# create the output directory
path = os.path.dirname(self.files [filename].Path)
outputPath = os.path.join(path, string.replace(filename, ".zip", ""))
# extract the files
unzip (os.path.join (path,filename), outputPath, flatten)
def gotResults_copy(self , words,fullResults):
# print words
keys = {
"copy" : "{ctrl+c}",
"move" : "{ctrl+x}",
}
action = keys [words [0]]
natlink.playString(action)
self.openFolder(words [2])
natlink.playString("{ctrl+v}")
def gotResults_file(self,words,fullResults):
# print "recognized: "
# print words
# retrieve the appropriate FolderItem name from the word list
if words [1] in self.fileWordList.keys ():
folderItemName = self.fileWordList[words [1]]
# print folderItemName
folderItem= self.files [folderItemName]
folderItem.InvokeVerb (words [0])
def gotResults_text(self,words,fullResults):
# print "recognized: "
# print words
# retrieve the appropriate FolderItem name from the word list
if words [1] in self.fileWordList.keys ():
folderItemName = self.fileWordList[words [1]]
# print folderItemName
folderItem= self.files [folderItemName]
win32api.ShellExecute(-1, 'Open', r'C:\Program Files\Win32Pad\win32pad.exe', folderItem.Path, None, win32con.SW_SHOWNORMAL)
############################################################################
#
# This is the top-level code
#
# This code gets executed when this file is (re)loaded.
#
# Every grammar file should contains two lines like this for each grammar
# class defined in the file. These lines causes an instance of the grammar
# to be created and then initialized.
thisGrammar = ThisGrammar()
thisGrammar.initialize()
#
# Every grammar file must define a function called "unload" which will
# call the method "unload" for every grammar which the file loaded. The
# Python wrapper code will call unload when this file is reloaded. Calling
# unload first ensures that the classes are cleaned up.
#
def unload():
global thisGrammar
if thisGrammar: thisGrammar.unload()
thisGrammar = None
def getShellWindowByHandle(handle):
"""
Given a handle to a Windows Explorer window, returns the Windows ShellWindow
object being displayed by that window. The ShellWindow object represents
the folder being shown and contains methods and properties for manipulating
the window and obtaining information about what is being displayed.
Examples include:
* retrieving the list of files and folders being displayed
* reading/updating the selection state
* programmatically instructing the window to change directories
references:
* http://dbforums.com/t867088.html
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_programming/objectmap.asp
"""
clsid='{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
ShellWindows=win32com.client.Dispatch(clsid)
for i in range(ShellWindows.Count):
# 11.22.2004 djr: some objects returned by ShellWindows are not
# window objects...
try:
if ShellWindows[i].HWND == handle:
return ShellWindows[i]
except:
print "bad window? " +str(ShellWindows [i])
return None
def getFolderFileNames(window, limit =-1):
"""
given a ShellWindow object (a Windows Explorer shell window), return
two dictionaries containing the folder and filenames in the window
mapped to the FolderItem Windows objects for those filenames.
"""
if window == None:
return
folders = {}
files = {}
# print window.LocationURL
# window.document is a ShellFolderView object
folderItems=window.document.Folder.Items()
# 01.19.2006 22:58 don't process folders with more than limit items
if folderItems.Count >= limit and limit >= 0:
print "directory over size limit (%s, limit=%s): %s" % (folderItems.Count,limit,window.LocationURL)
return folders, files
for i in range(folderItems.Count):
folderItem=folderItems.Item (i)
# Windows Unicode string encoding
itemName=folderItem.Name.encode('mbcs')
if folderItem.IsFolder:
# put zip files in the files bin (Windows XP lists them as folders)
if string.lower(itemName[-4:]) == ".zip":
files [itemName] =folderItem
else:
folders[itemName] = folderItem
else:
files [itemName] =folderItem
return folders, files
def generateAllFileWords(filename):
"""
take a filename and split it into "words" and generate all contiguous subphrases
of the filename words. For example:
generateAllFileWords("CVS") returns ["CVS"]
generateAllFileWords("Documents and Settings") returns
["Documents and Settings", "Documents and", "Documents",
"and Settings", "and", "Settings"]
01.14.2005 18:03 djr: added camel case processing
"""
fileWords=[filename]
# words = string.split(filename, " ")
# split the filename on whitespace characters and "camel casing" (e.g. ThisIsACamelCaseWord)
# only insert the word in the resultant list if it is non-None and nonempty
words = [word for word in re.split("([A-Z][^A-Z\s]+)|\s+", filename) if word]
# print words
length = len (words)
if length <= 1:
return fileWords
for i in range(1,length+1):
for j in range(length):
word = ""
for part in [x+ " " for x in words[j:i+j] if x != 'dot']:
word += part
word = word [:-1]
if word not in fileWords and len (word) >= 1:
fileWords.append(word)
# print fileWords
return fileWords
def friendlyName(filename):
"""
given a string, return a more "voice friendly" version of the string by
replacing hard to say characters with spelled-out versions and eliminating
unnecessary characters.
characters removed: _ + -
substitutions:
'.' becomes " dot "
01.27.2005 10:39 djr: added hyphen removal
"""
replacements = {
"-": " ",
"_": " ",
".": " dot ",
"+": " ",
}
friendly = filename
for unfriendly,replacement in replacements.iteritems():
friendly = string.replace(friendly, unfriendly, replacement)
return friendly
def createFileWordList(filenames):
fileWordList= {}
duplicates = []
for filename in filenames:
# convert the file name to a more "voice friendly" format
friendly = friendlyName(filename)
for word in generateAllFileWords(friendly):
if word not in duplicates:
if word in fileWordList.keys():
duplicates.append (word)
del fileWordList[word]
else:
fileWordList[word] = filename
return fileWordList
# fix spaces in spawn calls, from http://mail.python.org/pipermail/python-bugs-list/2001-July/005937.html
def escape(arg):
import re
# If arg contains no space or double-quote then
# no escaping is needed.
if not re.search(r'[ "]', arg):
return arg
# Otherwise the argument must be quoted and all
# double-quotes, preceding backslashes, and
# trailing backslashes, must be escaped.
def repl(match):
if match.group(2):
return match.group(1) * 2 + '\"'
else:
return match.group(1) * 2
return '"' + re.sub(r'(\*)("|$)', repl, arg) + '"'
def unzip (filename,outputPath,flatten=False):
"""
unzip the file into the given directory
"""
command = "x"
if flatten:
command = "e"
# check if directory exists, if not create it
if not os.path.exists(outputPath):
os.mkdir(outputPath)
command = r'"C:\Program Files\7-Zip\7z.exe" ' + command + ' -o"' + outputPath + '" "' + filename + '"'
os.system(escape(command))
#print command