summaryrefslogtreecommitdiff
path: root/lib/inx/utils.py
blob: 944c265ce46000ccd0272080126698e4a43b2726 (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
# Authors: see git history
#
# Copyright (c) 2010 Authors
# Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.

import os
import sys
from os.path import dirname

from jinja2 import Environment, FileSystemLoader

from ..i18n import translation as default_translation

_top_path = dirname(dirname(dirname(os.path.realpath(__file__))))
inx_path = os.path.join(_top_path, "inx")
template_path = os.path.join(_top_path, "templates")
version_path = _top_path

current_translation = default_translation


def build_environment():
    env = Environment(
        loader=FileSystemLoader(template_path),
        autoescape=True,
        extensions=['jinja2.ext.i18n']
    )

    env.install_gettext_translations(current_translation)

    with open(os.path.join(version_path, 'LICENSE'), 'r') as license:
        env.globals["inkstitch_license"] = "".join(license.readlines())

    if "BUILD" in os.environ:
        # building a ZIP release, with inkstitch packaged as a binary
        # About extension: add version information
        with open(os.path.join(version_path, 'VERSION'), 'r') as version:
            env.globals["inkstitch_version"] = "%s" % version.readline()
        # Command tag and icons path
        if sys.platform == "win32":
            env.globals["command_tag"] = '<command location="inx">../bin/inkstitch.exe</command>'
            env.globals["image_path"] = '../bin/icons/'
        elif sys.platform == "darwin":
            env.globals["command_tag"] = '<command location="inx">../MacOS/inkstitch</command>'
            env.globals["image_path"] = '../MacOS/icons/'
        else:
            env.globals["command_tag"] = '<command location="inx">../bin/inkstitch</command>'
            env.globals["image_path"] = '../bin/icons/'
    else:
        # user is running inkstitch.py directly as a developer
        env.globals["command_tag"] = '<command location="inx" interpreter="python">../inkstitch.py</command>'
        env.globals["image_path"] = '../icons/'
        env.globals["inkstitch_version"] = "Manual Install"
    return env


def write_inx_file(name, contents):
    inx_file_name = "inkstitch_%s.inx" % name
    with open(os.path.join(inx_path, inx_file_name), 'w', encoding="utf-8") as inx_file:
        print(contents, file=inx_file)