Skip to content

Commit 37fdbb3

Browse files
committed
feat(profiles): enhanced handling of environment variables in profile settings
This update allows the setting, overriding, and utilization of system environment variables in expressions and conditions within the robot.toml configuration files. This enhancement increases the flexibility and adaptability of profile configurations.
1 parent 92c3802 commit 37fdbb3

File tree

11 files changed

+89
-49
lines changed

11 files changed

+89
-49
lines changed

packages/analyze/src/robotcode/analyze/cli.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ def analyze(app: Application, paths: Tuple[str]) -> Union[str, int, None]:
3030
config_files, root_folder, _ = get_config_files(paths, app.config.config_files, verbose_callback=app.verbose)
3131

3232
try:
33+
robot_config = (
34+
load_robot_config_from_path(*config_files)
35+
.combine_profiles(*(app.config.profiles or []))
36+
.evaluated_with_env()
37+
)
38+
3339
analyzer_config = load_config_from_path(
3440
AnalyzerConfig,
3541
*config_files,
3642
tool_name="robotcode-analyze",
3743
robot_toml_tool_name="robotcode-analyze",
3844
).evaluated()
3945

40-
robot_config = (
41-
load_robot_config_from_path(*config_files).combine_profiles(*(app.config.profiles or [])).evaluated()
42-
)
43-
4446
except (TypeError, ValueError) as e:
4547
raise click.ClickException(str(e)) from e
4648

packages/language_server/src/robotcode/language_server/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def language_server(
8080
profile = (
8181
load_robot_config_from_path(*config_files)
8282
.combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose)
83-
.evaluated()
83+
.evaluated_with_env()
8484
)
8585
except (TypeError, ValueError) as e:
8686
app.echo(str(e), err=True)

packages/language_server/src/robotcode/language_server/robotframework/protocol.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,6 @@ def server_initialized(self, sender: Any) -> None:
213213
for folder in self.workspace.workspace_folders:
214214
config: RobotConfig = self.workspace.get_configuration(RobotConfig, folder.uri)
215215

216-
for k, v in (self.robot_profile.env or {}).items():
217-
os.environ[k] = str(v)
218-
219216
for p in self.robot_profile.python_path or []:
220217
pa = Path(str(p))
221218
if not pa.is_absolute():

packages/plugin/src/robotcode/plugin/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ def verbose(
100100
self,
101101
message: Union[str, Callable[[], Any], None],
102102
file: Optional[IO[AnyStr]] = None,
103-
nl: bool = True,
104-
err: bool = True,
103+
nl: Optional[bool] = True,
104+
err: Optional[bool] = True,
105105
) -> None:
106106
if self.config.verbose:
107107
click.secho(
108108
message() if callable(message) else message,
109109
file=file,
110-
nl=nl,
111-
err=err,
110+
nl=nl if nl is not None else True,
111+
err=err if err is not None else True,
112112
color=self.colored,
113113
fg="bright_black",
114114
)

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ def add_options(self, config: "BaseOptions", combine_extends: bool = False) -> N
364364
if new is not None:
365365
setattr(self, f.name, new)
366366

367-
def evaluated(self) -> Self:
367+
def evaluated(self, verbose_callback: Optional[Callable[[Union[str, Callable[[], Any]]], None]] = None) -> Self:
368+
if verbose_callback is not None:
369+
verbose_callback("Evaluating options")
370+
368371
result = dataclasses.replace(self)
369372
for f in dataclasses.fields(result):
370373
try:
@@ -2191,6 +2194,17 @@ def save(self, path: "os.PathLike[str]") -> None:
21912194
with Path(path).open("w", encoding="utf-8") as f:
21922195
f.write(tomli_w.dumps(as_dict(self, remove_defaults=True)))
21932196

2197+
def evaluated_with_env(
2198+
self, verbose_callback: Optional[Callable[[Union[str, Callable[[], Any]]], None]] = None
2199+
) -> Self:
2200+
if self.env:
2201+
for k, v in self.env.items():
2202+
os.environ[k] = str(v)
2203+
if verbose_callback:
2204+
verbose_callback(lambda: f"Set environment variable {k} to {v}")
2205+
2206+
return self.evaluated(verbose_callback)
2207+
21942208

21952209
@dataclass
21962210
class RobotExtraBaseProfile(RobotBaseProfile):
@@ -2285,7 +2299,7 @@ class RobotConfig(RobotExtraBaseProfile):
22852299
def select_profiles(
22862300
self,
22872301
*names: str,
2288-
verbose_callback: Optional[Callable[..., None]] = None,
2302+
verbose_callback: Optional[Callable[[Union[str, Callable[[], Any]]], None]] = None,
22892303
) -> Dict[str, RobotProfile]:
22902304
result: Dict[str, RobotProfile] = {}
22912305

