demo_commons.bak 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import json
  2. from magic_pdf.libs.config_reader import get_s3_config_dict
  3. from magic_pdf.libs.commons import join_path, read_file, json_dump_path
  4. local_json_path = "Z:/format.json"
  5. local_jsonl_path = "Z:/format.jsonl"
  6. def get_json_from_local_or_s3(book_name=None):
  7. if book_name is None:
  8. with open(local_json_path, "r", encoding="utf-8") as json_file:
  9. json_line = json_file.read()
  10. json_object = json.loads(json_line)
  11. else:
  12. # error_log_path & json_dump_path
  13. # 可配置从上述两个地址获取源json
  14. json_path = join_path(json_dump_path, book_name + ".json")
  15. s3_config = get_s3_config_dict(json_path)
  16. file_content = read_file(json_path, s3_config)
  17. json_str = file_content.decode("utf-8")
  18. # logger.info(json_str)
  19. json_object = json.loads(json_str)
  20. return json_object
  21. def write_json_to_local(jso, book_name=None):
  22. if book_name is None:
  23. with open(local_json_path, "w", encoding="utf-8") as file:
  24. file.write(json.dumps(jso, ensure_ascii=False))
  25. else:
  26. pass