2021-12-01 12:46:28 +05:30
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2021-08-02 15:08:30 +05:30
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-12-01 12:46:28 +05:30
|
|
|
|
2021-10-15 00:22:59 +02:00
|
|
|
import io
|
|
|
|
import json
|
2021-12-19 00:35:12 +01:00
|
|
|
import os
|
2021-08-14 10:18:52 -07:00
|
|
|
import pathlib
|
2021-08-01 14:27:44 -07:00
|
|
|
from abc import ABC, abstractmethod
|
2021-08-14 10:18:52 -07:00
|
|
|
from typing import IO, Any, Optional
|
2021-10-15 00:22:59 +02:00
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2021-08-01 14:27:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigModel(BaseModel):
|
|
|
|
class Config:
|
|
|
|
extra = "forbid"
|
|
|
|
|
|
|
|
|
|
|
|
class DynamicTypedConfig(ConfigModel):
|
|
|
|
type: str
|
|
|
|
config: Optional[Any]
|
|
|
|
|
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
class WorkflowExecutionError(Exception):
|
2021-08-01 14:27:44 -07:00
|
|
|
"""An error occurred when executing the workflow"""
|
|
|
|
|
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
class ConfigurationError(Exception):
|
2021-08-01 14:27:44 -07:00
|
|
|
"""A configuration error has happened"""
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigurationMechanism(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def load_config(self, config_fp: IO) -> dict:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
def load_config_file(config_file: pathlib.Path) -> dict:
|
|
|
|
if not config_file.is_file():
|
|
|
|
raise ConfigurationError(f"Cannot open config file {config_file}")
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
if config_file.suffix not in [".json"]:
|
|
|
|
raise ConfigurationError(
|
|
|
|
"Only json are supported. Cannot process file type {}".format(
|
|
|
|
config_file.suffix
|
|
|
|
)
|
|
|
|
)
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
with config_file.open() as raw_config_file:
|
|
|
|
raw_config = raw_config_file.read()
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-12-19 00:35:12 +01:00
|
|
|
expanded_config_file = os.path.expandvars(raw_config)
|
2021-08-14 10:18:52 -07:00
|
|
|
config_fp = io.StringIO(expanded_config_file)
|
|
|
|
config = json.load(config_fp)
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-08-14 10:18:52 -07:00
|
|
|
return config
|