Asciifarm/asciifarm/client/loaders.py

82 lines
2.3 KiB
Python
Raw Normal View History

import os
2019-01-18 16:29:37 +00:00
from .paths import keybindingsPath, charmapPath
import json
standardKeyFiles = {
2019-01-18 17:11:04 +00:00
"default": os.path.join(keybindingsPath, "default.json"),
"azerty": os.path.join(keybindingsPath, "azerty.json")
}
def loadKeybindings(name):
fname = None
if name in standardKeyFiles:
2019-01-18 16:29:37 +00:00
fname = standardKeyFiles[name]
else:
fname = name
with open(fname) as f:
data = json.load(f)
bindings = {}
2019-01-18 16:29:37 +00:00
help = ""
for ftemplate in data.get("templates", []):
if ftemplate.partition(os.sep)[0] in {".", ".."}:
ftemplate = os.path.relpath(ftemplate, fname)
2019-01-18 16:59:45 +00:00
template = loadKeybindings(ftemplate)
2019-01-18 16:29:37 +00:00
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}
2019-01-18 16:59:45 +00:00
standardCharFiles = {name: os.path.join(charmapPath, file) for name, file in {
"default": "fullwidth.json",
2019-01-18 17:11:04 +00:00
"halfwidth": "halfwidth.json",
"hw": "halfwidth.json",
"fullwidth": "fullwidth.json",
2019-01-18 16:29:37 +00:00
"fw": "fullwidth.json",
"emoji": "emoji.json"
}.items()}
def loadCharmap(name):
fname = None
2019-01-18 16:29:37 +00:00
if name in standardCharFiles:
fname = standardCharFiles[name]
else:
fname = name
with open(fname) as f:
data = json.load(f)
2019-01-18 16:29:37 +00:00
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
alphabet = ""
2019-01-20 00:19:24 +00:00
msgcolours = {}
2019-01-18 16:29:37 +00:00
for template in templates:
mapping.update(template.get("mapping", {}))
writable.update(template.get("writable", {}))
default = template.get("default", default)
charwidth = template.get("charwidth", charwidth)
alphabet = template.get("alphabet", alphabet)
2019-01-20 00:19:24 +00:00
msgcolours.update(template.get("msgcolours", {}))
2019-01-18 16:29:37 +00:00
return {
"mapping": mapping,
"writable": writable,
"default": default,
"charwidth": charwidth,
2019-01-20 00:19:24 +00:00
"alphabet": alphabet,
"msgcolours": msgcolours
2019-01-18 16:29:37 +00:00
}