diff --git a/src/stbl/commands/__init__.py b/src/stbl/commands/__init__.py index 20c9373..49ac2e6 100644 --- a/src/stbl/commands/__init__.py +++ b/src/stbl/commands/__init__.py @@ -1 +1,3 @@ -command_group = [] +from .export import export_command + +command_group = [export_command] diff --git a/src/stbl/commands/export.py b/src/stbl/commands/export.py new file mode 100644 index 0000000..d737171 --- /dev/null +++ b/src/stbl/commands/export.py @@ -0,0 +1,21 @@ +import json + +import click + +from stbl.stbl import STBLFile + + +@click.command('export') +@click.argument('stbl_file', type=click.Path(exists=True, dir_okay=False)) +def export_command(stbl_file): + """Export .stbl file in JSON format.""" + + stbl = STBLFile() + stbl.read(stbl_file) + + if not stbl.validate(): + raise RuntimeError(f'Invalid .stbl file format "{stbl_file}".') + + data = list(map(lambda entry: {'instance': '0x' + ('%08x' % entry.instance_id).upper(), 'text': entry.text}, stbl.entries)) + + print(json.dumps(data, ensure_ascii=False, indent=4))