annotate resfinder/cge/out/translate.py @ 0:55051a9bc58d draft default tip

Uploaded
author dcouvin
date Mon, 10 Jan 2022 20:06:07 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
1 #!/usr/bin/env python3
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
2
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
3 from result import Result
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
4 from exceptions import CGECoreOutTypeError, CGECoreOutTranslateError
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
5
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
6
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
7 class Translate():
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
8
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
9 def __init__(self, type, transl_table):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
10 self.transl_table = transl_table
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
11 self.type = type
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
12
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
13 if(type not in Result.beone_defs):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
14 raise CGECoreOutTypeError(
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
15 "Unknown type given to Translate object. type given: {}. "
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
16 "type must be one of:\n{}"
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
17 .format(type, list(Result.beone_defs.keys())))
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
18
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
19 self._check_translation_keys()
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
20
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
21 def translate(self, source_dict):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
22 dest_dict = {}
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
23 for key, val in source_dict.items():
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
24 dest_key = self.transl_table.get(key, None)
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
25 if(dest_key is not None and val is not None):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
26 dest_dict[dest_key] = val
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
27 return dest_dict
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
28
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
29 def _check_translation_keys(self):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
30 for key, val in self.transl_table.items():
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
31 if(val not in Result.beone_defs[self.type]):
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
32 raise CGECoreOutTranslateError(
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
33 "Value in the translation table given was not found in the"
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
34 " definition of the given type. Type given: {}. Value not "
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
35 "found: {}. Possible values for the given type: {}"
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
36 .format(self.type, self.key,
55051a9bc58d Uploaded
dcouvin
parents:
diff changeset
37 list(self.transl_table.keys())))