<commit_before>import collections
import copy
import json
import os
import random
import string
import shutil
import tempfile
import time

import jinja2
import lxml.html
import lxml.html.builder
import requests

from constants import *
import chapter


class _Minetype():

    def __init__(self, parent_directory):
        minetype_template = os.path.join(EPUB_TEMPLATES_DIR, 'minetype.txt')
        shutil.copy(minetype_template,
                os.path.join(parent_directory, 'minetype.txt'))


class _ContainerFile():

    def __init__(self, parent_directory):
        container_template = os.path.join(EPUB_TEMPLATES_DIR, 'container.xml')
        shutil.copy(container_template,
                os.path.join(parent_directory, 'container.xml'))


class _EpubFile(object):

    def __init__(self, template_file, **non_chapter_parameters):
        self.content = u''
        self.file_name = ''
        self.template_file = template_file
        self.non_chapter_parameters = non_chapter_parameters

    def write(self, file_name):
        self.file_name = file_name
        with open(file_name, 'wb') as f:
            f.write(self.content.encode('utf-8'))

    def _renderTemplate(self, **variable_value_pairs):
        def readTemplate():
            with open(self.template_file, 'r') as f:
                template = f.read().decode('utf-8')
            return jinja2.Template(template)
        template = readTemplate()
        rendered_template = template.render(variable_value_pairs)
        self.content = rendered_template

    def add_chapters(self, **parameter_lists):
        def checkListLengths(lists):
            list_length = None
            for value in lists.values():
                assert isinstance(value, list)
                if list_length is None:
                    list_length = len(value)
                else:
                    assert len(value) == list_length
        checkListLengths(parameter_lists)
        template_chapter = collections.namedtuple('template_chapter', parameter_lists.keys())
        chapters = [template_chapter(*items) for items in zip(*parameter_lists.values())]
        self._renderTemplate(chapters=chapters, **self.non_chapter_parameters)

    def get_content(self):
        return self.content


class _TOC_HTML(_EpubFile):

    def __init__(self,
            template_file=os.path.join(EPUB_TEMPLATES_DIR, 'toc.html'),
            **non_chapter_parameters):
        super(_TOC_HTML, self).__init__(template_file, **non_chapter_parameters)

    def add_chapters(self, chapter_list):
        chapter_numbers = range(len(chapter_list))
        link_list = [str(n) + '.xhtml' for n in chapter_numbers]
        chapter_titles = [chapter.title for chapter in chapter_list]
        super(_TOC_HTML, self).add_chapters(title=chapter_titles, link=link_list)

    def get_content_as_element(self):
        root = lxml.html.fromstring(self.content.encode('utf-8'))
        return root


class _TOC_NCX(_EpubFile):

    def __init__(self,
            template_file=os.path.join(EPUB_TEMPLATES_DIR, 'toc_ncx.xml'),
            **non_chapter_parameters):
        super(_TOC_NCX, self).__init__(template_file, **non_chapter_parameters)

    def add_chapters(self, chapter_list):
        id_list = range(len(chapter_list))
        play_order_list = [n + 1 for n in id_list]
        title_list = [chapter.title for chapter in chapter_list]
        link_list = [str(n) + '.xhtml' for n in id_list]
        super(_TOC_NCX, self).add_chapters(**{'id': id_list,
                'play_order': play_order_list,
                'title': title_list,
                'link': link_list})

    def get_content_as_element(self):
        root = lxml.etree.fromstring(self.content.encode('utf-8'))
        return root


class _Content_OPF(_EpubFile):

    def __init__(self, title, creator='', language='', rights='',
            publisher='', uid='', date = time.strftime("%m-%d-%Y")):
        super(_Content_OPF, self).__init__(os.path.join(EPUB_TEMPLATES_DIR, 'opf.xml'),
                title=title,
                creator=creator,
                language=language,
                rights=rights,
                publisher=publisher,
                uid=uid,
                date=date)

    def add_chapters(self, chapter_list):
        id_list = range(len(chapter_list))
        link_list = [str(n) + '.xhtml' for n in id_list]
        super(_Content_OPF, self).add_chapters(**{'id': id_list,
                'link': link_list})

    def get_content_as_element(self):
        root = lxml.etree.fromstring(self.content.encode('utf-8'))
        return root


class Epub():

    def __init__(self, output_directory, title, creator='',
            language='en', rights='', publisher='Cember Corporation'):
        def create_directories():<commit_msg>styling<commit_after>import collections
import copy
import json
import os
import random
import string
import shutil
import tempfile
import time

