config_init_to_json.py 1.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from loguru import logger
import json
import os
from magic_pdf.config import s3_buckets, s3_clusters, s3_users


def get_bucket_configs_dict(buckets, clusters, users):
    bucket_configs = {}
    for s3_bucket in buckets.items():
        bucket_name = s3_bucket[0]
        bucket_config = s3_bucket[1]
        cluster, user = bucket_config
        cluster_config = clusters[cluster]
        endpoint_key = "outside"
        endpoints = cluster_config[endpoint_key]
        endpoint = endpoints[0]
        user_config = users[user]
        # logger.info(bucket_name)
        # logger.info(endpoint)
        # logger.info(user_config)
赵小蒙's avatar
赵小蒙 committed
21
        bucket_config = [user_config["ak"], user_config["sk"], endpoint]
22
23
24
25
26
27
28
29
30
        bucket_configs[bucket_name] = bucket_config

    return bucket_configs


def write_json_to_home(my_dict):
    # Convert dictionary to JSON
    json_data = json.dumps(my_dict, indent=4, ensure_ascii=False)

赵小蒙's avatar
赵小蒙 committed
31
    home_dir = os.path.expanduser("~")
32
33

    # Define the output file path
赵小蒙's avatar
赵小蒙 committed
34
    output_file = os.path.join(home_dir, "magic-pdf.json")
35
36
37
38
39
40
41
42
43
44

    # Write JSON data to the output file
    with open(output_file, "w") as f:
        f.write(json_data)

    # Print a success message
    print(f"Dictionary converted to JSON and saved to {output_file}")


if __name__ == '__main__':
赵小蒙's avatar
赵小蒙 committed
45
46
    bucket_configs_dict = get_bucket_configs_dict(s3_buckets, s3_clusters, s3_users)
    logger.info(bucket_configs_dict)
赵小蒙's avatar
赵小蒙 committed
47
48
49
50
51
    config_dict = {
        "bucket_info": bucket_configs_dict,
        "temp-output-dir": "/tmp"
    }
    write_json_to_home(config_dict)