當前位置: 首頁>>代碼示例>>Python>>正文


Python Config.add_or_update方法代碼示例

本文整理匯總了Python中st2common.persistence.pack.Config.add_or_update方法的典型用法代碼示例。如果您正苦於以下問題:Python Config.add_or_update方法的具體用法?Python Config.add_or_update怎麽用?Python Config.add_or_update使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在st2common.persistence.pack.Config的用法示例。


在下文中一共展示了Config.add_or_update方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_get_config_default_value_from_config_schema_is_used

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_default_value_from_config_schema_is_used(self):
        # No value is provided for "region" in the config, default value from config schema
        # should be used
        loader = ContentPackConfigLoader(pack_name='dummy_pack_5')
        config = loader.get_config()
        self.assertEqual(config['region'], 'default-region-value')

        # Here a default value is specified in schema but an explicit value is provided in the
        # config
        loader = ContentPackConfigLoader(pack_name='dummy_pack_1')
        config = loader.get_config()
        self.assertEqual(config['region'], 'us-west-1')

        # Config item attribute has required: false
        # Value is provided in the config - it should be used as provided
        pack_name = 'dummy_pack_5'

        loader = ContentPackConfigLoader(pack_name=pack_name)
        config = loader.get_config()
        self.assertEqual(config['non_required_with_default_value'], 'config value')

        config_db = Config.get_by_pack(pack_name)
        del config_db['values']['non_required_with_default_value']
        Config.add_or_update(config_db)

        # No value in the config - default value should be used
        config_db = Config.get_by_pack(pack_name)
        config_db.delete()

        # No config exists for that pack - default value should be used
        loader = ContentPackConfigLoader(pack_name=pack_name)
        config = loader.get_config()
        self.assertEqual(config['non_required_with_default_value'], 'some default value')
開發者ID:StackStorm,項目名稱:st2,代碼行數:35,代碼來源:test_config_loader.py

