0
|
1 #!/usr/bin/env python3
|
|
2
|
|
3 from result import Result
|
|
4 from exceptions import CGECoreOutTypeError, CGECoreOutTranslateError
|
|
5
|
|
6
|
|
7 class Translate():
|
|
8
|
|
9 def __init__(self, type, transl_table):
|
|
10 self.transl_table = transl_table
|
|
11 self.type = type
|
|
12
|
|
13 if(type not in Result.beone_defs):
|
|
14 raise CGECoreOutTypeError(
|
|
15 "Unknown type given to Translate object. type given: {}. "
|
|
16 "type must be one of:\n{}"
|
|
17 .format(type, list(Result.beone_defs.keys())))
|
|
18
|
|
19 self._check_translation_keys()
|
|
20
|
|
21 def translate(self, source_dict):
|
|
22 dest_dict = {}
|
|
23 for key, val in source_dict.items():
|
|
24 dest_key = self.transl_table.get(key, None)
|
|
25 if(dest_key is not None and val is not None):
|
|
26 dest_dict[dest_key] = val
|
|
27 return dest_dict
|
|
28
|
|
29 def _check_translation_keys(self):
|
|
30 for key, val in self.transl_table.items():
|
|
31 if(val not in Result.beone_defs[self.type]):
|
|
32 raise CGECoreOutTranslateError(
|
|
33 "Value in the translation table given was not found in the"
|
|
34 " definition of the given type. Type given: {}. Value not "
|
|
35 "found: {}. Possible values for the given type: {}"
|
|
36 .format(self.type, self.key,
|
|
37 list(self.transl_table.keys())))
|