From: Lingchao Xin Date: Mon, 20 Jan 2014 04:09:46 +0000 (+0800) Subject: Pylint the world X-Git-Tag: submit/devel/20190730.075508~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6a5fb9ef161a9507ae02011d2477e5cd8e4728b2;p=services%2Fpython-snapdiff.git Pylint the world Change-Id: I8dd986e2f72c5ec308bcd4e67cc9292bae1f71c1 --- diff --git a/snapdiff/__init__.py b/snapdiff/__init__.py index 03ceb65..8801109 100644 --- a/snapdiff/__init__.py +++ b/snapdiff/__init__.py @@ -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 diff --git a/snapdiff/image.py b/snapdiff/image.py index e52dbf1..32dfbe4 100644 --- a/snapdiff/image.py +++ b/snapdiff/image.py @@ -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() diff --git a/snapdiff/render.py b/snapdiff/render.py index f3283f4..3e64b5f 100644 --- a/snapdiff/render.py +++ b/snapdiff/render.py @@ -1,3 +1,6 @@ + +"""This module renders html templates""" + import os from jinja2 import Environment, FileSystemLoader diff --git a/snapdiff/repo.py b/snapdiff/repo.py index 2ab0270..4a57a31 100644 --- a/snapdiff/repo.py +++ b/snapdiff/repo.py @@ -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: diff --git a/snapdiff/utils.py b/snapdiff/utils.py index 67fd0f7..1131925 100644 --- a/snapdiff/utils.py +++ b/snapdiff/utils.py @@ -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. diff --git a/tools/snap-diff b/tools/snap-diff index 6409e79..445cbb4 100755 --- a/tools/snap-diff +++ b/tools/snap-diff @@ -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: