From a7b02c8875d5b26d8979c50bbfd5a81c19a9be26 Mon Sep 17 00:00:00 2001 From: Dafydd Harries Date: Wed, 20 Mar 2013 21:14:17 -0400 Subject: factor out multi-file output --- export.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/export.py b/export.py index 1198f82..c891cc3 100644 --- a/export.py +++ b/export.py @@ -290,6 +290,21 @@ def output(path, xs): for x in xs: f.write(str(x) + '\n') +def output_multi(path, xs): + index = {} + + if not os.path.exists(path): + os.makedirs(path) + + for (name, templates) in xs: + fname = filename(name) + fpath = os.path.join(path, fname) + index[fname] = {'page': name, 'file': fname} + output(fpath, templates) + + fpath = os.path.join(path, 'index.json') + json.dump(index, file(fpath, 'w')) + def uname_srcpkgs(data, name): pkg_cps = data.cps[data.cps['Upstream-Name'] == name] srcpkg_names = list(pkg_cps['_srcpkg']) @@ -325,24 +340,19 @@ def export_all(data): yield (name, templates) def export_all_to_directory(data, outputdir): - index = {} - - if not os.path.exists(outputdir): - os.makedirs(outputdir) + def _export(): + for (name, templates) in export_all(data): + print (name.encode('utf8') if isinstance(name, unicode) else name) - for (name, templates) in export_all(data): - print (name.encode('utf8') if isinstance(name, unicode) else name) - fname = filename(name) - path = os.path.join(outputdir, fname) - index[fname] = {'page': name, 'file': fname} + try: + # Force errors. + templates = list(templates) + except ExportFailure, e: + warn('export failed: %s: %s' % (name, e.message)) - try: - output(path, templates) - except ExportFailure, e: - warn('export failed: %s: %s' % (name, e.message)) + yield(name, templates) - path = os.path.join(outputdir, 'index.json') - json.dump(index, file(path, 'w')) + output_multi(outputdir, _export()) def main(): data = PkgData() -- cgit v1.2.3