comparison cmd2.py @ 398:7812e00ff5b1

encode paste buffer write for Python 3
author catherine.devlin@gmail.com
date Sun, 19 Sep 2010 09:20:56 -0400
parents 50acba85cf9e
children f7cb69b0da9e
comparison
equal deleted inserted replaced
397:50acba85cf9e 398:7812e00ff5b1
44 if sys.version_info[0] > 2: 44 if sys.version_info[0] > 2:
45 import pyparsing_py3 as pyparsing 45 import pyparsing_py3 as pyparsing
46 raw_input = input 46 raw_input = input
47 else: 47 else:
48 import pyparsing 48 import pyparsing
49 49
50 __version__ = '0.6.0' 50 _version__ = '0.6.1'
51 51
52 class OptionParser(optparse.OptionParser): 52 class OptionParser(optparse.OptionParser):
53 def exit(self, status=0, msg=None): 53 def exit(self, status=0, msg=None):
54 self.values._exit = True 54 self.values._exit = True
55 if msg: 55 if msg:
203 def get_paste_buffer(): 203 def get_paste_buffer():
204 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 204 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
205 return xclipproc.stdout.read() 205 return xclipproc.stdout.read()
206 def write_to_paste_buffer(txt): 206 def write_to_paste_buffer(txt):
207 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 207 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
208 xclipproc.stdin.write(txt) 208 xclipproc.stdin.write(txt.encode())
209 xclipproc.stdin.close() 209 xclipproc.stdin.close()
210 # but we want it in both the "primary" and "mouse" clipboards 210 # but we want it in both the "primary" and "mouse" clipboards
211 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 211 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
212 xclipproc.stdin.write(txt) 212 xclipproc.stdin.write(txt.encode())
213 xclipproc.stdin.close() 213 xclipproc.stdin.close()
214 else: 214 else:
215 def get_paste_buffer(*args): 215 def get_paste_buffer(*args):
216 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 216 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
217 write_to_paste_buffer = get_paste_buffer 217 write_to_paste_buffer = get_paste_buffer
397 timing Report execution times 397 timing Report execution times
398 abbrev Accept abbreviated commands 398 abbrev Accept abbreviated commands
399 ''') 399 ''')
400 400
401 def poutput(self, msg): 401 def poutput(self, msg):
402 '''Convenient shortcut for self.stdout.write(); adds newline if necessary.'''
402 if msg: 403 if msg:
403 self.stdout.write(msg) 404 self.stdout.write(msg)
404 if msg[-1] != '\n': 405 if msg[-1] != '\n':
405 self.stdout.write('\n') 406 self.stdout.write('\n')
406 def perror(self, errmsg, statement=None): 407 def perror(self, errmsg, statement=None):
765 except Exception, e: 766 except Exception, e:
766 self.perror(str(e), statement) 767 self.perror(str(e), statement)
767 finally: 768 finally:
768 return self.postparsing_postcmd(stop) 769 return self.postparsing_postcmd(stop)
769 def complete_statement(self, line): 770 def complete_statement(self, line):
771 """Keep accepting lines of input until the command is complete."""
770 if (not line) or ( 772 if (not line) or (
771 not pyparsing.Or(self.commentGrammars). 773 not pyparsing.Or(self.commentGrammars).
772 setParseAction(lambda x: '').transformString(line)): 774 setParseAction(lambda x: '').transformString(line)):
773 raise EmptyStatement 775 raise EmptyStatement
774 statement = self.parsed(line) 776 statement = self.parsed(line)
796 mode = 'w' 798 mode = 'w'
797 if statement.parsed.output == '>>': 799 if statement.parsed.output == '>>':
798 mode = 'a' 800 mode = 'a'
799 sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) 801 sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)
800 else: 802 else:
801 sys.stdout = self.stdout = tempfile.TemporaryFile() 803 sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+")
802 if statement.parsed.output == '>>': 804 if statement.parsed.output == '>>':
803 self.stdout.write(get_paste_buffer()) 805 self.stdout.write(get_paste_buffer())
804 806
805 def restore_output(self, statement): 807 def restore_output(self, statement):
806 if self.kept_state: 808 if self.kept_state:
1383 """Tries to force a new value into the same type as the current.""" 1385 """Tries to force a new value into the same type as the current."""
1384 typ = type(current) 1386 typ = type(current)
1385 if typ == bool: 1387 if typ == bool:
1386 try: 1388 try:
1387 return bool(int(new)) 1389 return bool(int(new))
1388 except ValueError, TypeError: 1390 except (ValueError, TypeError):
1389 pass 1391 pass
1390 try: 1392 try:
1391 new = new.lower() 1393 new = new.lower()
1392 except: 1394 except:
1393 pass 1395 pass
1454 for fname in glob.glob(fileset): 1456 for fname in glob.glob(fileset):
1455 tfile = open(fname) 1457 tfile = open(fname)
1456 self.transcripts[fname] = iter(tfile.readlines()) 1458 self.transcripts[fname] = iter(tfile.readlines())
1457 tfile.close() 1459 tfile.close()
1458 if not len(self.transcripts): 1460 if not len(self.transcripts):
1459 raise StandardError, "No test files found - nothing to test." 1461 raise (StandardError,), "No test files found - nothing to test."
1460 def setUp(self): 1462 def setUp(self):
1461 if self.CmdApp: 1463 if self.CmdApp:
1462 self.outputTrap = OutputTrap() 1464 self.outputTrap = OutputTrap()
1463 self.cmdapp = self.CmdApp() 1465 self.cmdapp = self.CmdApp()
1464 self.fetchTranscripts() 1466 self.fetchTranscripts()