From 48e3e41ee508ae97406e65a391b10fe1312a2bb3 Mon Sep 17 00:00:00 2001 From: Lingchao Xin Date: Tue, 21 Jan 2014 16:25:32 +0800 Subject: [PATCH] Make method interface flexible Change-Id: Ic5aa5bbf3536fc1df7e9a796d35ee6c8432199f9 --- snapdiff/__init__.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/snapdiff/__init__.py b/snapdiff/__init__.py index 8801109..69ac8f6 100644 --- a/snapdiff/__init__.py +++ b/snapdiff/__init__.py @@ -36,12 +36,19 @@ from .render import output_html from .repo import Repo -def diff_to_json(old_url, new_url, style='repo'): - """Output diffs' json format""" +def diff_to_json(old_url, new_url, **kwargs): + """Output diffs' json format. + + :param old_url: old repo or image url + :param new_url: new repo or image url + :param style: (optional) repo or image type, default is repo + """ if not old_url or not new_url: return + style = kwargs.get('style') or 'repo' + if style == 'repo': old, new = Repo(old_url).packages, Repo(new_url).packages elif style == 'image': @@ -167,10 +174,17 @@ def diff_to_json(old_url, new_url, style='repo'): return json.dumps(obj, indent=4) -def diff_to_html(old_url, new_url, style='repo'): - """Output diffs' html format""" +def diff_to_html(old_url, new_url, **kwargs): + """Output diffs' html format. + + :param old_url: old repo or image url + :param new_url: new repo or image url + :param style: repo or image type, default is repo + """ + + style = kwargs.get('style') or 'repo' - json_obj = diff_to_json(old_url, new_url, style) + json_obj = diff_to_json(old_url, new_url, style=style) if json_obj is None: return @@ -189,11 +203,19 @@ def diff_to_html(old_url, new_url, style='repo'): elif style == 'image': return output_html('image_diff.html', **context) # pylint: disable=W0142 -def diff_to_dist(old_url, new_url, dist_path, style='repo', diff_name='diff'): - """create a dist-dir of diffs, contains html and css""" +def diff_to_dist(old_url, new_url, dist_path, **kwargs): + """Create a dist-dir of diffs, contains html and css. + + :param old_url: old repo or image url + :param new_url: new repo or image url + :param dist_path: where you put diff + :param diff_name: (optional) the diff file name + :param style: (optional) repo or image type, default is repo + """ static_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'static') - output = diff_to_html(old_url, new_url, style) + + output = diff_to_html(old_url, new_url, style=kwargs.get('style')) if not os.path.exists(dist_path): os.makedirs(dist_path) @@ -211,6 +233,8 @@ def diff_to_dist(old_url, new_url, dist_path, style='repo', diff_name='diff'): shutil.copy(os.path.join(root, filename), os.path.join(dist_path, 'img', filename)) + diff_name = kwargs.get('diff_name') or 'diff' + with open(os.path.join(dist_path, '%s.html' % diff_name), 'w') as fname: fname.write(output) return dist_path -- 2.34.1