示例2: test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown(self):
        pack_name = 'dummy_pack_schema_with_nested_object_5'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        # Render fails on top-level item
        values = {
            'level0_key': '{{st2kvXX.invalid}}'
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key "level0_key" with '
                        'value "{{st2kvXX.invalid}}" for pack ".*?" config: '
                        '\'st2kvXX\' is undefined')
        self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on fist level item
        values = {
            'level0_object': {
                'level1_key': '{{st2kvXX.invalid}}'
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.level1_key" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on second level item
        values = {
            'level0_object': {
                'level1_object': {
                    'level2_key': '{{st2kvXX.invalid}}'
                }
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.level1_object.level2_key" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
        config_db.delete()
開發者ID:Plexxi,項目名稱:st2,代碼行數:50,代碼來源:test_config_loader.py

示例3: _register_config_for_pack

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def _register_config_for_pack(self, pack, config_path):
        content = {}
        values = self._meta_loader.load(config_path)

        content['pack'] = pack
        content['values'] = values

        config_api = ConfigAPI(**content)
        config_api.validate(validate_against_schema=self._validate_configs)
        config_db = ConfigAPI.to_model(config_api)

        try:
            config_db.id = Config.get_by_pack(config_api.pack).id
        except StackStormDBObjectNotFoundError:
            LOG.debug('Config for pack "%s" not found. Creating new entry.', pack)

        try:
            config_db = Config.add_or_update(config_db)
            extra = {'config_db': config_db}
            LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra)
        except Exception:
            LOG.exception('Failed to config for pack %s.', pack)
            raise

        return config_db
開發者ID:janjaapbos,項目名稱:st2,代碼行數:27,代碼來源:configsregistrar.py

示例4: test_get_config_dynamic_config_item_list

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item_list(self):
        pack_name = 'dummy_pack_schema_with_nested_object_7'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
        KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))

        ####################
        # values in list
        values = {
            'level0_key': [
                'a',
                '{{st2kv.system.k0}}',
                'b',
                '{{st2kv.system.k1}}',
            ]
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        config_rendered = loader.get_config()

        self.assertEquals(config_rendered,
                          {
                              'level0_key': [
                                  'a',
                                  'v0',
                                  'b',
                                  'v1'
                              ]
                          })

        config_db.delete()
開發者ID:StackStorm,項目名稱:st2,代碼行數:35,代碼來源:test_config_loader.py

示例5: test_empty_config_object_in_the_database

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_empty_config_object_in_the_database(self):
        pack_name = 'dummy_pack_empty_config'

        config_db = ConfigDB(pack=pack_name)
        config_db = Config.add_or_update(config_db)

        loader = ContentPackConfigLoader(pack_name=pack_name)
        config = loader.get_config()
        self.assertEqual(config, {})
開發者ID:StackStorm,項目名稱:st2,代碼行數:11,代碼來源:test_config_loader.py

示例6: test_get_config_dynamic_config_item_nested_list

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item_nested_list(self):
        pack_name = 'dummy_pack_schema_with_nested_object_8'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
        KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
        KeyValuePair.add_or_update(KeyValuePairDB(name='k2', value='v2'))

        ####################
        # values in objects embedded in lists and nested lists
        values = {
            'level0_key': [
                {
                    'level1_key0': '{{st2kv.system.k0}}'
                },
                '{{st2kv.system.k1}}',
                [
                    '{{st2kv.system.k0}}',
                    '{{st2kv.system.k1}}',
                    '{{st2kv.system.k2}}',
                ],
                {
                    'level1_key2': [
                        '{{st2kv.system.k2}}',
                    ]
                }
            ]
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        config_rendered = loader.get_config()

        self.assertEquals(config_rendered,
                          {
                              'level0_key': [
                                  {
                                      'level1_key0': 'v0'
                                  },
                                  'v1',
                                  [
                                      'v0',
                                      'v1',
                                      'v2',
                                  ],
                                  {
                                      'level1_key2': [
                                          'v2',
                                      ]
                                  }
                              ]
                          })

        config_db.delete()
開發者ID:StackStorm,項目名稱:st2,代碼行數:56,代碼來源:test_config_loader.py

示例7: save_model

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def save_model(config_api):
        pack = config_api.pack
        config_db = ConfigAPI.to_model(config_api)

        try:
            config_db.id = Config.get_by_pack(pack).id
        except StackStormDBObjectNotFoundError:
            LOG.debug('Config for pack "%s" not found. Creating new entry.', pack)

        try:
            config_db = Config.add_or_update(config_db)
            extra = {'config_db': config_db}
            LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra)
        except Exception:
            LOG.exception('Failed to save config for pack %s.', pack)
            raise

        return config_db
開發者ID:nzlosh,項目名稱:st2,代碼行數:20,代碼來源:configsregistrar.py

示例8: test_get_config_dynamic_config_item

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item(self):
        pack_name = 'dummy_pack_schema_with_nested_object_6'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        ####################
        # value in top level item
        KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
        values = {
            'level0_key': '{{st2kv.system.k1}}'
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        config_rendered = loader.get_config()

        self.assertEquals(config_rendered, {'level0_key': 'v1'})

        config_db.delete()
開發者ID:StackStorm,項目名稱:st2,代碼行數:20,代碼來源:test_config_loader.py

示例9: test_default_values_are_used_when_default_values_are_falsey

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_default_values_are_used_when_default_values_are_falsey(self):
        pack_name = 'dummy_pack_17'

        loader = ContentPackConfigLoader(pack_name=pack_name)
        config = loader.get_config()

        # 1. Default values are used
        self.assertEqual(config['key_with_default_falsy_value_1'], False)
        self.assertEqual(config['key_with_default_falsy_value_2'], None)
        self.assertEqual(config['key_with_default_falsy_value_3'], {})
        self.assertEqual(config['key_with_default_falsy_value_4'], '')
        self.assertEqual(config['key_with_default_falsy_value_5'], 0)
        self.assertEqual(config['key_with_default_falsy_value_6']['key_1'], False)
        self.assertEqual(config['key_with_default_falsy_value_6']['key_2'], 0)

        # 2. Default values are overwrriten with config values which are also falsey
        values = {
            'key_with_default_falsy_value_1': 0,
            'key_with_default_falsy_value_2': '',
            'key_with_default_falsy_value_3': False,
            'key_with_default_falsy_value_4': None,
            'key_with_default_falsy_value_5': {},
            'key_with_default_falsy_value_6': {
                'key_2': False
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        loader = ContentPackConfigLoader(pack_name=pack_name)
        config = loader.get_config()

        self.assertEqual(config['key_with_default_falsy_value_1'], 0)
        self.assertEqual(config['key_with_default_falsy_value_2'], '')
        self.assertEqual(config['key_with_default_falsy_value_3'], False)
        self.assertEqual(config['key_with_default_falsy_value_4'], None)
        self.assertEqual(config['key_with_default_falsy_value_5'], {})
        self.assertEqual(config['key_with_default_falsy_value_6']['key_1'], False)
        self.assertEqual(config['key_with_default_falsy_value_6']['key_2'], False)
開發者ID:StackStorm,項目名稱:st2,代碼行數:41,代碼來源:test_config_loader.py

示例10: test_get_config_dynamic_config_item_nested_dict

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item_nested_dict(self):
        pack_name = 'dummy_pack_schema_with_nested_object_7'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
        KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
        KeyValuePair.add_or_update(KeyValuePairDB(name='k2', value='v2'))

        ####################
        # values nested dictionaries
        values = {
            'level0_key': '{{st2kv.system.k0}}',
            'level0_object': {
                'level1_key': '{{st2kv.system.k1}}',
                'level1_object': {
                    'level2_key': '{{st2kv.system.k2}}'
                }
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        config_rendered = loader.get_config()

        self.assertEquals(config_rendered,
                          {
                              'level0_key': 'v0',
                              'level0_object': {
                                  'level1_key': 'v1',
                                  'level1_object': {
                                      'level2_key': 'v2'
                                  }
                              }
                          })

        config_db.delete()
開發者ID:StackStorm,項目名稱:st2,代碼行數:38,代碼來源:test_config_loader.py

示例11: test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown

# 需要導入模塊: from st2common.persistence.pack import Config [as 別名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 別名]
    def test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown(self):
        pack_name = 'dummy_pack_schema_with_nested_object_5'
        loader = ContentPackConfigLoader(pack_name=pack_name)

        # Render fails on top-level item
        values = {
            'level0_key': '{{st2kvXX.invalid}}'
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        config_db = Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key "level0_key" with '
                        'value "{{st2kvXX.invalid}}" for pack ".*?" config: '
                        '<class \'jinja2.exceptions.UndefinedError\'> '
                        '\'st2kvXX\' is undefined')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on fist level item
        values = {
            'level0_object': {
                'level1_key': '{{st2kvXX.invalid}}'
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.level1_key" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
                        ' \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on second level item
        values = {
            'level0_object': {
                'level1_object': {
                    'level2_key': '{{st2kvXX.invalid}}'
                }
            }
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.level1_object.level2_key" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
                        ' \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on list item
        values = {
            'level0_object': [
                'abc',
                '{{st2kvXX.invalid}}'
            ]
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.1" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
                        ' \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on nested object in list item
        values = {
            'level0_object': [
                {'level2_key': '{{st2kvXX.invalid}}'}
            ]
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_object.0.level2_key" with value "{{st2kvXX.invalid}}"'
                        ' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
                        ' \'st2kvXX\' is undefined')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()

        # Renders fails on invalid syntax
        values = {
            'level0_key': '{{ this is some invalid Jinja }}'
        }
        config_db = ConfigDB(pack=pack_name, values=values)
        Config.add_or_update(config_db)

        expected_msg = ('Failed to render dynamic configuration value for key '
                        '"level0_key" with value "{{ this is some invalid Jinja }}"'
                        ' for pack ".*?" config: <class \'jinja2.exceptions.TemplateSyntaxError\'>'
                        ' expected token \'end of print statement\', got \'Jinja\'')
        self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
        config_db.delete()
開發者ID:StackStorm,項目名稱:st2,代碼行數:100,代碼來源:test_config_loader.py


注:本文中的st2common.persistence.pack.Config.add_or_update方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。