66from pathlib import Path
77from typing import Any
88
9- _toml = importlib .import_module ("tomllib" if __import__ ("sys" ).version_info >= (3 , 11 ) else "tomli" )
9+ _toml = importlib .import_module (
10+ "tomllib" if __import__ ("sys" ).version_info >= (3 , 11 ) else "tomli"
11+ )
1012
1113
1214def load_file (path : str ) -> dict [str , Any ]:
@@ -37,18 +39,23 @@ def load_file(path: str) -> dict[str, Any]:
3739
3840def _load_yaml (path : Path ) -> dict [str , Any ]:
3941 import yaml
42+
4043 with open (path , encoding = "utf-8" ) as f :
4144 data = yaml .safe_load (f )
4245 if not isinstance (data , dict ):
43- raise ValueError (f"YAML file must contain a mapping (dict), got { type (data ).__name__ } " )
46+ raise ValueError (
47+ f"YAML file must contain a mapping (dict), got { type (data ).__name__ } "
48+ )
4449 return _flatten_nested (data )
4550
4651
4752def _load_json (path : Path ) -> dict [str , Any ]:
4853 with open (path , encoding = "utf-8" ) as f :
4954 data = json .load (f )
5055 if not isinstance (data , dict ):
51- raise ValueError (f"JSON file must contain a mapping (dict), got { type (data ).__name__ } " )
56+ raise ValueError (
57+ f"JSON file must contain a mapping (dict), got { type (data ).__name__ } "
58+ )
5259 return _flatten_nested (data )
5360
5461
@@ -85,9 +92,9 @@ def _load_dotenv(path: Path) -> dict[str, Any]:
8592 if not line or line .startswith ("#" ):
8693 continue
8794 # Strip optional 'export ' prefix (shell-style .env files)
88- line = re .sub (r' ^export\s+' , '' , line )
95+ line = re .sub (r" ^export\s+" , "" , line )
8996 # Parse KEY=VALUE or KEY="VALUE" or KEY='VALUE'
90- match = re .match (r' ^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$' , line )
97+ match = re .match (r" ^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$" , line )
9198 if match :
9299 key = match .group (1 )
93100 val = match .group (2 ).strip ()
@@ -110,5 +117,7 @@ def _flatten_nested(d: dict[str, Any], prefix: str = "") -> dict[str, Any]:
110117 elif value is None :
111118 result [full_key ] = ""
112119 else :
113- result [full_key ] = str (value ) if not isinstance (value , str | int | float | bool ) else value
120+ result [full_key ] = (
121+ str (value ) if not isinstance (value , str | int | float | bool ) else value
122+ )
114123 return result
0 commit comments