changeset 369:11cf7018ebff

new crappy package for helping me with http://k0s.org/portfolio/python/python-tools.txt
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 21 Jul 2013 05:27:28 -0700
parents 6f1e4562c06a
children 4198a58cc520
files python/pypi.py
diffstat 1 files changed, 92 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/pypi.py	Sun Jul 21 05:27:28 2013 -0700
@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+
+"""
+pypi strawman that should really be in PaInt
+
+ref http://wiki.python.org/moin/PyPIJSON
+"""
+
+import json
+import optparse
+import os
+import sys
+import textwrap
+import urllib2
+
+def package_data(package, index='http://pypi.python.org/pypi'):
+    """
+    returns dict of {url:, description:, home_page: }
+    """
+
+    # if package name given, -> url
+    url = package
+    if '://' not in url:
+        url = '%s/%s' % (index, package)
+    url = url.rstrip('/')
+
+    if not url.endswith('/json'):
+        url = '%s/json' % (url)
+
+    # read the URL
+    try:
+        page = urllib2.urlopen(url).read()
+    except:
+        raise # TODO: better error handling
+
+    data = json.loads(page)
+    info = data['info']
+
+    # massage the data
+    return {'summary': info.get('summary', ''),
+            'package_url': info['package_url'],
+            'name': info['name'],
+            'home_page': info.get('home_page')
+    # etc
+            }
+
+
+def main(args=sys.argv[1:]):
+
+    # command line parser
+    usage = '%prog [options] package <another_package> <...>'
+    parser = optparse.OptionParser(usage=usage, description=__doc__)
+    parser.add_option('-t', '--tag', dest='tags',
+                      action='append', help="tags for packages")
+    options, args = parser.parse_args(args)
+    if not args:
+        parser.print_usage()
+        parser.exit()
+
+    # one crappy ass template
+    template = """ * %(package_url)s
+%(summary)s
+  %(tags)s
+"""
+
+    # get package data
+    packages = {}
+    for package in args:
+        data = package_data(package)
+        packages[data['name']] = data
+
+    # render the damn thing
+    rendered = []
+    for name in sorted(packages.keys(), key=lambda x: x.lower()):
+        values = packages[name].copy()
+        if options.tags:
+            values['tags'] = '{%s}' % ', '.join(options.tags)
+        else:
+            values['tags'] = ''
+        if values['home_page'] and values['home_page'] != values['package_url']:
+            values['summary'] = '%s %s' % (values['summary'],
+                                               values['home_page'])
+            values['summary'] = textwrap.fill(values['summary'], 69,
+                                              initial_indent='  ',
+                                              subsequent_indent='  ')
+        string = template % values
+        rendered.append(string.strip())
+
+    print '\n\n'.join([i.strip() for i in rendered])
+
+if __name__ == '__main__':
+    main()