summaryrefslogtreecommitdiff
path: root/asciifarmclient/loaders.py
blob: 790f71a7f6114a0b04f0f1f41908713b36805e2b (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

import os

from .paths import keybindingsPath, charmapPath
from .charmap import CharMap
import json


standardKeyFiles = {
    "default": os.path.join(keybindingsPath, "default.json"),
    "azerty": os.path.join(keybindingsPath, "azerty.json")
}

def loadKeybindings(name):
    fname = None
    if name in standardKeyFiles:
        fname = standardKeyFiles[name]
    else:
        fname = name
    with open(fname) as f:
        data = json.load(f)
    bindings = {}
    help = ""
    for ftemplate in data.get("templates", []):
        if ftemplate.partition(os.sep)[0] in {".", ".."}:
            ftemplate = os.path.relpath(ftemplate, fname)
        template = loadKeybindings(ftemplate)
        bindings.update(template.get("actions", {}))
        help = template.get("help", help)
    bindings.update(data.get("actions", {}))
    help = data.get("help", help)
    return {"actions": bindings, "help": help}


standardCharFiles = {name: os.path.join(charmapPath, file) for name, file in {
    "default": "fullwidth.json",
    "halfwidth": "halfwidth.json",
    "hw": "halfwidth.json",
    "fullwidth": "fullwidth.json",
    "fw": "fullwidth.json",
    "emoji": "emoji.json"
}.items()}

def loadCharmapJson(name):
    
    fname = None
    if name in standardCharFiles:
        fname = standardCharFiles[name]
    else:
        fname = name
    with open(fname) as f:
        data = json.load(f)
    
    templates = []
    for ftemplate in data.get("templates", []):
        if ftemplate.partition(os.sep)[0] in {".", ".."}:
            ftemplate = os.path.relpath(ftemplate, fname)
        templates.extend(loadCharmapJson(ftemplate))
    
    templates.append(data)
    return templates

def loadCharmap(name):
    
    templates = loadCharmapJson(name)
    charmap = CharMap()
    for template in templates:
        charmap.apply_json(template)
    
    return charmap