Пример #1
0
    def init_conf(self, *args, **kwargs):
        """
		This single line is the backbone virtually of this.

		To make this dynamic without having to force the plugin to pass __file__, this was done.

		This fetches the data about the module, and then retrieves the filepath of what is calling this method.

		This means if plugins/system/logger/logger.py and plugins/totp/twilio/twilio.py both want to load config
		references, they just need to do one thing:

		self.init_conf()

		A new config reference will be created for both, separate of each other.
		"""
        cur_path, _ = os.path.split(os.path.abspath(self.module_info.__file__))

        conf = ".env"
        defaults = {}

        try:
            conf = args[0]
            defaults = args[1]
        except IndexError:
            pass

        file_ = "%s/%s" % (cur_path, conf)

        # When ACL is used, config file seems to be read from the system/acl/ directory...this fixes it
        # (or you could just put the ACLs inside of system/acl, I just felt that would be too messy)
        if not os.path.isfile(file_):
            file_ = conf

        self.plugin_conf = Konf(conf_file=file_, defaults=defaults)
Пример #2
0
    def test_json(self):
        k_ = Konf(self._get_asset('1.json'))

        foo = k_('foo', STRING)
        self.assertEqual(foo, 'TheBar!')

        my_list = k_('my_list', [STRING])
        self.assertEqual(my_list, ['the_first', 'the_next'])
Пример #3
0
def parse_config(filename):
    k = Konf(filename)
    field_formats = {
        'lock command': str,
        'time to sleep': int,
        'sleep length': int
    }
    return {name: k(name, format) for name, format in field_formats.items()}
Пример #4
0
 def test_nested_default_value(self):
     from good import Any, Default
     k_ = Konf(self._get_asset('1.yml'))
     cool_dict = k_('cool_dict', dict(
         khrum=str,
         dfght=int,
         foo=Any(str, Default('bar'))
     ))
     self.assertEqual(cool_dict['foo'], 'bar')
Пример #5
0
    def test_involved_with_default(self):
        k_ = Konf(self._get_asset('2.yml'))
        checker = k_.check_redundant
        right_foot = k_('right_foot', int)
        left_foot = k_('left_foot', int)
        tail = k_('tail', int, 9)
        checker()
        self.assertEquals([left_foot, right_foot, tail], [0, 1, 9])

        self.assertRaises(k_.ReassignmentError, lambda: k_('tail', int, 10))
Пример #6
0
    def test_default(self):
        k_ = Konf(self._get_asset('1.yml'))

        x = k_('x', int, 777)
        self.assertEqual(x, 777)

        y = k_('y', dict, 888)
        self.assertEqual(y, 888)

        z = k_('z', float, None)
        self.assertEqual(z, None)
Пример #7
0
    def test_involved(self):
        k_ = Konf(self._get_asset('2.yml'))
        checker = k_.check_redundant

        right_foot = k_('right_foot', int)
        self.assertEqual(right_foot, 1)
        self.assertRaises(Konf.RedundantConfigError, checker)

        left_foot = k_('left_foot', int)
        checker()
        self.assertEqual(left_foot, 0)
Пример #8
0
def parse(filename):
    k = Konf(filename)

    # syntax:
    config = {
        'server': k('server', is_valid_server_conf),
        'steam api key': k('steam api key', str),
        'hooks': k('hooks', is_valid_hooklist)
    }

    # semantics:

    return config
Пример #9
0
    def test_yaml(self):
        k_ = Konf(self._get_asset('1.yml'))

        a = k_('a', int)
        self.assertEqual(a, 13)

        b = k_('b', STRING)
        self.assertEqual(b, 'foo')

        c = k_('c', float)
        self.assertEqual(c, 4.44)

        cool_list = k_('cool_list', [int])
        self.assertEqual(cool_list, [6, 12])

        coold_dict = k_('cool_dict', {'khrum': STRING, 'dfght': int})
        self.assertEqual(coold_dict, {
            'khrum': u'бар',
            'dfght': 90
        })
Пример #10
0
 def test_multiple_loading(self):
     k_ = Konf(self._get_asset('1.yml'))
     a = k_('a', int)
     self.assertRaises(Konf.ReassignmentError,
                       lambda: k_('a', int)
                       )
Пример #11
0
 def test_validation_error(self):
     k_ = Konf(self._get_asset('1.yml'))
     self.assertRaises(Konf.ValidationError,
                       lambda: k_('b', int)
                       )
