comparison cmd2/cmd2.py @ 106:2d3232693807

prompt-setting quirks
author catherine@dellzilla
date Fri, 24 Oct 2008 16:03:58 -0400
parents 130340609e19
children 3e2651cbe376
comparison
equal deleted inserted replaced
105:130340609e19 106:2d3232693807
96 win32clipboard.OpenClipboard(0) 96 win32clipboard.OpenClipboard(0)
97 win32clipboard.EmptyClipboard() 97 win32clipboard.EmptyClipboard()
98 win32clipboard.SetClipboardText(txt) 98 win32clipboard.SetClipboardText(txt)
99 win32clipboard.CloseClipboard() 99 win32clipboard.CloseClipboard()
100 except ImportError: 100 except ImportError:
101 def getPasteBuffer(): 101 def getPasteBuffer(*args):
102 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') 102 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/')
103 setPasteBuffer = getPasteBuffer 103 setPasteBuffer = getPasteBuffer
104 else: 104 else:
105 can_clip = False 105 can_clip = False
106 try: 106 try:
127 # but we want it in both the "primary" and "mouse" clipboards 127 # but we want it in both the "primary" and "mouse" clipboards
128 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 128 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
129 xclipproc.stdin.write(txt) 129 xclipproc.stdin.write(txt)
130 xclipproc.stdin.close() 130 xclipproc.stdin.close()
131 else: 131 else:
132 def getPasteBuffer(): 132 def getPasteBuffer(*args):
133 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 133 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
134 setPasteBuffer = getPasteBuffer 134 setPasteBuffer = getPasteBuffer
135 writeToPasteBuffer = getPasteBuffer
135 136
136 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') 137 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
137 def parseSearchResults(pattern, s): 138 def parseSearchResults(pattern, s):
138 generator = pattern.scanString(s) 139 generator = pattern.scanString(s)
139 try: 140 try:
149 echo = False 150 echo = False
150 caseInsensitive = True 151 caseInsensitive = True
151 multilineCommands = [] 152 multilineCommands = []
152 continuationPrompt = '> ' 153 continuationPrompt = '> '
153 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 154 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
154 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 155 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
156 noSpecialParse = 'set ed edit exit'.split()
155 defaultExtension = 'txt' 157 defaultExtension = 'txt'
156 defaultFileName = 'command.txt' 158 defaultFileName = 'command.txt'
157 editor = os.environ.get('EDITOR') 159 editor = os.environ.get('EDITOR')
158 _STOP_AND_EXIT = 2 160 _STOP_AND_EXIT = 2
159 if not editor: 161 if not editor:
233 ('got from ', '<', 'thisfile.txt') 235 ('got from ', '<', 'thisfile.txt')
234 ''' 236 '''
235 if isinstance(s, pyparsing.ParseResults): 237 if isinstance(s, pyparsing.ParseResults):
236 return s 238 return s
237 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s) 239 result = (pyparsing.SkipTo(pyparsing.StringEnd()))('fullStatement').parseString(s)
240 command = s.split()[0]
241 if self.caseInsensitive:
242 command = command.lower()
243 '''if command in self.noSpecialParse:
244 result['statement'] = result['upToIncluding'] = result['unterminated'] = result.fullStatement
245 result['command'] = command
246 result['args'] = ' '.join(result.fullStatement.split()[1:])
247 return result'''
238 if s[0] in self.shortcuts: 248 if s[0] in self.shortcuts:
239 s = self.shortcuts[s[0]] + ' ' + s[1:] 249 s = self.shortcuts[s[0]] + ' ' + s[1:]
240 result['statement'] = s 250 result['statement'] = s
241 result['parseable'] = s 251 result['parseable'] = s
242 result += parseSearchResults(self.terminatorPattern, s) 252 result += parseSearchResults(self.terminatorPattern, s)
250 result += parseSearchResults(self.pipePattern, result.parseable) 260 result += parseSearchResults(self.pipePattern, result.parseable)
251 result += parseSearchResults(self.redirectInPattern, result.parseable) 261 result += parseSearchResults(self.redirectInPattern, result.parseable)
252 result += parseSearchResults(self.redirectOutPattern, result.parseable) 262 result += parseSearchResults(self.redirectOutPattern, result.parseable)
253 result += parseSearchResults(self.argSeparatorPattern, result.statement) 263 result += parseSearchResults(self.argSeparatorPattern, result.statement)
254 if self.caseInsensitive: 264 if self.caseInsensitive:
255 result['command'] = result.command.lower() 265 result['command'] = result.command.lower()
256 result['statement'] = '%s %s' % (result.command, result.args) 266 result['statement'] = '%s %s' % (result.command, result.args)
257 return result 267 return result
258 268
259 def extractCommand(self, statement): 269 def extractCommand(self, statement):
260 try: 270 try:
431 'Sets a parameter' 441 'Sets a parameter'
432 try: 442 try:
433 paramName, val = arg.split(None, 1) 443 paramName, val = arg.split(None, 1)
434 paramName = self.clean(paramName) 444 paramName = self.clean(paramName)
435 if paramName not in self.settable: 445 if paramName not in self.settable:
436 raise NotSettableError 446 raise NotSettableError
437 currentVal = getattr(self, paramName) 447 currentVal = getattr(self, paramName)
438 val = cast(currentVal, self.parsed(val).unterminated) 448 if (val[0] == val[-1]) and val[0] in ("'", '"'):
449 val = val[1:-1]
450 else:
451 val = cast(currentVal, self.parsed(val).unterminated)
439 setattr(self, paramName, val) 452 setattr(self, paramName, val)
440 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) 453 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val))
441 except (ValueError, AttributeError, NotSettableError), e: 454 except (ValueError, AttributeError, NotSettableError), e:
442 self.do_show(arg) 455 self.do_show(arg)
443 456