<commit_before>#!/usr/bin/env python
''' yamlcfg.yml

Parse YAML configs
'''

import os
import yaml

from yamlcfg.conf import Config
from yamlcfg.util import validate_ext

def is_yaml(path):
    return validate_ext(path, ('yaml', 'yml'))

class YAMLConfig(Config):
    
    def __init__(self, *args, **kwargs):
        '''
        Create a config instance

        :param path: loads config at path (default behavior)
        :param paths: specify paths to load in succession, if they exist,
            from most authoritative to least.
        :param permute: If True, permutes paths to other possible lower-case
            extensions. eg: `foo.yml` would create a check for `foo.yaml` as
            well, and `foo` would check `foo`, `foo.yml` and `foo.yaml`
        :return: None
        '''
        super(YAMLConfig, self).__init__(*args, **kwargs)
        if self._path or self._paths:
            self.open(path=self._path, paths=self._paths, **kwargs)

    def open(self, path=None, paths=None, permute=False, **kwargs):
        super(YAMLConfig, self).open(path=path, paths=paths)
        if paths is not None:
            self.parse_paths(paths, permute=permute)
        if path is not None:
            self.parse_path(path)

    def parse_path(self, path):
        with open(path) as f:
            self._data.update(yaml.load(f))

    def permuted_paths(self, path):
        fname, ext = os.path.splitext(path)
        if ext == '.yml':
            return [path, fname + '.yaml']
        elif ext == '.yaml':
            return [path, fname + '.yml']
        elif ext == '':
            return [path, path + '.yml', path + '.yaml']<commit_msg>name keyword feature<commit_after>#!/usr/bin/env python
''' yamlcfg.yml

Parse YAML configs
'''

import os
import yaml

from yamlcfg.conf import Config
from yamlcfg.util import validate_ext

def is_yaml(path):
    return validate_ext(path, ('yaml', 'yml'))

class YAMLConfig(Config):
    
    def __init__(self, *args, **kwargs):
        '''
        Create a config instance

        :param path: loads config at path (default behavior)
        :param paths: specify paths to load in succession, if they exist,
            from most authoritative to least.
        :param name: Generate all default paths from a name
            eg. name='foo', then paths would be:
            ('./.foo{,.yml,.yaml}', '~/.foo{,.yml,.yaml}',
            '~/.config/foo/config{,.yml,.yaml}',
            '/etc/foo/config{,.yml,.yaml}')
        :param config_name: replace "config" in above paths with `config_name`
        :param permute: If True, permutes paths to other possible lower-case
            extensions. eg: `foo.yml` would create a check for `foo.yaml` as
            well, and `foo` would check `foo`, `foo.yml` and `foo.yaml`
        :return: None
        '''
        super(YAMLConfig, self).__init__(*args, **kwargs)
        if kwargs.get('name'):
            self._paths = self.paths_from_name(
                kwargs['name'],
                config_name=kwargs.get('config_name', 'config')
            )
            kwargs['permute'] = True
        if self._path or self._paths:
            self.open(path=self._path, paths=self._paths, **kwargs)

    def paths_from_name(self, name, config_name='config'):
        return [
            x.format(name=name, config=config_name)
            for x in
            (
                './.{name}', '~/.{name}', '~/.config/{name}/{config}', 
                '/etc/{name}/{config}',
            )
        ]

    def open(self, path=None, paths=None, permute=False, **kwargs):
        super(YAMLConfig, self).open(path=path, paths=paths)
        if paths is not None:
            self.parse_paths(paths, permute=permute)
        if path is not None:
            self.parse_path(path)

    def parse_path(self, path):
        with open(path) as f:
            self._data.update(yaml.load(f))

    def permuted_paths(self, path):
        fname, ext = os.path.splitext(path)
        if ext == '.yml':
            return [path, fname + '.yaml']
        elif ext == '.yaml':
            return [path, fname + '.yml']
        elif ext == '':
            return [path, path + '.yml', path + '.yaml']
{'fraction_non_alphanumeric': 0.13344473621389352, 'fraction_numerical': 0.0, 'mean_word_length': 2.1622641509433964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '568323', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1235, 'n_words': 366}