import yaml
import sys, logging


def parse_yaml_file(config_path):
    try:
        f = open(config_path,'r')
        return yaml.load(f)
    except Exception as e:
        logging.error('Error while parsing config file', config_path,
                        'Exception:', e)

def retrieve_repos(config):
    try:
        repos = []
        if 'repos' in config:
            [repos.append(r) for r in config['repos']]
        return repos
    except Exception as e:
        logging.error('Error while retrieving repos item', e)

def extract_post_filters(repo):
            return repo.get('postfilters')

def extract_api_filters(repo):
     try:
        filters = {}
        for f in repo['filters']:
            f_value = f['value']
            filters.setdefault(f['type'], []).append(f_value)
        return filters
     except Exception as e:
        logging.error('Error while extracting filter from repo item', e)

