@@ -134,45 +134,58 @@ def read_config(self, filepath: str):
134134 raise ConfigError ("allowlist.enabled must be a boolean value" )
135135 self .allowlist_enabled = allowlist_enabled
136136
137- allowlist_strings = self ._get_cfg (["allowlist" , "regexes" ], required = True )
138- if not isinstance (allowlist_strings , list ) or (
139- isinstance (allowlist_strings , list )
140- and any (not isinstance (x , str ) for x in allowlist_strings )
141- ):
142- raise ConfigError ("allowlist.regexes must be a list of strings" )
143-
144- allowlist_regexes = []
145- for regex in allowlist_strings :
146- try :
147- allowlist_regexes .append (re .compile (regex ))
148- except re .error :
149- raise ConfigError (
150- f"'{ regex } ' contained in allowlist.regexes is not a valid regular expression"
151- )
152- self .allowlist_regexes = allowlist_regexes
137+ self .allowlist_regexes = self ._compile_regexes (
138+ ["allowlist" , "regexes" ], required = True
139+ )
153140
154141 # Blocklist configuration
155142 blocklist_enabled = self ._get_cfg (["blocklist" , "enabled" ], required = True )
156143 if not isinstance (blocklist_enabled , bool ):
157144 raise ConfigError ("blocklist.enabled must be a boolean value" )
158145 self .blocklist_enabled = blocklist_enabled
159146
160- blocklist_strings = self ._get_cfg (["blocklist" , "regexes" ], required = True )
161- if not isinstance (blocklist_strings , list ) or (
162- isinstance (blocklist_strings , list )
163- and any (not isinstance (x , str ) for x in blocklist_strings )
147+ self .blocklist_regexes = self ._compile_regexes (
148+ ["blocklist" , "regexes" ], required = True
149+ )
150+
151+ def _compile_regexes (
152+ self , path : list [str ], required : bool = True
153+ ) -> list [re .Pattern ]:
154+ """Compile a config option containing a list of strings into re.Pattern objects.
155+
156+ Args:
157+ path: The path to the config option.
158+ required: True, if the config option is mandatory.
159+
160+ Returns:
161+ A list of re.Pattern objects.
162+
163+ Raises:
164+ ConfigError:
165+ - If required is specified, but the config option does not exist.
166+ - If the config option is not a list of strings.
167+ - If the config option contains an invalid regular expression.
168+ """
169+
170+ readable_path = "." .join (path )
171+ regex_strings = self ._get_cfg (path , required = required ) # raises ConfigError
172+
173+ if not isinstance (regex_strings , list ) or (
174+ isinstance (regex_strings , list )
175+ and any (not isinstance (x , str ) for x in regex_strings )
164176 ):
165- raise ConfigError ("blocklist.regexes must be a list of strings" )
177+ raise ConfigError (f" { readable_path } must be a list of strings" )
166178
167- blocklist_regexes = []
168- for regex in blocklist_strings :
179+ compiled_regexes = []
180+ for regex in regex_strings :
169181 try :
170- blocklist_regexes .append (re .compile (regex ))
171- except re .error :
182+ compiled_regexes .append (re .compile (regex ))
183+ except re .error as e :
172184 raise ConfigError (
173- f"'{ regex } ' contained in blocklist.regexes is not a valid regular expression"
185+ f"'{ e . pattern } ' contained in { readable_path } is not a valid regular expression"
174186 )
175- self .blocklist_regexes = blocklist_regexes
187+
188+ return compiled_regexes
176189
177190 def _get_cfg (
178191 self ,
0 commit comments