1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-09-11 23:23:27 +02:00

Use constants for field/flag types

Avoids typos, such as one I just found which was 'strsng' isntead of
'string'. The static analyzer can catch those if they are constants.
Comparing ints is also faster than strings, which adds up in readmeta.
This commit is contained in:
Daniel Martí 2015-12-03 12:55:19 +01:00
parent 992406de0e
commit b1a9180d12
2 changed files with 47 additions and 37 deletions

View File

@ -108,7 +108,7 @@ def check_regexes(app):
for m, r in checks: for m, r in checks:
v = app.get_field(f) v = app.get_field(f)
t = metadata.metafieldtype(f) t = metadata.metafieldtype(f)
if t == 'multiline': if t == metadata.TYPE_MULTILINE:
for l in v.splitlines(): for l in v.splitlines():
if m.match(l): if m.match(l):
yield "%s at line '%s': %s" % (f, l, r) yield "%s at line '%s': %s" % (f, l, r)

View File

@ -202,21 +202,31 @@ class App():
else: else:
self.set_field(f, v) self.set_field(f, v)
TYPE_UNKNOWN = 0
TYPE_OBSOLETE = 1
TYPE_STRING = 2
TYPE_BOOL = 3
TYPE_LIST = 4
TYPE_SCRIPT = 5
TYPE_MULTILINE = 6
TYPE_BUILD = 7
TYPE_BUILD_V2 = 8
def metafieldtype(name): def metafieldtype(name):
if name in ['Description', 'Maintainer Notes']: if name in ['Description', 'Maintainer Notes']:
return 'multiline' return TYPE_MULTILINE
if name in ['Categories', 'AntiFeatures']: if name in ['Categories', 'AntiFeatures']:
return 'list' return TYPE_LIST
if name == 'Build Version': if name == 'Build Version':
return 'build' return TYPE_BUILD
if name == 'Build': if name == 'Build':
return 'buildv2' return TYPE_BUILD_V2
if name == 'Use Built': if name == 'Use Built':
return 'obsolete' return TYPE_OBSOLETE
if name in app_fields: if name in app_fields:
return 'string' return TYPE_STRING
return 'unknown' return TYPE_UNKNOWN
# In the order in which they are laid out on files # In the order in which they are laid out on files
@ -342,12 +352,12 @@ bool_flags = set(['submodules', 'oldsdkloc', 'forceversion', 'forcevercode',
def flagtype(name): def flagtype(name):
if name in list_flags: if name in list_flags:
return 'list' return TYPE_LIST
if name in script_flags: if name in script_flags:
return 'script' return TYPE_SCRIPT
if name in bool_flags: if name in bool_flags:
return 'bool' return TYPE_BOOL
return 'string' return TYPE_STRING
# Designates a metadata field type and checks that it matches # Designates a metadata field type and checks that it matches
@ -822,13 +832,13 @@ def post_metadata_parse(app):
continue continue
ftype = flagtype(k) ftype = flagtype(k)
if ftype == 'script': if ftype == TYPE_SCRIPT:
build.__dict__[k] = re.sub(esc_newlines, '', v).lstrip().rstrip() build.__dict__[k] = re.sub(esc_newlines, '', v).lstrip().rstrip()
elif ftype == 'bool': elif ftype == TYPE_BOOL:
# TODO handle this using <xsd:element type="xsd:boolean> in a schema # TODO handle this using <xsd:element type="xsd:boolean> in a schema
if isinstance(v, basestring) and v == 'true': if isinstance(v, basestring) and v == 'true':
build.__dict__[k] = True build.__dict__[k] = True
elif ftype == 'string': elif ftype == TYPE_BOOL:
if isinstance(v, bool) and v: if isinstance(v, bool) and v:
build.__dict__[k] = 'yes' build.__dict__[k] = 'yes'
@ -1004,12 +1014,12 @@ def parse_txt_metadata(metadatapath):
pk, pv = bv pk, pv = bv
pk = pk.lstrip() pk = pk.lstrip()
t = flagtype(pk) t = flagtype(pk)
if t == 'list': if t == TYPE_LIST:
pv = split_list_values(pv) pv = split_list_values(pv)
build.set_flag(pk, pv) build.set_flag(pk, pv)
elif t == 'string' or t == 'script': elif t == TYPE_STRING or t == TYPE_SCRIPT:
build.set_flag(pk, pv) build.set_flag(pk, pv)
elif t == 'bool': elif t == TYPE_BOOL:
if pv == 'yes': if pv == 'yes':
build.set_flag(pk, True) build.set_flag(pk, True)
@ -1099,17 +1109,17 @@ def parse_txt_metadata(metadatapath):
f = 'Current Version Code' f = 'Current Version Code'
ftype = metafieldtype(f) ftype = metafieldtype(f)
if ftype not in ['build', 'buildv2']: if ftype not in [TYPE_BUILD, TYPE_BUILD_V2]:
add_comments(f) add_comments(f)
if ftype == 'multiline': if ftype == TYPE_MULTILINE:
mode = 1 mode = 1
if v: if v:
raise MetaDataException("Unexpected text on same line as " + f + " in " + linedesc) raise MetaDataException("Unexpected text on same line as " + f + " in " + linedesc)
elif ftype == 'string': elif ftype == TYPE_STRING:
app.set_field(f, v) app.set_field(f, v)
elif ftype == 'list': elif ftype == TYPE_LIST:
app.set_field(f, split_list_values(v)) app.set_field(f, split_list_values(v))
elif ftype == 'build': elif ftype == TYPE_BUILD:
if v.endswith("\\"): if v.endswith("\\"):
mode = 2 mode = 2
del buildlines[:] del buildlines[:]
@ -1118,7 +1128,7 @@ def parse_txt_metadata(metadatapath):
build = parse_buildline([v]) build = parse_buildline([v])
app.builds.append(build) app.builds.append(build)
add_comments('build:' + app.builds[-1].vercode) add_comments('build:' + app.builds[-1].vercode)
elif ftype == 'buildv2': elif ftype == TYPE_BUILD_V2:
build = Build() build = Build()
vv = v.split(',') vv = v.split(',')
if len(vv) != 2: if len(vv) != 2:
@ -1132,7 +1142,7 @@ def parse_txt_metadata(metadatapath):
vc_seen[build.vercode] = True vc_seen[build.vercode] = True
del buildlines[:] del buildlines[:]
mode = 3 mode = 3
elif ftype == 'obsolete': elif ftype == TYPE_OBSOLETE:
pass # Just throw it away! pass # Just throw it away!
else: else:
raise MetaDataException("Unrecognised field type for " + f + " in " + linedesc) raise MetaDataException("Unrecognised field type for " + f + " in " + linedesc)
@ -1257,9 +1267,9 @@ def write_txt_metadata(mf, app):
def w_field(f, v): def w_field(f, v):
t = metafieldtype(f) t = metafieldtype(f)
if t == 'list': if t == TYPE_LIST:
v = ','.join(v) v = ','.join(v)
elif t == 'multiline': elif t == TYPE_MULTILINE:
v = '\n' + v + '\n.' v = '\n' + v + '\n.'
mf.write("%s:%s\n" % (f, v)) mf.write("%s:%s\n" % (f, v))
@ -1273,13 +1283,13 @@ def write_txt_metadata(mf, app):
t = flagtype(f) t = flagtype(f)
out = ' %s=' % f out = ' %s=' % f
if t == 'string': if t == TYPE_STRING:
out += v out += v
elif t == 'bool': elif t == TYPE_BOOL:
out += 'yes' out += 'yes'
elif t == 'script': elif t == TYPE_SCRIPT:
out += '&& \\\n '.join([s.lstrip() for s in v.split('&& ')]) out += '&& \\\n '.join([s.lstrip() for s in v.split('&& ')])
elif t == 'list': elif t == TYPE_LIST:
out += ','.join(v) if type(v) == list else v out += ','.join(v) if type(v) == list else v
mf.write(out) mf.write(out)
@ -1304,20 +1314,20 @@ def write_yaml_metadata(mf, app):
if t is None: if t is None:
t = metafieldtype(f) t = metafieldtype(f)
v = '' v = ''
if t == 'list': if t == TYPE_LIST:
v = '\n' v = '\n'
for e in v: for e in v:
v += prefix + ' - ' + escape(e) + '\n' v += prefix + ' - ' + escape(e) + '\n'
elif t == 'multiline': elif t == TYPE_MULTILINE:
v = ' |\n' v = ' |\n'
for l in v.splitlines(): for l in v.splitlines():
if l: if l:
v += prefix + ' ' + l + '\n' v += prefix + ' ' + l + '\n'
else: else:
v += '\n' v += '\n'
elif t == 'bool': elif t == TYPE_BOOL:
v = ' yes\n' v = ' yes\n'
elif t == 'script': elif t == TYPE_SCRIPT:
cmds = [s + '&& \\' for s in v.split('&& ')] cmds = [s + '&& \\' for s in v.split('&& ')]
if len(cmds) > 0: if len(cmds) > 0:
cmds[-1] = cmds[-1][:-len('&& \\')] cmds[-1] = cmds[-1][:-len('&& \\')]
@ -1340,8 +1350,8 @@ def write_yaml_metadata(mf, app):
mf.write("builds:\n") mf.write("builds:\n")
first_build = False first_build = False
w_field('versionName', build.version, ' - ', 'string') w_field('versionName', build.version, ' - ', TYPE_STRING)
w_field('versionCode', build.vercode, ' ', 'strsng') w_field('versionCode', build.vercode, ' ', TYPE_STRING)
for f in build_flags_order: for f in build_flags_order:
v = build.get_flag(f) v = build.get_flag(f)
if not v: if not v: