diff options
author | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-08-24 07:38:55 +0530 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-08-24 07:38:55 +0530 |
commit | 5314b521925498356e78652fe59866116d56e1d1 (patch) | |
tree | e36730034f23bb8cdfc8376c3052071d5b19c38f /yt_dlp/utils.py | |
parent | 13db4e7b9e3932595c6b78df47ab4a0382f031f8 (diff) | |
download | hypervideo-pre-5314b521925498356e78652fe59866116d56e1d1.tar.lz hypervideo-pre-5314b521925498356e78652fe59866116d56e1d1.tar.xz hypervideo-pre-5314b521925498356e78652fe59866116d56e1d1.zip |
[utils] Add orderedSet_from_options
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 13768d846..957c7eaa7 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -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) |