convert_rar.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import inspect
  2. import os
  3. import sys
  4. sys.path.append(os.path.dirname(__file__) + "/../")
  5. from format_convert.convert_tree import _Document, _Table, _Page, _Sentence
  6. import logging
  7. import traceback
  8. from format_convert.utils import get_platform, rename_inner_files, judge_error_code, judge_format, slash_replace, \
  9. my_subprocess_call, get_logger, log, memory_decorator
  10. @memory_decorator
  11. def rar2text(path, unique_type_dir):
  12. from format_convert.convert import getText
  13. log("into rar2text")
  14. try:
  15. rar_path = unique_type_dir
  16. try:
  17. # shell调用unrar解压
  18. # _signal = os.system("unrar x " + path + " " + rar_path)
  19. pid, _signal = my_subprocess_call(["unrar x ", path, rar_path])
  20. print("rar2text _signal", _signal)
  21. # =0, 解压成功
  22. if _signal != 0:
  23. raise Exception
  24. except Exception as e:
  25. log("rar format error!")
  26. print("rar format error!", e)
  27. return [-3]
  28. # 获取文件名
  29. # file_list = []
  30. # for root, dirs, files in os.walk(rar_path, topdown=False):
  31. # for name in dirs:
  32. # file_list.append(os.path.join(root, name) + os.sep)
  33. # for name in files:
  34. # file_list.append(os.path.join(root, name))
  35. if get_platform() == "Windows":
  36. print("============= rar file list")
  37. # 内部文件重命名
  38. file_list = rename_inner_files(rar_path)
  39. if judge_error_code(file_list):
  40. return file_list
  41. text = []
  42. for file in file_list:
  43. if os.path.isdir(file):
  44. continue
  45. # 无文件后缀,猜格式
  46. if len(file.split(".")) <= 1:
  47. log(str(file) + " has no type! Guess type...")
  48. _type = judge_format(file)
  49. if _type is None:
  50. log(str(file) + "cannot guess type!")
  51. sub_text = [""]
  52. else:
  53. log(str(file) + " guess type: " + _type)
  54. new_file = str(file) + "." + _type
  55. os.rename(file, new_file)
  56. file = new_file
  57. sub_text = getText(_type, file)
  58. # 有文件后缀,截取
  59. else:
  60. _type = file.split(".")[-1]
  61. sub_text = getText(_type, file)
  62. if judge_error_code(sub_text, code=[-3]):
  63. continue
  64. if judge_error_code(sub_text):
  65. return sub_text
  66. # print("sub text", sub_text, file, _type)
  67. text = text + sub_text
  68. return text
  69. except Exception as e:
  70. log("rar2text error!")
  71. print("rar2text", traceback.print_exc())
  72. return [-1]
  73. class RarConvert:
  74. def __init__(self, path, unique_type_dir):
  75. self._doc = _Document(path)
  76. self.path = path
  77. self.unique_type_dir = unique_type_dir
  78. self.rar_path = unique_type_dir
  79. @memory_decorator
  80. def init_package(self):
  81. try:
  82. # shell调用unrar解压
  83. _signal = os.system("unrar x " + self.path + " " + self.rar_path)
  84. print("rar2text _signal", _signal)
  85. # =0, 解压成功
  86. if _signal != 0:
  87. raise Exception
  88. except:
  89. log("cannot open rar!")
  90. traceback.print_exc()
  91. self._doc.error_code = [-3]
  92. def convert(self):
  93. from format_convert.convert import getText
  94. self.init_package()
  95. if self._doc.error_code is not None:
  96. return
  97. # 内部文件重命名
  98. file_list = rename_inner_files(self.rar_path)
  99. if judge_error_code(file_list):
  100. return file_list
  101. file_no = 0
  102. self._page = _Page(None, 0)
  103. for file in file_list:
  104. if os.path.isdir(file):
  105. continue
  106. bbox = (0, file_no, 0, 0)
  107. # 无文件后缀,猜格式
  108. if len(file.split(".")) <= 1:
  109. log(str(file) + " has no type! Guess type...")
  110. _type = judge_format(file)
  111. if _type is None:
  112. log(str(file) + "cannot guess type!")
  113. continue
  114. else:
  115. log(str(file) + " guess type: " + _type)
  116. new_file = str(file) + "." + _type
  117. os.rename(file, new_file)
  118. file = new_file
  119. sub_html = getText(_type, file)
  120. # 有文件后缀,截取
  121. else:
  122. _type = file.split(".")[-1]
  123. sub_html = getText(_type, file)
  124. if judge_error_code(sub_html, code=[-3]):
  125. continue
  126. if judge_error_code(sub_html):
  127. self._doc.error_code = sub_html
  128. return
  129. _sen = _Sentence(sub_html[0], bbox, is_html=True)
  130. self._page.add_child(_sen)
  131. self._doc.add_child(self._page)
  132. def get_html(self):
  133. try:
  134. self.convert()
  135. except:
  136. traceback.print_exc()
  137. self._doc.error_code = [-1]
  138. if self._doc.error_code is not None:
  139. return self._doc.error_code
  140. return self._doc.get_html()