Example #1
0
def config_init(args):
    config_root = os.environ['FPTBOT_CONFIG']
    
    bot = Bot(root=config_root, level=logging.INFO)
    bot.init(runlevel.LOCAL_FILESYSTEM)
    
    logger = logging.getLogger('tools.config')
    
    def _init(identifier, object):
        logger.info('initializing configuration: %s', identifier)
        
        object.init(object.default_values())
        object.save()
    
    if args.identifier is None:
        for (identifier, object) in bot.get_configs().items():
            _init(identifier, object)
    
    else:
        identifier = args.identifier
        
        try:
            object = bot.get_config(identifier)
            
            _init(identifier, object)
        
        except KeyError:
            logger.critical('invalid identifier: %s', identifier)
            
    bot.init(runlevel.HALT)
Example #2
0
def config_write(args):
    config_root = os.environ['FPTBOT_CONFIG']
    
    bot = Bot(root=config_root, level=logging.INFO)
    bot.init(runlevel.LOCAL_FILESYSTEM)
    
    logger = logging.getLogger('tools.config') 
    
    identifier = args.identifier
    key = args.key
    type = args.type
    value = args.value
    
    try:
        object = bot.get_config(identifier)
        
    except KeyError:
        logger.critical('invalid identifier: %s', identifier)
        
    if key not in object.valid_keys():
        logger.critical('invalid key: %s', key)
        return
        
    
    value = ' '.join(value)
    
    if type == 'int':
        new_value = int(value)
        
    elif type == 'bool':
        new_value = bool(value)
        
    elif type == 'list':
        new_value = value.split(',')
    
    else:
        new_value = value
    
    
    object.set(key, new_value)
    object.save()
    
    logger.info('%s.%s set to %s', identifier, key, new_value)
    
    bot.init(runlevel.HALT)
Example #3
0
def config_read(args):
    config_root = os.environ['FPTBOT_CONFIG']
    
    bot = Bot(root=config_root, level=logging.INFO)
    bot.init(runlevel.LOCAL_FILESYSTEM)
    
    logger = logging.getLogger('tools.config')
    
    identifier = args.identifier
    
    def _read(identifier, key, value, padding=0):
        print "{}.{} {}= '{}' ({})".format(identifier, key, ' ' * padding, value, type(value))
    
    try:
        object = bot.get_config(identifier)
    except KeyError:
        logger.critical('invalid identifier: %s', identifier)
        return
        
    if(args.key is None):
        spacelist = [len(item) for item in object.get_all().keys()]
        
        if spacelist:
            space = max(spacelist)
        else:
            space = 0
        
        for (key, value) in object.get_all().items():
            _read(identifier, key, value, (space - len(key)))
        
    else:
        try:
            key = args.key
            value = object.get(key)
        except KeyError:
            logger.critical('invalid key: %s', key)
            return
            
        _read(identifier, key, value)
    
    bot.init(runlevel.HALT)