Translator

Translator is simple command line interface for configuration transformation. It transforms single input configuration to single output configuration. Configurations are represented by JSON data encoded in JSON or YAML format. Each configuration structure is identified by unique identifier (configuration type) and optional JSON schema. Translator reads input configuration from stdin and outputs resulting configuration to stdout. If error during transformation occurs, information is outputted to stderr and program finishes with non zero return code. Translator comes with set of predefined translations which can be extended with additional definitions provided as command line arguments.

Running

By installing Translator from hat-translator package, executable hat-translator becomes available and can be used for starting this component.

usage: hat-translator [-h] {translate,list} ...

positional arguments:
  {translate,list}
    translate       translate stdin input to stdout output
    list            list available translations

optional arguments:
  -h, --help        show this help message and exit
usage: hat-translator list [-h] [--module [name [name ...]]]
                           [--additional-json-schemas-path [path [path ...]]]
                           [--format format]

optional arguments:
  -h, --help            show this help message and exit
  --module [name [name ...]]
                        additional translators module name
  --additional-json-schemas-path [path [path ...]]
                        additional json schemas paths
  --format format       input/output format: 'yaml' or 'json' (default 'yaml')
usage: hat-translator translate [-h] [--module [name [name ...]]]
                                [--additional-json-schemas-path [path [path ...]]]
                                [--format format] --input-type type
                                --output-type type

optional arguments:
  -h, --help            show this help message and exit
  --module [name [name ...]]
                        additional translators module name
  --additional-json-schemas-path [path [path ...]]
                        additional json schemas paths
  --format format       input/output format: 'yaml' or 'json' (default 'yaml')
  --input-type type     input configuration type
  --output-type type    output configuration type

If multiple modules provide transformation for same (input-type, output-type) pair, last transformation (from list of command line module arguments) is used.

Todo

  • consider adding additional output subtype label instead of encoding specific translation identification as part of output type (example: “input: editor; output: event_primary” vs “input: editor; output: event; variant: primary”)

Python implementation

Translator definition

hat.translator.common.Translate

Translate function type

alias of Callable[[Union[None, bool, int, float, str, List[Data], Dict[str, Data]]], Union[None, bool, int, float, str, List[Data], Dict[str, Data]]]

class hat.translator.common.Translator(input_type, input_schema, output_type, output_schema, translate)

Bases: tuple

Translator definition

Translators are dynamicly loaded from python modules that contain:

  • translators (List[Translator]): translators

Create new instance of Translator(input_type, input_schema, output_type, output_schema, translate)

input_schema

input JSON schema identifier

Type

Optional[str]

input_type

input configuration type

Type

str

output_schema

output JSON schema identifier

Type

Optional[str]

output_type

output configuration type

Type

str

translate

translate function

Type

Translate

Translator main

hat.translator.main.builtin_translators

builtin translators

Type

List[hat.translator.common.Translator]

hat.translator.main.main()

Main

hat.translator.main.act_list(module_names)

List action

Parameters

module_names (List[str]) – python module names with translator definitions

Returns

json.Data

hat.translator.main.act_translate(module_names, json_schema_repo, input_type, output_type, input_conf)

Translate action

Parameters
  • module_names (List[str]) – python module names with translator definitions

  • json_schema_repo (json.SchemaRepository) – json schemas repository

  • input_type (str) – input configuration type identifier

  • output_type (str) – output configuration type identifier

  • input_conf (json.Data) – input configuration

Returns

json.Data