@@ -18,13 +18,13 @@ class ConfigParser(configparser.ConfigParser):
1818 def get_list (
1919 self ,
2020 section : str ,
21- item : str ,
21+ option : str ,
2222 delimiter : str | None = "\n " ,
2323 fallback : list [str ] | None = None ,
24- duplicated_ok : bool = False ,
2524 strip : bool = True ,
25+ remove_duplicates : bool = True ,
2626 ) -> 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``.
2828
2929 This method uses str.split() to split the value into list of strings.
3030 References: https://docs.python.org/3/library/stdtypes.html#str.split.
@@ -37,24 +37,26 @@ def get_list(
3737 If ``strip`` is True (default: True), strings are whitespace-stripped and empty strings
3838 are removed from the final result.
3939
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.
4142
43+ The order of non-empty elements in the list is preserved.
4244 The content of each string in the list is not validated and should be handled separately.
4345
4446 Parameters
4547 ----------
4648 section : str
4749 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 .
5052 delimiter : str | None
5153 The delimiter used to split the strings.
5254 fallback : list | None
5355 The fallback value in case of errors.
54- duplicated_ok : bool
55- If True allow duplicate values.
56- strip: bool
56+ strip : bool
5757 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.
5860
5961 Returns
6062 -------
@@ -79,20 +81,23 @@ def get_list(
7981 allowed_hosts == ["github.com", "boo.com gitlab.com", "host com"]
8082 """
8183 try :
82- value = self .get (section , item )
84+ value = self .get (section , option )
8385 if isinstance (value , str ):
8486 content = value .split (sep = delimiter )
8587
8688 if strip :
8789 content = [x .strip () for x in content if x .strip ()]
8890
89- if duplicated_ok :
91+ if not remove_duplicates :
9092 return content
9193
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 :
96101 logger .error (error )
97102
98103 return fallback or []
0 commit comments