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 import get_memory_info
  9. from format_convert.utils import get_platform, rename_inner_files, judge_error_code, judge_format, slash_replace, \
  10. my_subprocess_call, get_logger, log
  11. @get_memory_info.memory_decorator
  12. def rar2text(path, unique_type_dir):
  13. from format_convert.convert import getText
  14. log("into rar2text")
  15. try:
  16. rar_path = unique_type_dir
  17. try:
  18. # shell调用unrar解压
  19. # _signal = os.system("unrar x " + path + " " + rar_path)
  20. pid, _signal = my_subprocess_call(["unrar x ", path, rar_path])
  21. print("rar2text _signal", _signal)
  22. # =0, 解压成功
  23. if _signal != 0:
  24. raise Exception
  25. except Exception as e:
  26. log("rar format error!")
  27. print("rar format error!", e)
  28. return [-3]
  29. # 获取文件名
  30. # file_list = []
  31. # for root, dirs, files in os.walk(rar_path, topdown=False):
  32. # for name in dirs:
  33. # file_list.append(os.path.join(root, name) + os.sep)
  34. # for name in files:
  35. # file_list.append(os.path.join(root, name))
  36. if get_platform() == "Windows":
  37. print("============= rar file list")
  38. # 内部文件重命名
  39. file_list = rename_inner_files(rar_path)
  40. if judge_error_code(file_list):
  41. return file_list
  42. text = []
  43. for file in file_list:
  44. if os.path.isdir(file):
  45. continue
  46. # 无文件后缀,猜格式
  47. if len(file.split(".")) <= 1:
  48. log(str(file) + " has no type! Guess type...")
  49. _type = judge_format(file)
  50. if _type is None:
  51. log(str(file) + "cannot guess type!")
  52. sub_text = [""]
  53. else:
  54. log(str(file) + " guess type: " + _type)
  55. new_file = str(file) + "." + _type
  56. os.rename(file, new_file)
  57. file = new_file
  58. sub_text = getText(_type, file)
  59. # 有文件后缀,截取
  60. else:
  61. _type = file.split(".")[-1]
  62. sub_text = getText(_type, file)
  63. if judge_error_code(sub_text, code=[-3]):
  64. continue
  65. if judge_error_code(sub_text):
  66. return sub_text
  67. # print("sub text", sub_text, file, _type)
  68. text = text + sub_text
  69. return text
  70. except Exception as e:
  71. log("rar2text error!")
  72. print("rar2text", traceback.print_exc())
  73. return [-1]
  74. class RarConvert:
  75. def __init__(self, path, unique_type_dir):
  76. self._doc = _Document(path)
  77. self.path = path
  78. self.unique_type_dir = unique_type_dir
  79. self.rar_path = unique_type_dir
  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()