import os
parser = argparse.ArgumentParser()
-parser.add_argument('input_path', nargs='+',
- help='Input folder that is traversed for OpenWrt JSON device files.')
-parser.add_argument('--download-url', action='store', default='',
- help='Link to get the image from. May contain {target}, {version} and {commit}')
-parser.add_argument('--formatted', action='store_true',
- help='Output formatted JSON data.')
-parser.add_argument('--change-prefix',
- help='Change the openwrt- file name prefix.')
+parser.add_argument("input_path", nargs="+",
+ help="Input folder that is traversed for OpenWrt JSON device files.")
+parser.add_argument("--download-url", action="store", default="",
+ help="Link to get the image from. May contain {target}, {version} and {commit}")
+parser.add_argument("--formatted", action="store_true",
+ help="Output formatted JSON data.")
+parser.add_argument("--change-prefix",
+ help="Change the openwrt- file name prefix.")
args = parser.parse_args()
def change_prefix(images, old_prefix, new_prefix):
for image in images:
- if image['name'].startswith(old_prefix):
- image['name'] = new_prefix + image['name'][len(old_prefix):]
+ if image["name"].startswith(old_prefix):
+ image["name"] = new_prefix + image["name"][len(old_prefix):]
# OpenWrt JSON device files
paths = []
for path in args.input_path:
if os.path.isdir(path):
- for file in Path(path).rglob('*.json'):
+ for file in Path(path).rglob("*.json"):
paths.append(file)
else:
- if not path.endswith('.json'):
- sys.stderr.write(f'Folder does not exists: {path}\n')
+ if not path.endswith(".json"):
+ sys.stderr.write(f"Folder does not exists: {path}\n")
exit(1)
paths.append(path)
def get_title_name(title):
- if 'title' in title:
- return title['title']
+ if "title" in title:
+ return title["title"]
else:
- return "{} {} {}".format(title.get('vendor', ''), title['model'], title.get('variant', '')).strip()
+ return "{} {} {}".format(title.get("vendor", ""), title["model"], title.get("variant", "")).strip()
def add_profile(id, target, profile, code=None):
images = []
- for image in profile['images']:
- images.append({'name': image['name'], 'type': image['type']})
+ for image in profile["images"]:
+ images.append({"name": image["name"], "type": image["type"]})
if target is None:
- target = profile['target']
+ target = profile["target"]
if args.change_prefix:
- change_prefix(images, 'openwrt-', args.change_prefix)
+ change_prefix(images, "openwrt-", args.change_prefix)
- for title in profile['titles']:
+ for title in profile["titles"]:
name = get_title_name(title)
if len(name) == 0:
- sys.stderr.write(f'Empty title. Skip title in {path}\n')
+ sys.stderr.write(f"Empty title. Skip title in {path}\n")
continue
- output['models'][name] = {'id': id, 'target': target, 'images': images}
+ output["models"][name] = {"id": id, "target": target, "images": images}
if code is not None:
- output['models'][name]['code'] = code
+ output["models"][name]["code"] = code
for path in paths:
with open(path, "r") as file:
obj = json.load(file)
- if obj['metadata_version'] != SUPPORTED_METADATA_VERSION:
- sys.stderr.write(f'{path} has unsupported metadata version: {obj["metadata_version"]} => skip\n')
+ if obj["metadata_version"] != SUPPORTED_METADATA_VERSION:
+ sys.stderr.write(f"{path} has unsupported metadata version: {obj["metadata_version"]} => skip\n")
continue
- code = obj.get('version_code', obj.get('version_commit'))
+ code = obj.get("version_code", obj.get("version_commit"))
- if not 'version_code' in output:
+ if not "version_code" in output:
output = {
- 'version_code': code,
- 'download_url': args.download_url,
- 'models' : {}
+ "version_code": code,
+ "download_url": args.download_url,
+ "models" : {}
}
# if we have mixed codes/commits, store in device object
- if output['version_code'] == code:
+ if output["version_code"] == code:
code = None;
try:
- if 'profiles' in obj:
- for id in obj['profiles']:
- add_profile(id, obj.get('target'), obj['profiles'][id], code)
+ if "profiles" in obj:
+ for id in obj["profiles"]:
+ add_profile(id, obj.get("target"), obj["profiles"][id], code)
else:
- add_profile(obj['id'], obj['target'], obj, code)
+ add_profile(obj["id"], obj["target"], obj, code)
except json.decoder.JSONDecodeError as e:
- sys.stderr.write(f'Skip {path}\n {e}\n')
+ sys.stderr.write(f"Skip {path}\n {e}\n")
except KeyError as e:
- sys.stderr.write(f'Abort on {path}\n Missing key {e}\n')
+ sys.stderr.write(f"Abort on {path}\n Missing key {e}\n")
exit(1)
if args.formatted: