Skip to content

Commit 850c751

Browse files
committed
feat(robotcode): better formatting and include active, selected, enabled state of a profile in profile list command
1 parent d4747e2 commit 850c751

File tree

6 files changed

+86
-15
lines changed

6 files changed

+86
-15
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
// "--no-color",
4444
// "--no-pager",
4545
//"config", "info", "list",
46-
"analyze",
46+
// "analyze",
47+
"profiles", "list"
4748
// "."
4849
]
4950
},

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,24 @@ def echo_as_markdown(self, text: str) -> None:
171171
if self.colored:
172172
try:
173173
from rich.console import Console, ConsoleOptions, RenderResult
174-
from rich.markdown import Heading, Markdown
174+
from rich.markdown import (
175+
Heading,
176+
Markdown,
177+
TableBodyElement,
178+
TableDataElement,
179+
TableElement,
180+
TableHeaderElement,
181+
TableRowElement,
182+
)
175183
from rich.text import Text
176184

185+
# this is needed because of https://github.com/Textualize/rich/issues/3027
186+
TableElement.new_line = False
187+
TableHeaderElement.new_line = False
188+
TableBodyElement.new_line = False
189+
TableRowElement.new_line = False
190+
TableDataElement.new_line = False
191+
177192
class MyHeading(Heading):
178193
def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:
179194
for result in super().__rich_console__(console, options):

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ def evaluate(self) -> bool:
189189
except Exception as e:
190190
raise EvaluationError(self.if_, str(e)) from e
191191

192+
def __bool__(self) -> bool:
193+
return self.evaluate()
194+
192195

