comparison cmd2.py @ 41:1275d59c08a7

do_load accepts file object
author catherine@cordelia
date Thu, 22 May 2008 20:07:07 -0400
parents 71151157ac7c
children b3200c6e5763
comparison
equal deleted inserted replaced
40:4f2d6f9f2080 41:1275d59c08a7
485 def do_load(self, fname=None): 485 def do_load(self, fname=None):
486 """Runs command(s) from a file.""" 486 """Runs command(s) from a file."""
487 if fname is None: 487 if fname is None:
488 fname = self.defaultFileName 488 fname = self.defaultFileName
489 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt')) 489 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
490 try: 490 if isinstance(fname, file):
491 self.stdin = open(fname, 'r') 491 self.stdin = fname
492 except IOError, e: 492 else:
493 try: 493 try:
494 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r') 494 self.stdin = open(fname, 'r')
495 except IOError: 495 except IOError, e:
496 print 'Problem opening file %s: \n%s' % (fname, e) 496 try:
497 keepstate.restore() 497 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r')
498 return 498 except IOError:
499 print 'Problem opening file %s: \n%s' % (fname, e)
500 keepstate.restore()
501 return
499 self.use_rawinput = False 502 self.use_rawinput = False
500 self.prompt = self.continuationPrompt = '' 503 self.prompt = self.continuationPrompt = ''
501 self.cmdloop() 504 self.cmdloop()
502 self.stdin.close() 505 self.stdin.close()
503 keepstate.restore() 506 keepstate.restore()