simplegen.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ### @Jason
  2. ### Code Generator @ 2019.09.03
  3. ### RealGen
  4. import json
  5. import os
  6. from Config import class_meta, fangan_meta
  7. def gen_for_controls(meta):
  8. for one in meta.get("searchMeta").get("controls"):
  9. type = "Unknown"
  10. extra = ""
  11. js_type = one.get("dataType")
  12. if js_type == "String":
  13. type = "Text"
  14. extra = ", max_length = 100"
  15. if js_type == "int" or js_type == "Timestamp":
  16. type = "Integer"
  17. if js_type == "float" or js_type == "double":
  18. type = "Float"
  19. name = str(one.get("name"))
  20. read_name = name
  21. if one.get("caption") is not None:
  22. read_name =one.get("caption")
  23. print("%s = models.%sField(u'%s'%s) ### %s" % (name.lower(), type, read_name, extra, one.get("dataType")))
  24. def read_meta_from_data(file_name):
  25. all_names = []
  26. with open(file_name, "rb") as input_file:
  27. bytes_in_file = input_file.read()
  28. try:
  29. strme = bytes_in_file.decode("UTF-8")
  30. except:
  31. strme = bytes_in_file.decode("GBK")
  32. local_obj = dict(json.loads(strme))
  33. datas = dict(local_obj.get("datas"))
  34. for model_key in datas.keys():
  35. print("\nKey : %s \n" % model_key)
  36. rows = datas.get(model_key).get("rows")
  37. for each in rows:
  38. for k,v in each.items():
  39. if isinstance(v, float):
  40. all_names.append({"name": k, "kind": "Float", "val":v})
  41. elif isinstance(v, int):
  42. all_names.append({"name": k, "kind": "Integer", "val":v})
  43. elif isinstance(v, str):
  44. all_names.append({"name": k, "kind": "Text", "extra": "String", "val": v})
  45. elif v is None:
  46. all_names.append({"name": k, "kind": "Text", "extra": "NoneType", "val":v})
  47. else:
  48. all_names.append({"name": k, "kind": "Unknown", "extra": "Unknown", "val": v})
  49. return all_names
  50. pass
  51. if __name__ == "__main__":
  52. print ("Class Meta:")
  53. print (class_meta)
  54. gen_for_controls(class_meta)
  55. print ("Fangan Meta:")
  56. print (fangan_meta)
  57. gen_for_controls(fangan_meta)
  58. print("\n\n\n\nfanganode.json")
  59. all_names = read_meta_from_data(os.path.join(os.path.dirname(__file__), "static", "All", "0a0fa2e3b5fb43458ef1ccb8ef5e3ca7_369d8f7961404b5294c1c4f7ea15c175.json"))
  60. for i in all_names:
  61. print ( "%s = models.%sField(u'%s') ## %s <= %s" % (i.get("name").lower(), i.get("kind"), i.get("name"), i.get("extra"), i.get("val")))
  62. print ("\n\n\n\nqxpyfa.json")
  63. all_names = read_meta_from_data(os.path.join(os.path.dirname(__file__), "qxpyfa.json"))
  64. for i in all_names:
  65. print ( "%s = models.%sField(u'%s') ## %s <= %s" % (i.get("name").lower(), i.get("kind"), i.get("name"), i.get("extra"), i.get("val")))