-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam.py
executable file
·306 lines (252 loc) · 8.51 KB
/
bam.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
306
#!/usr/bin/env python
import os
import re
import readline
import shelve
import subprocess
import sys
RESERVED_KEYWORDS = set(
['help', 'setup', 'new', 'list', 'delete', 'destroy']
)
def find_home():
return os.path.dirname(os.path.realpath(__file__))
def handle_input(args):
""" Entry point """
if len(args) == 1:
command = raw_input('yes?\n')
if command in RESERVED_KEYWORDS:
getattr(Bam, command)()
else:
Bam.run(command.split())
elif len(args) == 2:
command = args[1]
if command in RESERVED_KEYWORDS:
getattr(Bam, command)()
else:
Bam.run([command])
else:
Bam.run(args[1:])
class Input(str):
"""
"""
@staticmethod
def normalized(input, arg_positions):
return ' '.join(
token for token in input if not
input.index(token) in arg_positions.values()
)
class Alias(Input):
"""
"""
def __init__(self, string):
self._string = string
self._tokens = string.split()
def __repr__(self):
return self._string
@property
def arg_positions(self):
args = dict()
if '[' or ']' in self._string:
words = re.sub('[\[\]]', '', self._string).split()
for word in words:
if re.match('\d+', word):
args[word] = words.index(word)
return args
@property
def normalized(self):
return super(Alias, self).normalized(self._tokens, self.arg_positions)
class Command(Alias):
"""
"""
def execute(self, input, arg_positions):
full_command = self.__replace_wildcards(input, arg_positions)
print 'Running "%s" ... \n' % full_command
subprocess.call(full_command, shell=True)
def __replace_wildcards(self, input, arg_positions):
full_command = self._tokens
for arg, pos in arg_positions.items():
full_command[self.arg_positions[arg]] = input[pos]
return ' '.join(full_command)
class DatabaseAlreadyInitializedError(StandardError):
pass
class CommandStore(object):
"""
"""
database = None
def init(self):
if not self.database.has_key('aliases'):
self.database['aliases'] = dict()
else:
raise DatabaseAlreadyInitializedError
def access(self):
self.database = shelve.open(
os.path.join(find_home(), 'commands.db'), writeback=True
)
def close(self):
self.database.close()
def initialized(self):
return self.database.has_key('aliases')
def is_empty(self):
return True if not self.database['aliases'].items() else False
def get_aliases(self):
return [alias for alias in self.database['aliases'].keys()]
def get_commands(self):
return [command for command in self.database['aliases'].values()]
def get_entries(self):
return self.database['aliases'].items()
def add_alias(self, alias, command):
self.database['aliases'][Alias(alias)] = Command(command)
def rm_alias(self, alias):
l = filter(lambda x: x == alias, self.get_aliases())
del self.database['aliases'][l[0]]
class Bam(object):
command_store = CommandStore()
def db_access(func):
def wrapper(cls, *args, **kwargs):
Bam.command_store.access()
if (Bam.command_store.initialized() or
func.__name__ == 'help' or
func.__name__ == 'setup'):
func(cls, *args, **kwargs)
else:
Bam.__respond_with(
'Database not initialized. Please run setup first.'
)
Bam.command_store.close()
return wrapper
@classmethod
def __ask_for_confirmation(cls, message):
print message
confirmation = Bam.__prompt_user_for('y/n')
return True if confirmation == 'y' else False
@classmethod
def __prompt_user_for(cls, string):
return raw_input('Enter %s: ' % string)
@classmethod
def __respond_with(cls, string):
print ('BAM! %s' % string)
@classmethod
def help(cls):
print 'Hi! Besides "help", I respond to the following commands:'
print '- setup'
print '- new'
print '- list'
print '- delete'
print '- destroy'
@classmethod
@db_access
def setup(cls):
try:
Bam.command_store.init()
Bam.__respond_with('Initialized your database.')
except DatabaseAlreadyInitializedError:
Bam.__respond_with(
'No need to do that. Everything is already configured.'
)
@classmethod
@db_access
def new(cls):
global RESERVED_KEYWORDS
command = Bam.__prompt_user_for('command')
if command not in Bam.command_store.get_commands():
Bam.__respond_with('This is a brand new command.')
else:
Bam.__respond_with('Adding new alias to existing command...')
alias = Bam.__prompt_user_for('alias')
if alias in Bam.command_store.get_aliases():
Bam.__respond_with('Can\'t do this. Alias exists.')
return
elif alias in RESERVED_KEYWORDS:
Bam.__respond_with(
'Can\'t do this. "%s" is a reserved keyword.' % alias
)
return
Bam.command_store.add_alias(alias, command)
Bam.__respond_with(
'"%s" can now be run via "%s".' % (command, alias)
)
@classmethod
@db_access
def list(cls):
if Bam.command_store.is_empty():
Bam.__respond_with('You don\'t have any commands yet.')
return
col_width = max(map(
lambda command: len(command), Bam.command_store.get_commands()
)) + 2
template = "{0:<4}{1:%d}{2}" % col_width
print template.format('ID', "COMMAND", "ALIAS")
for id, entry in enumerate(Bam.command_store.get_entries()):
command = entry[1]
alias = entry[0]
item = (id, command, alias)
print template.format(*item)
print
@classmethod
@db_access
def delete(cls):
alias = Bam.__prompt_user_for('alias')
if alias in RESERVED_KEYWORDS:
Bam.__respond_with(
'Can\'t do that: "%s" is a built-in command.' % alias
)
return
elif alias not in Bam.command_store.get_aliases():
Bam.__respond_with('Can\'t do that: Alias doesn\'t exist.')
return
confirmation = Bam.__ask_for_confirmation('Srsly?')
if confirmation:
Bam.command_store.rm_alias(alias)
Bam.__respond_with('"%s" is an ex-alias.' % alias)
else:
Bam.__respond_with('Aborting.')
@classmethod
def destroy(cls):
db_path = os.path.join(find_home(), 'commands.db')
if os.path.exists(db_path):
confirmation = Bam.__ask_for_confirmation(
'Really delete *everything*? This is irreversible!'
)
if confirmation:
os.remove(db_path)
Bam.__respond_with('Nuked your database.')
else:
Bam.__respond_with('Aborting.')
else:
Bam.__respond_with('Can\'t do that. Database does not exist.')
@classmethod
@db_access
def run(cls, input):
for alias, command in Bam.command_store.get_entries():
if alias.normalized == Input.normalized(
input, alias.arg_positions
):
command.execute(input, alias.arg_positions)
return
Bam.__respond_with('Unknown alias.')
class AliasCompleter(object):
"""
"""
_options = list(RESERVED_KEYWORDS)
def __init__(self):
command_store = CommandStore()
command_store.access()
if command_store.initialized():
self._options += command_store.get_aliases()
command_store.close()
def complete(self, text, state):
if state == 0:
if text:
self.matches = [
s for s in self._options if s and s.startswith(text)
]
else:
self.matches = self._options[:]
try:
return self.matches[state]
except IndexError:
return None
readline.set_completer(AliasCompleter().complete)
readline.parse_and_bind('tab: complete')
if __name__ == '__main__':
handle_input(sys.argv)