From ef2dbc64851ce9c5048504041f6b391a597b8fdb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0tetiar?= Date: Thu, 6 Aug 2020 10:46:10 +0200 Subject: [PATCH] collect.py: add flake8 code style tool MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Refactor the code in order to fix following flake8 errors: misc/collect.py:55:1: E302 expected 2 blank lines, found 1 misc/collect.py:98:12: E713 test for membership should be 'not in' Signed-off-by: Petr Å tetiar --- .flake8 | 5 +++ misc/collect.py | 111 ++++++++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 50 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..bd71867 --- /dev/null +++ b/.flake8 @@ -0,0 +1,5 @@ +[flake8] +ignore = E203, E266, E501, W503 +max-line-length = 88 +max-complexity = 18 +select = B,C,E,F,W,T4,B9 diff --git a/misc/collect.py b/misc/collect.py index 05caa37..2e6016d 100755 --- a/misc/collect.py +++ b/misc/collect.py @@ -1,4 +1,7 @@ #!/usr/bin/env python3 +""" +Tool to create overview.json files and update the config.js. +""" from pathlib import Path import urllib.request @@ -10,47 +13,9 @@ import sys import os import re -""" -Tool to create overview.json files and update the config.js. -""" - -parser = argparse.ArgumentParser() -parser.add_argument( - "--formatted", action="store_true", help="Output formatted JSON data." -) -subparsers = parser.add_subparsers(dest="action", required=True) - -parser_merge = subparsers.add_parser( - "merge", help="Create a grid structure with horizontal and vertical connections." -) -parser_merge.add_argument( - "input_path", - nargs="+", - help="Input folder that is traversed for OpenWrt JSON device files.", -) -parser_merge.add_argument( - "--download-url", - action="store", - default="", - help="Link to get the image from. May contain {target}, {version} and {commit}", -) - -parser_scrape = subparsers.add_parser( - "scrape", - help="Create a grid structure of horizontal, vertical and vertical connections.", -) -parser_scrape.add_argument( - "domain", help="Domain to scrape. E.g. https://downloads.openwrt.org" -) -parser_scrape.add_argument("selector", help="Path the config.js file is in.") -parser_scrape.add_argument( - "--use-wget", action="store_true", help="Use wget to scrape the site." -) - -args = parser.parse_args() - SUPPORTED_METADATA_VERSION = 1 + # accepts {: } def merge_profiles(profiles, download_url): # json output data @@ -95,7 +60,7 @@ def merge_profiles(profiles, download_url): code = obj.get("version_code", obj.get("version_commit")) - if not "version_code" in output: + if "version_code" not in output: output = {"version_code": code, "download_url": download_url, "models": {}} # if we have mixed codes/commits, store in device object @@ -134,7 +99,9 @@ Update config.json. """ -def scrape(url, selector_path): +def scrape(args): + url = args.domain + selector_path = args.selector config_path = f"{selector_path}/config.js" data_path = f"{selector_path}/data" versions = {} @@ -182,7 +149,9 @@ Update config.json. """ -def scrape_wget(url, selector_path): +def scrape_wget(args): + url = args.domain + selector_path = args.selector config_path = f"{selector_path}/config.js" data_path = f"{selector_path}/data" versions = {} @@ -229,7 +198,8 @@ Find and merge json files for a single release. """ -def merge(input_paths): +def merge(args): + input_paths = args.input_path # OpenWrt JSON device files profiles = {} @@ -255,11 +225,52 @@ def merge(input_paths): json.dump(output, sys.stdout, sort_keys=True) -if args.action == "merge": - merge(args.input_path) +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--formatted", action="store_true", help="Output formatted JSON data." + ) + subparsers = parser.add_subparsers(dest="action", required=True) + + parser_merge = subparsers.add_parser( + "merge", + help="Create a grid structure with horizontal and vertical connections.", + ) + parser_merge.add_argument( + "input_path", + nargs="+", + help="Input folder that is traversed for OpenWrt JSON device files.", + ) + parser_merge.add_argument( + "--download-url", + action="store", + default="", + help="Link to get the image from. May contain {target}, {version} and {commit}", + ) + + parser_scrape = subparsers.add_parser( + "scrape", + help="Create a grid structure of horizontal, vertical and vertical connections.", + ) + parser_scrape.add_argument( + "domain", help="Domain to scrape. E.g. https://downloads.openwrt.org" + ) + parser_scrape.add_argument("selector", help="Path the config.js file is in.") + parser_scrape.add_argument( + "--use-wget", action="store_true", help="Use wget to scrape the site." + ) + + args = parser.parse_args() + + if args.action == "merge": + merge(args) + + if args.action == "scrape": + if args.use_wget: + scrape_wget(args) + else: + scrape(args) -if args.action == "scrape": - if args.use_wget: - scrape_wget(args.domain, args.selector) - else: - scrape(args.domain, args.selector) + +if __name__ == "__main__": + main() -- 2.30.2