<commit_before>'''Exceedingly simple interface for programmatically generating reStructuredText
documents.'''

import sys
import textwrap


class ReStUtilException(Exception) : pass


class BaseReSt(object) :
    '''Base reStructuredText component, does nothing, should be subclassed with
    *build_text* method overridden. Components are expected to add their own
    newline character at the end of their text'''

    def __init__(self) :
        self.text = ''

    def get_text(self) :
        self.build_text()
        return self.text

    def build_text(self) :
        pass


class ReStDocument(BaseReSt) :
    '''Basic Document class, used to collect reStructuredText classes to produce a
    single output file.  Add a component by appending to the *components* member,
    alternatively use the .add() method. Constructor accepts either a file-like 
    object or a filename.'''

    def __init__(self,f) :
        BaseReSt.__init__(self)

        # check for file-like object
        if hasattr(f,'write') :
            self._f = f
        # check for filename
        elif isinstance(f,str) :
            self._f = open(f,'w')
        else :
            raise ReStUtilException('Unrecognized parameter format to ReStDocument, \
                                     must either have a .write(str) method or be a \
                                     filename')
        self.components = []
        self.add("\n")

    def build_text(self) :
        self.text = ''.join([x.get_text()+'\n' for x in self.components])

    def add(self,component) :
        '''Add the component to the end of the document.  Component is either a
        BaseReSt subclass instance or a string.  In the latter case, the string
        is wrapped in a ReStText object for maximal convenience'''

        # convenience case, adding a string wraps string in ReStText object
        if isinstance(component,str) :
            component = ReStText(component)
        self.components.append(component)

    def write(self) :
        self._f.write(self.get_text())
<commit_msg>added unicode as acceptable argument to ReStDocument<commit_after>'''Exceedingly simple interface for programmatically generating reStructuredText
documents.'''

import sys
import textwrap


class ReStUtilException(Exception) : pass


class BaseReSt(object) :
    '''Base reStructuredText component, does nothing, should be subclassed with
    *build_text* method overridden. Components are expected to add their own
    newline character at the end of their text'''

    def __init__(self) :
        self.text = ''

    def get_text(self) :
        self.build_text()
        return self.text

    def build_text(self) :
        pass


class ReStDocument(BaseReSt) :
    '''Basic Document class, used to collect reStructuredText classes to produce a
    single output file.  Add a component by appending to the *components* member,
    alternatively use the .add() method. Constructor accepts either a file-like 
    object or a filename.'''

    def __init__(self,f) :
        BaseReSt.__init__(self)

        # check for file-like object
        if hasattr(f,'write') :
            self._f = f
        # check for filename
        elif isinstance(f,str) or isinstance(f,unicode) :
            self._f = open(f,'w')
        else :
            raise ReStUtilException('Unrecognized parameter format to ReStDocument, \
                                     must either have a .write(str) method or be a \
                                     filename')
        self.components = []
        self.add("\n")

    def build_text(self) :
        self.text = ''.join([x.get_text()+'\n' for x in self.components])

    def add(self,component) :
        '''Add the component to the end of the document.  Component is either a
        BaseReSt subclass instance or a string.  In the latter case, the string
        is wrapped in a ReStText object for maximal convenience'''

        # convenience case, adding a string wraps string in ReStText object
        if isinstance(component,str) :
            component = ReStText(component)
        self.components.append(component)

    def write(self) :
        self._f.write(self.get_text())

{'fraction_non_alphanumeric': 0.08186157517899761, 'fraction_numerical': 0.0, 'mean_word_length': 2.4100895036615135, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5161973', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1089, 'n_words': 394}