~pvsr/qpm

ref: 762b429a05c0d081856493f1855dc63b5cd0988e qpm/qpm/operations.py -rw-r--r-- 1.7 KiB
762b429aPeter Rice readd list 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
import os
import shutil
import subprocess
from pathlib import Path
from typing import Iterable, Optional

from xdg import BaseDirectory

from qpm import profiles
from qpm.profiles import Profile
from qpm.utils import error


def from_session(
    session_name: str, profile_name: Optional[str] = None
) -> Optional[Profile]:
    session = profiles.main_data_dir / "sessions" / (session_name + ".yml")
    if not session.is_file():
        error(f"{session} is not a file")
        return None

    profile = Profile(profile_name or session_name, None)
    if not profiles.new_profile(profile):
        return None

    session_dir = profile.root / "data" / "sessions"
    session_dir.mkdir(parents=True)
    shutil.copy(session, session_dir / "_autosave.yml")

    return profile


def launch(
    profile: Profile, strict: bool, foreground: bool, qb_args: Iterable[str]
) -> bool:
    if not profiles.ensure_profile_exists(profile, not strict):
        return False

    if foreground:
        os.execlp("qutebrowser", "qutebrowser", "-B", str(profile.root), *qb_args)
    else:
        p = subprocess.Popen(
            ["qutebrowser", "-B", str(profile.root), *qb_args],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.PIPE,
        )
        try:
            # give qb a chance to validate input before returning to shell
            stdout, stderr = p.communicate(timeout=0.1)
            print(stderr.decode(errors="ignore"), end="")
        except subprocess.TimeoutExpired:
            pass

    return True


DEFAULT_PROFILE_DIR = Path(BaseDirectory.xdg_data_home) / "qutebrowser-profiles"


def list_() -> None:
    for profile in DEFAULT_PROFILE_DIR.iterdir():
        print(profile.name)