193196
@dataclass()
194197
class NamePattern(ValidateMixin):
@@ -348,7 +351,9 @@ def evaluated(self) -> Self:
348351
result = dataclasses.replace(self)
349352
for f in dataclasses.fields(result):
350353
try:
351-
if isinstance(getattr(result, f.name), Expression):
354+
if isinstance(getattr(result, f.name), Condition):
355+
setattr(result, f.name, getattr(result, f.name).evaluate())
356+
elif isinstance(getattr(result, f.name), Expression):
352357
setattr(result, f.name, getattr(result, f.name).evaluate())
353358
elif isinstance(getattr(result, f.name), list):
354359
setattr(
@@ -2277,11 +2282,9 @@ def combine_profiles(self, *names: str, verbose_callback: Optional[Callable[...,
22772282

22782283
for profile_name, profile in sorted(selected_profiles.items(), key=lambda x: x[1].precedence or 0):
22792284
try:
2280-
if profile.enabled is not None and (
2281-
isinstance(profile.enabled, Condition) and not profile.enabled.evaluate() or not profile.enabled
2282-
):
2285+
if profile.enabled is not None and not bool(profile.enabled):
22832286
if verbose_callback:
2284-
verbose_callback(f'Skipping profile "{profile_name}" because it\'s disabled.')
2287+
verbose_callback(f'Skipping profile "{profile_name}" because it\'s disabled.')
22852288
continue
22862289
except EvaluationError as e:
22872290
raise ValueError(f'Error evaluating "enabled" condition for profile "{profile_name}": {e}') from e

src/robotcode/cli/commands/profiles.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
DiscoverdBy,
88
load_robot_config_from_path,
99
)
10+
from robotcode.robot.config.model import EvaluationError, RobotProfile
1011
from robotcode.robot.config.utils import get_config_files
1112

1213

@@ -66,9 +67,20 @@ def list(
6667
k for k in config.select_profiles(*(app.config.profiles or []), verbose_callback=app.verbose).keys()
6768
]
6869

70+
def check_enabled(name: str, profile: RobotProfile) -> bool:
71+
try:
72+
return profile.enabled is None or bool(profile.enabled)
73+
except EvaluationError as e:
74+
raise ValueError(f"Cannot evaluate profile '{name}'.enabled: {e}") from e
75+
6976
result: Dict[str, Any] = {
7077
"profiles": [
71-
{"name": k, "description": v.description or "", "selected": True if k in selected_profiles else False}
78+
{
79+
"name": k,
80+
"enabled": check_enabled(k, v),
81+
"description": v.description or "",
82+
"selected": True if k in selected_profiles else False,
83+
}
7284
for k, v in (config.profiles or {}).items()
7385
]
7486
}
@@ -85,7 +97,30 @@ def list(
8597

8698
if app.config.output_format is None or app.config.output_format == OutputFormat.TEXT:
8799
for v in result["profiles"]:
88-
app.echo(f'{"* " if v["selected"] else " "}{v["name"]} {v["description"] if v["description"] else ""}')
100+
for k in ["name", "description"]:
101+
lines = v[k].splitlines()
102+
v[k] = " ".join(lines[:1]) + (" ..." if len(lines) > 1 else "")
103+
104+
header = ""
105+
max_name = max(*(len(profile["name"]) for profile in result["profiles"]), len("Name"))
106+
max_description = max(*(len(profile["description"]) for profile in result["profiles"]), len("Description"))
107+
header += (
108+
f'| Active | Selected | Enabled | Name{(max_name-len("Name"))*" "} '
109+
f'| Description{(max_description-len("Description"))*" "} |\n'
110+
)
111+
header += f"|:------:|:--------:|:-------:|:{max_name*'-'}-|:{max_description*'-'}-|\n"
112+
for selected, enabled, name, description in (
113+
(v["selected"], v["enabled"], v["name"], v["description"]) for v in result["profiles"]
114+
):
115+
header += (
116+
f'| {"*" if selected and enabled else " "} '
117+
f'| {"*" if selected else " "} '
118+
f'| {"*" if enabled else " "} '
119+
f'| {name}{(max_name-len(name))*" "} '
120+
f'| {description if description else ""}{(max_description-len(description))*" "} |\n'
121+
)
122+
123+
app.echo_as_markdown(header)
89124
else:
90125
app.print_data(result)
91126

tests/robotcode/language_server/robotframework/parts/data/.robot.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
default-profiles = "firefox"
1+
#default-profiles = "firefox"
22

33

4-
#extra-args = ["--profile", "default"]
4+
#extends-args = ["--profile", "default"]
55
#[extra-profiles.abc]
6-
[extra-profiles.firefox]
6+
[extend-profiles.firefox]
77
description = "The overridden firefox profile"
88
extend-python-path = ["./python"]
99
#extra-python-path = ["./python1"]

tests/robotcode/language_server/robotframework/parts/data/robot.toml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
default-profiles = ["devel"]
1+
default-profiles = [
2+
"devel",
3+
"disabled_profile",
4+
"disabled_profile_with_env_var",
5+
]
26
paths = ["."]
37
python-path = ["./lib", "./resources"]
48
output-dir = { expr = "f'results/run-{datetime.now().strftime(\"%d-%m-%Y-%H.%M.%S\")}'" }
@@ -24,7 +28,13 @@ CMD_VAR_LONG = "a test"
2428
ANOTHER_VAR = { expr = "environ.get('CMD_VAR_LONG', 'default')" }
2529

2630
[profiles.devel]
27-
description = "The devel profile"
31+
description = """The devel profile
32+
this is a second line
33+
this is a third line
34+
this is a fourth line
35+
this is a fifth line
36+
37+
and this is a sixth line"""
2838

2939
[profiles.devel.extend-variables]
3040
BROWSER = "chromium"
@@ -49,10 +59,17 @@ extend-variables = { BROWSER = "chromium" }
4959
API_URL = "https://api.prod.company.com"
5060
APP_URL = "https://app.prod.company.com"
5161

62+
[profiles.disabled_profile]
63+
enabled = false
64+
65+
[profiles.disabled_profile_with_env_var]
66+
enabled = { if = "environ.get('DISABLED_PROFILE') in ['true', '1']" }
67+
68+
5269
[profiles.test1.extend-variables]
5370
API_URL = "https://api.test1.company.com"
5471
APP_URL = "https://app.test1.company.com"
5572

5673
[tool.robotcode-analyze]
5774
select = ["all"]
58-
ignore = ["KeywordNotFound"]
75+
ignore = ["KeywordNotFound"]

0 commit comments

Comments
 (0)