Ben Bass avatar Ben Bass committed 751ac22

add README.txt, improve setup.py

Comments (0)

Files changed (7)

+xmlcmd
+~~~~~~
+
+xmlcmd is a proof of concept experiment in augmenting standard POSIX commands
+with superpowers, such as outputting xml.
+
+Sadly it is more sleight of hand than any real magic.
+
+To begin with, a directory which is first on the ``PATH`` must be created
+(e.g. ~/bin). This should not be on the path which ``whereis`` uses, so if
+things go wrong you can always do ``$(whereis cmd)`` to execute affected
+commands.
+
+The idea is to put symlinks to xmlcmd (which is created in the normal ``python
+setup.py install`` installation) into this directory for a number of commands.
+When these commands are run with an --xml option, the _{cmd} module will be
+imported from the xmlcmd package and the ``main()`` function run with two
+arguments: the original list of command line arguments (typically corresponding
+to sys.argv) minus the ``--xml``, and the full path to the 'original' file
+which would have been run if the ``--xml`` option had not been specified.
+
+::
+
+    ben$ sudo pip install xmlcmd
+    ...
+    ben$ ln -sf $(which xmlcmd) ~/bin/ls
+    ben$ ls --xml
+    <a XMLish representation of a file listing...>
+
+Ben Bass 2011
+#!/usr/bin/env python
+from xmlcmd import xmlcmd
+raise SystemExit(xmlcmd.main())
     author_email="benbass@codedstructure.net",
     url="http://bitbucket.org/codedstructure/xmlcmd",
     packages=["xmlcmd"],
-    install_requires=['which'],
+    requires=['which'],
+    scripts=['scripts/xmlcmd'],
     classifiers=[
         "Development Status :: 3 - Alpha",
         "Environment :: Console",

xmlcmd/__init__.py

+"""xmlcmd"""
 from xml.etree import cElementTree as ET
 
 
-def main(args=None):
+def main(args=None,orig_cmd_path=None):
     if args is None:
         args = sys.argv
 
         ET.SubElement(f_el, 'name').text = fn
         ET.SubElement(f_el, 'size').text = str(stat.st_size)
     ET.ElementTree(root).write(sys.stdout, 'utf-8')
+    sys.stdout.write('\n')
 
 if __name__ == '__main__':
     main()
                 heading = heading[1:]
             ET.SubElement(p, escape(heading.lower())).text = escape(item)
     ET.ElementTree(root).write(sys.stdout, 'utf-8')
+    sys.stdout.write('\n')
 
 if __name__ == '__main__':
     main()
 
 def process_cmd(cmd_name, args, orig_cmd_path):
     """
+    import and call the main() function from the module
+    xmlcmd._{cmd}
     """
-    module = __import__('_{cmd_name}'.format(cmd_name=cmd_name))
-    raise SystemExit(module.main(args))
+    module = __import__('xmlcmd._%s' % cmd_name,
+                        fromlist=['_%s' % cmd_name])
+    raise SystemExit(module.main(args, orig_cmd_path))
 
 def main(args=None):
     """
 
     # args[0] will be a full path - we only want the command name
     cmd_name = os.path.basename(args[0])
+    if cmd_name.startswith('xmlcmd'):
+        raise SystemExit('xmlcmd should not be called directly')
 
     # get the command which would have run if we hadn't sneaked
     # ahead of it in the $PATH
     if '--xml' in args:
         args.remove('--xml')
         # forward to our xmlized version...
-        process_cmd(cmd_name, args, orig_cmd_path=orig_cmd_path)
+        process_cmd(cmd_name, args, orig_cmd_path)
     else:
         # execv *replaces* this process, so it has no idea it
         # wasn't called directly. Total transparency.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.