compose: Allow c-style comments in json files

Useful to manager growing the collection of sequences.
This commit is contained in:
Jules Aguillon 2024-09-10 22:43:05 +02:00
parent c57ae83bfa
commit e734d1d0e2

View File

@ -90,10 +90,17 @@ def parse_sequences_file_xkb(fname, xkb_char_extra_names):
seqs.append(s) seqs.append(s)
return seqs return seqs
# Basic support for comments in json files. Reads a file
def strip_cstyle_comments(inp):
def strip_line(line):
i = line.find("//")
return line[:i] + "\n" if i >= 0 else line
return "".join(map(strip_line, inp))
# Parse from a json file containing a dictionary sequence → result string. # Parse from a json file containing a dictionary sequence → result string.
def parse_sequences_file_json(fname): def parse_sequences_file_json(fname):
with open(fname, "r") as inp: with open(fname, "r") as inp:
seqs = json.load(inp) seqs = json.loads(strip_cstyle_comments(inp))
return list(seqs.items()) return list(seqs.items())
# Format of the sequences file is determined by its extension # Format of the sequences file is determined by its extension