mirror of
https://github.com/f-droid/fdroidserver.git
synced 2025-11-05 15:00:30 +03:00
metadata: Always use regex to verify values
Lists can be made into regexes very easily. Major code simplification. Also no need to construct sets to avoid iterating over lists.
This commit is contained in:
parent
7ff833b948
commit
e8eda55d9d
1 changed files with 18 additions and 36 deletions
|
|
@ -392,29 +392,13 @@ def flagtype(name):
|
||||||
#
|
#
|
||||||
class FieldValidator():
|
class FieldValidator():
|
||||||
|
|
||||||
def __init__(self, name, matching, sep, fields, flags):
|
def __init__(self, name, matching, fields, flags):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.matching = matching
|
self.matching = matching
|
||||||
if type(matching) is str:
|
|
||||||
self.compiled = re.compile(matching)
|
self.compiled = re.compile(matching)
|
||||||
else:
|
|
||||||
self.matching = set(self.matching)
|
|
||||||
self.sep = sep
|
|
||||||
self.fields = fields
|
self.fields = fields
|
||||||
self.flags = flags
|
self.flags = flags
|
||||||
|
|
||||||
def _assert_regex(self, values, appid):
|
|
||||||
for v in values:
|
|
||||||
if not self.compiled.match(v):
|
|
||||||
raise MetaDataException("'%s' is not a valid %s in %s. Regex pattern: %s"
|
|
||||||
% (v, self.name, appid, self.matching))
|
|
||||||
|
|
||||||
def _assert_list(self, values, appid):
|
|
||||||
for v in values:
|
|
||||||
if v not in self.matching:
|
|
||||||
raise MetaDataException("'%s' is not a valid %s in %s. Possible values: %s"
|
|
||||||
% (v, self.name, appid, ', '.join(self.matching)))
|
|
||||||
|
|
||||||
def check(self, v, appid):
|
def check(self, v, appid):
|
||||||
if not v:
|
if not v:
|
||||||
return
|
return
|
||||||
|
|
@ -422,70 +406,68 @@ class FieldValidator():
|
||||||
values = v
|
values = v
|
||||||
else:
|
else:
|
||||||
values = [v]
|
values = [v]
|
||||||
if type(self.matching) is set:
|
for v in values:
|
||||||
self._assert_list(values, appid)
|
if not self.compiled.match(v):
|
||||||
else:
|
raise MetaDataException("'%s' is not a valid %s in %s. Regex pattern: %s"
|
||||||
self._assert_regex(values, appid)
|
% (v, self.name, appid, self.matching))
|
||||||
|
|
||||||
|
|
||||||
# Generic value types
|
# Generic value types
|
||||||
valuetypes = {
|
valuetypes = {
|
||||||
FieldValidator("Integer",
|
FieldValidator("Integer",
|
||||||
r'^[1-9][0-9]*$', None,
|
r'^[1-9][0-9]*$',
|
||||||
[],
|
[],
|
||||||
['vercode']),
|
['vercode']),
|
||||||
|
|
||||||
FieldValidator("Hexadecimal",
|
FieldValidator("Hexadecimal",
|
||||||
r'^[0-9a-f]+$', None,
|
r'^[0-9a-f]+$',
|
||||||
['FlattrID'],
|
['FlattrID'],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("HTTP link",
|
FieldValidator("HTTP link",
|
||||||
r'^http[s]?://', None,
|
r'^http[s]?://',
|
||||||
["WebSite", "SourceCode", "IssueTracker", "Changelog", "Donate"], []),
|
["WebSite", "SourceCode", "IssueTracker", "Changelog", "Donate"], []),
|
||||||
|
|
||||||
FieldValidator("Email",
|
FieldValidator("Email",
|
||||||
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', None,
|
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
|
||||||
["AuthorEmail"], []),
|
["AuthorEmail"], []),
|
||||||
|
|
||||||
FieldValidator("Bitcoin address",
|
FieldValidator("Bitcoin address",
|
||||||
r'^[a-zA-Z0-9]{27,34}$', None,
|
r'^[a-zA-Z0-9]{27,34}$',
|
||||||
["Bitcoin"],
|
["Bitcoin"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Litecoin address",
|
FieldValidator("Litecoin address",
|
||||||
r'^L[a-zA-Z0-9]{33}$', None,
|
r'^L[a-zA-Z0-9]{33}$',
|
||||||
["Litecoin"],
|
["Litecoin"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Repo Type",
|
FieldValidator("Repo Type",
|
||||||
['git', 'git-svn', 'svn', 'hg', 'bzr', 'srclib'], None,
|
r'^(git|git-svn|svn|hg|bzr|srclib)$',
|
||||||
["RepoType"],
|
["RepoType"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Binaries",
|
FieldValidator("Binaries",
|
||||||
r'^http[s]?://', None,
|
r'^http[s]?://',
|
||||||
["Binaries"],
|
["Binaries"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Archive Policy",
|
FieldValidator("Archive Policy",
|
||||||
r'^[0-9]+ versions$', None,
|
r'^[0-9]+ versions$',
|
||||||
["ArchivePolicy"],
|
["ArchivePolicy"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Anti-Feature",
|
FieldValidator("Anti-Feature",
|
||||||
["Ads", "Tracking", "NonFreeNet", "NonFreeDep", "NonFreeAdd",
|
r'^(Ads|Tracking|NonFreeNet|NonFreeDep|NonFreeAdd|UpstreamNonFree|NonFreeAssets)$',
|
||||||
"UpstreamNonFree", "NonFreeAssets"], ',',
|
|
||||||
["AntiFeatures"],
|
["AntiFeatures"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Auto Update Mode",
|
FieldValidator("Auto Update Mode",
|
||||||
r"^(Version .+|None)$", None,
|
r"^(Version .+|None)$",
|
||||||
["AutoUpdateMode"],
|
["AutoUpdateMode"],
|
||||||
[]),
|
[]),
|
||||||
|
|
||||||
FieldValidator("Update Check Mode",
|
FieldValidator("Update Check Mode",
|
||||||
r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$", None,
|
r"^(Tags|Tags .+|RepoManifest|RepoManifest/.+|RepoTrunk|HTTP|Static|None)$",
|
||||||
["UpdateCheckMode"],
|
["UpdateCheckMode"],
|
||||||
[])
|
[])
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue