Skip to content

Commit e9480ec

Browse files
committed
simplify --launch handling and session parsing
1 parent 1ee6447 commit e9480ec

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

qbpm/main.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from . import __version__, operations, profiles
1010
from .profiles import Profile
11-
from .utils import SUPPORTED_MENUS, default_profile_dir, error
11+
from .utils import SUPPORTED_MENUS, default_profile_dir, error, user_data_dir
1212

1313
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
1414

@@ -39,11 +39,7 @@ def main(ctx, profile_dir: Path) -> None:
3939
@click.pass_context
4040
def new(ctx, profile_name: str, launch: bool, foreground: bool, **kwargs):
4141
profile = Profile(profile_name, ctx.obj["PROFILE_DIR"])
42-
result = profiles.new_profile(profile, **kwargs)
43-
if result and launch:
44-
# TODO args?
45-
exit_with(then_launch(profile, foreground, []))
46-
exit_with(result)
42+
then_launch(profiles.new_profile, profile, **kwargs)
4743

4844

4945
@main.command()
@@ -56,15 +52,12 @@ def new(ctx, profile_name: str, launch: bool, foreground: bool, **kwargs):
5652
@click.pass_context
5753
def from_session(
5854
ctx,
59-
launch: bool,
60-
foreground: bool,
55+
session: str,
56+
profile_name: Optional[str],
6157
**kwargs,
6258
):
63-
profile = operations.from_session(profile_dir=ctx.obj["PROFILE_DIR"], **kwargs)
64-
if profile and launch:
65-
# TODO args?
66-
exit_with(then_launch(profile, foreground, []))
67-
exit_with(bool(profile))
59+
profile, session_path = session_info(session, profile_name, ctx.obj["PROFILE_DIR"])
60+
then_launch(operations.from_session, profile, session_path=session_path, **kwargs)
6861

6962

7063
@main.command()
@@ -86,7 +79,7 @@ def desktop(
8679
def launch(ctx, profile_name: str, **kwargs):
8780
profile = Profile(profile_name, ctx.obj["PROFILE_DIR"])
8881
# TODO qb args
89-
exit_with(operations.launch(profile, **kwargs))
82+
exit_with(operations.launch(profile, qb_args=[], **kwargs))
9083

9184

9285
@main.command()
@@ -119,11 +112,36 @@ def list_(ctx):
119112
print(profile.name)
120113

121114

122-
def then_launch(profile: Profile, foreground: bool, qb_args: list[str]) -> bool:
123-
result = False
124-
if profile:
125-
result = operations.launch(profile, False, foreground, qb_args)
126-
return result
115+
def then_launch(
116+
operation: Callable[..., bool],
117+
profile: Profile,
118+
launch: bool,
119+
foreground: bool,
120+
qb_args: list[str] = [],
121+
**kwargs,
122+
) -> None:
123+
exit_with(
124+
operation(profile, **kwargs)
125+
and ((not launch) or operations.launch(profile, False, foreground, qb_args))
126+
)
127+
128+
129+
def session_info(
130+
session: str, profile_name: Optional[str], profile_dir: Path
131+
) -> tuple[Profile, Path]:
132+
user_session_dir = user_data_dir() / "sessions"
133+
session_paths = []
134+
if not "/" in session:
135+
session_paths.append(user_session_dir / (session + ".yml"))
136+
session_paths.append(Path(session))
137+
session_path = next(filter(lambda path: path.is_file(), session_paths), None)
138+
139+
if not session_path:
140+
tried = ", ".join([str(p.resolve()) for p in session_paths])
141+
error(f"could not find session at the following paths: {tried}")
142+
exit(1)
143+
144+
return (Profile(profile_name or session_path.stem, profile_dir), session_path)
127145

128146

129147
def exit_with(result: bool):

qbpm/operations.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,19 @@
1313

1414

1515
def from_session(
16-
session: str,
17-
profile_name: Optional[str] = None,
18-
profile_dir: Optional[Path] = None,
16+
profile: Profile,
17+
session_path: Path,
1918
desktop_file: bool = True,
2019
overwrite: bool = False,
21-
) -> Optional[Profile]:
22-
if session.endswith(".yml"):
23-
session_file = Path(session).expanduser()
24-
session_name = session_file.stem
25-
else:
26-
session_name = session
27-
session_file = user_data_dir() / "sessions" / (session_name + ".yml")
28-
if not session_file.is_file():
29-
error(f"{session_file} is not a file")
30-
return None
31-
32-
profile = Profile(profile_name or session_name, profile_dir)
20+
) -> bool:
3321
if not profiles.new_profile(profile, None, desktop_file, overwrite):
34-
return None
22+
return False
3523

3624
session_dir = profile.root / "data" / "sessions"
3725
session_dir.mkdir(parents=True, exist_ok=overwrite)
38-
shutil.copy(session_file, session_dir / "_autosave.yml")
26+
shutil.copy(session_path, session_dir / "_autosave.yml")
3927

40-
return profile
28+
return True
4129

4230

4331
def launch(

0 commit comments

Comments
 (0)