Pylint the world
authorLingchao Xin <lingchaox.xin@intel.com>
Mon, 20 Jan 2014 04:09:46 +0000 (12:09 +0800)
committerLingchao Xin <lingchaox.xin@intel.com>
Mon, 20 Jan 2014 04:09:46 +0000 (12:09 +0800)
Change-Id: I8dd986e2f72c5ec308bcd4e67cc9292bae1f71c1

snapdiff/__init__.py
snapdiff/image.py
snapdiff/render.py
snapdiff/repo.py
snapdiff/utils.py
tools/snap-diff

index 03ceb65d04444c9034793dc6a6082ca6450557e3..8801109449209fb373d7e33c3f2516c3113f83aa 100644 (file)
@@ -2,8 +2,8 @@
 Snapdiff is used to generate image and repo diffs. You can use it as a module:
 
    >>> import snapdiff
-   >>> snapdiff.diff_to_JSON('old url', 'new url', style='repo')
-   >>> snapdiff.diff_to_HTML('old url', 'new url', style='image')
+   >>> snapdiff.diff_to_json('old url', 'new url', style='repo')
+   >>> snapdiff.diff_to_html('old url', 'new url', style='image')
 
 or use it directly:
     snap-diff [-h] [--json] [-t] old new
@@ -25,6 +25,7 @@ or use it directly:
 __title__ = 'python-snapdiff'
 __version__ = '0.1'
 
+from itertools import izip_longest
 import json
 import os
 import shutil
@@ -35,7 +36,7 @@ from .render import output_html
 from .repo import Repo
 
 
-def diff_to_JSON(old_url, new_url, style='repo'):
+def diff_to_json(old_url, new_url, style='repo'):
     """Output diffs' json format"""
 
     if not old_url or not new_url:
@@ -52,6 +53,7 @@ def diff_to_JSON(old_url, new_url, style='repo'):
     package_names = set(old.__dict__.keys() + new.__dict__.keys())
 
     def _pair_old_new():
+        """Generate old and new pkg pair"""
         for name in package_names:
             if old[name] is None:
                 for pkg in new[name]:
@@ -66,7 +68,7 @@ def diff_to_JSON(old_url, new_url, style='repo'):
                             yield (old_pkg, new_pkg)
                             old[name].remove(old_pkg)
                             new[name].remove(new_pkg)
-                for pair in map(None, old[name], new[name]):
+                for pair in izip_longest(old[name], new[name]):
                     yield pair
     for old_pkg, new_pkg in _pair_old_new():
         if old_pkg is None:
@@ -165,10 +167,10 @@ 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'):
+def diff_to_html(old_url, new_url, style='repo'):
     """Output diffs' html format"""
 
-    json_obj = diff_to_JSON(old_url, new_url, style)
+    json_obj = diff_to_json(old_url, new_url, style)
 
     if json_obj is None:
         return
@@ -177,26 +179,26 @@ def diff_to_HTML(old_url, new_url, style='repo'):
 
     context = {
         'old': {'url': old_url,
-            'name': re.search('\w*_\d{8}\.\d*', old_url).group(0)},
+            'name': re.search(r'\w*_\d{8}\.\d*', old_url).group(0)},
         'new': {'url': new_url,
-            'name': re.search('\w*_\d{8}\.\d*', new_url).group(0)},
+            'name': re.search(r'\w*_\d{8}\.\d*', new_url).group(0)},
         'diff': data['diff'],
         }
     if style == 'repo':
-        return output_html('diff.html', **context)
+        return output_html('diff.html', **context) # pylint: disable=W0142
     elif style == 'image':
