~pvsr/qpm

ref: 7869c94f1bcf3228aa74532e110fbddd29ccbe24 qpm/qpm/profiles.py -rw-r--r-- 2.7 KiB
7869c94fPeter Rice sigh 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import platform
import sys
from pathlib import Path
from typing import Optional

from xdg import BaseDirectory  # type: ignore

from qpm.utils import error


class Profile:
    name: str
    profile_dir: Path
    root: Path

    def __init__(self, name: str, profile_dir: Optional[Path]) -> None:
        self.name = name
        self.profile_dir = profile_dir or Path(
            BaseDirectory.save_data_path("qutebrowser-profiles")
        )
        self.root = self.profile_dir / name

    def check(self) -> Optional["Profile"]:
        if not self.profile_dir.resolve().is_dir():
            error("{self.profile_dir} is not a directory")
            return None
        if self.profile_dir.resolve() not in self.root.resolve().parents:
            error("will not create profile outside of profile dir. consider using -P")
            return None
        if self.root.exists():
            error(f"{self.root} already exists")
            return None
        for parent in self.root.parents:
            if parent == self.profile_dir:
                break
            if parent.exists():
                error(f"{parent} already exists")
                return None
        return self


main_config_dir = Path(BaseDirectory.xdg_config_home) / "qutebrowser"

if platform.system() == "Linux":
    main_data_dir = Path(BaseDirectory.xdg_data_home) / "qutebrowser"
elif platform.system() == "Darwin":
    main_data_dir = Path.home() / "Library" / "Application Support" / "qutebrowser"
else:
    error("lol")
    sys.exit(1)


def create_profile(profile: Profile) -> bool:
    if not profile.check():
        return False

    config_dir = profile.root / "config"
    config_dir.mkdir(parents=True)
    return True


def create_config(profile: Profile) -> None:
    with (profile.root / "config" / "config.py").open(mode="x") as dest_config:
        print(
            "c.window.title_format = '{perc}{current_title}{title_sep}"
            + f"qutebrowser ({profile.name})'",
            file=dest_config,
        )
        print(f"config.source('{main_config_dir / 'config.py'}')", file=dest_config)
        for conf in main_config_dir.glob("conf.d/*.py"):
            print(f"config.source('{conf}')", file=dest_config)


def ensure_profile_exists(profile: Profile, create: bool = True) -> bool:
    if profile.root.exists() and not profile.root.is_dir():
        error(f"{profile.root} is not a directory")
        return False
    if not profile.root.exists() and create:
        return new_profile(profile)
    if not profile.root.exists():
        error(f"{profile.root} does not exist")
        return False
    return True


def new_profile(profile: Profile) -> bool:
    if create_profile(profile):
        create_config(profile)
        return True
    return False