基于openpyxl的excel转换工具。支持xlsx文件转换为erlang,elixir,lua,json,xml,python等配置文件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
8.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #! python
  2. # -*- coding:utf-8 -*-
  3. import os
  4. import sys
  5. from writer import *
  6. try:
  7. basestring
  8. except NameError:
  9. basestring = str
  10. try:
  11. long
  12. except NameError:
  13. long = int
  14. # python3中没有unicode了
  15. try:
  16. unicode
  17. except NameError:
  18. unicode = str
  19. # 加上不确定的层级缩进,60比较合适
  20. BASE_LENGTH = 60
  21. BASE_INDENT = " "
  22. INDENT_LIST = {}
  23. class ErlangerlWriter(Writer):
  24. # 文件后缀
  25. def suffix(self):
  26. return ".erl"
  27. def comment(self):
  28. comment = [
  29. '%% Automatic generation from -->>'
  30. '\n%% excel file name : ' + self.doc_name +
  31. '\n%% excel sheet name : ' + self.sheet_name,
  32. '\n-module(' + self.base_name + ').',
  33. '\n'
  34. ]
  35. return "\n".join( comment )
  36. # 获取缩进字符串
  37. def indent_ctx( self,indent ):
  38. if indent <= 0: return ""
  39. if indent not in INDENT_LIST:
  40. INDENT_LIST[indent] = ""
  41. else:
  42. ctx = BASE_INDENT*indent
  43. INDENT_LIST[indent] = ctx
  44. return INDENT_LIST[indent]
  45. def dict_to_text(self, value, indent) :
  46. dict_text_list = []
  47. dict_text_list.append("-compile([export_all, nowarn_export_all]).\n\n")
  48. for k in ( value ) :
  49. k_indent,lk = self.to_target_lang( k,indent )
  50. is_indent,lv = self.to_target_lang( value[k],indent + 1 )
  51. comment = self.comment_text[k]
  52. val_type = type( lk )
  53. if str == val_type :
  54. lk = lk.replace("<<\"", "\'")
  55. lk = lk.replace("\"/utf8>>", "\'")
  56. key = "".join( ["get(",lk,") ->\n"] )
  57. val = "".join( ["%% ", comment, "\n", key, " ", lv, ";\n\n"] )
  58. dict_text_list.append( val )
  59. dict_str = "".join( dict_text_list )
  60. dict_str = dict_str + "get(_) ->\n undefined."
  61. return False, dict_str
  62. def list_to_text(self, value, indent):
  63. list_text_list = []
  64. val = "-include(\"" + self.base_name + ".hrl\").\n" + "-compile([export_all, nowarn_export_all]).\n\n"
  65. list_text_list.append(val)
  66. all_key_list = []
  67. # 生成 get() 函数
  68. for i, onedict in enumerate( value ) :
  69. # 生成对应的 key
  70. key_list = []
  71. for k in ( onedict ) :
  72. if None != self.keys_list.get(k, None) :
  73. is_indent,lv = self.to_target_lang( onedict[k],indent + 1 )
  74. key_list.append(lv)
  75. all_key_list.append(key_list)
  76. tem = ", ".join( key_list )
  77. key = "".join( ["get(",tem,") ->\n #", self.base_name, "{\n "] )
  78. # 生成对应的value
  79. value_list = []
  80. for k in ( onedict ) :
  81. k_indent,lk = self.to_target_lang( k,indent )
  82. is_indent,lv = self.to_target_lang( onedict[k],indent + 1 )
  83. val_type = type( lk )
  84. if str == val_type :
  85. lk = lk.replace("<<\"", "\'")
  86. lk = lk.replace("\"/utf8>>", "\'")
  87. oneval = "".join( [lk, " = ", lv , "\n"] )
  88. value_list.append( oneval )
  89. value_list_str = " , ".join(value_list)
  90. end_str = "".join( [key, value_list_str, "};\n\n"] )
  91. list_text_list.append( end_str )
  92. underline_list = []
  93. for i in self.keys_list :
  94. underline_list.append('_')
  95. end_str = ", ".join(underline_list)
  96. no_match_str = "get(" + end_str + ") ->\n undefined.\n\n"
  97. list_text_list.append(no_match_str)
  98. # 生成 get_all() 函数
  99. get_all_fun = []
  100. for i, ival in enumerate(all_key_list) :
  101. oneval = '{' + ", ".join(ival) + '}\n'
  102. get_all_fun.append(oneval)
  103. value_list_str = " , ".join(get_all_fun)
  104. start_str = 'getList() ->\n [\n '
  105. end_str = "".join( [start_str, value_list_str, " ].\n\n"] )
  106. # 生成 get_list() 函数
  107. get_list_fun = []
  108. keys_len = len(self.keys_list)
  109. for key in self.keys_list :
  110. keyindex = self.keys_list[key]
  111. if keyindex == 1 :
  112. "skip"
  113. elif keyindex <= keys_len :
  114. get_tem_dict = {}
  115. underline_list = []
  116. for i, ival in enumerate(all_key_list) :
  117. key_tem_list = []
  118. j = 0
  119. underline_list = []
  120. while j < keyindex - 1 :
  121. key_tem_list.append(ival[j])
  122. underline_list.append('_')
  123. j += 1
  124. keystr = '(' + ", ".join(key_tem_list) + ')'
  125. if None != get_tem_dict.get(keystr, None) :
  126. oldlist = get_tem_dict[keystr]
  127. oldlist.append(ival)
  128. get_tem_dict[keystr] = oldlist
  129. else :
  130. get_tem_dict[keystr] = [ival]
  131. for onekey in get_tem_dict :
  132. value_tem_list = []
  133. valuelist = get_tem_dict[onekey]
  134. for l, lval in enumerate(valuelist) :
  135. oneval = '{' + ", ".join(lval) + '}\n'
  136. value_tem_list.append(oneval)
  137. start = 'getList' + onekey + ' ->\n [\n '
  138. val = "".join( [start, " , ".join(value_tem_list), " ];\n\n"] )
  139. get_list_fun.append(val)
  140. no_match_str = "".join('getList(' + ", ".join(underline_list) + ') ->\n [].\n\n')
  141. get_list_fun.append(no_match_str)
  142. if self.is_list == True:
  143. value_list = "".join(get_list_fun)
  144. list_text_list.append( end_str )
  145. list_text_list.append( value_list )
  146. dict_str = "".join( list_text_list )
  147. return False, dict_str
  148. # 转换为文本数据 之前解析出来的excel数据存放方式存在LIST(array格式)和DICT(object格式)两种类型
  149. def to_text(self,value,indent):
  150. val_type = type( value )
  151. if dict == val_type :
  152. return self.dict_to_text(value, indent)
  153. else :
  154. return self.list_to_text(value, indent)
  155. # python的dict转换为erlang的map类型
  156. def dict_to_erlang(self,value,indent):
  157. dict_ctx_list = []
  158. for k in ( value ) :
  159. k_indent,lk = self.to_target_lang( k,indent )
  160. is_indent,lv = self.to_target_lang( value[k],indent + 1 )
  161. val_type = type( lk )
  162. if str == val_type :
  163. lk = lk.replace("<<\"", "\'")
  164. lk = lk.replace("\"/utf8>>", "\'")
  165. key = "".join( [lk," => "] )
  166. val = "".join( [key, lv] )
  167. dict_ctx_list.append(val)
  168. dict_str = ", ".join( dict_ctx_list )
  169. return False,"".join( ["#{",dict_str,"}"] )
  170. # python的list转换为erlang的list类型
  171. def list_to_erlang(self,value,indent):
  172. list_ctx_list = []
  173. for v in value :
  174. is_indent,lv = self.to_target_lang( v,indent + 1 )
  175. list_ctx_list.append( lv )
  176. list_str = ", ".join( list_ctx_list )
  177. return False,"".join( ["[",list_str,"]"] )
  178. # python的tuple转换为erlang的tuple类型
  179. def tuple_to_erlang(self,value,indent):
  180. tuple_ctx_list = []
  181. for v in value :
  182. is_indent,lv = self.to_target_lang( v,indent + 1 )
  183. tuple_ctx_list.append( lv )
  184. # 返回 {a,b,c}这种不换行的格式
  185. list_str = ", ".join( tuple_ctx_list )
  186. return False,"".join( ["{",list_str,"}"] )
  187. # 变量转换到目标语言的字符串
  188. def to_target_lang(self,value,indent):
  189. val_type = type( value )
  190. if int == val_type :
  191. return False,str( value )
  192. elif long == val_type :
  193. return False,str( value )
  194. elif float == val_type :
  195. # 1001.0 -->> 001 去除多余小数点
  196. if int( value ) == value :
  197. return False,str( int(value) )
  198. return False,str( value )
  199. elif str == val_type or unicode == val_type:
  200. return False, "".join(["<<\"",value,"\"/utf8>>"])
  201. elif tuple == val_type :
  202. return self.tuple_to_erlang(value,indent)
  203. elif dict == val_type :
  204. return self.dict_to_erlang(value,indent)
  205. elif list == val_type :
  206. return self.list_to_erlang(value,indent)
  207. else :
  208. raise Exception( "invalid type",val_type )
  209. #文件内容
  210. def context(self,ctx):
  211. is_indent,str_ctx = self.to_text( ctx,0 )
  212. return "".join( [self.comment(),"",str_ctx] )