changeset 10:c1fe07bb6955

Use Requirement instead of VersionPredicate Requirement allow to use the project name while VersionPredicate only works with module name.
author Cédric Krier <ced@b2ck.com>
date Mon, 23 Feb 2015 16:54:10 +0100
parents 57028de987ac
children 0d6bc7c56cd6
files farmhouse.py
diffstat 1 files changed, 32 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/farmhouse.py	Mon Feb 23 11:37:57 2015 +0100
+++ b/farmhouse.py	Mon Feb 23 16:54:10 2015 +0100
@@ -12,10 +12,8 @@
 import xmlrpc.client
 from datetime import datetime
 from distutils.versionpredicate import VersionPredicate
-try:
-    from pkg_resources import parse_requirements
-except ImportError:
-    parse_requirements = None
+
+from pkg_resources import parse_requirements
 
 from flask import Flask, request, render_template, redirect, url_for, flash
 from flask.ext.bootstrap import Bootstrap
@@ -36,11 +34,11 @@
     packages = []
     for req in requirements.splitlines():
         try:
-            predicate = get_predicate(req)
+            req = get_requirement(req)
         except ValueError as exception:
             flash(str(exception))
             return render_template('index.html')
-        packages.append(str(predicate))
+        packages.append(str(req))
     if len(packages) == 1:
         package, = packages
         return redirect(url_for('atom', package=package))
@@ -48,7 +46,24 @@
         return redirect(url_for('atom', packages=packages))
 
 
+def get_requirement(req):
+    try:
+        req, = parse_requirements(req)
+        return req
+    except ValueError as exception:
+        try:
+            pred = VersionPredicate(req)
+        except ValueError:
+            raise exception
+        req = '%s%s' % (
+            pred.name,
+            ','.join('%s%s' % (cond, ver) for cond, ver in pred.pred))
+        req, = parse_requirements(req)
+        return req
+
+
 def get_predicate(req):
+    req = req.replace('-', '_')
     try:
         return VersionPredicate(req)
     except ValueError as exception:
@@ -83,20 +98,23 @@
 def render_atom(packages, title=None):
     packages = sorted(packages)
     releases = []
-    predicates = [VersionPredicate(p) for p in packages]
-    all_versions = get_versions((p.name for p in predicates))
-    all_urls = get_urls(((p.name, v)
-            for p, versions in zip(predicates, all_versions)
+    requirements = [get_requirement(p) for p in packages]
+    predicates = [get_predicate(p) for p in packages]
+    all_versions = get_versions((p.project_name for p in requirements))
+    all_urls = get_urls(((r.project_name, v)
+            for r, p, versions in zip(requirements, predicates, all_versions)
             for v in versions))
-    for predicate, versions in zip(predicates, all_versions):
+    for requirement, predicate, versions in zip(
+            requirements, predicates, all_versions):
         for version, urls in zip(versions, all_urls):
             if not predicate.satisfied_by(version):
                 continue
             for url in urls:
-                releases.append(Release(predicate.name, version, url))
+                releases.append(
+                    Release(requirement.project_name, version, url))
     context = {}
     if not title:
-        title = ', '.join(packages)
+        title = '; '.join(packages)
     context['title'] = '%s Feed' % title
     context['id'] = uuid.uuid5(uuid.NAMESPACE_URL, title).urn
     releases.sort(key=lambda r: r.datetime, reverse=True)
@@ -127,8 +145,7 @@
 
 @cache.memoize(timeout=2 * 60 * 60)
 def get_user_packages(user):
-    # package name could contains '-' which is not a valid name
-    return [p.replace('-', '_') for _, p in client.user_packages(user)]
+    return [p for _, p in client.user_packages(user)]
 
 
 class Release: