Skip to content

Commit 1c0e956

Browse files
committed
move session code to a module
1 parent 25d0722 commit 1c0e956

File tree

4 files changed

+78
-67
lines changed

4 files changed

+78
-67
lines changed

src/qbpm/main.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
import click
1010

11-
from . import Profile, operations, profiles
11+
from . import Profile, profiles
1212
from .choose import choose_profile
1313
from .config import DEFAULT_CONFIG_FILE, Config, find_config
14+
from .desktop import create_desktop_file
1415
from .launch import launch_qutebrowser
15-
from .log import error, or_phrase
1616
from .menus import supported_menus
17-
from .paths import default_qbpm_config_dir, qutebrowser_data_dir
17+
from .paths import default_qbpm_config_dir
18+
from .session import profile_from_session
1819

1920
CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"], "max_content_width": 91}
2021

@@ -156,7 +157,6 @@ def new(
156157
config.qutebrowser_config_directory = c_opts.qb_config_dir.absolute()
157158
if c_opts.desktop_file is not None:
158159
config.generate_desktop_file = c_opts.desktop_file
159-
160160
exit_with(
161161
profiles.new_profile(
162162
profile,
@@ -185,20 +185,18 @@ def from_session(
185185
or a path to a session yaml file.
186186
"""
187187
config = context.load_config()
188-
profile, session_path = session_info(
189-
session, profile_name, config.profile_directory
190-
)
191188
if c_opts.qb_config_dir:
192189
config.qutebrowser_config_directory = c_opts.qb_config_dir.absolute()
193190
if c_opts.desktop_file is not None:
194191
config.generate_desktop_file = c_opts.desktop_file
192+
profile = profile_from_session(
193+
session,
194+
profile_name,
195+
config,
196+
c_opts.overwrite,
197+
)
195198
exit_with(
196-
operations.from_session(
197-
profile,
198-
session_path,
199-
config,
200-
c_opts.overwrite,
201-
)
199+
profile is not None
202200
and ((not c_opts.launch) or launch_qutebrowser(profile, c_opts.foreground))
203201
)
204202

@@ -284,7 +282,10 @@ def desktop(
284282
"""Create an XDG desktop entry for an existing profile."""
285283
config = context.load_config()
286284
profile = Profile(profile_name, config.profile_directory)
287-
exit_with(operations.desktop(profile, config.desktop_file_directory))
285+
exists = profiles.check(profile)
286+
if exists:
287+
create_desktop_file(profile, config.desktop_file_directory)
288+
return exit_with(exists)
288289

289290

290291
@main.group(context_settings={"help_option_names": []})
@@ -314,28 +315,5 @@ def default() -> None:
314315
print(DEFAULT_CONFIG_FILE.read_text(), end="")
315316

316317

317-
def session_info(
318-
session: str, profile_name: str | None, profile_dir: Path
319-
) -> tuple[Profile, Path]:
320-
user_session_dir = qutebrowser_data_dir() / "sessions"
321-
session_paths = []
322-
if "/" not in session:
323-
session_paths.append(user_session_dir / (session + ".yml"))
324-
session_paths.append(Path(session))
325-
session_path = next(filter(lambda path: path.is_file(), session_paths), None)
326-
327-
if session_path:
328-
return (
329-
Profile(
330-
profile_name or session_path.stem,
331-
profile_dir,
332-
),
333-
session_path,
334-
)
335-
tried = or_phrase([str(p.resolve()) for p in session_paths])
336-
error(f"could not find session file at {tried}")
337-
sys.exit(1)
338-
339-
340318
def exit_with(result: bool) -> NoReturn:
341319
sys.exit(0 if result else 1)

src/qbpm/operations.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/qbpm/session.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import shutil
2+
import sys
3+
from pathlib import Path
4+
5+
from . import Profile, profiles
6+
from .config import Config
7+
from .log import error, or_phrase
8+
from .paths import qutebrowser_data_dir
9+
10+
11+
def profile_from_session(
12+
session: str,
13+
profile_name: str | None,
14+
config: Config,
15+
overwrite: bool = False,
16+
) -> Profile | None:
17+
profile, session_path = session_info(
18+
session, profile_name, config.profile_directory
19+
)
20+
if not profiles.new_profile(profile, config, None, overwrite):
21+
return None
22+
23+
session_dir = profile.root / "data" / "sessions"
24+
session_dir.mkdir(parents=True, exist_ok=overwrite)
25+
shutil.copy(session_path, session_dir / "_autosave.yml")
26+
27+
return profile
28+
29+
30+
def session_info(
31+
session: str, profile_name: str | None, profile_dir: Path
32+
) -> tuple[Profile, Path]:
33+
user_session_dir = qutebrowser_data_dir() / "sessions"
34+
session_paths = []
35+
if "/" not in session:
36+
session_paths.append(user_session_dir / (session + ".yml"))
37+
session_paths.append(Path(session))
38+
session_path = next(filter(lambda path: path.is_file(), session_paths), None)
39+
40+
if session_path:
41+
return (
42+
Profile(
43+
profile_name or session_path.stem,
44+
profile_dir,
45+
),
46+
session_path,
47+
)
48+
tried = or_phrase([str(p.resolve()) for p in session_paths])
49+
error(f"could not find session file at {tried}")
50+
sys.exit(1)

tests/test_main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_relative_config_dir(tmp_path: Path):
4949
assert str(config) in (tmp_path / "test/config/config.py").read_text()
5050

5151

52-
def test_from_session(tmp_path: Path):
52+
def test_from_session_path(tmp_path: Path):
5353
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
5454
(tmp_path / "config.py").touch()
5555
session = tmp_path / "test.yml"
@@ -60,6 +60,18 @@ def test_from_session(tmp_path: Path):
6060
assert (tmp_path / "test/data/sessions/_autosave.yml").read_text() == ("windows:\n")
6161

6262

63+
def test_from_session_name(tmp_path: Path):
64+
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
65+
(tmp_path / "config.py").touch()
66+
environ["XDG_DATA_HOME"] = str(tmp_path)
67+
(tmp_path / "qutebrowser" / "sessions").mkdir(parents=True)
68+
(tmp_path / "qutebrowser" / "sessions" / "test.yml").write_text("windows:\n")
69+
result = run("from-session", "-C", str(tmp_path), "test")
70+
assert result.exit_code == 0
71+
assert result.output.strip() == str(tmp_path / "test")
72+
assert (tmp_path / "test/data/sessions/_autosave.yml").read_text() == ("windows:\n")
73+
74+
6375
def test_config_file(tmp_path: Path):
6476
environ["QBPM_PROFILE_DIR"] = str(tmp_path)
6577
(tmp_path / "config.py").touch()

0 commit comments

Comments
 (0)