Skip to content

Commit 98cd345

Browse files
committed
move system path handling to paths.py
1 parent 5510d8a commit 98cd345

File tree

7 files changed

+64
-64
lines changed

7 files changed

+64
-64
lines changed

src/qbpm/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from pathlib import Path
22
from typing import Optional
33

4-
from xdg_base_dirs import xdg_data_home
5-
6-
from .utils import error, qutebrowser_exe
4+
from .paths import qutebrowser_exe
5+
from .utils import error
76

87
try:
98
from qbpm.version import version as __version__ # type: ignore
@@ -16,9 +15,9 @@ class Profile:
1615
profile_dir: Path
1716
root: Path
1817

19-
def __init__(self, name: str, profile_dir: Path | None) -> None:
18+
def __init__(self, name: str, profile_dir: Path) -> None:
2019
self.name = name
21-
self.profile_dir = profile_dir or (xdg_data_home() / "qutebrowser-profiles")
20+
self.profile_dir = profile_dir
2221
self.root = self.profile_dir / name
2322

2423
def check(self) -> Optional["Profile"]:

src/qbpm/desktop.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import textwrap
22
from pathlib import Path
33

4-
from xdg_base_dirs import xdg_data_home
5-
64
from . import Profile
5+
from .paths import default_qbpm_application_dir
76

87
MIME_TYPES = [
98
"text/html",
@@ -21,9 +20,7 @@
2120

2221

2322
# TODO expose application_dir through config
24-
def create_desktop_file(
25-
profile: Profile, application_dir: Path | None = None
26-
) -> None:
23+
def create_desktop_file(profile: Profile, application_dir: Path | None = None) -> None:
2724
text = textwrap.dedent(f"""\
2825
[Desktop Entry]
2926
Name={profile.name} (qutebrowser profile)
@@ -49,9 +46,3 @@ def create_desktop_file(
4946
""")
5047
application_dir = application_dir or default_qbpm_application_dir()
5148
(application_dir / f"{profile.name}.desktop").write_text(text)
52-
53-
54-
def default_qbpm_application_dir() -> Path:
55-
path = xdg_data_home() / "applications" / "qbpm"
56-
path.mkdir(parents=True, exist_ok=True)
57-
return path

src/qbpm/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import click
99

1010
from . import Profile, operations, profiles
11-
from .utils import SUPPORTED_MENUS, default_profile_dir, error, or_phrase, user_data_dir
11+
from .paths import default_profile_dir, qutebrowser_data_dir
12+
from .utils import SUPPORTED_MENUS, error, or_phrase
1213

1314
CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}
1415

@@ -68,7 +69,7 @@ def format(self, record: logging.LogRecord) -> str:
6869
type=click.Path(file_okay=False, writable=True, path_type=Path),
6970
envvar="QBPM_PROFILE_DIR",
7071
show_envvar=True,
71-
default=default_profile_dir,
72+
default=None,
7273
help="Location to store qutebrowser profiles.",
7374
)
7475
@click.option(
@@ -78,13 +79,13 @@ def format(self, record: logging.LogRecord) -> str:
7879
type=click.Choice(["debug", "info", "error"], case_sensitive=False),
7980
)
8081
@click.pass_context
81-
def main(ctx: click.Context, profile_dir: Path, log_level: str) -> None:
82+
def main(ctx: click.Context, profile_dir: Path | None, log_level: str) -> None:
8283
root_logger = logging.getLogger()
8384
root_logger.setLevel(log_level.upper())
8485
handler = logging.StreamHandler()
8586
handler.setFormatter(LowerCaseFormatter("{levelname}: {message}", style="{"))
8687
root_logger.addHandler(handler)
87-
ctx.obj = Context(profile_dir)
88+
ctx.obj = Context(profile_dir or default_profile_dir())
8889

8990

