def test_integration_yaml(self):
     self.maxDiff = None
     IntegrationBase.result.pop('complex')
     IntegrationBase.result.pop('tuple')
     Pconf.file('./tests/integration/example.yaml', encoding='yaml')
     config = Pconf.get()
     self.assertEqual(config, IntegrationBase.result)
Example #2
0
    def test_file_optional_params(self):
        encoding = "custom"
        mock_parser = MagicMock()
        Pconf.file(path=TEST_FILE_PATH, encoding=encoding, parser=mock_parser)

        pconf.store.file.File.assert_called_once_with(TEST_FILE_PATH, encoding,
                                                      mock_parser)
        self.assertEqual(len(Pconf._Pconf__hierarchy), 1)
Example #3
0
 def test_integration_yaml(self):
     self.maxDiff = None
     IntegrationBase.result.pop("complex")
     IntegrationBase.result.pop("tuple")
     IntegrationBase.result.pop("secret")
     Pconf.file("./tests/integration/example.yaml", encoding="yaml")
     config = Pconf.get()
     self.assertEqual(config, IntegrationBase.result)
 def test_integration_json(self):
     self.maxDiff = None
     # Remove values that are not json encodeable
     IntegrationBase.result.pop('complex')
     IntegrationBase.result.pop('tuple')
     Pconf.file('./tests/integration/example.json', encoding='json')
     config = Pconf.get()
     self.assertEqual(config, IntegrationBase.result)
Example #5
0
 def test_integration_json(self):
     self.maxDiff = None
     # Remove values that are not json encodeable
     IntegrationBase.result.pop("complex")
     IntegrationBase.result.pop("tuple")
     IntegrationBase.result.pop("secret")
     Pconf.file("./tests/integration/example.json", encoding="json")
     config = Pconf.get()
     self.assertEqual(config, IntegrationBase.result)
Example #6
0
    def test_file_get(self, mock_file):
        mocked_file = MagicMock()
        mocked_file.get.return_value = TEST_FILE_RESULT
        mock_file.return_value = mocked_file

        Pconf.file(path=TEST_FILE_PATH)
        results = Pconf.get()

        mocked_file.get.assert_called_once()
        for key in TEST_FILE_RESULT:
            self.assertTrue(key in results)
            self.assertEqual(results[key], TEST_FILE_RESULT[key])
Example #7
0
    def test_backward(self, mock_file, mock_env):
        mocked_env = MagicMock()
        mocked_env.get.return_value = self.TEST_ENV_RESULT
        mock_env.return_value = mocked_env

        mocked_file = MagicMock()
        mocked_file.get.return_value = self.TEST_FILE_RESULT
        mock_file.return_value = mocked_file

        Pconf.file(self.TEST_FILE_PATH)
        Pconf.env()
        results = Pconf.get()

        expected = {
            "file": "result",
            "env": "result",
            "overlapping": "file",
            "deep": {"stillhere": "stillhere", "overlapping": "file"},
        }

        self.assertEqual(expected, results)
Example #8
0
 def _init(self, logger):
     """Read the configuration data from files and profile"""
     if not self.is_init:
         if self.pconf_obj:
             self.pconf_obj.clear()
             Pconf.clear()
         Pconf.env()
         dir = self.get_dir()
         env_name = self.profile if (len(self.profile)) else os.getenv(
             'PY_ENV', 'production')
         logger.debug('Configuration dir %s with env %s', dir, env_name)
         if path.isdir(dir):
             env_file = ''
             if env_name == 'production':
                 env_file = '%s/production_config.json' % dir
             elif env_name == 'development':
                 env_file = '%s/development_conf.json' % dir
             elif env_name == 'integration':
                 env_file = '%s/integration_config.json' % dir
             elif env_name == 'test':
                 env_file = '%s/test_config.json' % dir
             elif env_name == 'test_e2e':
                 env_file = '%s/test_e2e_config.json' % dir
             else:
                 raise ConfigLoadError('Unknown configuration profile: %s' %
                                       env_name)
             if not os.path.exists(env_file):
                 raise ConfigLoadError('Missing configuration file: %s' %
                                       env_file)
             Pconf.file(env_file, encoding='json')
             base_cfg_file = '%s/config.json' % dir
             if not os.path.exists(base_cfg_file):
                 raise ConfigLoadError('Missing configuration file: %s' %
                                       base_cfg_file)
             Pconf.file('%s/config.json' % dir, encoding='json')
         else:
             raise ConfigLoadError('Missing directory: %s' % dir)
         self.is_init = True
         self.pconf_obj = Pconf.get()
