Skip to content

Commit 470b438

Browse files
committed
fix(robot): handle OSErrors if creating/reading user robot.toml
should fix: #187
1 parent e1ac0cb commit 470b438

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

packages/robot/src/robotcode/robot/config/loader.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ConfigType(str, Enum):
3434
ROBOT_TOML = "robot.toml (project file)"
3535
LOCAL_ROBOT_TOML = ".robot.toml (local file)"
3636
USER_DEFAULT_CONFIG_TOML = "robot.toml (user default config)"
37+
DEFAULT_CONFIG_TOML = "(default config)"
3738
CUSTOM_TOML = ".toml (custom file)"
3839

3940

@@ -118,6 +119,12 @@ def load_config_from_path(
118119
result = config_type()
119120

120121
for __path in __paths:
122+
if isinstance(__path, tuple):
123+
path, c_type = __path
124+
if path.name == "__no_user_config__.toml" and c_type == ConfigType.DEFAULT_CONFIG_TOML:
125+
result.add_options(get_default_config())
126+
continue
127+
121128
result.add_options(
122129
_load_config_data_from_path(
123130
config_type,

packages/robot/src/robotcode/robot/config/utils.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@ def get_user_config_file(
1616
create: bool = True,
1717
verbose_callback: Optional[Callable[[str], None]] = None,
1818
) -> Optional[Path]:
19-
result = Path(platformdirs.user_config_dir("robotcode", appauthor=False), "robot.toml")
20-
if result.is_file():
21-
if verbose_callback:
22-
verbose_callback(f"Found user configuration file:\n {result}")
23-
return result
19+
try:
20+
result = Path(platformdirs.user_config_dir("robotcode", appauthor=False), "robot.toml")
21+
if result.is_file():
22+
if verbose_callback:
23+
verbose_callback(f"Found user configuration file:\n {result}")
24+
return result
25+
26+
if not create:
27+
if verbose_callback:
28+
verbose_callback("User configuration file not found, but create is set to False.")
29+
return None
2430

25-
if not create:
2631
if verbose_callback:
27-
verbose_callback("User configuration file not found, but create is set to False.")
28-
return None
32+
verbose_callback(f"User configuration file not found, try to create it at:\n {result}")
2933

30-
if verbose_callback:
31-
verbose_callback(f"User configuration file not found, try to create it at:\n {result}")
32-
try:
3334
get_default_config().save(result)
35+
36+
return result
3437
except OSError as e:
3538
if verbose_callback:
3639
verbose_callback(f"Cannot create user configuration file `{result}`:\n {e}")
3740
return None
3841

39-
return result
40-
4142

4243
def get_config_files(
4344
paths: Optional[Sequence[Union[str, Path]]] = None,
@@ -73,7 +74,11 @@ def get_config_files(
7374

7475
return (
7576
[
76-
*([(user_config, ConfigType.USER_DEFAULT_CONFIG_TOML)] if user_config else []),
77+
*(
78+
[(user_config, ConfigType.USER_DEFAULT_CONFIG_TOML)]
79+
if user_config
80+
else [(Path("__no_user_config__.toml"), ConfigType.DEFAULT_CONFIG_TOML)]
81+
),
7782
*result,
7883
],
7984
root_folder,

0 commit comments

Comments
 (0)