comparison cmd2.py @ 68:e06961ebd035

worked out terminator keeping
author catherine@Elli.myhome.westell.com
date Mon, 23 Jun 2008 20:52:41 -0400
parents a78dff1e7bca
children 824651b4d1b1
comparison
equal deleted inserted replaced
67:a78dff1e7bca 68:e06961ebd035
117 return xclipproc.stdout.read() 117 return xclipproc.stdout.read()
118 def writeToPasteBuffer(txt): 118 def writeToPasteBuffer(txt):
119 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 119 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
120 xclipproc.stdin.write(txt) 120 xclipproc.stdin.write(txt)
121 xclipproc.stdin.close() 121 xclipproc.stdin.close()
122 # but we want it in both the "primary" and "mouse" clipboards
123 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
124 xclipproc.stdin.write(txt)
125 xclipproc.stdin.close()
122 else: 126 else:
123 def getPasteBuffer(): 127 def getPasteBuffer():
124 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 128 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
125 setPasteBuffer = getPasteBuffer 129 setPasteBuffer = getPasteBuffer
126 130
148 processed[0] = pyparsing.Literal(processed[0]) 152 processed[0] = pyparsing.Literal(processed[0])
149 processed = reduce(lambda x, y: x ^ y, processed) 153 processed = reduce(lambda x, y: x ^ y, processed)
150 processed.ignore(pyparsing.sglQuotedString) 154 processed.ignore(pyparsing.sglQuotedString)
151 processed.ignore(pyparsing.dblQuotedString) 155 processed.ignore(pyparsing.dblQuotedString)
152 pattern = pyparsing.SkipTo(processed) + processed + pyparsing.restOfLine 156 pattern = pyparsing.SkipTo(processed) + processed + pyparsing.restOfLine
153 def parser(self, txt): 157 def parser(txt):
154 result = pattern.searchString(txt) 158 result = pattern.searchString(txt)
155 if result: 159 if result:
156 return result[0][0].strip(), result[0][1:-1], result[0][-1].strip() 160 return result[0][0].strip(), result[0][1:-1], result[0][-1].strip()
157 else: 161 else:
158 return None 162 return None
159 return parser 163 return parser
160
161
162 164
163 class Cmd(cmd.Cmd): 165 class Cmd(cmd.Cmd):
164 caseInsensitive = True 166 caseInsensitive = True
165 multilineCommands = [] 167 terminators = [';','\n']
168 multilineCommands = [] # commands that need a terminator to be finished
169 terminatorKeepingCommands = [] # commands that expect to process their own terminators (else it will be stripped during parse)
166 continuationPrompt = '> ' 170 continuationPrompt = '> '
167 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 171 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
168 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 172 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
169 defaultExtension = 'txt' 173 defaultExtension = 'txt'
170 defaultFileName = 'command.txt' 174 defaultFileName = 'command.txt'
177 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: 181 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']:
178 if not os.system('which %s' % (editor)): 182 if not os.system('which %s' % (editor)):
179 break 183 break
180 184
181 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive'] 185 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
182 terminators = [';','\n']
183 _TO_PASTE_BUFFER = 1 186 _TO_PASTE_BUFFER = 1
184 def do_cmdenvironment(self, args): 187 def do_cmdenvironment(self, args):
185 self.stdout.write(""" 188 self.stdout.write("""
186 Commands are %(casesensitive)scase-sensitive. 189 Commands are %(casesensitive)scase-sensitive.
187 Commands may be terminated with: %(terminators)s 190 Commands may be terminated with: %(terminators)s
202 pass 205 pass
203 206
204 def __init__(self, *args, **kwargs): 207 def __init__(self, *args, **kwargs):
205 cmd.Cmd.__init__(self, *args, **kwargs) 208 cmd.Cmd.__init__(self, *args, **kwargs)
206 self.history = History() 209 self.history = History()
210 self.commmand_terminator_finder = punctuationParser(self.terminators)
211 self.output_destination_finder = punctuationParser(['>>', '>'])
212 self.input_source_finder = punctuationParser(['<'])
213 self.pipe_destination_finder = punctuationParser(['|'])
207 214
208 def do_shortcuts(self, args): 215 def do_shortcuts(self, args):
209 """Lists single-key shortcuts available.""" 216 """Lists single-key shortcuts available."""
210 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 217 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
211 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 218 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
212 219
213 commmand_terminator_finder = punctuationParser(terminators)
214 output_destination_finder = punctuationParser(['>>', '>'])
215 input_source_finder = punctuationParser(['<'])
216 pipe_destination_finder = punctuationParser(['|'])
217 def strip_terminators(self, txt): 220 def strip_terminators(self, txt):
218 termination = self.commmand_terminator_finder(txt) 221 termination = self.commmand_terminator_finder(txt)
219 if termination: 222 if termination:
220 txt = termination[0] 223 txt = termination[0]
221 return txt 224 return txt
383 if shortcut and hasattr(self, 'do_%s' % shortcut): 386 if shortcut and hasattr(self, 'do_%s' % shortcut):
384 line = '%s %s' % (shortcut, line[1:]) 387 line = '%s %s' % (shortcut, line[1:])
385 i, n = 0, len(line) 388 i, n = 0, len(line)
386 while i < n and line[i] in self.identchars: i = i+1 389 while i < n and line[i] in self.identchars: i = i+1
387 cmd, arg = line[:i], line[i:].strip() 390 cmd, arg = line[:i], line[i:].strip()
388 arg = self.strip_terminators(arg) 391 if cmd not in self.terminatorKeepingCommands:
392 arg = self.strip_terminators(arg)
389 return cmd, arg, line 393 return cmd, arg, line
390 394
391 def showParam(self, param): 395 def showParam(self, param):
392 param = self.clean(param) 396 param = self.clean(param)
393 if param in self.settable: 397 if param in self.settable: