wheat-cache/shell/make-struct.py

73 lines
1.3 KiB
Python

import os
from jinja2 import Template
import yaml
sysPath = os.getcwd()
tem_text = '''// Code generated by gen-struct. DO NOT EDIT.
// make gen-struct generated
package generate
const (
DEFAULT_KEY = iota
{% for key in keys %}
{{key}}
{% endfor %}
)
const (
DEFAULT_COMM = iota
{% for key in sets %}
{{key}}
{% endfor %}
)
var CommKey = map[string]int {
{% for kmp in key_maps %}
{% for comm in kmp.val %}
"{{comm}}": {{kmp.key}},
{% endfor %}
{% endfor %}
}
'''
class KeyMap(object):
def __init__(self, key, val) -> None:
self.key = key
self.val = val
def load_conf():
conf_path = f"{sysPath}/structure/generate/tem.yaml"
with open(conf_path, 'r', encoding='utf-8') as f:
cfg = f.read()
cfg = yaml.load(cfg)
return cfg
def set_structure_template(conf: dict):
keys = conf.keys()
key_map = []
val_set = []
for k, v in conf.items():
key_map.append(KeyMap(key=k, val=v))
for val in v:
val_set.append(val.upper() + "_COMM")
template = Template(tem_text)
text = template.render(keys=keys, key_maps=key_map, sets=val_set)
temp_path = f"{sysPath}/structure/generate/structure.gen.go"
with open(temp_path, 'w', encoding='utf-8') as f:
f.write(text)
if __name__ == "__main__":
conf = load_conf()
set_structure_template(conf)