-        return output_html('image_diff.html', **context)
+        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"""
     static_dir = os.path.join(
         os.path.dirname(os.path.abspath(__file__)), 'static')
-    output_html = diff_to_HTML(old_url, new_url, style)
+    output = diff_to_html(old_url, new_url, style)
 
     if not os.path.exists(dist_path):
         os.makedirs(dist_path)
 
-    for root, dirs, files in os.walk(static_dir):
+    for root, _dirs, files in os.walk(static_dir):
         for filename in files:
             if filename.endswith('.css'):
                 if not os.path.exists(os.path.join(dist_path, 'css')):
@@ -209,6 +211,6 @@ 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))
 
-    with open(os.path.join(dist_path, '%s.html' % diff_name), 'w') as fp:
-        fp.write(output_html)
+    with open(os.path.join(dist_path, '%s.html' % diff_name), 'w') as fname:
+        fname.write(output)
     return dist_path
index e52dbf1bd29b42420982ccbac7ed8087156f111c..32dfbe4346f34e0ccb80103dfd90ac2a0ea1e3f0 100644 (file)
@@ -1,3 +1,6 @@
+
+"""This module handles image related"""
+
 import difflib
 import glob
 import os
@@ -9,6 +12,7 @@ from .utils import JsonDict
 
 
 def _get_file(url, suffix='.ks'):
+    """Return (name, content) of ks file"""
     if url.startswith('http') or url.startswith('https'):
         response = requests.get(url + '/MD5SUMS')
         if response.status_code == 200:
@@ -102,18 +106,10 @@ def _parse_hunks(arr, range_):
     return hunks
 
 def _parse_sections(stream):
+    """Parse ks diff sections"""
     sections = []
     stack = []
     head_stack = ''
-    def parse_range_hunks(line):
-        print head_stack
-        range_ = _parse_range(head_stack)
-        hunks = _parse_hunks(stack, range_)
-        stack = []
-        head_stack = line
-        sections.append({
-            'hunks': hunks
-            })
     for line in stream:
         if not line.startswith('@@ '):
             stack.append(line)
@@ -137,15 +133,18 @@ def _parse_sections(stream):
     return sections
 
 def ks_diff(old, new):
+    """Return a ks file diff dict"""
     ks_old, ks_new = _get_file(old), _get_file(new)
     diff_list = []
 
-    stream = difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0], tofile=ks_new[0])
+    stream = difflib.unified_diff(ks_old[1], ks_new[1], fromfile=ks_old[0], \
+            tofile=ks_new[0])
     for section in _parse_sections(stream):
         diff_list.append(section)
     return {'file_name': [ks_old[0], ks_new[0]], 'sections': diff_list}
 
 def packages(url):
+    """Return an image packages info dict"""
     content = _get_file(url, suffix='.packages')
 
     packages_info = JsonDict()
index f3283f41dd80d572520f85263aa5c1c6aec1a957..3e64b5f315559dd22615bf81ca61c83ba5a63784 100644 (file)
@@ -1,3 +1,6 @@
+
+"""This module renders html templates"""
+
 import os
 
 from jinja2 import Environment, FileSystemLoader
index 2ab027058a67a1b5cfe7480f41f84f127efc536d..4a57a3117e1a3118efdc992ea58ea15ae00a6ed8 100644 (file)
@@ -1,3 +1,6 @@
+
+"""This module handles repo related"""
+
 import gzip
 import mimetypes
 from StringIO import StringIO
@@ -61,6 +64,7 @@ class Repo(object):
 
     @property
     def packages(self):
+        """Return a packages info dict"""
         packages_info = JsonDict()
 
         for package in self._primary.package:
index 67fd0f7fec3e03f80c6828831850e979a639e842..11319257badc850ce54fadfae49fdf937f2d65af 100644 (file)
@@ -1,8 +1,12 @@
+
+"""This module contains usefull tools used frequently"""
+
 import re
 import xml.sax.handler
 
 
 def xml2obj(src):
+    # pylint: disable=C0301, C0111, R0903, R0912, W0212, C0103, W0231
     """
     A simple function to converts XML data into native Python object,
     from http://goo.gl/Ymc6Nl, Licensed under the PSF License.
index 6409e790becbaafe76a1daa02af363d363c3c2a0..445cbb48ee0cd395e867de81a69874103ed0f294 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/env python
 
+"""snap-diff is an extra tool used to test python-snapdiff functions or
+provide as a command line tool"""
+
 import argparse
 import sys
 
@@ -7,6 +10,7 @@ import snapdiff
 
 
 def main(argv):
+    """The main body"""
     description = 'Diff two repos with different urls'
     parser = argparse.ArgumentParser(description=description)
     parser.add_argument(dest='old', help='old repo')
@@ -16,19 +20,22 @@ def main(argv):
             help="which diff you want(default is 'repo')")
     group = parser.add_mutually_exclusive_group()
     group.add_argument('--json', help='output json diff', action='store_true')
-    group.add_argument('-d', dest='directory', help="output html diff into the directory")
+    group.add_argument('-d', dest='directory', \
+            help='output html diff into directory')
     parser.add_argument('-n', dest='name', \
             help="give a name to diff html(effective to '-d' option only)")
     args = parser.parse_args(argv)
 
     if args.directory and args.name:
-        snapdiff.diff_to_dist(args.old, args.new, args.directory, style=args.type, diff_name=args.name)
+        snapdiff.diff_to_dist(args.old, args.new, args.directory, \
+                style=args.type, diff_name=args.name)
     elif args.directory:
-        snapdiff.diff_to_dist(args.old, args.new, args.directory, style=args.type)
+        snapdiff.diff_to_dist(args.old, args.new, args.directory, \
+                style=args.type)
     elif args.json:
-        print snapdiff.diff_to_JSON(args.old, args.new, style=args.type)
+        print snapdiff.diff_to_json(args.old, args.new, style=args.type)
     else:
-        print snapdiff.diff_to_HTML(args.old, args.new, style=args.type)
+        print snapdiff.diff_to_html(args.old, args.new, style=args.type)
 
 if __name__ == '__main__':
     try: