Skip to content

Commit 5114612

Browse files
committed
Expand the ability to specify parameters by is_base or not
1 parent d0fce33 commit 5114612

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

menuinst/_schema.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ def Field(*args, **kwargs):
6262
return _Field(*args, **kwargs)
6363

6464

65-
class MenuItemNameDict(BaseModel):
65+
class TargetIsBaseConStr(BaseModel):
6666
"""
67-
Variable menu item name.
68-
Use this dictionary if the menu item name depends on installation parameters
69-
such as the target environment.
67+
Variable that depends on whether or not the the installation is in the base env.
68+
69+
This can help configure the menu item on if the system is in a base environment.
7070
"""
7171

7272
target_environment_is_base: Optional[constr(min_length=1)] = Field(
@@ -78,6 +78,21 @@ class MenuItemNameDict(BaseModel):
7878
description=("Name when target environment is not the base environment."),
7979
)
8080

81+
class TargetIsBaseConList(BaseModel):
82+
"""
83+
Variable that depends on whether or not the the installation is in the base env.
84+
85+
This can help configure the menu item on if the system is in a base environment.
86+
"""
87+
88+
target_environment_is_base: Optional[conlist(str, min_items=1)] = Field(
89+
None,
90+
description=("Name when target environment is the base environment."),
91+
)
92+
target_environment_is_not_base: Optional[conlist(str, min_items=1)] = Field(
93+
None,
94+
description=("Name when target environment is not the base environment."),
95+
)
8196

8297
class BasePlatformSpecific(BaseModel):
8398
"""
@@ -105,7 +120,7 @@ class BasePlatformSpecific(BaseModel):
105120
None,
106121
description=("Path to the file representing or containing the icon."),
107122
)
108-
command: Optional[conlist(str, min_items=1)] = Field(
123+
command: Optional[conlist(str, min_items=1), TargetIsBaseConList] = Field(
109124
None,
110125
description=(
111126
"""
@@ -119,7 +134,7 @@ class BasePlatformSpecific(BaseModel):
119134
description=(
120135
"""
121136
Working directory for the running process.
122-
Defaults to user directory on each platform.
137+
Defaults to user recipe/owl-mcam_viewer-menu-unix.jsondirectory on each platform.
123138
"""
124139
),
125140
)
@@ -322,7 +337,7 @@ class Linux(BasePlatformSpecific):
322337
"""
323338
),
324339
)
325-
StartupWMClass: Optional[str] = Field(
340+
StartupWMClass: Optional[str, TargetIsBaseConStr]] = Field(
326341
None,
327342
description=(
328343
"""
@@ -331,7 +346,7 @@ class Linux(BasePlatformSpecific):
331346
"""
332347
),
333348
)
334-
TryExec: Optional[str] = Field(
349+
TryExec: Optional[str, TargetIsBaseConStr] = Field(
335350
None,
336351
description=(
337352
"""
@@ -350,6 +365,10 @@ class Linux(BasePlatformSpecific):
350365
"""
351366
),
352367
)
368+
run_in_bash: Optional[bool] = Field(
369+
True,
370+
description=("Whether to activate the target environment before running `command`."),
371+
)
353372

354373

355374
class MacOS(BasePlatformSpecific):
@@ -620,7 +639,7 @@ class Platforms(BaseModel):
620639
class MenuItem(BaseModel):
621640
"Instructions to create a menu item across operating systems."
622641

623-
name: Union[constr(min_length=1), MenuItemNameDict] = Field(
642+
name: Union[constr(min_length=1), TargetIsBaseConStr] = Field(
624643
...,
625644
description=(
626645
"""
@@ -633,7 +652,7 @@ class MenuItem(BaseModel):
633652
...,
634653
description=("A longer description of the menu item. Shown on popup messages."),
635654
)
636-
command: conlist(str, min_items=1) = Field(
655+
command: Union[conlist(str, min_items=1), TargetIsBaseConList] = Field(
637656
...,
638657
description=(
639658
"""

menuinst/platforms/linux.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ def _update_desktop_database(self):
219219
def _command(self) -> str:
220220
parts = []
221221
precommand = self.render_key("precommand")
222+
if isinstance(precommand, dict):
223+
if self.menu.prefix.samefile(self.menu.base_prefix):
224+
precommand = precommand.get("target_environment_is_base", "")
225+
else:
226+
precommand = precommand.get("target_environment_is_not_base", "")
222227
if precommand:
223228
parts.append(precommand)
224229
if self.metadata["activate"]:
@@ -228,8 +233,17 @@ def _command(self) -> str:
228233
else:
229234
activate = "shell.bash activate"
230235
parts.append(f'eval "$("{conda_exe}" {activate} "{self.menu.prefix}")"')
231-
parts.append(" ".join(UnixLex.quote_args(self.render_key("command"))))
232-
return "bash -c " + shlex.quote(" && ".join(parts))
236+
command = self.render_key("command")
237+
if isinstance(command, dict):
238+
if self.menu.prefix.samefile(self.menu.base_prefix):
239+
command = command.get("target_environment_is_base", "")
240+
else:
241+
command = command.get("target_environment_is_not_base", "")
242+
parts.append(" ".join(UnixLex.quote_args(command)))
243+
if self.metadata["run_in_bash"]:
244+
return "bash -c " + shlex.quote(" && ".join(parts))
245+
else:
246+
return " && ".join(parts)
233247

234248
def _write_desktop_file(self):
235249
if self.location.exists():
@@ -263,6 +277,15 @@ def _write_desktop_file(self):
263277
value = self.render_key(key)
264278
if value is None:
265279
continue
280+
if isinstance(value, dict):
281+
if self.menu.prefix.samefile(self.menu.base_prefix):
282+
value = value.get("target_environment_is_base", "")
283+
else:
284+
value = value.get("target_environment_is_not_base", "")
285+
if not value:
286+
raise ValueError(f"Cannot parse `{key}` from dictionary representation.")
287+
288+
# Now parse as bool/list/str
266289
if isinstance(value, bool):
267290
value = str(value).lower()
268291
elif isinstance(value, (list, tuple)):

0 commit comments

Comments
 (0)