comparison cmd2.py @ 202:da8c265934ee

liberal use of os.path.expanduser
author catherine@dellzilla
date Mon, 02 Mar 2009 10:55:33 -0500
parents 6cdf53ce84f5
children 68a609fc516b
comparison
equal deleted inserted replaced
201:6cdf53ce84f5 202:da8c265934ee
416 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 416 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
417 417
418 inputMark = pyparsing.Literal('<') 418 inputMark = pyparsing.Literal('<')
419 inputMark.setParseAction(lambda x: '') 419 inputMark.setParseAction(lambda x: '')
420 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom') 420 inputFrom = pyparsing.Word(self.legalChars + '/\\')('inputFrom')
421 inputFrom.setParseAction(lambda x: (x and open(x[0]).read()) or getPasteBuffer()) 421 inputFrom.setParseAction(lambda x: (x and open(os.path.expanduser(x[0])).read()) or getPasteBuffer())
422 self.inputParser = inputMark + pyparsing.Optional(inputFrom) 422 self.inputParser = inputMark + pyparsing.Optional(inputFrom)
423 self.inputParser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 423 self.inputParser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
424 424
425 def parsed(self, raw, **kwargs): 425 def parsed(self, raw, **kwargs):
426 if isinstance(raw, ParsedString): 426 if isinstance(raw, ParsedString):
484 if statement.parsed.outputTo: 484 if statement.parsed.outputTo:
485 mode = 'w' 485 mode = 'w'
486 if statement.parsed.output == '>>': 486 if statement.parsed.output == '>>':
487 mode = 'a' 487 mode = 'a'
488 try: 488 try:
489 self.stdout = open(statement.parsed.outputTo, mode) 489 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)
490 except OSError, e: 490 except OSError, e:
491 print e 491 print e
492 return 0 492 return 0
493 else: 493 else:
494 statekeeper = Statekeeper(self, ('stdout',)) 494 statekeeper = Statekeeper(self, ('stdout',))
702 buffer = '' 702 buffer = ''
703 else: 703 else:
704 buffer = self.history[-1] 704 buffer = self.history[-1]
705 705
706 if buffer: 706 if buffer:
707 f = open(filename, 'w') 707 f = open(os.path.expanduser(filename), 'w')
708 f.write(buffer or '') 708 f.write(buffer or '')
709 f.close() 709 f.close()
710 710
711 os.system('%s %s' % (self.editor, filename)) 711 os.system('%s %s' % (self.editor, filename))
712 self.do__load(filename) 712 self.do__load(filename)
732 elif args.idx: 732 elif args.idx:
733 saveme = self.history[int(args.idx)-1] 733 saveme = self.history[int(args.idx)-1]
734 else: 734 else:
735 saveme = self.history[-1] 735 saveme = self.history[-1]
736 try: 736 try:
737 f = open(fname, 'w') 737 f = open(os.path.expanduser(fname), 'w')
738 f.write(saveme) 738 f.write(saveme)
739 f.close() 739 f.close()
740 print 'Saved to %s' % (fname) 740 print 'Saved to %s' % (fname)
741 except Exception, e: 741 except Exception, e:
742 print 'Error saving %s: %s' % (fname, str(e)) 742 print 'Error saving %s: %s' % (fname, str(e))
749 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) 749 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
750 if isinstance(fname, file): 750 if isinstance(fname, file):
751 self.stdin = fname 751 self.stdin = fname
752 else: 752 else:
753 try: 753 try:
754 self.stdin = open(fname, 'r') 754 self.stdin = open(os.path.expanduser(fname), 'r')
755 except IOError, e: 755 except IOError, e:
756 try: 756 try:
757 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r') 757 self.stdin = open('%s.%s' % (os.path.expanduser(fname), self.defaultExtension), 'r')
758 except IOError: 758 except IOError:
759 print 'Problem opening file %s: \n%s' % (fname, e) 759 print 'Problem opening file %s: \n%s' % (fname, e)
760 keepstate.restore() 760 keepstate.restore()
761 return 761 return
762 self.use_rawinput = False 762 self.use_rawinput = False
785 stop = self.postcmd(stop, runme) 785 stop = self.postcmd(stop, runme)
786 do_r = do_run 786 do_r = do_run
787 787
788 def fileimport(self, statement, source): 788 def fileimport(self, statement, source):
789 try: 789 try:
790 f = open(source) 790 f = open(os.path.expanduser(source))
791 except IOError: 791 except IOError:
792 self.stdout.write("Couldn't read from file %s\n" % source) 792 self.stdout.write("Couldn't read from file %s\n" % source)
793 return '' 793 return ''
794 data = f.read() 794 data = f.read()
795 f.close() 795 f.close()
907 def setUp(self): 907 def setUp(self):
908 if self.CmdApp: 908 if self.CmdApp:
909 self.outputTrap = OutputTrap() 909 self.outputTrap = OutputTrap()
910 self.cmdapp = self.CmdApp() 910 self.cmdapp = self.CmdApp()
911 try: 911 try:
912 tfile = open(self.transcriptFileName) 912 tfile = open(os.path.expanduser(self.transcriptFileName))
913 self.transcript = iter(tfile.readlines()) 913 self.transcript = iter(tfile.readlines())
914 tfile.close() 914 tfile.close()
915 except IOError: 915 except IOError:
916 self.transcript = [] 916 self.transcript = []
917 def assertEqualEnough(self, got, expected, message): 917 def assertEqualEnough(self, got, expected, message):