<commit_before>from hitchhttp.mock_rest_uri import MockRestURI
from jinja2.environment import Environment
from jinja2 import FileSystemLoader, exceptions
from os import path
import yaml
import re
import sys
import os


class MockRestConfig(object):
    def __init__(self, filename):
        """Load config from YAML file."""
        if filename is None:
            self._config = []
        else:
            try:
                env = Environment()
                env.loader = FileSystemLoader(path.split(filename)[0])
                try:
                    template = env.get_template(path.split(filename)[1])
                except exceptions.TemplateError as error:
                    sys.stderr.write("Jinja2 template error in '{}' on line {}:\n==> {}\n".format(
                        error.filename, error.lineno, str(error)
                    ))
                    sys.exit(1)

                self._config = yaml.load(template.render())
            except Exception as e:
                sys.stderr.write("Error rendering jinja2/reading yaml config file: {0}\n".format(str(e)))
                sys.exit(1)

            # Read and store all references to external content files
            for pair in self._config:
                content = pair.get('response', {}).get('content')
                if "file" in content:
                    with open(path.join(path.dirname(filename), content['file']), 'r') as content_file_handle:
                        pair['response']['content'] = content_file_handle.read()

    def get_matching_uri(self, request):
        """Get a URI from the config with a specific method/path (or return None if nothing matches)."""
        # TODO: Issue warning when a second or third URI matches the first.
        for uri in self.all_uris():
            if uri.match(request):
                return uri
        return None

    def groups(self):
        """Get all groups and the URIs inside them as a list of dictionaries containing name, description and MockRestURI object."""
        all_groups = []
        for group_dict in self._config:
            mock_rest_uris = [MockRestURI(x) for x in group_dict.get('uris', [])]
            group_dict['uris'] = mock_rest_uris
            all_groups.append(group_dict)
        return all_groups

    def all_uris(self):
        uri_list = []
        for pair in self._config:
            uri_list.append(MockRestURI(pair))
            #for uri_dict in group_dict.get('uris', []):
                #uri_list.append(MockRestURI(uri_dict))
        return uri_list
<commit_msg>BUG : Fixed CLI output on invalid YAML.<commit_after>from hitchhttp.mock_rest_uri import MockRestURI
from jinja2.environment import Environment
from jinja2 import FileSystemLoader, exceptions
from os import path
import yaml
import re
import sys
import os


class MockRestConfig(object):
    def __init__(self, filename):
        """Load config from YAML file."""
        if filename is None:
            self._config = []
        else:
            try:
                env = Environment()
                env.loader = FileSystemLoader(path.split(filename)[0])
                try:
                    template = env.get_template(path.split(filename)[1])
                except exceptions.TemplateError as error:
                    sys.stderr.write("Jinja2 template error in '{}' on line {}:\n==> {}\n".format(
                        error.filename, error.lineno, str(error)
                    ))
                    sys.exit(1)

                self._config = yaml.load(template.render())
            except Exception as e:
                sys.stderr.write("Error reading YAML config file: {0}\n".format(str(e)))
                sys.exit(1)

            # Read and store all references to external content files
            for pair in self._config:
                content = pair.get('response', {}).get('content')
                if "file" in content:
                    with open(path.join(path.dirname(filename), content['file']), 'r') as content_file_handle:
                        pair['response']['content'] = content_file_handle.read()

    def get_matching_uri(self, request):
        """Get a URI from the config with a specific method/path (or return None if nothing matches)."""
        # TODO: Issue warning when a second or third URI matches the first.
        for uri in self.all_uris():
            if uri.match(request):
                return uri
        return None

    def groups(self):
        """Get all groups and the URIs inside them as a list of dictionaries containing name, description and MockRestURI object."""
        all_groups = []
        for group_dict in self._config:
            mock_rest_uris = [MockRestURI(x) for x in group_dict.get('uris', [])]
            group_dict['uris'] = mock_rest_uris
            all_groups.append(group_dict)
        return all_groups

    def all_uris(self):
        uri_list = []
        for pair in self._config:
            uri_list.append(MockRestURI(pair))
            #for uri_dict in group_dict.get('uris', []):
                #uri_list.append(MockRestURI(uri_dict))
        return uri_list

{'fraction_non_alphanumeric': 0.09791707222114074, 'fraction_numerical': 0.0033093245084679776, 'mean_word_length': 1.9889470622454917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1261316', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1318, 'n_words': 405}