]> git.decadent.org.uk Git - dak.git/commitdiff
Allow json format for madison
authorJoerg Jaspert <joerg@debian.org>
Sat, 6 Dec 2014 14:03:56 +0000 (15:03 +0100)
committerJoerg Jaspert <joerg@debian.org>
Sat, 6 Dec 2014 14:14:20 +0000 (15:14 +0100)
Signed-off-by: Joerg Jaspert <joerg@debian.org>
daklib/ls.py
dakweb/queries/madison.py

index aeb8cfcbb8b92eb7008d663a05e933b5f5666e06..dacc23bab2edaec684c7baccae46c1fda6f04a3a 100644 (file)
@@ -22,6 +22,8 @@ import sqlalchemy.sql as sql
 import daklib.daksql as daksql
 
 from daklib.dbconn import DBConn, session_wrapper
+from collections import defaultdict
+
 
 @session_wrapper
 def list_packages(packages, suites=None, components=None, architectures=None, binary_types=None,
@@ -72,6 +74,40 @@ def list_packages(packages, suites=None, components=None, architectures=None, bi
         result = session.execute(query)
         for row in result:
             yield "{0} {1} {2}".format(row[t.c.package], row[t.c.version], row[t.c.architecture])
+    elif format == "python":
+        c_architectures = daksql.string_agg(t.c.architecture, ',', order_by=[t.c.architecture_is_source.desc(), t.c.architecture])
+        query = sql.select([t.c.package,
+                            t.c.version,
+                            t.c.display_suite,
+                            c_architectures,
+                            t.c.source,
+                            t.c.source_version,
+                            t.c.component]) \
+            .where(where) \
+            .group_by(t.c.package,
+                      t.c.version,
+                      t.c.display_suite,
+                      t.c.source,
+                      t.c.component,
+                      t.c.source_version)
+        result = session.execute(query).fetchall()
+
+        if len(result) == 0:
+            raise StopIteration
+
+        val = lambda: defaultdict(val)
+        ret = val()
+        for row in result:
+            ret[row[t.c.package]] \
+               [row[t.c.display_suite]] \
+               [row[t.c.version]]={'component':      row[t.c.component],
+                                   'architectures':  row[c_architectures].split(','),
+                                   'source':         row[t.c.source],
+                                   'source_version': row[t.c.source_version]
+                               }
+
+        yield ret
+        return
     else:
         raise ValueError("Unknown output format requested.")
 
index d27d4784864caa1fbf9b2696dfbf1bcbe6d93eba..eb8c45cd6ffaac27891d840bd624486125369fed 100644 (file)
@@ -24,12 +24,19 @@ def madison():
         kwargs['source_and_binary'] = True
     #if 'r' in r.query:
     #    kwargs['regex'] = True
+    format = r.query.get('f', None)
+    if format is not None:
+        kwargs['format'] = 'python'
 
     result = list_packages(packages, **kwargs)
 
-    bottle.response.content_type = 'text/plain'
-    for row in result:
-        yield row
-        yield "\n"
+    if format is None:
+        bottle.response.content_type = 'text/plain'
+        for row in result:
+            yield row
+            yield "\n"
+    else:
+        yield json.dumps(list(result))
+
 
 QueryRegister().register_path('/madison', madison)