获取一个rng文件并生成一个html表单
rng-to-html-form的Python项目详细描述
要求
- Python3.4
- lxml 3.4.4
安装
pip install rng-to-html-form
用法
获取一个rng文件并生成一个html表单:
fromrng_to_form.rngimportRNGfromrng_to_form.form_makerimportmake_form,make_tree,set_in_nested_dict,order_dicts""" List of rng tools """# Creates an RNG object to use rng toolsrng=RNG(file="/path/to/my/rng/file.rng",target="ArchiveTransfer")# 1 - Tools to make the form# Builds an html form in a stringgenerated_form=make_form(rng)# Returns elements orderorder=rng.find_definitions()# From the specified element, returns all nested ordersform_root=rng.get_form_root(root_name="ArchiveTransfer")# 2 - Tools to get data from the form# Generates a dict of dicts from dot separated keys. Result will be : {'a': {'c': {}, 'b': {}}, 'b': {'d': {}, 'e': {'b': {}, 'a': {}}}}data={'a.b':1,'a.c':2,'b.d':1,'b.e.a':1,'b.e.b':2}tree=make_tree(data)# Makes a nested python object from the form data. It modifies "tree"set_in_nested_dict(tree,data)# Builds the final python object with all ordered elementsorder=rng.find_definitions()all_data={rng.get_form_root(root_name="ArchiveTransfer"):tree}final=order_dicts(all_data,ordered=order)# 3 - Tools to get data from the form# Flattens nested dicts into a single level dict. Result will be : {'a.b': 1, 'a.c': 2, 'b.d' : 1, 'b.e.a': 1, 'b.e.b': 2}dp=dict_path({'a':{'b':1,'c':2},'b':{'d':1,'e':{'a':1,'b':2}}})