import jinja2
import lxml.html
import lxml.html.builder
import requests

from constants import *
import chapter


class _Minetype():

    def __init__(self, parent_directory):
        minetype_template = os.path.join(EPUB_TEMPLATES_DIR, 'minetype.txt')
        shutil.copy(minetype_template,
                os.path.join(parent_directory, 'minetype.txt'))


class _ContainerFile():

    def __init__(self, parent_directory):
        container_template = os.path.join(EPUB_TEMPLATES_DIR, 'container.xml')
        shutil.copy(container_template,
                os.path.join(parent_directory, 'container.xml'))


class _EpubFile(object):

    def __init__(self, template_file, **non_chapter_parameters):
        self.content = u''
        self.file_name = ''
        self.template_file = template_file
        self.non_chapter_parameters = non_chapter_parameters

    def write(self, file_name):
        self.file_name = file_name
        with open(file_name, 'wb') as f:
            f.write(self.content.encode('utf-8'))

    def _renderTemplate(self, **variable_value_pairs):
        def readTemplate():
            with open(self.template_file, 'r') as f:
                template = f.read().decode('utf-8')
            return jinja2.Template(template)
        template = readTemplate()
        rendered_template = template.render(variable_value_pairs)
        self.content = rendered_template

    def add_chapters(self, **parameter_lists):
        def checkListLengths(lists):
            list_length = None
            for value in lists.values():
                assert isinstance(value, list)
                if list_length is None:
                    list_length = len(value)
                else:
                    assert len(value) == list_length
        checkListLengths(parameter_lists)
        template_chapter = collections.namedtuple('template_chapter',
                parameter_lists.keys())
        chapters = [template_chapter(*items) for items in zip(*parameter_lists.values())]
        self._renderTemplate(chapters=chapters, **self.non_chapter_parameters)

    def get_content(self):
        return self.content


class _TOC_HTML(_EpubFile):

    def __init__(self,
            template_file=os.path.join(EPUB_TEMPLATES_DIR, 'toc.html'),
            **non_chapter_parameters):
        super(_TOC_HTML, self).__init__(template_file,
                **non_chapter_parameters)

    def add_chapters(self, chapter_list):
        chapter_numbers = range(len(chapter_list))
        link_list = [str(n) + '.xhtml' for n in chapter_numbers]
        chapter_titles = [chapter.title for chapter in chapter_list]
        super(_TOC_HTML, self).add_chapters(title=chapter_titles,
                link=link_list)

    def get_content_as_element(self):
        root = lxml.html.fromstring(self.content.encode('utf-8'))
        return root


class _TOC_NCX(_EpubFile):

    def __init__(self,
            template_file=os.path.join(EPUB_TEMPLATES_DIR, 'toc_ncx.xml'),
            **non_chapter_parameters):
        super(_TOC_NCX, self).__init__(template_file, **non_chapter_parameters)

    def add_chapters(self, chapter_list):
        id_list = range(len(chapter_list))
        play_order_list = [n + 1 for n in id_list]
        title_list = [chapter.title for chapter in chapter_list]
        link_list = [str(n) + '.xhtml' for n in id_list]
        super(_TOC_NCX, self).add_chapters(**{'id': id_list,
                'play_order': play_order_list,
                'title': title_list,
                'link': link_list})

    def get_content_as_element(self):
        root = lxml.etree.fromstring(self.content.encode('utf-8'))
        return root


class _Content_OPF(_EpubFile):

    def __init__(self, title, creator='', language='', rights='',
            publisher='', uid='', date = time.strftime("%m-%d-%Y")):
        super(_Content_OPF, self).__init__(os.path.join(EPUB_TEMPLATES_DIR,
                'opf.xml'),
                title=title,
                creator=creator,
                language=language,
                rights=rights,
                publisher=publisher,
                uid=uid,
                date=date)

    def add_chapters(self, chapter_list):
        id_list = range(len(chapter_list))
        link_list = [str(n) + '.xhtml' for n in id_list]
        super(_Content_OPF, self).add_chapters(**{'id': id_list,
                'link': link_list})

    def get_content_as_element(self):
        root = lxml.etree.fromstring(self.content.encode('utf-8'))
        return root


class Epub():

    def __init__(self, output_directory, title, creator='',
            language='en', rights='', publisher='Cember Corporation'):
        def create_directories():
{'fraction_non_alphanumeric': 0.1076986926748288, 'fraction_numerical': 0.0016600954554886907, 'mean_word_length': 2.6860420650095604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, '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': '6049687', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2832, 'n_words': 563}