| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- coding: utf-8; mode: python; eval: (blacken-mode); -*- |
| 3 |
""" |
| 4 |
spa-json-po [OPTIONS] FILES... |
| 5 |
|
| 6 |
Extract strings from SPA-JSON files and output a POT file. Values to |
| 7 |
extract are addressed by \"key paths\", such as |
| 8 |
|
| 9 |
/wireplumber.settings.schema/device.restore-routes/name |
| 10 |
|
| 11 |
which corresponds to the value of the key 'name' in the JSON object. |
| 12 |
Only string values are supported. |
| 13 |
|
| 14 |
""" |
| 15 |
import os |
| 16 |
import sys |
| 17 |
import re |
| 18 |
import argparse |
| 19 |
import subprocess |
| 20 |
import json |
| 21 |
|
| 22 |
|
| 23 |
def main(): |
| 24 |
parser = argparse.ArgumentParser(usage=__doc__.strip()) |
| 25 |
parser.add_argument( |
| 26 |
"--key-match", |
| 27 |
"-k", |
| 28 |
metavar="REGEX", |
| 29 |
dest="keys", |
| 30 |
default=[], |
| 31 |
action="append", |
| 32 |
help="Regex applied on key path to extract a value", |
| 33 |
) |
| 34 |
parser.add_argument( |
| 35 |
"--output", "-o", default=None, help="Produce output to this file, not stdout" |
| 36 |
) |
| 37 |
parser.add_argument( |
| 38 |
"--spa-json-dump", |
| 39 |
default="spa-json-dump", |
| 40 |
help="Path to spa-json-dump executable", |
| 41 |
) |
| 42 |
parser.add_argument("files", nargs="+", metavar="FILES", help="Input files") |
| 43 |
args = parser.parse_args() |
| 44 |
|
| 45 |
strings = {} |
| 46 |
for fn in args.files: |
| 47 |
res = subprocess.run( |
| 48 |
[args.spa_json_dump, fn], stdout=subprocess.PIPE, check=True |
| 49 |
) |
| 50 |
strings.update(parse(res.stdout, keys=args.keys, filename=os.path.basename(fn))) |
| 51 |
|
| 52 |
if args.output is not None: |
| 53 |
stream = open(args.output, "w") |
| 54 |
else: |
| 55 |
stream = sys.stdout |
| 56 |
|
| 57 |
try: |
| 58 |
dump(stream, strings) |
| 59 |
finally: |
| 60 |
stream.close() |
| 61 |
|
| 62 |
|
| 63 |
def parse(text, keys, filename): |
| 64 |
data = json.loads(text) |
| 65 |
return walk(data, "", keys, filename) |
| 66 |
|
| 67 |
|
| 68 |
def walk(obj, path, keys, filename): |
| 69 |
result = {} |
| 70 |
|
| 71 |
if isinstance(obj, str): |
| 72 |
for key in keys: |
| 73 |
if re.match(key, path, flags=re.S): |
| 74 |
result.setdefault(obj, []) |
| 75 |
result[obj].append((filename, path)) |
| 76 |
elif isinstance(obj, dict): |
| 77 |
for k, v in obj.items(): |
| 78 |
result.update(walk(v, f"{path}/{k}", keys, filename)) |
| 79 |
|
| 80 |
return result |
| 81 |
|
| 82 |
|
| 83 |
def dump(stream, strings): |
| 84 |
stream.write( |
| 85 |
'msgid ""\n' |
| 86 |
'msgstr ""\n' |
| 87 |
'"Content-Type: text/plain; charset=UTF-8\\n"\n' |
| 88 |
'"Content-Transfer-Encoding: 8bit\\n"\n\n' |
| 89 |
) |
| 90 |
|
| 91 |
def sort_key(item): |
| 92 |
msgid, infos = item |
| 93 |
infos.sort() |
| 94 |
return infos |
| 95 |
|
| 96 |
for msgid, infos in sorted(strings.items(), key=sort_key): |
| 97 |
escaped = json.dumps(msgid) |
| 98 |
for filename, path in infos: |
| 99 |
stream.write(f"#. {path}\n#: {filename}\n") |
| 100 |
stream.write(f'msgid {escaped}\nmsgstr ""\n\n') |
| 101 |
|
| 102 |
|
| 103 |
if __name__ == "__main__": |
| 104 |
main() |