From 02c7ef89254ade53e60797bb0be8c1de68b501d5 Mon Sep 17 00:00:00 2001 From: Joerg Jaspert Date: Sat, 6 Dec 2014 15:03:56 +0100 Subject: [PATCH] Allow json format for madison Signed-off-by: Joerg Jaspert --- daklib/ls.py | 36 ++++++++++++++++++++++++++++++++++++ dakweb/queries/madison.py | 15 +++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/daklib/ls.py b/daklib/ls.py index aeb8cfcb..dacc23ba 100644 --- a/daklib/ls.py +++ b/daklib/ls.py @@ -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.") diff --git a/dakweb/queries/madison.py b/dakweb/queries/madison.py index d27d4784..eb8c45cd 100644 --- a/dakweb/queries/madison.py +++ b/dakweb/queries/madison.py @@ -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) -- 2.39.5