Example #9
0
    def test_backward(self, mock_file, mock_env):
        mocked_env = MagicMock()
        mocked_env.get.return_value = self.TEST_ENV_RESULT
        mock_env.return_value = mocked_env

        mocked_file = MagicMock()
        mocked_file.get.return_value = self.TEST_FILE_RESULT
        mock_file.return_value = mocked_file

        Pconf.file(self.TEST_FILE_PATH)
        Pconf.env()
        results = Pconf.get()

        expected = {
            'file': 'result',
            'env': 'result',
            'overlapping': 'file',
            'deep': {
                'stillhere': 'stillhere',
                'overlapping': 'file'
            }
        }

        self.assertEqual(expected, results)
Example #10
0
    def test_file(self):
        Pconf.file(path=TEST_FILE_PATH)

        pconf.store.file.File.assert_called_once_with(TEST_FILE_PATH, None,
                                                      None)
        self.assertEqual(len(Pconf._Pconf__hierarchy), 1)
def _read_configure(config_filepath, return_empty=False) -> dict:
    import os
    import pprint
    from pconf import Pconf
    from ioflow.configure.get_configure_path_from_argv import get_configure_path_from_argv
    from ioflow.configure.read_configure import find_best_file_candidate, guess_configure_file_type

    # set return_empty to True for not read config from env
    # which can prevent unexpected result
    # e.g. './configure.json' is not for this app, but for other using
    if return_empty:
        return {}

    config_fileprefix = os.path.splitext(os.path.split(config_filepath)[-1])[0]

    default_configure_candidate = [
        ".".join([config_fileprefix, ext]) for ext in ["yaml", "yml", "json"]
    ]
    builtin_configure_candidate = [
        ".".join(["./builtin_configure", ext])
        for ext in ["yaml", "yml", "json"]
    ]

    default_configure = find_best_file_candidate(default_configure_candidate)
    builtin_configure = find_best_file_candidate(builtin_configure_candidate)

    active_configure_file = get_configure_path_from_argv()
    if not active_configure_file:
        active_configure_file = os.getenv("_DEFAULT_CONFIG_FILE",
                                          default_configure)

    builtin_configure_file = os.getenv("_BUILTIN_CONFIG_FILE",
                                       builtin_configure)

    # Note: this is a safeguard, before using any Pconf function, do execute this
    # In case former Pconf usage influence current usage
    # which will lead to a hidden and wired bug
    Pconf.clear()

    # disable read configure from environment
    # Pconf.env()

    active_configure_file_abs_path = os.path.realpath(active_configure_file)

    if not os.path.exists(active_configure_file):
        msg = "default configure file is not found! CWD: {}; activate_config: {}; builtin_configure: {}".format(
            os.getcwd(), active_configure_file, builtin_configure_file)
        print(msg)
        raise ValueError(msg)
    else:
        print(">>> Using configure read from file: {}".format(
            active_configure_file_abs_path))

    file_encoding = guess_configure_file_type(active_configure_file_abs_path)
    Pconf.file(active_configure_file, file_encoding)

    # try loading builtin configure file
    if builtin_configure_file and os.path.exists(builtin_configure_file):
        print(
            "loading builtin configure from {}".format(builtin_configure_file))
        file_encoding = guess_configure_file_type(builtin_configure_file)
        Pconf.file(builtin_configure_file, encoding=file_encoding)
    else:
        print(">>> builtin configure file is not found!")

    # Get all the config values parsed from the sources
    config = Pconf.get()

    # NOTE: clean Pconf for later brand new use
    Pconf.clear()

    print("++" * 8, "configure", "++" * 8)
    pprint.pprint(config)

    return config
Example #12
0
"""Application Config"""
import os
from pconf import Pconf
DIR_PATH = os.path.dirname(os.path.realpath(__file__))

Pconf.defaults({
    'bot-move-topic-name': 'bot-move',
    "bot-speak-topic-name": 'bot-speak',
    "bot-cam-topic-name": 'cam-command'
})
localpath = os.path.join(DIR_PATH, 'dev.json').format()
Pconf.file(localpath, encoding='json')
Pconf.env()

CONFIG = Pconf.get()

MQTT_HOST = CONFIG.get("mqtt-host", '')
MQTT_USER = CONFIG.get("mqtt-user", '')
MQTT_PWD = CONFIG.get("mqtt-pwd", '')
MQTT_PORT = int(CONFIG.get("mqtt-port", -1))
MQTT_MOTOR_MOVE_TOPIC_NAME = CONFIG.get("bot-move-topic-name", '')
MQTT_SPEAK_TOPIC_NAME = CONFIG.get("bot-speak-topic-name", '')
MQTT_CAM_TOPIC_NAME = CONFIG.get("bot-cam-topic-name", '')

WEB_PORT = int(CONFIG.get("PORT", 5000))