summaryrefslogtreecommitdiff
path: root/fail2ban/helpers.py
diff options
context:
space:
mode:
authorsebres <serg.brester@sebres.de>2017-12-05 17:49:22 +0100
committersebres <serg.brester@sebres.de>2017-12-05 17:49:22 +0100
commitde97dedba0202559193e7654e183974ab335bcf7 (patch)
tree140409c5bc8431db5ea9966f06570a65991a767c /fail2ban/helpers.py
parentff987b60cda4ae0c46fd88d972161e6d0b9da789 (diff)
downloadfail2ban-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.py28
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.
#