<commit_before>
from string import Template
from . import ast

"""
Note : Binary search inside directives should be implemented soon to improve
        performance.
"""

class Environment(dict):
    def update(self, *args, **kwargs):
        super().update(*args, **kwargs)
        return self
    
    def __getattribute__(self, attr):
        try:
            return super().__getattribute__(attr)
        except AttributeError:
            try:
                return self[attr]
            except KeyError:
                raise AttributeError
    
    def __setattr__(self, attr, value):
        if not hasattr(self, attr) and attr in self:
            self[attr] = value
        else:
            super().__setattr__(attr, value)



NstlDefaultEnvironment = Environment(
    nstl_depth = 'NSTL_DEPTH',
    nstl_unique = 'NSTL_UNIQUE',
    nstl_depth_incr = '#include <params/depth/incr.h>',
    nstl_depth_decr = '#include <params/depth/decr.h>',
    maxdepth = 10,<commit_msg>Create the StructuredEmitter class.<commit_after>
from string import Template
from . import ast

"""
Note : Binary search inside directives should be implemented soon to improve
        performance.
"""

class Environment(dict):
    def update(self, *args, **kwargs):
        super().update(*args, **kwargs)
        return self
    
    def __getattribute__(self, attr):
        try:
            return super().__getattribute__(attr)
        except AttributeError:
            try:
                return self[attr]
            except KeyError:
                raise AttributeError
    
    def __setattr__(self, attr, value):
        if not hasattr(self, attr) and attr in self:
            self[attr] = value
        else:
            super().__setattr__(attr, value)
class StructuredEmitter(object):
    """A class to handle emitting output to a stream in a structured way.
    """
    def __init__(self, ostream=sys.stdout, indent=' '*4):
        self._ostream = ostream
        self._knownstreams = { }
        self._indent_level = 0
        self._indent_str = indent
    
    
    def setstream(self, ostream, reset=True):
        """Remembers the current stream and its associated state.
        If reset is True and the stream was not previously used with this
        emitter, the indentation level starts over at 0. In all cases, if
        the stream was previously used with this emitter, state in which the
        stream was left is restored. The resetindent() method can be called
        in order to manually reset the state.
        """
        self._knownstreams[self._ostream] = self._indent_level
        if ostream in self._knownstreams:
            self._indent_level = self._knownstreams[ostream]
        elif reset:
            self.resetindent()
        self._ostream = ostream
    
    
    def emit(self, text, newline=True):
        self._ostream.write(self.indentation() + text + '\n' if newline else'')
    
    
    def emit_indented(self, text, newline=True):
        self.indent()
        self.emit(text, newline)
        self.dedent()
    
    
    def indentation(self):
        return self._indent_level * self.indent_str()
    
    
    def resetindent(self):
        self._indent_level = 0
    
    
    def indent_str(self):
        return self._indent_str
    
    
    def indent(self):
        """Add an indent level to the emitted code.
        """
        self._indent_level += 1
    
    
    def dedent(self):
        """Remove an indent level to the emitted code.
        """
        self._indent_level -= 1




NstlDefaultEnvironment = Environment(
    nstl_depth = 'NSTL_DEPTH',
    nstl_unique = 'NSTL_UNIQUE',
    nstl_depth_incr = '#include <params/depth/incr.h>',
    nstl_depth_decr = '#include <params/depth/decr.h>',
    maxdepth = 10,
{'fraction_non_alphanumeric': 0.09194488606253312, 'fraction_numerical': 0.002649708532061473, 'mean_word_length': 2.207306711979609, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3974228', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1063, 'n_words': 316}