[Idle-dev] CVS: idle PyShell.py,1.36,1.37

Kurt B. Kaiser kbk@users.sourceforge.net
Tue, 10 Dec 2002 20:42:07 -0800


Update of /cvsroot/idlefork/idle
In directory sc8-pr-cvs1:/tmp/cvs-serv30655

Modified Files:
	PyShell.py 
Log Message:
Rework the command line interface, incorporating the shell/edit
configuration selection. Rework the usage message to match.

Also some minor code polishing.


Index: PyShell.py
===================================================================
RCS file: /cvsroot/idlefork/idle/PyShell.py,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -r1.36 -r1.37
*** PyShell.py	6 Dec 2002 21:45:23 -0000	1.36
--- PyShell.py	11 Dec 2002 04:42:04 -0000	1.37
***************
*** 11,15 ****
  import traceback
  import types
- import warnings
  import exceptions
  
--- 11,14 ----
***************
*** 33,39 ****
  IDENTCHARS = string.ascii_letters + string.digits + "_"
  
- # XX hardwire this for now, remove later  KBK 09Jun02
- use_subprocess = 1 # Set to 1 to spawn subprocess for command execution
- 
  # Change warnings module to write to sys.__stderr__
  try:
--- 32,35 ----
***************
*** 72,79 ****
      "Regular text edit window when a shell is present"
  
!     # XXX KBK 19Oct02 Breakpoints are currently removed if module is
!     # changed or closed.  Future plans include saving breakpoints in a
!     # project file and possibly preserving breakpoints by changing their
!     # line numbers as a module is modified.
  
      def __init__(self, *args):
--- 68,74 ----
      "Regular text edit window when a shell is present"
  
!     # XXX KBK 10Dec02 Breakpoints are currently removed if module is modified.
!     # In the future, it may be possible to preserve breakpoints by changing
!     # their line numbers as a module is modified.
  
      def __init__(self, *args):
***************
*** 318,321 ****
--- 313,317 ----
          self.rpcclt.register("stderr", self.tkconsole.stderr)
          self.rpcclt.register("flist", self.tkconsole.flist)
+         self.transfer_path()
          self.poll_subprocess()
  
***************
*** 329,332 ****
--- 325,329 ----
          self.spawn_subprocess()
          self.rpcclt.accept()
+         self.transfer_path()
          # restart remote debugger
          if debug:
***************
*** 335,338 ****
--- 332,342 ----
              debug.load_breakpoints()
  
+     def transfer_path(self):
+         self.runcommand("""if 1:
+         import sys as _sys
+         _sys.path = %s
+         del _sys
+         \n""" % `sys.path`)
+ 
      active_seq = None
  
***************
*** 587,591 ****
      UndoDelegator = ModifiedUndoDelegator
  
