def test_find(self): class TestDataSource(DataSource): TYPE = 'test_find' assert DataSource.find('test_find') == TestDataSource with pytest.raises(KeyError): DataSource.find('foo')
def gramola(): """ Entry point called from binary generated by setuptools. Beyond the main command Gramola immplements a sub set of commands that each one implements an specific opeartion. The anatomy of a Gramola command looks like: $ gramola <global options> <subcommand> <subcommand options> <args ..> """ # Build as many datasource-echo commands as many types of datasources there are. echo_commands = [build_datasource_echo_type(datasource) for datasource in DataSource.implementations()] # Build as many datasource-add commands as many types of datasources there are. add_commands = [build_datasource_add_type(datasource) for datasource in DataSource.implementations()] # Build as many query commands as many types of datasources there are. query_commands = [build_datasource_query_type(datasource) for datasource in DataSource.implementations()] # Use the gramola.contrib.subcommands implementation to wraper the # GramolaCommands as a subcommands availables from the main command. subcommands = [] for gramola_subcommand in GramolaCommand.commands(): cmd = Subcommand(gramola_subcommand.NAME, optparse.OptionParser(usage=gramola_subcommand.USAGE), gramola_subcommand.DESCRIPTION) for option_args, option_kwargs in gramola_subcommand.options(): cmd.parser.add_option(*option_args, **option_kwargs) subcommands.append(cmd) parser = SubcommandsOptionParser(subcommands=subcommands) parser.add_option('-s', '--store', dest='store', help='alternative store directory, default ~/.gramola') parser.add_option('-q', dest='quite', help='Be quite', action='store_true') parser.add_option('-v', dest='verbose', help='Be verbose', action='store_true') options, subcommand, suboptions, subargs = parser.parse_args() log.setup(verbose=options.verbose, quite=options.quite) try: cmd = GramolaCommand.find(subcommand.name) except KeyError: print("Command not found {}".format(subcommand.name)) print("") parser.print_help() sys.exit(1) try: cmd.execute(options, suboptions, *subargs) except InvalidParams, e: print("Invalid params for {} command, error: {}".format(subcommand.name, e.error_params)) print("Get help with gramola {} --help".format(subcommand.name)) sys.exit(1)
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
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))
def gramola(): """ Entry point called from binary generated by setuptools. Beyond the main command Gramola immplements a sub set of commands that each one implements an specific opeartion. The anatomy of a Gramola command looks like: $ gramola <global options> <subcommand> <subcommand options> <args ..> """ # Build as many datasource-echo commands as many types of datasources there are. echo_commands = [ build_datasource_echo_type(datasource) for datasource in DataSource.implementations() ] # Build as many datasource-add commands as many types of datasources there are. add_commands = [ build_datasource_add_type(datasource) for datasource in DataSource.implementations() ] # Build as many query commands as many types of datasources there are. query_commands = [ build_datasource_query_type(datasource) for datasource in DataSource.implementations() ] # Use the gramola.contrib.subcommands implementation to wraper the # GramolaCommands as a subcommands availables from the main command. subcommands = [] for gramola_subcommand in GramolaCommand.commands(): cmd = Subcommand(gramola_subcommand.NAME, optparse.OptionParser(usage=gramola_subcommand.USAGE), gramola_subcommand.DESCRIPTION) for option_args, option_kwargs in gramola_subcommand.options(): cmd.parser.add_option(*option_args, **option_kwargs) subcommands.append(cmd) parser = SubcommandsOptionParser(subcommands=subcommands) parser.add_option('-s', '--store', dest='store', help='alternative store directory, default ~/.gramola') parser.add_option('-q', dest='quite', help='Be quite', action='store_true') parser.add_option('-v', dest='verbose', help='Be verbose', action='store_true') options, subcommand, suboptions, subargs = parser.parse_args() log.setup(verbose=options.verbose, quite=options.quite) try: cmd = GramolaCommand.find(subcommand.name) except KeyError: print("Command not found {}".format(subcommand.name)) print("") parser.print_help() sys.exit(1) try: cmd.execute(options, suboptions, *subargs) except InvalidParams, e: print("Invalid params for {} command, error: {}".format( subcommand.name, e.error_params)) print("Get help with gramola {} --help".format(subcommand.name)) sys.exit(1)