Пример #12
0
 def test_incomplete_config(self):
     k_ = Konf(self._get_asset('1.yml'))
     self.assertRaises(Konf.IncompleteConfigError,
                       lambda: k_('I_Want_This_Please', int)
                       )
Пример #13
0
 def test_cant_load(self):
     self.assertRaises(Konf.ParseError,
                       lambda: Konf(self._get_asset('bad.json'))
                       )
Пример #14
0
 def test_cant_access(self):
     self.assertRaises(Konf.ReadError,
                       lambda: Konf(self._get_asset('99.yml'))
                       )
Пример #15
0
    def test_validators(self):
        # 0. Select configuration file
        k_ = Konf(self._get_asset('5.yml'))

        # 1. Declare validators
        # You can cache validators inside a Konf object as if it's a standard python dict
        k_['v1'] = {
            'key': STRING,
            'secret': STRING,
        }
        k_['v2'] = {
            'key': STRING,
            'secret': STRING,
            'public_name': STRING
        }

        # 2. Get variables from config
        # For avoid copy-paste and massive chunks of code, just declare a new variable
        # and pass data from config to it
        sn_ = k_('SN', {
            'vk': k_['v1'],  # You can get validator you want, for example v1...
            'google': k_['v1'],
            'twitter': k_['v1'],
            'ok': k_['v2']  # ...or v2
        })

        # 3. Fill everything to a python variables which are required for 3rd-party library
        SOCIAL_AUTH_VK_OAUTH2_KEY = sn_['vk']['key']
        SOCIAL_AUTH_VK_OAUTH2_SECRET = sn_['vk']['secret']
        SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = sn_['google']['key']
        SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = sn_['google']['secret']
        SOCIAL_AUTH_TWITTER_KEY = sn_['twitter']['key']
        SOCIAL_AUTH_TWITTER_SECRET = sn_['twitter']['secret']
        SOCIAL_AUTH_ODNOKLASSNIKI_KEY = sn_['ok']['key']
        SOCIAL_AUTH_ODNOKLASSNIKI_SECRET = sn_['ok']['secret']
        SOCIAL_AUTH_ODNOKLASSNIKI_OAUTH2_PUBLIC_NAME = sn_['ok']['public_name']

        # 4. Check that config doesn't contain some garbage
        # (this might mean you forgot to get these variables, or this config is wrong, some draft for example)
        k_.check_redundant()

        # 5. If server is running without errors, and you will meet issue with this 3rd-party library later,
        # you can be sure that problem isn't in your configuration file.
        # Otherwise, you'll just catch a error on a start server stage.

        from good import Optional
        k2_ = Konf(self._get_asset('6.yml'))
        k2_['sn_data'] = {
            'key': STRING,
            'secret': STRING,
            Optional('public_name'): STRING
        }
        sn2_ = lambda: k2_('SN', {
            name: k2_['sn_data'] for name in ['vk', 'google', 'twitter']
        })
        self.assertRaises(Konf.ValidationError, sn2_)

        def assign():
            k_['v1'] = {'foo': 'bar'}

        self.assertRaises(Konf.ValidatorManagementError, assign)
        self.assertRaises(Konf.ValidatorManagementError, lambda: k_['v3'])
Пример #16
0
 def test_no_content_default(self):
     for module in ['3.yml', '4.yml']:
         k_ = Konf(self._get_asset(module))
         a = k_('i_am_not_in_file_but_with_default', int, 3)
         self.assertEqual(a, 3)
Пример #17
0
 def test_no_content(self):
     for module in ['3.yml', '4.yml']:
         k_ = Konf(self._get_asset(module))
         self.assertRaises(Konf.IncompleteConfigError, lambda: k_('i_am_not_in_file', int))
Пример #18
0
 def test_wrong_extension(self):
     self.assertRaises(Konf.FileExtensionError,
                       lambda: Konf(self._get_asset('1.wrong'))
                       )
Пример #19
0
log = logging.getLogger(__name__)

_BASE_DEFAULTS = {
    'arg_sep': " ",
    "listen_port": 5222,
    "tcp": True,
    "threaded": True,
    "sshport": 22,
    "intf": "eth0",

    # See _LOG_LEVELS to set
    "log_level": "debug"
}

conf = Konf(defaults=_BASE_DEFAULTS)

NULL_RESP = {}

_LOG_LEVELS = {
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
    "warning": logging.WARNING,
    "info": logging.INFO,
    "debug": logging.DEBUG
}

loglevel = _LOG_LEVELS.get(str(conf.log_level).lower(), "info")

logging.basicConfig(level=loglevel,
                    format='%(levelname)-8s at %(asctime)s : %(message)s')