summaryrefslogtreecommitdiff
path: root/asciifarm/client/loaders.py
blob: d3d7ed1da616cfbf4fdbeb2ee6e21511b763fc57 (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
86
87

import os

from .paths import keybindingsPath, charmapPath
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": "halfwidth.json",
    "halfwidth": "halfwidth.json",
    "hw": "halfwidth.json",
    "fullwidth": "fullwidth.json",
    "fw": "fullwidth.json",
    "emoji": "emoji.json"
}.items()}

def loadCharmap(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.append(loadCharmap(ftemplate))
    
    templates.append(data)
    
    mapping = {}
    writable = {}
    default = None
    charwidth = 1
    healthfull = None
    healthempty = None
    alphabet = ""
    msgcolours = {}
    
    for template in templates:
        mapping.update(template.get("mapping", {}))
        writable.update(template.get("writable", {}))
        default = template.get("default", default)
        charwidth = template.get("charwidth", charwidth)
        healthfull = template.get("healthfull", healthfull)
        healthempty = template.get("healthempty", healthempty)
        alphabet = template.get("alphabet", alphabet)
        msgcolours.update(template.get("msgcolours", {}))
    return {
        "mapping": mapping,
        "writable": writable,
        "default": default,
        "charwidth": charwidth,
        "healthfull": healthfull,
        "healthempty": healthempty,
        "alphabet": alphabet,
        "msgcolours": msgcolours
    }