Exemplo n.º 1
0
class DatabaseConfig(ConfigSection):
    def __init__(self, *args, **kwargs):
        ConfigSection.__init__(self, *args, **kwargs)
        self.db_name = None
        self.dbn     = None
        self.config  = Config(self.global_options.config_dir, False)

    @staticmethod
    def get_description():
        return 'add, edit, or remove databases'

    @staticmethod
    def get_commands():
        return (('add',  'configure a new database'),
                ('edit', 'configure an existing database'))

    def prepare_add(self, parser, db_name, dbn):
        self.db_name = db_name
        self.dbn     = dbn
        if self.config.has_database(self.db_name):
            parser.error('database already exists')

    def start_add(self):
        self.config.add_database(self.db_name, self.dbn)
        print 'Database added.'

    def prepare_edit(self, parser, db_name, dbn):
        self.db_name = db_name
        self.dbn     = dbn
        if not self.config.has_database(self.db_name):
            parser.error('database not found')

    def start_edit(self):
        if self.config.add_database(self.db_name, self.dbn):
            print 'Database configured.'
        else:
            print 'No changes were made.'
Exemplo n.º 2
0
class DaemonConfig(ConfigSection):
    def __init__(self, *args, **kwargs):
        ConfigSection.__init__(self, *args, **kwargs)
        self.daemon_name = None
        self.config      = None

    def _read_config(self):
        self.config = Config(self.global_options.config_dir, False)

    @staticmethod
    def get_description():
        return 'daemon-specific configuration'

    @staticmethod
    def get_commands():
        return (('add',  'configure a new daemon'),
                ('edit', 'configure an existing daemon'))

    def getopt_add(self, parser):
        parser.add_option('--address',
                          dest    = 'address',
                          metavar = 'STRING',
                          help    = 'the address to listen on, all by default')
        parser.add_option('--port',
                          dest    = 'port',
                          metavar = 'INT',
                          default = 8132,
                          help    = 'the TCP port number')
        parser.add_option('--database',
                          dest    = 'database',
                          metavar = 'STRING',
                          help    = 'name of the order database')
        parser.add_option('--account-pool',
                          dest    = 'account_pool',
                          metavar = 'STRING',
                          help    = 'the account pool used for authenticating' \
                                  + 'HTTP clients')

    def prepare_add(self, parser, daemon_name):
        self.daemon_name = daemon_name
        self._read_config()
        if self.config.has_daemon(self.daemon_name):
            parser.error('daemon already exists')

    def start_add(self):
        self.config.add_daemon(self.daemon_name,
                               self.options.address,
                               self.options.port,
                               self.options.account_pool,
                               self.options.database)
        print 'Daemon added.'

    def getopt_edit(self, parser):
        self.getopt_add(parser)

    def prepare_edit(self, parser, daemon_name):
        self.daemon_name = daemon_name
        self._read_config()
        if not self.config.has_daemon(self.daemon_name):
            parser.error('daemon not found')
        if not self.config.has_database(self.options.database):
            parser.error('database not found')

    def start_edit(self):
        self.config.add_daemon(self.daemon_name,
                               self.options.address,
                               self.options.port,
                               self.options.account_pool,
                               self.options.database)
        print 'Daemon configured.'