Mercurial > python-cmd2
comparison cmd2.py @ 223:1702ce785a8d
trying @http://
author | catherine@Elli.myhome.westell.com |
---|---|
date | Thu, 19 Mar 2009 05:55:15 -0400 |
parents | 9aa1f34455b4 |
children | 0a7e97f79a60 |
comparison
equal
deleted
inserted
replaced
222:1d3ad27f2b9e | 223:1702ce785a8d |
---|---|
24 CHANGES: | 24 CHANGES: |
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
26 flagReader.py options are still supported for backward compatibility | 26 flagReader.py options are still supported for backward compatibility |
27 """ | 27 """ |
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
29 import unittest, string, datetime | 29 import unittest, string, datetime, urllib |
30 from optparse import make_option | 30 from optparse import make_option |
31 __version__ = '0.4.8' | 31 __version__ = '0.4.8' |
32 | 32 |
33 class OptionParser(optparse.OptionParser): | 33 class OptionParser(optparse.OptionParser): |
34 def exit(self, status=0, msg=None): | 34 def exit(self, status=0, msg=None): |
871 f.close() | 871 f.close() |
872 print 'Saved to %s' % (fname) | 872 print 'Saved to %s' % (fname) |
873 except Exception, e: | 873 except Exception, e: |
874 print 'Error saving %s: %s' % (fname, str(e)) | 874 print 'Error saving %s: %s' % (fname, str(e)) |
875 | 875 |
876 def do_load(self, fname=None): | 876 urlre = re.compile('(https?://[-\\w\\./]+)') |
877 def do_load(self, fname=None): | |
877 """Runs command(s) from a file.""" | 878 """Runs command(s) from a file.""" |
878 if fname is None: | 879 if fname is None: |
879 fname = self.default_file_name | 880 fname = self.default_file_name |
880 fname = os.path.expanduser(fname) | 881 #keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuation_prompt')) |
881 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuation_prompt')) | 882 keepstate = Statekeeper(self, ('stdin','use_rawinput','continuation_prompt')) |
882 if isinstance(fname, file): | 883 try: |
883 self.stdin = fname | 884 if isinstance(fname, file): |
884 else: | 885 target = open(fname, 'r') |
885 try: | 886 else: |
886 self.stdin = open(os.path.expanduser(fname), 'r') | 887 match = self.urlre.match(fname) |
887 except IOError, e: | 888 if match: |
888 try: | 889 target = urllib.urlopen(match.group(1)) |
889 self.stdin = open('%s.%s' % (os.path.expanduser(fname), self.defaultExtension), 'r') | 890 else: |
890 except IOError: | 891 fname = os.path.expanduser(fname) |
891 print 'Problem opening file %s: \n%s' % (fname, e) | 892 try: |
892 keepstate.restore() | 893 target = open(os.path.expanduser(fname), 'r') |
893 return | 894 except IOError, e: |
895 target = open('%s.%s' % (os.path.expanduser(fname), | |
896 self.defaultExtension), 'r') | |
897 except IOError, e: | |
898 print 'Problem opening file %s: \n%s' % (fname, e) | |
899 keepstate.restore() | |
900 return | |
901 self.stdin = target | |
894 self.use_rawinput = False | 902 self.use_rawinput = False |
895 self.prompt = self.continuation_prompt = '' | 903 self.prompt = self.continuation_prompt = '' |
896 stop = self.cmdloop() | 904 stop = self.cmdloop() |
897 self.stdin.close() | 905 self.stdin.close() |
898 keepstate.restore() | 906 keepstate.restore() |