summaryrefslogtreecommitdiff
path: root/lib/extensions/install.py
blob: 43e95414e223ea03e52c052fbe224eecce51b445 (plain)
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
# Authors: see git history
#
# Copyright (c) 2025 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

import os
import sys
from glob import glob

from inkex import Boolean, errormsg

from ..i18n import _
from ..utils import get_bundled_dir, guess_inkscape_config_path
from .base import InkstitchExtension


class Install(InkstitchExtension):
    def __init__(self, *args, **kwargs):
        InkstitchExtension.__init__(self, *args, **kwargs)
        self.arg_parser.add_argument("--notebook")
        self.arg_parser.add_argument("--install-palettes", type=Boolean, default=False, dest="install_palettes")
        self.arg_parser.add_argument("--install-symbol-libraries", type=Boolean, default=False, dest="install_symbol_libraries")

        self.inkscape_config_path = guess_inkscape_config_path()
        self.install_resources = get_bundled_dir('addons')

    def effect(self):
        installation_success = []
        if self.options.install_palettes:
            installation_success.append(self.install_palettes())
        if self.options.install_symbol_libraries:
            installation_success.append(self.install_symbol_libraries())
        if all(installation_success):
            self.install_success_message()

    def install_palettes(self):
        path = os.path.join(self.inkscape_config_path, 'palettes')
        # palettes are also used otherwise, so they have their own location
        src_dir = get_bundled_dir('palettes')
        try:
            copy_files(glob(os.path.join(src_dir, "*")), path)
            return True
        except IOError as error:
            self.install_error_message(_("Could not install color palettes. Please file an issue on"), error)
            return False

    def install_symbol_libraries(self):
        path = os.path.join(self.inkscape_config_path, 'symbols')
        src_dir = os.path.join(self.install_resources, 'symbols')
        try:
            copy_files(glob(os.path.join(src_dir, "*")), path)
            return True
        except IOError as error:
            self.install_error_message(_("Could not install color palettes. Please file an issue on"), error)
            return False

    def install_success_message(self):
        errormsg(_("Successfully installed Addons.\n\nPlease restart Inkscape."))

    def install_error_message(self, text, error):
        errormsg(text + " https://github.com/inkstitch/inkstitch/issues\n\n")
        errormsg(error)


if sys.platform == "win32":
    # If we try to just use shutil.copy it says the operation requires elevation.
    def copy_files(files, dest):
        import pythoncom
        import winutils

        pythoncom.CoInitialize()

        if not os.path.exists(dest):
            os.makedirs(dest)

        winutils.copy(files, dest)
else:
    def copy_files(files, dest):
        import shutil

        if not os.path.exists(dest):
            os.makedirs(dest)

        for palette_file in files:
            shutil.copy(palette_file, dest)