create_item.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2018-08-28 17:38:43
  4. ---------
  5. @summary: 创建item
  6. ---------
  7. @author: Boris
  8. @email: boris_liu@foxmail.com
  9. """
  10. import getpass
  11. import os
  12. import feapder.utils.tools as tools
  13. from feapder import setting
  14. from feapder.db.mysqldb import MysqlDB
  15. from .create_init import CreateInit
  16. def deal_file_info(file):
  17. file = file.replace("{DATE}", tools.get_current_date())
  18. file = file.replace("{USER}", getpass.getuser())
  19. return file
  20. class CreateItem:
  21. def __init__(self):
  22. self._db = MysqlDB()
  23. self._create_init = CreateInit()
  24. def select_columns(self, table_name):
  25. # sql = 'SHOW COLUMNS FROM ' + table_name
  26. sql = f"SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, EXTRA, COLUMN_KEY, COLUMN_COMMENT FROM INFORMATION_SCHEMA.Columns WHERE table_name = '{table_name}' and table_schema = '{setting.MYSQL_DB}'"
  27. columns = self._db.find(sql)
  28. return columns
  29. def select_tables_name(self, tables_name):
  30. """
  31. @summary:
  32. ---------
  33. @param tables_name: 一类tables 如 qidian*
  34. ---------
  35. @result:
  36. """
  37. sql = f"select table_name from information_schema.tables where table_name like '{tables_name}' and table_schema = '{setting.MYSQL_DB}'"
  38. tables_name = self._db.find(sql)
  39. return tables_name
  40. def convert_table_name_to_hump(self, table_name):
  41. """
  42. @summary: 格式化表明为驼峰格式
  43. ---------
  44. @param table:
  45. ---------
  46. @result:
  47. """
  48. table_hump_format = ""
  49. words = table_name.split("_")
  50. for word in words:
  51. table_hump_format += word.capitalize() # 首字母大写
  52. return table_hump_format
  53. def get_item_template(self):
  54. template_path = os.path.abspath(
  55. os.path.join(__file__, "../../../templates/item_template.tmpl")
  56. )
  57. with open(template_path, "r", encoding="utf-8") as file:
  58. item_template = file.read()
  59. return item_template
  60. def create_item(self, item_template, columns, table_name, support_dict):
  61. table_name_hump_format = self.convert_table_name_to_hump(table_name)
  62. # 组装 类名
  63. item_template = item_template.replace("${item_name}", table_name_hump_format)
  64. if support_dict:
  65. item_template = item_template.replace("${table_name}", table_name + " 1")
  66. else:
  67. item_template = item_template.replace("${table_name}", table_name)
  68. # 组装 属性
  69. propertys = ""
  70. for column in columns:
  71. column_name = column[0]
  72. column_type = column[1]
  73. is_nullable = column[2]
  74. column_default = column[3]
  75. column_extra = column[4]
  76. column_key = column[5]
  77. column_comment = column[6]
  78. try:
  79. value = (
  80. "kwargs.get('{column_name}')".format(column_name=column_name)
  81. if support_dict
  82. else (
  83. column_default != "CURRENT_TIMESTAMP" and column_default or None
  84. )
  85. and eval(column_default)
  86. )
  87. except:
  88. value = (
  89. "kwargs.get('{column_name}')".format(column_name=column_name)
  90. if support_dict
  91. else (
  92. column_default != "CURRENT_TIMESTAMP" and column_default or None
  93. )
  94. and column_default
  95. )
  96. if column_extra == "auto_increment" or column_default is not None:
  97. propertys += f"# self.{column_name} = {value}"
  98. else:
  99. if value is None or isinstance(value, (float, int)) or support_dict:
  100. propertys += f"self.{column_name} = {value}"
  101. else:
  102. propertys += f"self.{column_name} = '{value}'"
  103. if column_comment:
  104. propertys += f" # {column_comment}"
  105. propertys += "\n" + " " * 8
  106. item_template = item_template.replace("${propertys}", propertys.strip())
  107. item_template = deal_file_info(item_template)
  108. return item_template
  109. def save_template_to_file(self, item_template, table_name):
  110. item_file = table_name + "_item.py"
  111. if os.path.exists(item_file):
  112. confirm = input("%s 文件已存在 是否覆盖 (y/n). " % item_file)
  113. if confirm != "y":
  114. print("取消覆盖 退出")
  115. return
  116. with open(item_file, "w", encoding="utf-8") as file:
  117. file.write(item_template)
  118. print("\n%s 生成成功" % item_file)
  119. self._create_init.create()
  120. def create(self, tables_name, support_dict):
  121. input_tables_name = tables_name
  122. tables_name = self.select_tables_name(tables_name)
  123. if not tables_name:
  124. print(tables_name)
  125. tip = "mysql数据库中无 %s 表 " % input_tables_name
  126. raise KeyError(tip)
  127. for table_name in tables_name:
  128. table_name = table_name[0]
  129. columns = self.select_columns(table_name)
  130. item_template = self.get_item_template()
  131. item_template = self.create_item(
  132. item_template, columns, table_name, support_dict
  133. )
  134. self.save_template_to_file(item_template, table_name)