<commit_before>"""Functionality for handling the logic
of traversing and working with a project.
"""

import os
import imp
import codecs
from copy import deepcopy
from coati import excel, merge, powerpoint
from coati.resources import factory
from coati.utils import flatten

def loadcode(path, name):
    module = imp.new_module(name)
    code = codecs.open(path, 'r', 'utf-8').read()
    exec code in module.__dict__
    return module


def filllabels(resources, funmodule):
    new_resources = deepcopy(resources)

    def callfn(label):
        return getattr(funmodule, label)()

    new_resources['labels'] = [(label, callfn(label)) for label
                               in resources.get('labels', [])]

    return new_resources


class SlideBuilder(object):
    """Manage the logic for extracting
    information for the current configuration
    of a given slide and build it into a document instance"""

    def __init__(self, template_pptx, path):
        self.template_pptx = template_pptx
        self.path = path

    @property
    def pptx(self):
        if not hasattr(self, '_pptx'):
            self._pptx = self.template.pptx_from_template()
        return self._pptx

    @property
    def template(self):
        if not hasattr(self, '_template'):
            self._template = powerpoint.SlideTemplate(self.template_pptx)
        return self._template

    def loadtemplate(self):
        if not hasattr(self, '_pptx'):
            self._pptx = self.template.pptx_from_template()

    def loadconfig(self, fname):
        config = self._attemptload(fname)
        if not config or not config.slides or type(config.slides) is not list:
            self.slidesdata = []
            return None
        self.slidesdata = flatten(config.slides)
        return config

    def _attemptload(self, fname):
        path = os.path.join(self.path, fname)
        if not os.path.isfile(path):
            return None

        module_name = fname.split('.')[0]
        return loadcode(path, module_name)

    def fillexcel(self):
        xlsx_src = os.path.join(self.path, 'slide.xlsx')

        if not os.path.isfile(xlsx_src):
            return None

        self.excelapp = excel.runexcel()
        self.xlsx = excel.open_xlsx(self.excelapp, xlsx_src)

        fillmodule = self._attemptload('fill.py')

        if fillmodule:
            fillmodule.fill(self.xlsx)

    def closexcel(self):
        if hasattr(self, 'xlsx'):
            self.excelapp.CutCopyMode = False
            self.xlsx.Close()

    def loadresources(self):
        return ((number, self.prepareresource(params)) for number, params in self.slidesdata)

    def prepareresource(self, params):
        if type(params) is not list:
            params = [params]
        return (factory(key, dict_[key]) for dict_ in params for key in dict_)

<commit_msg>refactor resource factory<commit_after>"""Functionality for handling the logic
of traversing and working with a project.
"""

import os
import imp
import codecs
import logging
from copy import deepcopy
from coati import excel, merge, powerpoint
from coati.resources import factory
from coati.utils import flatten

def loadcode(path, name):
    module = imp.new_module(name)
    code = codecs.open(path, 'r', 'utf-8').read()
    exec code in module.__dict__
    return module


def filllabels(resources, funmodule):
    new_resources = deepcopy(resources)

    def callfn(label):
        return getattr(funmodule, label)()

    new_resources['labels'] = [(label, callfn(label)) for label
                               in resources.get('labels', [])]

    return new_resources


class SlideBuilder(object):
    """Manage the logic for extracting
    information for the current configuration
    of a given slide and build it into a document instance"""

    def __init__(self, template_pptx, path):
        self.template_pptx = template_pptx
        self.path = path

    @property
    def pptx(self):
        if not hasattr(self, '_pptx'):
            self._pptx = self.template.pptx_from_template()
        return self._pptx

    @property
    def template(self):
        if not hasattr(self, '_template'):
            self._template = powerpoint.SlideTemplate(self.template_pptx)
        return self._template

    def loadtemplate(self):
        if not hasattr(self, '_pptx'):
            self._pptx = self.template.pptx_from_template()

    def loadconfig(self, fname):
        config = self._attemptload(fname)
        if not config or not config.slides or type(config.slides) is not list:
            self.slidesdata = []
            logging.warning("config file no valid")
            return None
        self.slidesdata = flatten(config.slides)
        return config

    def _attemptload(self, fname):
        path = os.path.join(self.path, fname)
        if not os.path.isfile(path):
            return None

        module_name = fname.split('.')[0]
        return loadcode(path, module_name)

    def fillexcel(self):
        xlsx_src = os.path.join(self.path, 'slide.xlsx')

        if not os.path.isfile(xlsx_src):
            return None

        self.excelapp = excel.runexcel()
        self.xlsx = excel.open_xlsx(self.excelapp, xlsx_src)

        fillmodule = self._attemptload('fill.py')

        if fillmodule:
            fillmodule.fill(self.xlsx)

    def closexcel(self):
        if hasattr(self, 'xlsx'):
            self.excelapp.CutCopyMode = False
            self.xlsx.Close()

    def loadresources(self):
        return ((number, self.prepareresource(params)) for number, params in self.slidesdata)

    def prepareresource(self, params):
        if type(params) is not list:
            params = [params]
        return (factory(key, dict_[key]) for dict_ in params for key in dict_)


{'fraction_non_alphanumeric': 0.09709918360257078, 'fraction_numerical': 0.000694806322737537, 'mean_word_length': 2.5675340768277573, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4354017', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1680, 'n_words': 466}