diff options
author | sebres <serg.brester@sebres.de> | 2017-12-05 17:49:22 +0100 |
---|---|---|
committer | sebres <serg.brester@sebres.de> | 2017-12-05 17:49:22 +0100 |
commit | de97dedba0202559193e7654e183974ab335bcf7 (patch) | |
tree | 140409c5bc8431db5ea9966f06570a65991a767c /fail2ban/helpers.py | |
parent | ff987b60cda4ae0c46fd88d972161e6d0b9da789 (diff) | |
download | fail2ban-de97dedba0202559193e7654e183974ab335bcf7.tar.gz |
move extractOptions from JailReader to helpers (common usage server- / client-side);
Diffstat (limited to 'fail2ban/helpers.py')
-rw-r--r-- | fail2ban/helpers.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index 5b027b32..378924df 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -238,6 +238,34 @@ else: # +# Following function used for parse options from parameter (e.g. `name[p1=0, p2="..."][p3='...']`). +# + +# regex, to extract list of options: +OPTION_CRE = re.compile(r"^([^\[]+)(?:\[(.*)\])?\s*$", re.DOTALL) +# regex, to iterate over single option in option list, syntax: +# `action = act[p1="...", p2='...', p3=...]`, where the p3=... not contains `,` or ']' +# since v0.10 separator extended with `]\s*[` for support of multiple option groups, syntax +# `action = act[p1=...][p2=...]` +OPTION_EXTRACT_CRE = re.compile( + r'([\w\-_\.]+)=(?:"([^"]*)"|\'([^\']*)\'|([^,\]]*))(?:,|\]\s*\[|$)', re.DOTALL) + +def extractOptions(option): + match = OPTION_CRE.match(option) + if not match: + # TODO proper error handling + return None, None + option_name, optstr = match.groups() + option_opts = dict() + if optstr: + for optmatch in OPTION_EXTRACT_CRE.finditer(optstr): + opt = optmatch.group(1) + value = [ + val for val in optmatch.group(2,3,4) if val is not None][0] + option_opts[opt.strip()] = value.strip() + return option_name, option_opts + +# # Following facilities used for safe recursive interpolation of # tags (<tag>) in tagged options. # |