From 91a33b792f41e3ed11ef13c935aa418f83162bb6 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 16 2015 09:01:41 +0000 Subject: [PATCH 1/3] Add the possibility to output the JSON in a human-friendly way --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 5c45c45..55fc552 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -95,6 +95,9 @@ def _get_pkg(branch, name): @asyncio.coroutine def get_pkg(request): branch = request.match_info.get('branch') + pretty = False + if request.query_string.lower() in ['pretty=1', 'pretty=true']: + pretty = True name = request.match_info.get('name') pkg, repotype = _get_pkg(branch, name) @@ -135,13 +138,20 @@ def get_pkg(request): output['co-packages'] = [] output['repo'] = repotype if repotype else 'release' session.close() - return web.Response(body=json.dumps(output).encode('utf-8')) + + return web.Response(body=json.dumps( + output, + sort_keys=pretty, indent=4, separators=(',', ': ') + ).encode('utf-8')) @asyncio.coroutine def get_pkg_files(request): branch = request.match_info.get('branch') name = request.match_info.get('name') + pretty = False + if request.query_string.lower() in ['pretty=1', 'pretty=true']: + pretty = True pkg, repotype = _get_pkg(branch, name) dbfile = '%s/mdapi-%s%s-filelists.sqlite' % ( @@ -153,16 +163,22 @@ def get_pkg_files(request): session2 = mdapilib.create_session('sqlite:///%s' % dbfile) filelist = mdapilib.get_files(session2, pkg.pkgId) session2.close() - return web.Response(body=json.dumps({ - 'files': [fileinfo.to_json() for fileinfo in filelist], - 'repo': repotype if repotype else 'release', - }).encode('utf-8')) + return web.Response(body=json.dumps( + { + 'files': [fileinfo.to_json() for fileinfo in filelist], + 'repo': repotype if repotype else 'release', + }, + sort_keys=pretty, indent=4, separators=(',', ': ') + ).encode('utf-8')) @asyncio.coroutine def get_pkg_changelog(request): branch = request.match_info.get('branch') name = request.match_info.get('name') + pretty = False + if request.query_string.lower() in ['pretty=1', 'pretty=true']: + pretty = True pkg, repotype = _get_pkg(branch, name) dbfile = '%s/mdapi-%s%s-other.sqlite' % ( @@ -174,10 +190,13 @@ def get_pkg_changelog(request): session2 = mdapilib.create_session('sqlite:///%s' % dbfile) changelogs = mdapilib.get_changelog(session2, pkg.pkgId) session2.close() - return web.Response(body=json.dumps({ - 'files': [changelog.to_json() for changelog in changelogs], - 'repo': repotype if repotype else 'release', - }).encode('utf-8')) + return web.Response(body=json.dumps( + { + 'files': [changelog.to_json() for changelog in changelogs], + 'repo': repotype if repotype else 'release', + }, + sort_keys=pretty, indent=4, separators=(',', ': ') + ).encode('utf-8')) @asyncio.coroutine @@ -190,7 +209,9 @@ def list_branches(request): for filename in os.listdir(CONFIG['DB_FOLDER']) if filename.startswith('mdapi') and filename.endswith('.sqlite') ])) - return web.Response(body=json.dumps(output).encode('utf-8')) + return web.Response(body=json.dumps( + output, sort_keys=pretty, indent=4, separators=(',', ': ') + ).encode('utf-8')) @asyncio.coroutine From a1bfeead9af908ae34c3e0ca97a10273f94b55d5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 16 2015 09:01:59 +0000 Subject: [PATCH 2/3] Avoid code duplication by using a method --- diff --git a/mdapi/__init__.py b/mdapi/__init__.py index 55fc552..f89eabb 100644 --- a/mdapi/__init__.py +++ b/mdapi/__init__.py @@ -92,12 +92,17 @@ def _get_pkg(branch, name): return (pkg, repotype) -@asyncio.coroutine -def get_pkg(request): - branch = request.match_info.get('branch') +def _get_pretty(request): pretty = False if request.query_string.lower() in ['pretty=1', 'pretty=true']: pretty = True + return pretty + + +@asyncio.coroutine +def get_pkg(request): + branch = request.match_info.get('branch') + pretty = _get_pretty(request) name = request.match_info.get('name') pkg, repotype = _get_pkg(branch, name) @@ -149,9 +154,7 @@ def get_pkg(request): def get_pkg_files(request): branch = request.match_info.get('branch') name = request.match_info.get('name') - pretty = False - if request.query_string.lower() in ['pretty=1', 'pretty=true']: - pretty = True + pretty = _get_pretty(request) pkg, repotype = _get_pkg(branch, name) dbfile = '%s/mdapi-%s%s-filelists.sqlite' % ( @@ -176,9 +179,7 @@ def get_pkg_files(request): def get_pkg_changelog(request): branch = request.match_info.get('branch') name = request.match_info.get('name') - pretty = False - if request.query_string.lower() in ['pretty=1', 'pretty=true']: - pretty = True + pretty = _get_pretty(request) pkg, repotype = _get_pkg(branch, name) dbfile = '%s/mdapi-%s%s-other.sqlite' % ( @@ -203,6 +204,7 @@ def get_pkg_changelog(request): def list_branches(request): ''' Return the list of all branches currently supported by mdapi ''' + pretty = _get_pretty(request) output = list(set([ # Remove the front part `mdapi-` and the end part -.sqlite filename.replace('mdapi-', '').rsplit('-', 2)[0].replace('-updates', '') From 512ce1555a960d2b1171f31af243a02142def129 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 16 2015 09:02:57 +0000 Subject: [PATCH 3/3] Use kernel-core instead of kernel as an example for the files endpoint --- diff --git a/mdapi/index.html b/mdapi/index.html index b2939f4..b1cf33a 100644 --- a/mdapi/index.html +++ b/mdapi/index.html @@ -91,9 +91,9 @@ specific branch by querying: /{branch}/files/{package name} -So for example, for the kernel in rawhide: +So for example, for the kernel-core in rawhide: - /rawhide/files/kernel + /rawhide/files/kernel-core Retrieve the changelog of a package