-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.py
executable file
·305 lines (250 loc) · 10.9 KB
/
parser.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set expandtab tabstop=4 shiftwidth=4 :
# parser.py - Module for copy_bot.py that implements all of the parsing related
# functions.
# By: The Doctor <drwho at virtadpt dot net>
# 0x807B17C1 / 7960 1CDC 85C9 0B63 8D9F DD89 3BD8 FF2B 807B 17C1
# License: GPLv3
# v2.0 - Ported to Python 3.
# v1.0 - Initial release.
# TO-DO:
# -
# Load modules.
import logging
import pyparsing as pp
# Parser primitives
# We define them up here because they'll be re-used over and over.
help_command = pp.CaselessLiteral("help")
# File paths are of the form ((/)*a?)?
# * Zero or more slashes.
# * One or more alphanumeric characters or punctuation marks.
# * One or more of the above combinations.
pathname_match = pp.Word(pp.printables)
copy_command = pp.CaselessLiteral("copy")
from_command = pp.CaselessLiteral("from")
to_command = pp.CaselessLiteral("to")
into_command = pp.CaselessLiteral("into")
everything_command = pp.CaselessLiteral("everything")
in_command = pp.CaselessLiteral("in")
asterisk_command = pp.CaselessLiteral("*")
all_command = pp.CaselessLiteral("all")
files_command = pp.CaselessLiteral("files")
# Command parsers
# copy <file> <dir><optional filename>
copy_foo_bar_command = copy_command + pathname_match + pathname_match
# copy <file> to <dir><optional filename>
copy_to_command = copy_command + pathname_match + to_command + pathname_match
# copy <file> into <dir><optional filename>
copy_into_command = copy_command + pathname_match + into_command + pathname_match
# copy from <file> to <dir><optional filename>
copy_from_command = copy_command + from_command + pathname_match + to_command + pathname_match
# copy from <file> into <dir><optional filename>
copy_from_into_command = copy_command + from_command + pathname_match + into_command + pathname_match
# copy <dir> to <dir>
copy_dir_to_dir_command = copy_command + pathname_match + to_command + pathname_match
# copy <dir> into <dir>
copy_dir_into_dir_command = copy_command + pathname_match + into_command + pathname_match
# copy everything in <dir> to <dir>
copy_everything_in_dir_command = copy_command + everything_command + in_command + pathname_match + to_command + pathname_match
# copy everything in <dir> into <dir>
copy_everything_into_dir_command = copy_command + everything_command + in_command + pathname_match + into_command + pathname_match
# copy all files in <dir> to <dir>
copy_all_command = copy_command + all_command + files_command + in_command + pathname_match + to_command + pathname_match
# copy all files in <dir> into <dir>
copy_all_into_command = copy_command + all_command + files_command + in_command + pathname_match + into_command + pathname_match
# copy * in <dir> to <dir>
copy_asterisk_to_dir = copy_command + asterisk_command + in_command + pathname_match + to_command + pathname_match
# copy * in <dir> into <dir>
copy_asterisk_into_dir = copy_command + asterisk_command + in_command + pathname_match + into_command + pathname_match
# copy all files in <dir> to <dir>
copy_all_files_to_dir = copy_command + all_command + files_command + in_command + pathname_match + to_command + pathname_match
# copy all files in <dir> into <dir>
copy_all_files_into_dir = copy_command + all_command + files_command + in_command + pathname_match + into_command + pathname_match
# Functions
# parse_help(): Function that matches the word "help" all by itself in an input
# string. Returns the string "help" on a match and None if not.
def parse_help(command):
try:
parsed_command = help_command.parseString(command)
return "help"
except:
return None
# parse_single_file_copy(): Function that matches one-to-one copy requests.
def parse_single_file_copy(command):
logging.debug("Entered function parser.parse_single_file_copy().")
logging.debug("Value of command: " + str(command))
# Handle that holds the output from each parser.
parsed_command = None
# Hash that holds the filespecs.
filespecs = {}
# "copy foo bar"
try:
parsed_command = copy_foo_bar_command.parseString(command)
filespecs['from'] = parsed_command[1]
filespecs['to'] = parsed_command[2]
filespecs['type'] = "copy"
logger.debug("Copy " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy foo to bar"
try:
parsed_command = copy_to_command.parseString(command)
filespecs['from'] = parsed_command[1]
filespecs['to'] = parsed_command[3]
filespecs['type'] = "copy"
logger.debug("Copy " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy from foo to bar"
try:
parsed_command = copy_from_command.parseString(command)
filespecs['from'] = parsed_command[2]
filespecs['to'] = parsed_command[4]
filespecs['type'] = "copy"
logger.debug("Copy from " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy foo into bar"
try:
parsed_command = copy_into_command.parseString(command)
filespecs['from'] = parsed_command[1]
filespecs['to'] = parsed_command[3]
filespecs['type'] = "copy"
logger.debug("Copy " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# "copy from foo into bar"
try:
parsed_command = copy_from_into_command.parseString(command)
filespecs['from'] = parsed_command[2]
filespecs['to'] = parsed_command[4]
filespecs['type'] = "copy"
logger.debug("Copy from " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# Return the filespecs hash table.
logging.debug("Value of parsed_command: " + str(parsed_command))
logging.debug("Value of filespecs: " + str(filespecs))
return filespecs
# parse_multiple_file_copy(): Function that matches multiple file copy requests.
# This pretty much means everything in a directory into another directory.
def parse_multiple_file_copy(command):
logging.debug("Entered function parser.parse_multiple_file_copy().")
logging.debug("Value of command: " + str(command))
# Handle that holds the output from each parser.
parsed_command = None
# Hash that holds the filespecs.
filespecs = {}
# "copy /path/to to /another/path"
# (The * is implied.)
try:
parsed_command = copy_dir_to_dir_command.parseString(command)
filespecs['from'] = parsed_command[1]
filespecs['to'] = parsed_command[3]
filespecs['type'] = "copy"
logger.debug("Copy " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy /path/to into /another/path"
# (The * is implied.)
try:
parsed_command = copy_dir_to_dir_command.parseString(command)
filespecs['from'] = parsed_command[1]
filespecs['to'] = parsed_command[3]
filespecs['type'] = "copy"
logger.debug("Copy " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# "copy everything in /path/to to /another/path"
# (The * is implied.)
try:
parsed_command = copy_everything_in_dir_command.parseString(command)
filespecs['from'] = parsed_command[3]
filespecs['to'] = parsed_command[5]
filespecs['type'] = "copy"
logger.debug("Copy everything in " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy everything in /path/to into /another/path"
# (The * is implied.)
try:
parsed_command = copy_everything_into_dir_command.parseString(command)
filespecs['from'] = parsed_command[3]
filespecs['to'] = parsed_command[5]
filespecs['type'] = "copy"
logger.debug("Copy everything in " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# "copy * in /path/to to /another/path"
try:
parsed_command = copy_asterisk_to_dir.parseString(command)
filespecs['from'] = parsed_command[3]
filespecs['to'] = parsed_command[5]
filespecs['type'] = "copy"
logger.debug("Copy * in " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy * in /path/to into /another/path"
try:
parsed_command = copy_asterisk_into_dir.parseString(command)
filespecs['from'] = parsed_command[3]
filespecs['to'] = parsed_command[5]
filespecs['type'] = "copy"
logger.debug("Copy * in " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# "copy all files in /path/to to /another/path"
try:
parsed_command = copy_all_files_to_dir.parseString(command)
filespecs['from'] = parsed_command[4]
filespecs['to'] = parsed_command[6]
filespecs['type'] = "copy"
logger.debug("Copy all files in " + str(filespecs['from']) + " to " + str(filespecs['to']) + " detected.")
except:
pass
# "copy all files in /path/to into /another/path"
try:
parsed_command = copy_all_files_into_dir.parseString(command)
filespecs['from'] = parsed_command[4]
filespecs['to'] = parsed_command[6]
filespecs['type'] = "copy"
logger.debug("Copy all files in " + str(filespecs['from']) + " into " + str(filespecs['to']) + " detected.")
except:
pass
# Return the filespecs hash table.
logging.debug("Value of parsed_command: " + str(parsed_command))
logging.debug("Value of filespecs: " + str(filespecs))
return filespecs
# parse_command(): Function that parses commands from the message bus.
# Commands come as strings and are run through PyParsing to figure out what
# they are. A single-word string is returned as a match or None on no match.
# Conditionals are short-circuited to speed up execution.
def parse_command(command):
logging.debug("Entered function parser.parse_command().")
logging.debug("Value of command: " + str(command))
# Handle to the post-parsing command.
parsed_command = None
# Clean up the incoming command.
command = command.strip()
command = command.strip('.')
command = command.strip(',')
# If the get request is empty (i.e., nothing in the queue), bounce.
if "no commands" in command:
return None
# Online help?
parsed_command = parse_help(command)
if parsed_command == "help":
return parsed_command
# Multiple file copy?
parsed_command = parse_multiple_file_copy(command)
if parsed_command:
return parsed_command
# Single file copy?
parsed_command = parse_single_file_copy(command)
if parsed_command:
return parsed_command
# Fall-through: Nothing matched.
return "unknown"
if "__name__" == "__main__":
pass