[BACKEND] Do the match and extraction in one function

This commit is contained in:
Maria Mozgunova 2022-07-29 17:26:27 +03:00 committed by GitHub
parent 232b1ac75a
commit 6056e662e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ import re
import shlex import shlex
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Tuple
from mergedeep import mergedeep from mergedeep import mergedeep
@ -18,6 +19,9 @@ class DownloadArgsParser:
:class:`~ytdl_subscribe.validators.config.preset_validator.PresetValidator` :class:`~ytdl_subscribe.validators.config.preset_validator.PresetValidator`
""" """
# pattern to search for the [...] part in the argument name
_list_index_pattern = re.compile(r"\[([1-9]\d*)\]$")
def __init__(self, extra_arguments: List[str], config_options: ConfigOptions): def __init__(self, extra_arguments: List[str], config_options: ConfigOptions):
""" """
Parameters Parameters
@ -62,25 +66,23 @@ class DownloadArgsParser:
return arg_name[2:] return arg_name[2:]
@classmethod @classmethod
def _uses_list(cls, argument_name: str) -> bool: def _get_list_index_if_exists(cls, argument_name: str) -> Tuple[str, int]:
""" """
:param argument_name: The argument name which might be using list :param argument_name: Argument name
:return: True if the argument uses list, denoted by ending with '[<positive integer>]'. :return: Argument name and -1 if argument name does not use list.
False otherwise. Argument name and list index (a non-negative integer) otherwise.
"""
pattern = re.compile(r"\[[1-9]\d*\]$")
return bool(pattern.search(argument_name)) Examples
--------
@classmethod _get_list_index_if_exists("--key1.key2") -> ("--key1.key2", -1)
def _extract_list_index(cls, argument_name: str) -> int: _get_list_index_if_exists("--key1.key2[5]") -> ("--key1.key2", 5)
""" """
:param argument_name: The argument name using list search = cls._list_index_pattern.search(argument_name)
:return: Splits the argument and returns the name and the list index.
"""
prefix, _, last = argument_name.rpartition("[")
return prefix, int(last[:-1]) - 1 if search is None:
return argument_name, -1
return argument_name[: search.start()], int(search.group(1)) - 1
@classmethod @classmethod
def _find_largest_consecutive(cls, indices: List[int]) -> int: def _find_largest_consecutive(cls, indices: List[int]) -> int:
@ -148,14 +150,13 @@ class DownloadArgsParser:
for idx in range(0, len(arguments), 2): for idx in range(0, len(arguments), 2):
argument_name, argument_value = arguments[idx], arguments[idx + 1] argument_name, argument_value = arguments[idx], arguments[idx + 1]
argument_name, list_index = cls._get_list_index_if_exists(argument_name=argument_name)
if not cls._uses_list(argument_name=argument_name): if list_index == -1:
new_arguments.append(argument_name) new_arguments.append(argument_name)
new_arguments.append(argument_value) new_arguments.append(argument_value)
continue continue
argument_name, list_index = cls._extract_list_index(argument_name=argument_name)
if argument_name not in list_arguments: if argument_name not in list_arguments:
list_arguments[argument_name] = ([], []) list_arguments[argument_name] = ([], [])