config_init_to_json.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from loguru import logger
  2. import json
  3. import os
  4. from magic_pdf.config import s3_buckets, s3_clusters, s3_users
  5. def get_bucket_configs_dict(buckets, clusters, users):
  6. bucket_configs = {}
  7. for s3_bucket in buckets.items():
  8. bucket_name = s3_bucket[0]
  9. bucket_config = s3_bucket[1]
  10. cluster, user = bucket_config
  11. cluster_config = clusters[cluster]
  12. endpoint_key = "outside"
  13. endpoints = cluster_config[endpoint_key]
  14. endpoint = endpoints[0]
  15. user_config = users[user]
  16. # logger.info(bucket_name)
  17. # logger.info(endpoint)
  18. # logger.info(user_config)
  19. bucket_config = {
  20. "endpoint": endpoint,
  21. "ak": user_config["ak"],
  22. "sk": user_config["sk"],
  23. }
  24. bucket_configs[bucket_name] = bucket_config
  25. return bucket_configs
  26. def write_json_to_home(my_dict):
  27. # Convert dictionary to JSON
  28. json_data = json.dumps(my_dict, indent=4, ensure_ascii=False)
  29. # Determine the home directory path based on the operating system
  30. if os.name == "posix": # Linux or macOS
  31. home_dir = os.path.expanduser("~")
  32. elif os.name == "nt": # Windows
  33. home_dir = os.path.expandvars("%USERPROFILE%")
  34. else:
  35. raise Exception("Unsupported operating system")
  36. # Define the output file path
  37. output_file = os.path.join(home_dir, "magic_pdf_config.json")
  38. # Write JSON data to the output file
  39. with open(output_file, "w") as f:
  40. f.write(json_data)
  41. # Print a success message
  42. print(f"Dictionary converted to JSON and saved to {output_file}")
  43. if __name__ == '__main__':
  44. bucket_configs = get_bucket_configs_dict(s3_buckets, s3_clusters, s3_users)
  45. logger.info(bucket_configs)
  46. write_json_to_home(bucket_configs)