blob: 7c65be6e3da5b693095428e2c157718a98d8c023 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/router/bin/python
from texttable import Texttable
import yaml
def handle_data_items(field_yaml_dict):
data_items = field_yaml_dict['data']
return [ [json_key, meaning['type'], meaning['exp'] ]
for json_key,meaning in data_items.items() ]
def json_dict_to_txt_table(dict_yaml_file):
# table = Texttable(max_width=120)
with open(dict_yaml_file, 'r') as f:
yaml_stream = yaml.safe_load(f)
for main_field, sub_key in yaml_stream.items():
print(main_field + ' field' '\n' + '~'*len(main_field+' field') + '\n')
field_data_rows = handle_data_items(sub_key)
table = Texttable(max_width=120)
table.set_cols_align(["l", "c", "l"])
table.set_cols_valign(["t", "m", "m"])
# create table's header
table.add_rows([["Sub-key", "Type", "Meaning"]])
table.add_rows(field_data_rows, header=False)
print(table.draw() + "\n")
json_dict_to_txt_table("json_dictionary.yaml")
|