示例#1
0
 def test_equal_instances_ok(self):
     config1 = ConfFile(
         config={"test-1": {
             "test-1-1": "a",
             "test_1-2": "b"
         }})
     config2 = ConfFile(
         config={"test-1": {
             "test-1-1": "a",
             "test_1-2": "b"
         }})
     self.assertEqual(config1, config2)
示例#2
0
 def test_dictionary_recursive_dict_normal_key_dinamyc(self):
     config = ConfFile(config={
         "test-1": {
             "test-1-1": "a",
             "test_1-2": "b"
         },
         "test_2": "c"
     })
     self.assertEqual(getattr(config, "test_1.test_1_2"), "b")
示例#3
0
 def test_dictionary_recursive_dict_replace_key(self):
     config = ConfFile(config={
         "test-1": {
             "test-1-1": "a",
             "test_1-2": "b"
         },
         "test_2": "b"
     })
     self.assertEqual(config.test_1.test_1_1, "a")
示例#4
0
 def test_dictionary_recursive_dict_normal_key(self):
     config = ConfFile(config={
         "test-1": {
             "test-1-1": "a",
             "test_1-2": "b"
         },
         "test_2": "c"
     })
     self.assertEqual(config.test_1.test_1_2, "b")
示例#5
0
 def config(self, *args, **kwargs):
     """Set the configuration, if our yaml file is like:
     myservice:
       myservice1:
        myvar1
     and we want to get the configuration of service1, our self.service will be "myservice.myservice1"
     """
     if not self._config:
         self._config = ConfFile(*args, **kwargs)
     if not self.service:
         raise ServiceDoesNotExistException("Service not defined")
     return getattr(self._config, self.service)
示例#6
0
文件: conf.py 项目: pkjmesra/pyms
def get_conf(*args, **kwargs):
    """
    Returns an object with a set of attributes retrieved from the configuration file. Each subblock is a append of the
    parent and this name, in example of the next yaml, tracer will be `pyms.tracer`. If we have got his config file:
    See these docs:
    * https://python-microservices.github.io/configuration/
    * https://python-microservices.github.io/services/
    :param args:
    :param kwargs:

    :return:
    """
    service = kwargs.pop("service", None)
    if not service:
        raise ServiceDoesNotExistException("Service not defined")
    config = ConfFile(*args, **kwargs)
    return getattr(config, service)
示例#7
0
def get_conf(*args, **kwargs):
    """
    Returns an object with a set of attributes retrieved from the configuration file. Each subblock is a append of the
    parent and this name, in example of the next yaml, tracer will be `pyms.tracer`. If we have got his config file:
    ```
    pyms:
      metrics: true
      requests:
        data: data
      swagger:
        path: ""
        file: "swagger.yaml"
      tracer:
        client: "jaeger"
        host: "localhost"
        component_name: "Python Microservice"
    my-ms:
      DEBUG: true
      TESTING: true
    ```
    * `pyms` block is the default key to load in the pyms.flask.app.create_app.Microservice class.
        * `metrics`: is set as the service `pyms.metrics`
        * `swagger`: is set as the service `pyms.swagger`
        * `tracer`: is set as the service `pyms.tracer`
    * `my-ms` block is defined by the env var `CONFIGMAP_SERVICE`. By default is `ms`. This block is the default flask
    block config
    :param args:
    :param kwargs:
    :return:
    """
    service = kwargs.pop('service', None)
    memoize = kwargs.pop('memoize', True)
    if not service:
        raise ServiceDoesNotExistException("Service not defined")
    if not memoize or service not in __service_configs:
        __service_configs[service] = ConfFile(*args, **kwargs)
    return getattr(__service_configs[service], service)
示例#8
0
 def test_dictionary_normal_key(self):
     config = ConfFile(config={"test-1": "a", "test_2": "b"})
     self.assertEqual(config.test_2, "b")
示例#9
0
 def test_example_test_file_from_env(self):
     config = ConfFile()
     self.assertEqual(config.my_ms.test_var, "general")
示例#10
0
 def test_empty_conf_three_levels(self):
     config = ConfFile(empty_init=True)
     self.assertEqual(config.my_ms.level_two.level_three, {})
示例#11
0
 def test_empty_conf(self):
     config = ConfFile(empty_init=True)
     self.assertEqual(config.my_ms, {})
示例#12
0
 def test_example_test_json_file(self):
     config = ConfFile(
         path=os.path.join(self.BASE_DIR, "config-tests.json"))
     self.assertEqual(config.my_ms.test_var, "general")
示例#13
0
 def test_example_test_file_not_exists(self):
     with self.assertRaises(ConfigDoesNotFoundException):
         config = ConfFile(path="path/not/exist.yml")
示例#14
0
 def test_example_test_config_not_exixsts(self):
     with self.assertRaises(ConfigDoesNotFoundException):
         config = ConfFile()
示例#15
0
 def test_dictionary_attribute_not_exists(self):
     config = ConfFile(config={"test-1": "a"})
     with self.assertRaises(AttrDoesNotExistException):
         config.not_exist
示例#16
0
def validate_conf(*args, **kwargs):

    config = ConfFile(*args, **kwargs)
    is_config_ok = True
    try:
        config.pyms
    except AttrDoesNotExistException:
        is_config_ok = False
    if not is_config_ok:
        raise ConfigErrorException(
            """Config file must start with `pyms` keyword, for example:
    pyms:
      services:
        metrics: true
        requests:
          data: data
        swagger:
          path: ""
          file: "swagger.yaml"
        tracer:
          client: "jaeger"
          host: "localhost"
          component_name: "Python Microservice"
      config:
        DEBUG: true
        TESTING: true""")
    try:
        config.pyms.config
    except AttrDoesNotExistException:
        is_config_ok = False
    if not is_config_ok:
        raise ConfigErrorException(
            """`pyms` block must contain a `config` keyword in your Config file, for example:
    pyms:
      services:
        metrics: true
        requests:
          data: data
        swagger:
          path: ""
          file: "swagger.yaml"
        tracer:
          client: "jaeger"
          host: "localhost"
          component_name: "Python Microservice"
      config:
        DEBUG: true
        TESTING: true""")
    wrong_keywords = [
        i for i in config.pyms if i not in PYMS_CONFIG_WHITELIST_KEYWORDS
    ]
    if len(wrong_keywords) > 0:
        raise ConfigErrorException(
            """{} isn`t a valid keyword for pyms block, for example:
        pyms:
          services:
            metrics: true
            requests:
              data: data
            swagger:
              path: ""
              file: "swagger.yaml"
            tracer:
              client: "jaeger"
              host: "localhost"
              component_name: "Python Microservice"
          config:
            DEBUG: true
            TESTING: true""".format(wrong_keywords))

    # TODO Remove temporally deprecated warnings on future versions
    __verify_deprecated_env_variables(config)