~pvsr/qpm

qpm/qpm/main.py -rw-r--r-- 6.3 KiB
8b6b26a0Peter Rice document dmenu-wl support 2 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
from os import environ
from pathlib import Path
from typing import Any, Callable, Optional

from xdg import BaseDirectory  # type: ignore

from qpm import __version__, operations, profiles
from qpm.profiles import Profile
from qpm.utils import SUPPORTED_MENUS

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


def main(mock_args=None) -> None:
    parser = argparse.ArgumentParser(description="qutebrowser profile manager")
    parser.set_defaults(operation=lambda args: parser.print_help())
    parser.add_argument(
        "-P",
        "--profile-dir",
        metavar="directory",
        type=Path,
        help="directory in which profiles are stored",
    )
    parser.add_argument(
        "--set-app-id",
        action="store_true",
        help="set wayland app_id to this profile's name. requires qutebrowser v2.0.0+",
    )
    parser.add_argument(
        "--version",
        action="version",
        version=__version__,
    )

    subparsers = parser.add_subparsers()
    new = subparsers.add_parser("new", help="create a new profile")
    new.add_argument("profile_name", metavar="profile", help="name of the new profile")
    new.add_argument("home_page", metavar="url", nargs="?", help="profile's home page")
    new.set_defaults(
        operation=lambda args: profiles.new_profile(
            build_profile(args),
            args.home_page,
            args.desktop_file,
            args.overwrite,
        )
    )
    creator_args(new)

    session = subparsers.add_parser(
        "from-session", help="create a new profile from a qutebrowser session"
    )
    session.add_argument(
        "session",
        help="path to session file or name of session. "
        "e.g. ~/.local/share/qutebrowser/sessions/example.yml or example",
    )
    session.add_argument(
        "profile_name",
        metavar="profile",
        nargs="?",
        help="name of the new profile. if unset the session name will be used",
    )
    session.set_defaults(
        operation=lambda args: operations.from_session(
            args.session,
            args.profile_name,
            args.profile_dir,
            args.desktop_file,
            args.overwrite,
        )
    )
    creator_args(session)

    desktop = subparsers.add_parser(
        "desktop", help="create a desktop file for an existing profile"
    )
    desktop.add_argument(
        "profile_name", metavar="profile", help="profile to create a desktop file for"
    )
    desktop.set_defaults(operation=lambda args: operations.desktop(build_profile(args)))

    launch = subparsers.add_parser(
        "launch", aliases=["run"], help="launch qutebrowser with the given profile"
    )
    launch.add_argument(
        "profile_name",
        metavar="profile",
        help="profile to launch. it will be created if it does not exist, unless -s is set",
    )
    launch.add_argument(
        "-n",
        "--new",
        action="store_false",
        dest="strict",
        help="create the profile if it doesn't exist",
    )
    launch.add_argument(
        "-f",
        "--foreground",
        action="store_true",
        help="launch qutebrowser in the foreground and print its stdout and stderr to the console",
    )
    launch.set_defaults(
        operation=lambda args: operations.launch(
            build_profile(args), args.strict, args.foreground, args.qb_args
        )
    )

    list_ = subparsers.add_parser("list", help="list existing profiles")
    list_.set_defaults(operation=operations.list_)

    choose = subparsers.add_parser(
        "choose", help="choose profile using rofi, dmenu, or an applescript dialog"
    )
    choose.add_argument(
        "-m",
        "--menu",
        help=f"select which menu application to use (options: {SUPPORTED_MENUS})",
    )
    choose.add_argument(
        "--dmenu",
        metavar="DMENU_CMD",
        help='command used to invoke dmenu (e.g. "dmenu -i -p qbpm", "/path/to/dmenu-wl")',
    )
    choose.add_argument(
        "-f",
        "--foreground",
        action="store_true",
        help="launch qutebrowser in the foreground and print its stdout and stderr to the console",
    )
    choose.set_defaults(operation=operations.choose)

    edit = subparsers.add_parser(
        "edit", help="edit a profile's config.py using $EDITOR"
    )
    edit.add_argument("profile_name", metavar="profile", help="profile to edit")
    edit.set_defaults(operation=lambda args: operations.edit(build_profile(args)))

    raw_args = parser.parse_known_args(mock_args)
    args = raw_args[0]
    args.qb_args = raw_args[1]
    if not args.profile_dir:
        args.profile_dir = Path(environ.get("QPM_PROFILE_DIR") or DEFAULT_PROFILE_DIR)
    args.operation(args)


def creator_args(parser: argparse.ArgumentParser) -> None:
    parser.add_argument(
        "-l",
        "--launch",
        action=ThenLaunchAction,
        dest="operation",
        help="launch the profile after creating",
    )
    parser.add_argument(
        "-f",
        "--foreground",
        action="store_true",
        help="if --launch is set, launch qutebrowser in the foreground",
    )
    parser.add_argument(
        "--no-desktop-file",
        dest="desktop_file",
        action="store_false",
        help="do not generate a desktop file for the profile",
    )
    parser.add_argument(
        "--overwrite",
        action="store_true",
        help="replace existing profile config",
    )
    parser.set_defaults(strict=True)


class ThenLaunchAction(argparse.Action):
    def __init__(self, option_strings, dest, nargs=0, **kwargs):
        super(ThenLaunchAction, self).__init__(
            option_strings, dest, nargs=nargs, **kwargs
        )

    def __call__(self, parser, namespace, values, option_string=None):
        if operation := getattr(namespace, self.dest):
            setattr(namespace, self.dest, lambda args: then_launch(args, operation))


def then_launch(
    args: argparse.Namespace,
    operation: Callable[[argparse.Namespace], Optional[Any]],
) -> bool:
    if result := operation(args):
        if isinstance(result, Profile):
            profile = result
        else:
            profile = build_profile(args)
        return operations.launch(profile, False, args.foreground, [])
    return False


def build_profile(args: argparse.Namespace) -> Profile:
    return Profile(args.profile_name, args.profile_dir, args.set_app_id)


if __name__ == "__main__":
    main()