Skip to content

Commit

Permalink
Handle RegEx item format errors. Add clarification of rule 'key: valu…
Browse files Browse the repository at this point in the history
…e' form.
  • Loading branch information
thess committed Jan 29, 2024
1 parent 604e449 commit a0fa965
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 5 additions & 3 deletions callattendant/userinterface/templates/callers_regexlists.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ <h2><span class="px-2">Regular Expression Lists</span>
</h2>

<button id="save-button" type="button" class="btn btn-primary" style="float: right;" disabled>Save</button>
<p>Edit the Block (blacklist) and Permit (whitelist) regular expressions here. Each line of text should have the format:
<br/><code style="margin-left: 3em">&lt;regex&gt; [: &lt;description&gt;]</code><br/>
<p>Edit the Block (blacklist) and Permit (whitelist) regular expressions here. Each line of text is formatted as a
YAML dictionary item. A YAML dictionary item is represented as a simple "<i>key:&nbsp;value<i>" pair.
The colon must be followed by a space. Example:
<br/><code style="margin-left: 3em">&lt;regex&gt;: &lt;description&gt;</code><br/>
that is, a <a href="https://docs.python.org/3/library/re.html#regular-expression-syntax" target="_blank">regular expression</a>,
optionally followed by a colon and a human-readable description of who is being black or whitelisted.</p>
followed by ":&nbsp;" and an optional description of the callers being filtered.</p>
<p>Regular expressions for names are case-insensitive.</p>
<form id="regexlistsform" style="margin-top: 2em;">
<div class="mb-2" style="width: 100%; display: table;">
Expand Down
21 changes: 14 additions & 7 deletions callattendant/userinterface/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,12 @@ def stringlist2dict(s):
d = {}
for line in s.splitlines():
if line:
k, v = line.split(': ', 1)
d[k] = v
try:
k, v = line.split(': ', 1)
d[k] = v
except ValueError:
raise ValueError(line)

return d

@app.route('/callers/regexlists')
Expand All @@ -1064,14 +1068,17 @@ def callers_regexlists_save():

# Get the data from the request and convert each list to a dict
# Reload im-memory values (config object)
config.get("CALLERID_PATTERNS")['blocknames'] = stringlist2dict(request.form['blocknameslist'])
config.get("CALLERID_PATTERNS")['blocknumbers'] = stringlist2dict(request.form['blocknumberslist'])
config.get("CALLERID_PATTERNS")['permitnames'] = stringlist2dict(request.form['permitnameslist'])
config.get("CALLERID_PATTERNS")['permitnumbers'] = stringlist2dict(request.form['permitnumberslist'])
# Write the new patterns to a file
try:
config.get("CALLERID_PATTERNS")['blocknames'] = stringlist2dict(request.form['blocknameslist'])
config.get("CALLERID_PATTERNS")['blocknumbers'] = stringlist2dict(request.form['blocknumberslist'])
config.get("CALLERID_PATTERNS")['permitnames'] = stringlist2dict(request.form['permitnameslist'])
config.get("CALLERID_PATTERNS")['permitnumbers'] = stringlist2dict(request.form['permitnumberslist'])

# Write the new patterns to a file
with open(config.get("CALLERID_PATTERNS_FILE"), 'w') as file:
yaml.dump(config.get("CALLERID_PATTERNS"), file, default_flow_style=False)
except ValueError as e:
return "error:\nRegex list contains improperly formatted 'key: value' entry:\n{}".format(str(e))
except Exception as e:
return str(e)

Expand Down

0 comments on commit a0fa965

Please sign in to comment.