summaryrefslogtreecommitdiff
path: root/lib/extensions/lettering_custom_font_dir.py
blob: 1568e3916c4e7fabb945d965d771c8bd1cfa642e (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
# 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 json
import os

import appdirs
from inkex import errormsg

from ..i18n import _
from .base import InkstitchExtension


class LetteringCustomFontDir(InkstitchExtension):
    '''
    This extension will create a json file to store a custom directory path for additional user fonts
    '''
    def __init__(self, *args, **kwargs):
        InkstitchExtension.__init__(self, *args, **kwargs)
        self.arg_parser.add_argument("--notebook")
        self.arg_parser.add_argument("-d", "--path", type=str, default="", dest="path")

    def effect(self):
        path = self.options.path
        if not os.path.isdir(path):
            errormsg(_("Please specify the directory of your custom fonts."))
            return

        data = {'custom_font_dir': '%s' % path}

        try:
            config_path = appdirs.user_config_dir('inkstitch')
        except ImportError:
            config_path = os.path.expanduser('~/.inkstitch')
        if not os.path.exists(config_path):
            os.makedirs(config_path)
        config_path = os.path.join(config_path, 'custom_dirs.json')

        with open(config_path, 'w', encoding="utf8") as font_data:
            json.dump(data, font_data, indent=4, ensure_ascii=False)


def get_custom_font_dir():
    custom_font_dir_path = os.path.join(appdirs.user_config_dir('inkstitch'), 'custom_dirs.json')
    try:
        with open(custom_font_dir_path, 'r') as custom_dirs:
            custom_dir = json.load(custom_dirs)
    except (IOError, ValueError):
        return ""
    try:
        return custom_dir['custom_font_dir']
    except KeyError:
        pass
    return ""