Skip to content

install

Functions:

Attributes:

BASE_URL module-attribute

BASE_URL = 'https://api.github.com/repos'

BRANCH module-attribute

BRANCH = 'master'

CONTENT_URL module-attribute

CONTENT_URL = (
    f"https://raw.githubusercontent.com/{PLUGINS_PATH}/{BRANCH}/{path}"
)

PLUGINS_PATH module-attribute

PLUGINS_PATH = f'Jaded-Encoding-Thaumaturgy/{REPO_NAME}'

PLUGIN_STRING module-attribute

PLUGIN_STRING = ' {:25s}{install_from}{:10s} {:30s} {:s}'

REPO_NAME module-attribute

REPO_NAME = 'vs-preview-plugins'

plugins_commands module-attribute

plugins_commands = ('install', 'uninstall', 'update', 'available')

get_repo_plugins

get_repo_plugins() -> dict[str, Iterable[str]]
Source code in vspreview/plugins/install.py
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
def get_repo_plugins() -> dict[str, Iterable[str]]:
    from requests import get

    response = get(f'{BASE_URL}/{PLUGINS_PATH}/git/trees/{BRANCH}?recursive=1')

    if response.status_code != 200:
        logging.error('There was an error fetching plugin data!')
        sys.exit(1)

    data = response.json()

    if data['truncated']:
        logging.error('Bug setsu to fix!')
        sys.exit(1)

    data = data['tree']

    base_paths = list[str](set(d['path'] for d in data if d['type'] == 'tree' and '/' not in d['path']))

    def _get_generator(path: str) -> Iterable[str]:
        return (
            d['path'] for d in data
            if ('type' not in d or d['type'] != 'tree') and d['path'].startswith(f'{path}/')
        )

    return {path: _get_generator(path) for path in base_paths}

install_plugins

install_plugins(
    plugins: list[str], force: bool = False, no_deps: bool = False
) -> None
Source code in vspreview/plugins/install.py
 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
def install_plugins(plugins: list[str], force: bool = False, no_deps: bool = False) -> None:
    from shutil import copytree, rmtree
    from subprocess import Popen
    from tempfile import TemporaryDirectory

    from requests import Session

    existing_packages = get_repo_plugins()

    found_plugins = set(existing_packages.keys()).intersection(plugins)

    not_found_plugins = set(plugins) - found_plugins
    if not_found_plugins:
        logging.warning(f'Could not find the following plugins: "{", ".join(not_found_plugins)}"')

    with Session() as s:
        for plugin in found_plugins:
            if (MainWindow.global_plugins_dir / plugin).exists():
                if not force:
                    logging.info(f'Skipping "{plugin}" as it\'s already installed.')
                    continue
                else:
                    rmtree(MainWindow.global_plugins_dir / plugin)

            logging.info(f'Downloading "{plugin}"...')

            with TemporaryDirectory() as tmpdir:
                tempdir = Path(tmpdir)

                requirements = list[Path]()

                for file in existing_packages[plugin]:
                    logging.info(f'Collecting "{file}"...')

                    temp = tempdir / file
                    (temp if temp.is_dir() else temp.parent).mkdir(parents=True, exist_ok=True)

                    with s.get(CONTENT_URL.format(path=file), stream=True) as req:
                        req.raise_for_status()
                        with temp.open('wb') as f:
                            for chunk in req.iter_content(8192):
                                f.write(chunk)

                    if Path(file).name == 'requirements.txt':
                        requirements.append(temp)

                for requirement in requirements:
                    if not no_deps:
                        Popen([sys.executable, '-m', 'pip', 'install', '-r', str(requirement)]).wait()
                    requirement.unlink()

                copytree(tempdir / plugin, MainWindow.global_plugins_dir / plugin)

print_available_plugins

print_available_plugins() -> None
Source code in vspreview/plugins/install.py
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
def print_available_plugins() -> None:
    existing_packages = set(get_repo_plugins().keys())

    def _header(value: str) -> str:
        return f'{value}\n{"-" * len(value)}'

    installed_plugins = [
        (
            plugin_cls.__module__, kind, plugin_cls._config.display_name,
            plugin_cls._config.namespace, sys.modules[plugin_cls.__module__].__file__
        )
        for kind, plugin_parent in (('Generic', AbstractPlugin), ('Source', FileResolverPlugin))
        for plugin_cls in [v for _, v in sorted(get_installed_plugins(plugin_parent, True).items(), key=lambda v: v[0])]
    ]

    preinstalled_packages = set[Path]()
    installed_packages = set[str](module for module, *_ in installed_plugins)

    plugins_path = Path(__file__).parent

    print(_header('Installed plugins:'))
    print(PLUGIN_STRING.format('Package', 'Type', 'Name', 'Namespace', install_from=''))

    for *values, module_path in installed_plugins:
        if not module_path:
            continue

        install_path = Path(module_path).parent

        if plugins_path == install_path:
            preinstalled_packages.add(install_path)
        elif (MainWindow.global_plugins_dir in install_path.parents) or (install_path.parent.stem == REPO_NAME):
            installed_packages.add(install_path.stem)

    for *values, module_path in installed_plugins:
        if module_path:
            pkg_name = Path(module_path).parent

            if pkg_name in preinstalled_packages:
                install_from = '*'
            elif pkg_name.stem in existing_packages:
                install_from = '+'
            else:
                install_from = '?'
        else:
            install_from = ' '

        print(' ' + PLUGIN_STRING.format(*values, install_from=install_from))

    print('\n * => Pre-installed, + => Installed, ? => Custom\n')

    external_packages = existing_packages - installed_packages

    print(_header('Missing VSPreview Plugins:'))

    if not len(external_packages):
        print('  None found!')
    else:
        print('  ' + ', '.join(external_packages))

uninstall_plugins

uninstall_plugins(plugins: list[str], ignore: bool = False) -> None
Source code in vspreview/plugins/install.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def uninstall_plugins(plugins: list[str], ignore: bool = False) -> None:
    from shutil import rmtree

    not_found_plugins = set[str]()

    for plugin in plugins:
        if (MainWindow.global_plugins_dir / plugin).exists():
            rmtree(MainWindow.global_plugins_dir / plugin)
        else:
            not_found_plugins.add(plugin)

    if ignore:
        return

    found_plugins = set(plugins) - not_found_plugins

    if not_found_plugins:
        logging.warning(f'Could not find plugins: "{", ".join(not_found_plugins)}"')

    logging.info(f'Successfully uninstalled "{", ".join(found_plugins)}"')