1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4import os
5import configparser
6import wsgidav.util as util
7
8_logger = util.get_module_logger(__name__)
9
10
11def _load_path_from_env(key, check=True):
12    v = os.environ.get(key, '')
13    if not v:
14        if check:
15            raise ImportError(
16                "seaf_util cannot be imported, because environment variable %s is undefined." % key)
17        else:
18            return None
19    return os.path.normpath(os.path.expanduser(v))
20
21CCNET_CONF_DIR = _load_path_from_env('CCNET_CONF_DIR')
22SEAFILE_CONF_DIR = _load_path_from_env('SEAFILE_CONF_DIR')
23SEAFILE_CENTRAL_CONF_DIR = _load_path_from_env(
24    'SEAFILE_CENTRAL_CONF_DIR', check=False)
25
26_multi_tenancy_enabled = None
27
28
29def multi_tenancy_enabled():
30    global _multi_tenancy_enabled
31    if _multi_tenancy_enabled is None:
32        _multi_tenancy_enabled = False
33        try:
34            cp = configparser.ConfigParser()
35            cp.read(
36                os.path.join(SEAFILE_CENTRAL_CONF_DIR if SEAFILE_CENTRAL_CONF_DIR else SEAFILE_CONF_DIR, 'seafile.conf'))
37            if cp.has_option('general', 'multi_tenancy'):
38                _multi_tenancy_enabled = cp.getboolean(
39                    'general', 'multi_tenancy')
40        except:
41            _logger.exception('failed to read multi_tenancy')
42    return _multi_tenancy_enabled
43