delegate.py 1.68 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
import os
import importlib
import logging

Wei Shoulin's avatar
ephem    
Wei Shoulin committed
5
from csst_dfs_commons.models.errors import *
Wei Shoulin's avatar
Wei Shoulin committed
6
7
8
9
10
11
12
13
from .constants import *

log = logging.getLogger('csst')

API_MODULE_PREFIX = "csst_dfs_api_"

class Delegate(object):
    def __init__(self):
Wei Shoulin's avatar
Wei Shoulin committed
14
        self.mode = os.getenv("CSST_DFS_API_MODE", 'cluster')
Wei Shoulin's avatar
Wei Shoulin committed
15
        self.check_env()
Wei Shoulin's avatar
Wei Shoulin committed
16

Wei Shoulin's avatar
Wei Shoulin committed
17
18
19
20
21
22
23
24
25
26
27
28
    def check_env(self):
        if self.mode == MODE_LOCAL:
            self.local_path = os.getenv("CSST_LOCAL_FILE_ROOT")
            if not self.local_path:
                log.warn("enviroment variable CSST_DFS_API_MODE is not set, use default: /opt/temp/csst")
                self.local_path = "/opt/temp/csst"
            try:
                from csst_dfs_api_local._version import version  as local_version
            except ImportError:
                raise CSSTFatalException("please install csst_dfs_api_local firstly.")

        if self.mode == MODE_CLUSTER:
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
29
30
31
            self.gateway = os.getenv("CSST_DFS_GATEWAY")
            if not self.gateway:
                raise CSSTGenericException("enviroment variable CSST_DFS_GATEWAY is not set")
Wei Shoulin's avatar
Wei Shoulin committed
32
            try:
Wei Shoulin's avatar
Wei Shoulin committed
33
                from csst_dfs_api_cluster._version import version as cluster_version
Wei Shoulin's avatar
Wei Shoulin committed
34
35
            except ImportError:
                raise CSSTFatalException("please install csst_dfs_api_cluster firstly.")
Wei Shoulin's avatar
ephem    
Wei Shoulin committed
36
37
38
39
40
41

            if not os.getenv("CSST_DFS_APP_ID"):
                raise CSSTGenericException("enviroment variable CSST_DFS_APP_ID is not set")

            if not os.getenv("CSST_DFS_APP_TOKEN"):
                raise CSSTGenericException("enviroment variable CSST_DFS_APP_TOKEN is not set")
Wei Shoulin's avatar
Wei Shoulin committed
42
43
44
    
    def load(self, sub_module):
        return importlib.import_module(f"{API_MODULE_PREFIX}{self.mode}.{sub_module}")