convert_rar.py 5.2 KB

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