@@ -2316,9 +2330,7 @@ def select_profiles(
23162330
return result
23172331

23182332
def combine_profiles(
2319-
self,
2320-
*names: str,
2321-
verbose_callback: Optional[Callable[..., None]] = None,
2333+
self, *names: str, verbose_callback: Optional[Callable[[Union[str, Callable[[], Any]]], None]] = None
23222334
) -> RobotBaseProfile:
23232335
type_hints = get_type_hints(RobotBaseProfile)
23242336
base_field_names = [f.name for f in dataclasses.fields(RobotBaseProfile)]

packages/runner/src/robotcode/runner/cli/libdoc.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,12 @@ def libdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None:
7070
robot_arguments, app.config.config_files, verbose_callback=app.verbose
7171
)
7272
try:
73-
profile = load_robot_config_from_path(*config_files).combine_profiles(
74-
*(app.config.profiles or []), verbose_callback=app.verbose
73+
profile = (
74+
load_robot_config_from_path(*config_files)
75+
.combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose)
76+
.evaluated_with_env()
7577
)
7678

77-
if profile.env:
78-
for k, v in profile.env.items():
79-
os.environ[k] = str(v)
80-
app.verbose(lambda: f"Set environment variable {k} to {v}")
81-
profile = profile.evaluated()
8279
except (TypeError, ValueError) as e:
8380
raise click.ClickException(str(e)) from e
8481

packages/runner/src/robotcode/runner/cli/rebot.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,11 @@ def rebot(app: Application, robot_options_and_args: Tuple[str, ...]) -> None:
7171
)
7272

7373
try:
74-
profile = load_robot_config_from_path(*config_files).combine_profiles(
75-
*(app.config.profiles or []), verbose_callback=app.verbose
74+
profile = (
75+
load_robot_config_from_path(*config_files)
76+
.combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose)
77+
.evaluated_with_env()
7678
)
77-
if profile.env:
78-
for k, v in profile.env.items():
79-
os.environ[k] = str(v)
80-
app.verbose(lambda: f"Set environment variable {k} to {v}")
81-
profile = profile.evaluated()
8279

8380
except (TypeError, ValueError) as e:
8481
raise click.ClickException(str(e)) from e

packages/runner/src/robotcode/runner/cli/robot.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,11 @@ def handle_robot_options(
108108
robot_arguments, app.config.config_files, verbose_callback=app.verbose
109109
)
110110
try:
111-
profile = load_robot_config_from_path(*config_files).combine_profiles(
112-
*(app.config.profiles or []), verbose_callback=app.verbose
111+
profile = (
112+
load_robot_config_from_path(*config_files)
113+
.combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose)
114+
.evaluated_with_env()
113115
)
114-
115-
if profile.env:
116-
for k, v in profile.env.items():
117-
os.environ[k] = str(v)
118-
app.verbose(lambda: f"Set environment variable {k} to {v}")
119-
120-
profile = profile.evaluated()
121-
122116
except (TypeError, ValueError) as e:
123117
raise click.ClickException(str(e)) from e
124118

packages/runner/src/robotcode/runner/cli/testdoc.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ def testdoc(app: Application, robot_options_and_args: Tuple[str, ...]) -> None:
7171
)
7272

7373
try:
74-
profile = load_robot_config_from_path(*config_files).combine_profiles(
75-
*(app.config.profiles or []), verbose_callback=app.verbose
74+
profile = (
75+
load_robot_config_from_path(*config_files)
76+
.combine_profiles(*(app.config.profiles or []), verbose_callback=app.verbose)
77+
.evaluated_with_env()
7678
)
77-
if profile.env:
78-
for k, v in profile.env.items():
79-
os.environ[k] = str(v)
80-
app.verbose(lambda: f"Set environment variable {k} to {v}")
81-
profile = profile.evaluated()
79+
8280
except (TypeError, ValueError) as e:
8381
raise click.ClickException(str(e)) from e
8482

src/robotcode/cli/commands/profiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def show(app: Application, no_evaluate: bool, paths: List[Path]) -> None:
4848
)
4949

5050
if not no_evaluate:
51-
config = config.evaluated()
51+
config = config.evaluated_with_env()
5252

5353
app.print_data(
5454
config,

0 commit comments

Comments
 (0)