@@ -18,13 +18,13 @@ class ConfigParser(configparser.ConfigParser):
18
18
def get_list (
19
19
self ,
20
20
section : str ,
21
- item : str ,
21
+ option : str ,
22
22
delimiter : str | None = "\n " ,
23
23
fallback : list [str ] | None = None ,
24
- duplicated_ok : bool = False ,
25
24
strip : bool = True ,
25
+ remove_duplicates : bool = True ,
26
26
) -> list [str ]:
27
- r"""Parse and return a list of strings from an item in ``defaults.ini``.
27
+ r"""Parse and return a list of strings from an ``option`` for ``section`` in ``defaults.ini``.
28
28
29
29
This method uses str.split() to split the value into list of strings.
30
30
References: https://docs.python.org/3/library/stdtypes.html#str.split.
@@ -37,24 +37,26 @@ def get_list(
37
37
If ``strip`` is True (default: True), strings are whitespace-stripped and empty strings
38
38
are removed from the final result.
39
39
40
- If ``duplicated_ok`` is True (default: False), duplicated values are not removed from the final list.
40
+ If `remove_duplicates` is True, duplicated elements which come after the their first instances will
41
+ be removed from the list. This operation happens after ``strip`` is handled.
41
42
43
+ The order of non-empty elements in the list is preserved.
42
44
The content of each string in the list is not validated and should be handled separately.
43
45
44
46
Parameters
45
47
----------
46
48
section : str
47
49
The section in ``defaults.ini``.
48
- item : str
49
- The item to parse the list.
50
+ option : str
51
+ The option whose values will be split into the a list of strings .
50
52
delimiter : str | None
51
53
The delimiter used to split the strings.
52
54
fallback : list | None
53
55
The fallback value in case of errors.
54
- duplicated_ok : bool
55
- If True allow duplicate values.
56
- strip: bool
56
+ strip : bool
57
57
If True, strings are whitespace-stripped and any empty strings are removed.
58
+ remove_duplicates : bool
59
+ If True, duplicated elements will be removed from the list.
58
60
59
61
Returns
60
62
-------
@@ -79,20 +81,23 @@ def get_list(
79
81
allowed_hosts == ["github.com", "boo.com gitlab.com", "host com"]
80
82
"""
81
83
try :
82
- value = self .get (section , item )
84
+ value = self .get (section , option )
83
85
if isinstance (value , str ):
84
86
content = value .split (sep = delimiter )
85
87
86
88
if strip :
87
89
content = [x .strip () for x in content if x .strip ()]
88
90
89
- if duplicated_ok :
91
+ if not remove_duplicates :
90
92
return content
91
93
92
- distinct_values = set ()
93
- distinct_values .update (content )
94
- return list (distinct_values )
95
- except configparser .NoOptionError as error :
94
+ values = []
95
+ for ele in content :
96
+ if ele in values :
97
+ continue
98
+ values .append (ele )
99
+ return values
100
+ except (configparser .NoOptionError , configparser .NoSectionError ) as error :
96
101
logger .error (error )
97
102
98
103
return fallback or []
0 commit comments