def _render_document(self, document_template, output_format, sub_documents):
        """
        Render a single document of type 'output_format' using the odt file
        'document_template' as the generation template.
        Subdocuments is a dictionnary of previously generated subtemplate
        that will be merged into the current generated document.
        """
        temp_filename = tempfile.mktemp('.{extension}'.format(extension=output_format))

        # Prepare rendering context
        helper_view = self.get_generation_context_helper()
        generation_context = self._get_generation_context(helper_view)
        # enrich the generation context with previously generated documents
        generation_context.update(sub_documents)

        renderer = appy.pod.renderer.Renderer(
            StringIO(document_template.data),
            generation_context,
            temp_filename,
            pythonWithUnoPath=config.get_uno_path(),
        )

        # it is only now that we can initialize helper view's appy pod renderer
        helper_view._set_appy_renderer(renderer)

        renderer.run()

        return temp_filename
Exemplo n.º 2
0
    def _render_document(self, document_template, output_format,
                         sub_documents):
        """
        Render a single document of type 'output_format' using the odt file
        'document_template' as the generation template.
        Subdocuments is a dictionnary of previously generated subtemplate
        that will be merged into the current generated document.
        """
        temp_filename = tempfile.mktemp(
            '.{extension}'.format(extension=output_format))

        # Prepare rendering context
        helper_view = self.get_generation_context_helper()
        generation_context = self._get_generation_context(helper_view)
        # enrich the generation context with previously generated documents
        generation_context.update(sub_documents)

        renderer = appy.pod.renderer.Renderer(
            StringIO(document_template.data),
            generation_context,
            temp_filename,
            pythonWithUnoPath=config.get_uno_path(),
        )

        # it is only now that we can initialize helper view's appy pod renderer
        helper_view._set_appy_renderer(renderer)

        renderer.run()

        return temp_filename
Exemplo n.º 3
0
def create_pages(players, template, output_basename, uno_path,
                 output_directory='/tmp/'):
    """Create pages as individual files. The file format is decided from the
    output file suffix.

    :param players: list of players
    :param template: template file path
    :param output_basename: base name for output files
    :param output_directory: directory where outfiles are created
    :returns: list of filenames
    """
    page_num = 0
    player_num = 0
    page_list = []
    while player_num < len(players):
        page_output_file_name = ''.join(output_basename.split('.')[:-1]) \
            + '_' + str(page_num) + '.' + output_basename.split('.')[-1]
        page_output_path = os.path.join(output_directory,
                                        page_output_file_name)
        if os.path.exists(page_output_path):
            raise IOError(
                "File \"{}\" already exists".format(page_output_path)
            )
        page_list.append(page_output_path)
        renderer = appy.pod.renderer.Renderer(
            template,
            {'players':players, 'player_num':player_num},
            page_output_path,
            pythonWithUnoPath=uno_path
        )
        renderer.run()
        page_num = page_num+1
        player_num = page_num*PLAYERS_PER_PAGE

    return page_list
Exemplo n.º 4
0
 def copyFile(self,
              fileName,
              replacements,
              destName=None,
              destFolder=None,
              isPod=False):
     '''This method will copy p_fileName from self.templatesFolder to
        self.outputFolder (or in a subFolder if p_destFolder is given)
        after having replaced all p_replacements. If p_isPod is True,
        p_fileName is a POD template and the copied file is the result of
        applying p_fileName with context p_replacements.'''
     # Get the path of the template file to copy
     templatePath = os.path.join(self.templatesFolder, fileName)
     # Get (or create if needed) the path of the result file
     destFile = fileName
     if destName: destFile = destName
     if destFolder: destFile = '%s/%s' % (destFolder, destFile)
     absDestFolder = self.outputFolder
     if destFolder:
         absDestFolder = os.path.join(self.outputFolder, destFolder)
     if not os.path.exists(absDestFolder):
         os.makedirs(absDestFolder)
     resultPath = os.path.join(self.outputFolder, destFile)
     if os.path.exists(resultPath): os.remove(resultPath)
     if not isPod:
         # Copy the template file to result file after having performed some
         # replacements
         f = file(templatePath)
         fileContent = f.read()
         f.close()
         if not fileName.endswith('.png'):
             for rKey, rValue in replacements.iteritems():
                 fileContent = fileContent.replace('<!%s!>' % rKey,
                                                   str(rValue))
         f = file(resultPath, 'w')
         f.write(fileContent)
         f.close()
     else:
         # Call the POD renderer to produce the result
         rendererParams = {
             'template': templatePath,
             'context': replacements,
             'result': resultPath
         }
         renderer = appy.pod.renderer.Renderer(**rendererParams)
         renderer.run()
Exemplo n.º 5
0
 def copyFile(self, fileName, replacements, destName=None, destFolder=None,
              isPod=False):
     '''This method will copy p_fileName from self.templatesFolder to
        self.outputFolder (or in a subFolder if p_destFolder is given)
        after having replaced all p_replacements. If p_isPod is True,
        p_fileName is a POD template and the copied file is the result of
        applying p_fileName with context p_replacements.'''
     # Get the path of the template file to copy
     templatePath = os.path.join(self.templatesFolder, fileName)
     # Get (or create if needed) the path of the result file
     destFile = fileName
     if destName: destFile = destName
     if destFolder: destFile = '%s/%s' % (destFolder, destFile)
     absDestFolder = self.outputFolder
     if destFolder:
         absDestFolder = os.path.join(self.outputFolder, destFolder)
     if not os.path.exists(absDestFolder):
         os.makedirs(absDestFolder)
     resultPath = os.path.join(self.outputFolder, destFile)
     if os.path.exists(resultPath): os.remove(resultPath)
     if not isPod:
         # Copy the template file to result file after having performed some
         # replacements
         f = file(templatePath)
         fileContent = f.read()
         f.close()
         if not fileName.endswith('.png'):
             for rKey, rValue in replacements.iteritems():
                 fileContent = fileContent.replace(
                     '<!%s!>' % rKey, str(rValue))
         f = file(resultPath, 'w')
         f.write(fileContent)
         f.close()
     else:
         # Call the POD renderer to produce the result
         rendererParams = {'template': templatePath,
                           'context': replacements,
                           'result': resultPath}
         renderer = appy.pod.renderer.Renderer(**rendererParams)
         renderer.run()