@@ -55,14 +55,36 @@ def __init__(self, path: Path, *args: Any, **kwargs: Any) -> None:
55
55
_ConfigType = TypeVar ("_ConfigType" , bound = BaseOptions )
56
56
57
57
58
+ class InvalidTomlError (ValueError ):
59
+ pass
60
+
61
+
62
+ class InvalidTomlFileError (ValueError ):
63
+ pass
64
+
65
+
66
+ def _load_toml (data : Union [str , Path ]) -> Dict [str , Any ]:
67
+ file = None
68
+ if isinstance (data , Path ):
69
+ file = data
70
+ data = data .read_text ("utf-8" )
71
+
72
+ try :
73
+ return tomllib .loads (data )
74
+ except tomllib .TOMLDecodeError as e :
75
+ if file :
76
+ raise InvalidTomlFileError (f"Invalid TOML file'{ file .absolute ()} ': { e } " ) from e
77
+ raise InvalidTomlError (f"Invalid TOML: { e } " ) from e
78
+
79
+
58
80
def load_robot_config_from_robot_toml_str (__s : str ) -> RobotConfig :
59
81
return load_config_from_robot_toml_str (RobotConfig , __s )
60
82
61
83
62
84
def load_config_from_robot_toml_str (
63
- config_type : Type [_ConfigType ], data : Union [str , Dict [str , Any ]], tool_name : Optional [str ] = None
85
+ config_type : Type [_ConfigType ], data : Union [str , Dict [str , Any ], Path ], tool_name : Optional [str ] = None
64
86
) -> _ConfigType :
65
- dict_data = tomllib . loads (data ) if isinstance (data , str ) else data
87
+ dict_data = _load_toml (data ) if isinstance (data , ( str , Path ) ) else data
66
88
67
89
if tool_name :
68
90
try :
@@ -76,9 +98,9 @@ def load_config_from_robot_toml_str(
76
98
77
99
78
100
def load_config_from_pyproject_toml_str (
79
- config_type : Type [_ConfigType ], tool_name : str , data : Union [str , Dict [str , Any ]]
101
+ config_type : Type [_ConfigType ], tool_name : str , data : Union [str , Dict [str , Any ], Path ]
80
102
) -> _ConfigType :
81
- dict_data = tomllib . loads (data ) if isinstance (data , str ) else data
103
+ dict_data = _load_toml (data ) if isinstance (data , ( str , Path ) ) else data
82
104
83
105
return from_dict (dict_data .get ("tool" , {}).get (tool_name , {}), config_type )
84
106
@@ -93,13 +115,13 @@ def _load_config_data_from_path(
93
115
try :
94
116
if path .name == PYPROJECT_TOML :
95
117
return load_config_from_pyproject_toml_str (
96
- config_type , pyproject_toml_tool_name , path . read_text ( "utf-8" ) if data is None else data
118
+ config_type , pyproject_toml_tool_name , path if data is None else data
97
119
)
98
120
99
121
if path .name == ROBOT_TOML or path .name == LOCAL_ROBOT_TOML or path .suffix == ".toml" :
100
122
return load_config_from_robot_toml_str (
101
123
config_type ,
102
- path . read_text ( "utf-8" ) if data is None else data ,
124
+ path if data is None else data ,
103
125
tool_name = robot_toml_tool_name ,
104
126
)
105
127
raise TypeError ("Unknown config file type." )
@@ -143,7 +165,7 @@ def load_config_from_path(
143
165
verbose_callback (f"Load configuration from { __path if isinstance (__path , Path ) else __path [0 ]} " )
144
166
145
167
p = __path if isinstance (__path , Path ) else __path [0 ]
146
- data = tomllib . loads ( p . read_text ( "utf-8" ) )
168
+ data = _load_toml ( p )
147
169
148
170
result .add_options (
149
171
_load_config_data_from_path (
0 commit comments