go_api_w_render.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from .tool.func import *
  2. class api_w_render_include:
  3. def __init__(self, data_option):
  4. self.include_change_list = data_option
  5. def __call__(self, match):
  6. match_org = match.group(0)
  7. match = match.groups()
  8. if len(match) < 3:
  9. match = list(match) + ['']
  10. if match[2] == '\\':
  11. return match_org
  12. else:
  13. slash_add = ''
  14. if match[0]:
  15. if len(match[0]) % 2 == 1:
  16. slash_add = '\\' * (len(match[0]) - 1)
  17. else:
  18. slash_add = match[0]
  19. if match[1] in self.include_change_list:
  20. return slash_add + self.include_change_list[match[1]]
  21. else:
  22. return slash_add + match[2]
  23. def api_w_render(db_set, name = '', tool = ''):
  24. with get_db_connect() as conn:
  25. curs = conn.cursor()
  26. if flask.request.method == 'POST':
  27. name = flask.request.form.get('name', '')
  28. data_org = flask.request.form.get('data', '')
  29. data_option = flask.request.form.get('option', '')
  30. markup = ''
  31. if tool in ('', 'from', 'include'):
  32. curs.execute(db_change("select set_data from data_set where doc_name = ? and set_name = 'document_markup'"), [name])
  33. db_data = curs.fetchall()
  34. if db_data and db_data[0][0] != '' and db_data[0][0] != 'normal':
  35. markup = db_data[0][0]
  36. if markup == '':
  37. curs.execute(db_change('select data from other where name = "markup"'))
  38. db_data = curs.fetchall()
  39. markup = db_data[0][0] if db_data else 'namumark'
  40. data_type = ''
  41. if tool == '':
  42. data_type = 'api_view'
  43. elif tool == 'from':
  44. data_type = 'api_from'
  45. elif tool == 'include':
  46. data_type = 'api_include'
  47. else:
  48. data_type = 'api_thread'
  49. if markup in ('', 'namumark', 'namumark_beta'):
  50. if data_option != '':
  51. data_option = json.loads(data_option)
  52. data_option_func = api_w_render_include(data_option)
  53. # parameter replace
  54. data_org = re.sub(r'(\\+)?@([ㄱ-힣a-zA-Z0-9]+)=((?:\\@|[^@\n])+)@', data_option_func, data_org)
  55. data_org = re.sub(r'(\\+)?@([ㄱ-힣a-zA-Z0-9]+)@', data_option_func, data_org)
  56. # remove end br
  57. data_org = re.sub('^\n+', '', data_org)
  58. data_pas = render_set(conn,
  59. doc_name = name,
  60. doc_data = data_org,
  61. data_type = data_type
  62. )
  63. return flask.jsonify({
  64. "data" : data_pas[0],
  65. "js_data" : data_pas[1]
  66. })
  67. else:
  68. other_set = {}
  69. other_set["doc_name"] = name
  70. other_set["render_type"] = data_type
  71. other_set["data"] = data_org
  72. other_set = json.dumps(other_set)
  73. if platform.system() == 'Linux':
  74. if platform.machine() in ["AMD64", "x86_64"]:
  75. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  76. else:
  77. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.bin"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  78. else:
  79. if platform.machine() in ["AMD64", "x86_64"]:
  80. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  81. else:
  82. data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.exe"), sys._getframe().f_code.co_name, db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
  83. data = data.decode('utf8')
  84. print(data)
  85. return flask.Response(response = data, status = 200, mimetype = 'application/json')
  86. else:
  87. return flask.jsonify({})