Make method interface flexible
authorLingchao Xin <lingchaox.xin@intel.com>
Tue, 21 Jan 2014 08:25:32 +0000 (16:25 +0800)
committerLingchao Xin <lingchaox.xin@intel.com>
Tue, 21 Jan 2014 08:25:32 +0000 (16:25 +0800)
Change-Id: Ic5aa5bbf3536fc1df7e9a796d35ee6c8432199f9

snapdiff/__init__.py

index 8801109449209fb373d7e33c3f2516c3113f83aa..69ac8f6831c9b51e76b44fb7babc1f3ad02437fe 100644 (file)
@@ -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