9091
@main.command()
@@ -199,7 +200,7 @@ def then_launch(
199200
def session_info(
200201
session: str, profile_name: str | None, context: Context
201202
) -> tuple[Profile, Path]:
202-
user_session_dir = user_data_dir() / "sessions"
203+
user_session_dir = qutebrowser_data_dir() / "sessions"
203204
session_paths = []
204205
if "/" not in session:
205206
session_paths.append(user_session_dir / (session + ".yml"))

src/qbpm/operations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from . import Profile, profiles
88
from .desktop import create_desktop_file
9-
from .utils import env_menus, error, installed_menus, or_phrase, qutebrowser_exe
9+
from .paths import qutebrowser_exe
10+
from .utils import env_menus, error, installed_menus, or_phrase
1011

1112

1213
def from_session(

src/qbpm/paths.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import platform
2+
from pathlib import Path
3+
4+
from click import get_app_dir
5+
from xdg_base_dirs import xdg_config_home, xdg_data_home
6+
7+
8+
def qutebrowser_exe() -> str:
9+
macos_app = "/Applications/qutebrowser.app/Contents/MacOS/qutebrowser"
10+
if platform == "darwin" and Path(macos_app).exists():
11+
return macos_app
12+
else:
13+
return "qutebrowser"
14+
15+
16+
def default_qbpm_application_dir() -> Path:
17+
path = xdg_data_home() / "applications" / "qbpm"
18+
path.mkdir(parents=True, exist_ok=True)
19+
return path
20+
21+
22+
def default_profile_dir() -> Path:
23+
path = xdg_data_home() / "qutebrowser-profiles"
24+
path.mkdir(parents=True, exist_ok=True)
25+
return path
26+
27+
28+
def qutebrowser_data_dir() -> Path:
29+
if platform.system() == "Linux":
30+
return xdg_data_home() / "qutebrowser"
31+
# TODO confirm this works on windows
32+
return Path(get_app_dir("qutebrowser", roaming=True))
33+
34+
35+
def qutebrowser_config_dirs() -> list[Path]:
36+
# deduplicate while maintaining order
37+
return list(
38+
dict.fromkeys(
39+
[
40+
Path(get_app_dir("qutebrowser", roaming=True)),
41+
xdg_config_home() / "qutebrowser",
42+
Path.home() / ".qutebrowser",
43+
]
44+
)
45+
)

src/qbpm/profiles.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from . import Profile
66
from .desktop import create_desktop_file
7-
from .utils import error, or_phrase, user_config_dirs
7+
from .paths import qutebrowser_config_dirs
8+
from .utils import error, or_phrase
89

910
MIME_TYPES = [
1011
"text/html",
@@ -71,7 +72,7 @@ def new_profile(
7172
desktop_file: bool | None = None,
7273
overwrite: bool = False,
7374
) -> bool:
74-
qb_config_dir = resolve_qb_config_dir(qb_config_dir)
75+
qb_config_dir = find_qutebrowser_config_dir(qb_config_dir)
7576
if not qb_config_dir:
7677
return False
7778
if create_profile(profile, overwrite):
@@ -82,12 +83,12 @@ def new_profile(
8283
return False
8384

8485

85-
def resolve_qb_config_dir(qb_config_dir: Path | None) -> Path | None:
86+
def find_qutebrowser_config_dir(qb_config_dir: Path | None) -> Path | None:
8687
config_file = "config.py"
8788
dirs = (
8889
[qb_config_dir, qb_config_dir / "config"]
8990
if qb_config_dir
90-
else user_config_dirs()
91+
else qutebrowser_config_dirs()
9192
)
9293
for config_dir in dirs:
9394
if (config_dir / config_file).exists():

src/qbpm/utils.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
import platform
33
from collections.abc import Iterator
44
from os import environ
5-
from pathlib import Path
65
from shutil import which
76

8-
from click import get_app_dir
9-
from xdg_base_dirs import xdg_config_home, xdg_data_home
10-
117
WAYLAND_MENUS = ["fuzzel", "wofi", "dmenu-wl"]
128
X11_MENUS = ["rofi", "dmenu"]
139
SUPPORTED_MENUS = [*WAYLAND_MENUS, *X11_MENUS, "fzf", "applescript"]
@@ -21,32 +17,6 @@ def error(msg: str) -> None:
2117
logging.error(msg)
2218

2319

24-
def default_profile_dir() -> Path:
25-
path = xdg_data_home() / "qutebrowser-profiles"
26-
path.mkdir(parents=True, exist_ok=True)
27-
return path
28-
29-
30-
def user_data_dir() -> Path:
31-
if platform.system() == "Linux":
32-
return xdg_data_home() / "qutebrowser"
33-
# TODO confirm this works on windows
34-
return Path(get_app_dir("qutebrowser", roaming=True))
35-
36-
37-
def user_config_dirs() -> list[Path]:
38-
# deduplicate while maintaining order
39-
return list(
40-
dict.fromkeys(
41-
[
42-
Path(get_app_dir("qutebrowser", roaming=True)),
43-
xdg_config_home() / "qutebrowser",
44-
Path.home() / ".qutebrowser",
45-
]
46-
)
47-
)
48-
49-
5020
def installed_menus() -> Iterator[str]:
5121
if platform.system() == "Darwin":
5222
yield "applescript"
@@ -81,11 +51,3 @@ def or_phrase(items: list) -> str:
8151
return " or ".join(strings)
8252
else:
8353
return ", or ".join([", ".join(strings[0:-1]), strings[-1]])
84-
85-
86-
def qutebrowser_exe() -> str:
87-
macos_app = "/Applications/qutebrowser.app/Contents/MacOS/qutebrowser"
88-
if platform == "darwin" and Path(macos_app).exists():
89-
return macos_app
90-
else:
91-
return "qutebrowser"

0 commit comments

Comments
 (0)