99from pathlib import Path
1010from sys import exit as sysexit
1111from sys import stdin , stdout
12-
13- from fhconfparser import FHConfParser , SimpleConf
12+ from configurator import Config
13+ from configurator .node import ConfigNode
14+ from pathlib import Path
1415
1516from licensecheck import checker , fmt , license_matrix , packageinfo , types
1617
@@ -129,26 +130,29 @@ def main(args: dict) -> int:
129130 """
130131 exitCode = 0
131132
132- configparser = FHConfParser ()
133- namespace = ["tool" ]
134- configparser .parseConfigList (
135- [("pyproject.toml" , "toml" ), ("setup.cfg" , "ini" )]
136- + [
137- (f"{ directory } /licensecheck.{ ext } " , ext )
138- for ext in ("toml" , "json" , "ini" )
139- for directory in ["." , str (Path .home ())]
140- ],
141- namespace ,
142- namespace ,
143- )
144- simpleConf = SimpleConf (configparser , "licensecheck" , args )
133+ config : ConfigNode = Config ()
134+
135+ config_files = [
136+ "~/licensecheck.json" ,
137+ "~/licensecheck.toml" ,
138+ "licensecheck.json" ,
139+ "licensecheck.toml" ,
140+ "setup.cfg" ,
141+ "pyproject.toml" ,
142+ ]
143+
144+ for file in config_files :
145+ config += Config .from_path (file , optional = True )
146+
147+ scopedData : ConfigNode = config .get ("tool" , default = {}).get ("licensecheck" , default = ConfigNode ())
148+ scopedConfig = {** scopedData .data , ** args }
145149
146150 # File
147- requirements_paths = simpleConf .get ("requirements_paths" ) or ["__stdin__" ]
151+ requirements_paths = scopedConfig .get ("requirements_paths" ) or ["__stdin__" ]
148152 output_file = (
149153 stdout
150- if simpleConf .get ("file" ) is None
151- else Path (simpleConf .get ("file" )).open ("w" , encoding = "utf-8" )
154+ if scopedConfig .get ("file" ) in [ None , "" ]
155+ else Path (scopedConfig .get ("file" )).open ("w" , encoding = "utf-8" )
152156 )
153157
154158 # Get my license
@@ -158,14 +162,14 @@ def main(args: dict) -> int:
158162 this_license = license_matrix .licenseType (this_license_text )[0 ]
159163
160164 def getFromConfig (key : str ) -> list [types .ucstr ]:
161- return list (map (types .ucstr , simpleConf .get (key , [])))
165+ return list (map (types .ucstr , scopedConfig .get (key , [])))
162166
163- package_info_manager = packageinfo .PackageInfoManager (simpleConf .get ("pypi_api" ))
167+ package_info_manager = packageinfo .PackageInfoManager (scopedConfig .get ("pypi_api" ))
164168
165169 incompatible , depsWithLicenses = checker .check (
166170 requirements_paths = requirements_paths ,
167- groups = simpleConf .get ("groups" , []),
168- extras = simpleConf .get ("extras" , []),
171+ groups = scopedConfig .get ("groups" , []),
172+ extras = scopedConfig .get ("extras" , []),
169173 this_license = this_license ,
170174 package_info_manager = package_info_manager ,
171175 ignore_packages = getFromConfig ("ignore_packages" ),
@@ -177,18 +181,20 @@ def getFromConfig(key: str) -> list[types.ucstr]:
177181 )
178182
179183 # Format the results
180- hide_output_parameters = [types .ucstr (x ) for x in simpleConf .get ("hide_output_parameters" , [])]
184+ hide_output_parameters = [
185+ types .ucstr (x ) for x in scopedConfig .get ("hide_output_parameters" , [])
186+ ]
181187 available_params = [param .name .upper () for param in fields (types .PackageInfo )]
182188 if not all (hop in available_params for hop in hide_output_parameters ):
183189 msg = (
184190 f"Invalid parameter(s) in `hide_output_parameters`. "
185191 f"Valid parameters are: { ', ' .join (available_params )} "
186192 )
187193 raise ValueError (msg )
188- if simpleConf .get ("format" , "simple" ) in fmt .formatMap :
194+ if scopedConfig .get ("format" , "simple" ) in fmt .formatMap :
189195 print (
190196 fmt .fmt (
191- simpleConf .get ("format" , "simple" ),
197+ scopedConfig .get ("format" , "simple" ),
192198 this_license ,
193199 sorted (depsWithLicenses ),
194200 hide_output_parameters ,
@@ -200,10 +206,10 @@ def getFromConfig(key: str) -> list[types.ucstr]:
200206 exitCode = 2
201207
202208 # Exit code of 1 if args.zero
203- if simpleConf .get ("zero" , False ) and incompatible :
209+ if scopedConfig .get ("zero" , False ) and incompatible :
204210 exitCode = 1
205211
206212 # Cleanup + exit
207- if simpleConf .get ("file" ) is not None :
213+ if scopedConfig .get ("file" ) not in [ None , "" ] :
208214 output_file .close ()
209215 return exitCode
0 commit comments