[utils] Add orderedSet_from_options

This commit is contained in:
pukkandan 2022-08-24 07:38:55 +05:30
parent 13db4e7b9e
commit 5314b52192
No known key found for this signature in database
GPG key ID: 7EEE9E1E817D0A39
3 changed files with 47 additions and 45 deletions

View file

@ -5785,6 +5785,36 @@ def truncate_string(s, left, right=0):
return f'{s[:left-3]}...{s[-right:]}'
def orderedSet_from_options(options, alias_dict, *, use_regex=False, start=None):
assert 'all' in alias_dict, '"all" alias is required'
requested = list(start or [])
for val in options:
discard = val.startswith('-')
if discard:
val = val[1:]
if val in alias_dict:
val = alias_dict[val] if not discard else [
i[1:] if i.startswith('-') else f'-{i}' for i in alias_dict[val]]
# NB: Do not allow regex in aliases for performance
requested = orderedSet_from_options(val, alias_dict, start=requested)
continue
current = (filter(re.compile(val, re.I).fullmatch, alias_dict['all']) if use_regex
else [val] if val in alias_dict['all'] else None)
if current is None:
raise ValueError(val)
if discard:
for item in current:
while item in requested:
requested.remove(item)
else:
requested.extend(current)
return orderedSet(requested)
# Deprecated
has_certifi = bool(certifi)
has_websockets = bool(websockets)