示例#1
0
    def test_find(self):
        class TestDataSource(DataSource):
            TYPE = 'test_find'

        assert DataSource.find('test_find') == TestDataSource

        with pytest.raises(KeyError):
            DataSource.find('foo')
示例#2
0
文件: store.py 项目: pfreixes/gramola
    def datasources(self, name=None, type_=None):
        """
        Return all datasources stored as a list of dictionaries, each dictionary
        is instance of :class:gramola.datasources.base.DataSourceConfig or derivated
        one representing the data source by it self.

        Use the keywords `name` and `type_` to filter datasources for one or both
        fields.

        :param name: string, filter by name.
        :param type_: string, filter by type_ of datasource.
        :return: list
        """
        config = ConfigObj(self.datasources_filepath, create_empty=True)
        results = []
        for section in config.sections:
            # User filters
            if name and name != section:
                continue

            if type_ and type_ != config[section].get('type'):
                continue

            # The title of the section is the name of the data source, we have to
            # pack it by hand. Each section as at least the type key used to find out
            # the right DataSourceConfig derivated class.
            factory = DataSource.find(config[section].get('type')).DATA_SOURCE_CONFIGURATION_CLS
            keys = {k: v for k, v in config[section].items()}
            keys.update({'name': section})
            results.append(factory(**keys))

        return results
示例#3
0
    def execute(options, suboptions, *subargs):
        """ Test an already saved datasource."""
        try:
            name = subargs[0]
        except IndexError:
            raise InvalidParams("NAME")

        store = options.store and Store(path=options.store) or Store()
        try:
            config = store.datasources(name=name)[0]
        except IndexError:
            print("Datasource `{}` not found, NOT TESTED".format(name))
            return

        if DataSource.find(config.type)(config).test():
            print("Datasource `{}` seems ok".format(name))
        else:
            print("Datasource `{}` FAILED!".format(name))
示例#4
0
    def execute(options, suboptions, *subargs):
        """ Test an already saved datasource."""
        try:
            name = subargs[0]
        except IndexError:
            raise InvalidParams("NAME")

        store = options.store and Store(path=options.store) or Store()
        try:
            config = store.datasources(name=name)[0]
        except IndexError:
            print("Datasource `{}` not found, NOT TESTED".format(name))
            return

        if DataSource.find(config.type)(config).test():
            print("Datasource `{}` seems ok".format(name))
        else:
            print("Datasource `{}` FAILED!".format(name))