1. Richard Jones
  2. e

Commits

Richard Jones  committed 5e06411

add edit/view and only evaluate one expression using all arguments

  • Participants
  • Parent commits cdecfba
  • Branches default

Comments (0)

Files changed (2)

File e.py

View file
 #! /usr/bin/env python
 
 # Public domain
-# Idea from Georg Brandl. Foolishly implemented by Michael Foord
-# E-mail: fuzzyman AT voidspace DOT org DOT uk
+# Idea from Georg Brandl. Foolishly implemented by Michael Foord and Richard
+# Jones
+# E-mail: richard AT mechanicalcat DOT net
 
+import os
 import sys
+import subprocess
 
 
 def execute(arg):
     exec (compile(arg, '<cmdline>', 'single'))
 
+
+def locate(arg):
+    try:
+        __import__(arg)
+    except ImportError:
+        return None
+
+    try:
+        mod = sys.modules[arg]
+    except KeyError:
+        print ('%s is not a valid module name' % arg)
+        sys.exit(1)
+
+    location = getattr(mod, '__file__', 'None')
+    if location.endswith('.pyc'):
+        location = location[:-1]
+    return location
+
+
 def main(args):
     if not args:
-        print ('Idea from Georg Brandl. Foolishly implemented by Michael Foord')
+        print ('Idea from Georg Brandl. Foolishly implemented by Michael '
+            'Foord and Richard Jones')
         sys.exit()
 
-    first = args[0]
-    try:
-        execute(first)
-    except NameError:
-        try:
-            __import__(first)
-        except ImportError:
-            # easy way to re-raise the original error
-            execute(first)
-        else:
-            try:
-                mod = sys.modules[first]
-            except KeyError:
-                print ('%s is not a valid module name' % first)
-                sys.exit(1)
-        
-            location = getattr(mod, '__file__', 'None')
-            if location.endswith('.pyc'):
-                location = location[:-1]
-            print (location)
+    fn = locate(args[0])
+    if fn is None:
+        # right, just try to execute the stuff we're
+        execute(' '.join(args))
+        return
 
-    for x in args[1:]:
-       execute(x)
+    if len(args) == 1:
+        print (fn)
+        return
+
+    if not fn.endswith('.py'):
+        sys.exit('Module (%s) is not Python' % fn)
+
+    prog = args[1]
+    if prog == 'edit':
+        prog = os.environ['EDITOR']
+    elif prog == 'view':
+        prog = os.environ.get('PAGER', 'less')
+    sys.exit(subprocess.call(' '.join([prog, fn]), shell=True))
 
 
 if __name__ == '__main__':

File setup.py

View file
 # perform the setup action
 setup(
     name="e",
-    version='1.1',
-    description=u"python -me",
+    version='1.4',
+    description='''Evaluate and display command line expressions with
+        python -me expr''',
+    long_description=u"""Evaluate and display command line expressions with
+``python -me expr``.
+
+For example::
+
+.. code:
+
+    $ python -me 1+1
+    2
+
+Like python -c but no need for a print statement.
+
+It allows multiple expressions::
+
+    $ python -me 1+1 2+2
+    2
+    4
+
+As a bonus, if the first argument is a module name then it will output the
+location of the module source code:
+
+.. code:
+
+    $ python -me os
+    /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
+
+If you follow the name of the module with a command then the module will be
+opened with that command. For example, the following will open the os module
+source in vim:
+
+.. code:
+
+    $ python -me os vim
+
+The "e" module recognises the special command names "edit" and "view" which
+will result in it looking up your editor and viewer commands in the
+environment variables $EDITOR and $PAGER respectively. The latter defaults to
+"less". This is only just slightly easier than writing, for example:
+
+.. code:
+
+    $ vim `python -me os`
+""",
     author="Georg Brandl, Michael Foord and Richard Jones",
     author_email="richard@python.org",
     scripts=['e.py'],