def load_yaml(file_path, **options):
    yaml_file = _yamls(file_path)

    # If the encrypted yaml doesn't exist, bail out
    if not os.path.exists(yaml_file.encrypted):
        return

    # If there's an unencyrpted yaml, issue a warning and bail out
    if os.path.exists(yaml_file.unencrypted):
        warn_msg = ('yaml "{}" and eyaml present for "{}" config. '
            'Ignoring encrypted yaml.').format(*yaml_file)
        warnings.warn(warn_msg, YayclCryptWarning)
        return

    # set up the crypt!
    cipher = crypt_cipher(**options)

    # Sanity achieved; attempt decryption
    loaded_yaml = lya.AttrDict()
    with open(yaml_file.encrypted, 'rb') as eyaml:
        decrypted_yaml = cipher.decrypt(eyaml.read())
    try:
        loaded_conf = yaml.load(decrypted_yaml, Loader=lya.OrderedDictYAMLLoader)
    except Exception as exc:
        msg = '{} when loading {}, yaycl crypt key may be incorrect. Original traceback:\n{}'
        raise YayclCryptError(msg.format(type(exc), yaml_file.encrypted, exc))
    loaded_yaml.update(loaded_conf)

    return loaded_yaml
Exemplo n.º 2
0
def configure_db(settings):
    """Configure database."""
    if 'slaves' in settings.db:
        return

    pool_size = 50
    max_overflow = 49

    settings.db['slaves'] = [
        lya.AttrDict({
            'dsn': settings.db.master['dsn'],
            'pool_size': pool_size,
            'max_overflow': max_overflow
        })
    ]
Exemplo n.º 3
0
    def override(self, options):
        """Override setting within context.

        with settings.override(foo='test', bar=42):
            <new settings here>
        <old settings here>

        """
        self._load_settings()

        old_settings = deepcopy(self._settings)
        self._settings.update_dict(lya.AttrDict(options))

        try:
            yield
        finally:
            self._settings = old_settings
Exemplo n.º 4
0
def _yaml_load(filename, gitshow):

    od = None
    template_module = None

    if gitshow:
        default = "vacant: true"
        try:
            data = output_cmd('git', GIT_OPTIONS, 'show HEAD:' + filename)
            if data == '':
                data = default 
            else:
                template_module = get_template_module(StringIO(data).readline())
        except CmdError:
            data = default 
        od = yaml.load(data, lya.OrderedDictYAMLLoader)
    else:
        with open(os.path.join(WORK_TREE, filename), 'r') as f:
            od = yaml.load(f, lya.OrderedDictYAMLLoader)
            f.seek(0)
            template_module = get_template_module(f.readline())

    types = None
    module = None
    if template_module:
        __import__(template_module)
        module = sys.modules[template_module]
        types = module.placeholder_types()

    base = lya.AttrDict(od)

    flatten = lya.AttrDict.flatten(base)
    
    values = []
    state_order = [] 
    for l in flatten:
        try:
            path = l[0][1]
            if path not in state_order:
                state_order.append(path)
        except:
            pass
        path = '' 
        length = len(l[0])
        for i in range(length): 
            m = l[0][i]
            if path == '':
                path = m
            else:
                if i != length-1:
                    path += '.'+m
                elif len(l[0]) == 2 and type(l[1]) is list:
                    count = 0
                    for elm in l[1]:
                        if type(elm) is OrderedDict:
                            dic = dict(elm)
                            index = None 
                            try:
                                key = INDEXES[m]
                            except:
                                pass
                            if key != None:
                                value = dic[key]
                                index = key + ':' + dumps(value, types)    
                            else:
                                index = str(count)
                            for key in dic:
                                pl = path+'.'+m+'['+index+'].'+key+'='+dumps(dic[key], types)

                                values.append(pl)
                        else: 
                            pl = path+'.'+m+'['+str(count)+']='+dumps(elm, types)
                            values.append(pl)
                            
                        count += 1
                else:
                    path += '.'+m+'='+dumps(l[1], types)
                    values.append(path)
    
    if template_module:
        values = module.fillout(values)

    return (sorted(values), STATE_ORDER)