convert_rar.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, page_no, time_out):
  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. self.page_no = page_no
  80. self.time_out = time_out
  81. @memory_decorator
  82. def init_package(self):
  83. try:
  84. # shell调用unrar解压
  85. _signal = os.system("unrar x " + self.path + " " + self.rar_path)
  86. print("rar2text _signal", _signal)
  87. # =0, 解压成功
  88. if _signal != 0:
  89. raise Exception
  90. except:
  91. log("cannot open rar!")
  92. traceback.print_exc()
  93. self._doc.error_code = [-3]
  94. def convert(self):
  95. from format_convert.convert import getText
  96. self.init_package()
  97. if self._doc.error_code is not None:
  98. return
  99. # 内部文件重命名
  100. file_list = rename_inner_files(self.rar_path)
  101. if judge_error_code(file_list):
  102. return file_list
  103. file_no = 0
  104. self._page = _Page(None, 0)
  105. for file in file_list:
  106. if os.path.isdir(file):
  107. continue
  108. bbox = (0, file_no, 0, 0)
  109. # 无文件后缀,猜格式
  110. if len(file.split(".")) <= 1:
  111. log(str(file) + " has no type! Guess type...")
  112. _type = judge_format(file)
  113. if _type is None:
  114. log(str(file) + "cannot guess type!")
  115. continue
  116. else:
  117. log(str(file) + " guess type: " + _type)
  118. new_file = str(file) + "." + _type
  119. os.rename(file, new_file)
  120. file = new_file
  121. sub_html = getText(_type, file)
  122. # 有文件后缀,截取
  123. else:
  124. _type = file.split(".")[-1]
  125. if _type in ['pdf']:
  126. sub_html = getText(_type, file, self.page_no, time_out=self.time_out)
  127. else:
  128. sub_html = getText(_type, file, time_out=self.time_out)
  129. # 文件报错也继续
  130. if judge_error_code(sub_html):
  131. continue
  132. # if judge_error_code(sub_html, code=[-3]):
  133. # continue
  134. # if judge_error_code(sub_html):
  135. # self._doc.error_code = sub_html
  136. # return
  137. _sen = _Sentence(sub_html[0], bbox, is_html=True)
  138. self._page.add_child(_sen)
  139. self._doc.add_child(self._page)
  140. def get_html(self):
  141. try:
  142. self.convert()
  143. except:
  144. traceback.print_exc()
  145. self._doc.error_code = [-1]
  146. if self._doc.error_code is not None:
  147. return self._doc.error_code
  148. return self._doc.get_html()