!     # Override menus: Run and Format not desired in shell; add Debug
      menu_specs = [
          ("file", "_File"),
--- 591,595 ----
      UndoDelegator = ModifiedUndoDelegator
  
!     # Override menus
      menu_specs = [
          ("file", "_File"),
***************
*** 1009,1113 ****
  
  usage_msg = """\
- usage: idle.py [-c command] [-d] [-i] [-r script] [-s] [-t title] [arg] ...
  
! idle file(s)    (without options) edit the file(s)
  
! -c cmd     run the command in a shell
! -d         enable the debugger
! -e         edit mode; arguments are files to be edited
! -i         open an interactive shell
! -i file(s) open a shell and also an editor window for each file
! -r
! -s         run $IDLESTARTUP or $PYTHONSTARTUP before anything else
! -t title   set title of shell window
  
! Remaining arguments are applied to the command (-c) or script (-r).
  """
  
  def main():
      cmd = None
-     edit = 0
-     debug = 0
      script = None
!     startup = 0
! 
      try:
!         opts, args = getopt.getopt(sys.argv[1:], "c:deir:st:")
      except getopt.error, msg:
          sys.stderr.write("Error: %s\n" % str(msg))
          sys.stderr.write(usage_msg)
          sys.exit(2)
- 
      for o, a in opts:
          if o == '-c':
              cmd = a
          if o == '-d':
!             debug = 1
          if o == '-e':
!             edit = 1
          if o == '-r':
              script = a
          if o == '-s':
!             startup = 1
          if o == '-t':
              PyShell.shell_title = a
  
!     if args and args[0] != "-": edit = 1
! 
      for i in range(len(sys.path)):
          sys.path[i] = os.path.abspath(sys.path[i])
! 
!     pathx = []
!     if edit:
          for filename in args:
              pathx.append(os.path.dirname(filename))
!     elif args and args[0] != "-":
!         pathx.append(os.path.dirname(args[0]))
!     else:
!         pathx.append(os.curdir)
!     for dir in pathx:
!         dir = os.path.abspath(dir)
!         if not dir in sys.path:
!             sys.path.insert(0, dir)
! 
!     global flist, root
      root = Tk(className="Idle")
      fixwordbreaks(root)
      root.withdraw()
      flist = PyShellFileList(root)
! 
!     if edit:
!         for filename in args:
!             flist.open(filename)
!         if not args:
!             flist.new()
!     else:
!         if cmd:
!             sys.argv = ["-c"] + args
!         else:
!             sys.argv = args or [""]
! 
!     shell = PyShell(flist)
!     interp = shell.interp
!     flist.pyshell = shell
! 
      if startup:
          filename = os.environ.get("IDLESTARTUP") or \
                     os.environ.get("PYTHONSTARTUP")
          if filename and os.path.isfile(filename):
!             interp.execfile(filename)
! 
!     if debug:
!         shell.open_debugger()
!     if cmd:
!         interp.execsource(cmd)
!     elif script:
!         if os.path.isfile(script):
!             interp.execfile(script)
!         else:
!             print "No script file: ", script
!     shell.begin()
      root.mainloop()
      root.destroy()
  
  def display_port_binding_error():
--- 1013,1177 ----
  
  usage_msg = """\
  
! USAGE: idle  [-deis] [-t title] [file]*             
!        idle  [-ds] [-t title] (-c cmd | -r file) [arg]*
!        idle  [-ds] [-t title] - [arg]*
!        
!   -h         print this help message and exit
! 
! The following options will override the IDLE 'settings' configuration:
! 
!   -e         open an edit window
!   -i         open a shell window
! 
! The following options imply -i and will open a shell:
! 
!   -c cmd     run the command in a shell, or
!   -r file    run script from file
! 
!   -d         enable the debugger
!   -s         run $IDLESTARTUP or $PYTHONSTARTUP before anything else
!   -t title   set title of shell window
! 
! A default edit window will be bypassed when -c, -r, or - are used.
! 
! [arg]* are passed to the command (-c) or script (-r) in sys.argv[1:].
! 
! Examples:
  
! idle
!         Open an edit window or shell depending on IDLE's configuration.
  
! idle foo.py foobar.py
!         Edit the files, also open a shell if configured to start with shell.
! 
! idle -est "Baz" foo.py
!         Run $IDLESTARTUP or $PYTHONSTARTUP, edit foo.py, and open a shell
!         window with the title "Baz".
! 
! idle -c "import sys; print sys.argv" "foo"
!         Open a shell window and run the command, passing "-c" in sys.argv[0]
!         and "foo" in sys.argv[1].
! 
! idle -d -s -r foo.py "Hello World"
!         Open a shell window, run a startup script, enable the debugger, and
!         run foo.py, passing "foo.py" in sys.argv[0] and "Hello World" in
!         sys.argv[1].
! 
! echo "import sys; print sys.argv" | idle - "foobar"
!         Open a shell window, run the script piped in, passing '' in sys.argv[0]
!         and "foobar" in sys.argv[1].
  """
  
  def main():
+     global flist, root, use_subprocess
+ 
+     enable_shell = False
+     enable_edit = False
+     debug = False
      cmd = None
      script = None
!     startup = False
      try:
!         opts, args = getopt.getopt(sys.argv[1:], "c:deihr:st:")
      except getopt.error, msg:
          sys.stderr.write("Error: %s\n" % str(msg))
          sys.stderr.write(usage_msg)
          sys.exit(2)
      for o, a in opts:
          if o == '-c':
              cmd = a
+             enable_shell = True
          if o == '-d':
!             debug = True
!             enable_shell = True
          if o == '-e':
!             enable_edit = True
!         if o == '-h':
!             sys.stdout.write(usage_msg)
!             sys.exit()
!         if o == '-i':
!             enable_shell = True
          if o == '-r':
              script = a
+             if os.path.isfile(script):
+                 pass
+             else:
+                 print "No script file: ", script
+                 sys.exit()
+             enable_shell = True
          if o == '-s':
!             startup = True
!             enable_shell = True
          if o == '-t':
              PyShell.shell_title = a
+             enable_shell = True
+     if args and args[0] == '-':
+         cmd = sys.stdin.read()
+         enable_shell = True
+         
+     use_subprocess = True
  
!     # process sys.argv and sys.path:
      for i in range(len(sys.path)):
          sys.path[i] = os.path.abspath(sys.path[i])
!     if args and args[0] == '-':
!         sys.argv = [''] + args[1:]
!     elif cmd:
!         sys.argv = ['-c'] + args
!     elif script:
!         sys.argv = [script] + args
!     elif args:
!         enable_edit = True
!         pathx = []
          for filename in args:
              pathx.append(os.path.dirname(filename))
!         for dir in pathx:
!             dir = os.path.abspath(dir)
!             if not dir in sys.path:
!                 sys.path.insert(0, dir)
!     # check the IDLE settings configuration (but command line overrides)
!     edit_start = idleConf.GetOption('main', 'General',
!                                     'editor-on-startup', type='bool') 
!     enable_edit = enable_edit or edit_start
!     enable_shell = enable_shell or not edit_start  
!     # start editor and/or shell windows:
      root = Tk(className="Idle")
      fixwordbreaks(root)
      root.withdraw()
      flist = PyShellFileList(root)
!     if enable_edit:
!         if not (cmd or script):
!             for filename in args:
!                 flist.open(filename)
!             if not args:
!                 flist.new()
!         if enable_shell:
!             flist.open_shell()
!     elif enable_shell:
!         flist.pyshell = PyShell(flist)
!         flist.pyshell.begin()
!     shell = flist.pyshell
!     # handle remaining options:
!     if debug:
!         shell.open_debugger()
      if startup:
          filename = os.environ.get("IDLESTARTUP") or \
                     os.environ.get("PYTHONSTARTUP")
          if filename and os.path.isfile(filename):
!             shell.interp.execfile(filename)
!     if cmd or script:
!         shell.interp.runcommand("""if 1:
!             import sys as _sys
!             _sys.argv = %s
!             del _sys
!             \n""" % `sys.argv`)
!         if cmd:
!             shell.interp.execsource(cmd)
!         elif script:
!             shell.interp.execfile(script)
      root.mainloop()
      root.destroy()
+ 
  
  def display_port_binding_error():