Skip to content

Manage Config

This module does some configuration management for local/remote files.

Overview

This module manages the config.json file.

It is used to update the config.json file on GitHub.

The config.json file is used to store the configuration for the pipeline.

It can also update a local config.json file.

get_local_config()

Get the config.json file from the project's root.

Source code in opendata_pipeline/manage_config.py
20
21
22
23
def get_local_config() -> models.Settings:
    """Get the config.json file from the project's root."""
    console.log("Getting local config.json file")
    return models.Settings.parse_file("config.json")

get_remote_config()

Get the config.json file from the project's root on GitHub.

Source code in opendata_pipeline/manage_config.py
26
27
28
29
30
31
32
33
34
35
def get_remote_config() -> models.Settings:
    """Get the config.json file from the project's root on GitHub."""
    url = (
        "https://raw.githubusercontent.com/UK-IPOP/open-data-pipeline/main/config.json"
    )
    console.log("Getting remote config.json file")
    resp = requests.get(url)
    if resp.status_code != 200:
        raise ValueError(resp.content)
    return models.Settings.parse_raw(resp.content)

update_local_config(config)

Update the local config.json file.

Source code in opendata_pipeline/manage_config.py
38
39
40
41
42
43
def update_local_config(config: models.Settings):
    """Update the local config.json file."""
    console.log("Updating local config.json file")
    data = config.dict(exclude={"arcgis_api_key", "github_token"})
    with open(Path("config.json"), "w") as f:
        json.dump(data, f, indent=4)

update_remote_config(config)

Update the remote config.json file using the GitHub API.

Source code in opendata_pipeline/manage_config.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def update_remote_config(config: models.Settings):
    """Update the remote config.json file using the GitHub API."""
    if config.github_token is None:
        raise ValueError("github_token is required to update remote config")
    url = "https://api.github.com/repos/UK-IPOP/open-data-pipeline/contents/config.json"
    headers = {
        "Accept": "application/vnd.github+json",
        "Authorization": f"Bearer {config.github_token}",
    }
    get_file = requests.get(url, headers=headers)
    if get_file.status_code != 200:
        raise ValueError(get_file.content)
    file_sha = get_file.json()["sha"]

    data = {}
    today = date.today().strftime("%Y-%m-%d")
    data["message"] = f"Update config.json -- {today}"
    data["sha"] = file_sha

    # encode the file contents
    encoded = orjson.dumps(config.dict(exclude={"arcgis_api_key", "github_token"}))
    data["content"] = base64.b64encode(encoded).decode("utf-8")

    console.log("Updating remote config.json file")
    resp = requests.put(url, headers=headers, json=data)
    if resp.status_code != 200:
        raise ValueError(resp.content)