From 689786eea43bfdea46cc29b1acf63fb93e44f323 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 24 May 2023 18:43:48 +0200 Subject: [PATCH] metadata: refactor _builds_to_yaml to use dicts and _format functions _builds_to_yaml does not use any features of the metadata.Build class, so it can operate on plain dicts as well. It also does not need to output Build instances because those are converted to plain dicts when writing out to YAML. --- fdroidserver/metadata.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/fdroidserver/metadata.py b/fdroidserver/metadata.py index 29ac8b96..3692e7cb 100644 --- a/fdroidserver/metadata.py +++ b/fdroidserver/metadata.py @@ -1157,22 +1157,31 @@ def _del_duplicated_NoSourceSince(app): def _builds_to_yaml(app): + """Reformat Builds: flags for output to YAML 1.2. + + This will strip any flag/value that is not set or is empty. + TYPE_BOOL fields are removed when they are false. 0 is valid + value, it should not be stripped, so there are special cases to + handle that. + + """ builds = ruamel.yaml.comments.CommentedSeq() for build in app.get('Builds', []): - if not isinstance(build, Build): - build = Build(build) b = ruamel.yaml.comments.CommentedMap() for field in build_flags: - if hasattr(build, field): - value = getattr(build, field) - typ = flagtype(field) - # don't check value == True for TYPE_INT as it could be 0 - if value and typ == TYPE_STRINGMAP: - v = _format_stringmap(app['id'], field, value, build['versionCode']) - if v: - b[field] = v - elif value is not None and (typ == TYPE_INT or value): - b[field] = value + v = build.get(field) + if v is None or v is False or v == '' or v == dict() or v == list(): + continue + _flagtype = flagtype(field) + if _flagtype == TYPE_MULTILINE: + v = _format_multiline(v) + elif _flagtype == TYPE_SCRIPT: + v = _format_script(v) + elif _flagtype == TYPE_STRINGMAP: + v = _format_stringmap(app['id'], field, v, build['versionCode']) + + if v or v == 0: + b[field] = v builds.append(b)