def create_odt(self, first, filename): print("creating odt") odt_path = os.path.join(os.path.relpath("MyPatterns"), self.file_base + "_pattern.odt") foldsArr = [] for column in range(0, self.temp.size[0]): # add first page to get the correct page number, double for sheets instead of pages pagenum = column * 2 + first if column in self.final_pattern: # make the number a float with double precision, like 10.15 upper_corner = self.final_pattern[column][0]/100.0 lower_corner = self.final_pattern[column][1]/100.0 if SINGLE_PRECISION == True: upper_corner = ('%.1f' % round(upper_corner, 1)).rjust(7) lower_corner = ('%.1f' % round(lower_corner, 1)).rjust(7) else: upper_corner = ('%.2f' % upper_corner).rjust(6) lower_corner = ('%.2f' % lower_corner).rjust(6) foldsArr.append({"pagenum": str(pagenum).rjust(6), "top": upper_corner, "bottom": lower_corner}) #else: #foldsArr.append({"pagenum": "4", "top": "", "bottom": ""}) engine = Renderer(media_path='.') template = open('template.odt', 'rb') output = open(odt_path, 'wb') output.write(engine.render(template, TITLE=self.file_base, image=os.path.join(os.path.relpath("MyPatterns"), self.file_base + "_sheets.png"), image_orig=os.path.join(os.path.relpath("MyPictures"), filename), folds=foldsArr ) )
def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template)
def from_template(template, context): """ Generates odt file template - string path to odt template context - context variables like in Jinja2 """ engine = Renderer() result = engine.render(template, context=context) return result
def test_secr(): gr = find_all_groups() engine = Renderer() template = open('app/groups/templates/template.odt', 'rb') eng = engine.render(template, image='app/groups/writer.png', stroki=gr) #eng возвращает двоичный объект - заполненный шаблон response = make_response(eng) response.headers['Content-Type'] = 'application/vnd.oasis.opendocument.text' response.headers['Content-Disposition'] = \ 'attachment; filename=testfile.odt' return response
def get(self, request): engine = Renderer() template = os.path.join(settings.BASE_DIR, 'static', 'template.odt') context = { 'taskList' : Tasks.objects.filter(owner=self.request.user), 'DOC_NAME' : 'Reropt', } result = engine.render(template, **context) response = HttpResponse(content_type = 'application/odt',content = result) response['Content-Disposition'] = 'attachment; filename="renders.odt"' return response
def render_document(self, t_file, context): """ Render template with given context Args: t_file: template file context: (dict) template variables Returns: (str) downloaded file """ engine = Renderer() rendered = engine.render(t_file, **context) return "%s%s" % (S3_PUBLIC_URL, self.save_document(rendered))
def post(self, request, uid): template_file = os.path.join(ODT_TEMPLATES_PATH, 'hunting_ticket.odt') result_file = os.path.join(MEDIA_ROOT, f'{str(uuid.uuid4())}.odt') # renderer = Renderer(template_file, request.data, result_file) # renderer.run() engine = Renderer() result = engine.render(template_file, **request.data) output = open(result_file, 'wb') output.write(result) return Response({'path': result_file})
def remplir_template(self): ## from odf.opendocument import OpenDocumentText ## self.setNomFichierODF(nom_fichier) ## textdoc = OpenDocumentText() ## self.setHandleFichierODF(textdoc) ## return textdoc from secretary import Renderer engine = Renderer() result = engine.renderer(self.getNomFichierTemplate(), Auditeur=self.getVariableEnglobante()) output = open(self.getNomFichierDestination(), 'wb') output.write(result) output.close()
def create_report(template, report, filename="report.odt"): from secretary import Renderer engine = Renderer() result = engine.render(template, report=report) import tempfile with tempfile.NamedTemporaryFile() as output: output.write(result) output.flush() from django.http import FileResponse response = FileResponse(open(output.name, 'rb'), as_attachment=True, filename=filename) return response
def odtrender(request, pk): # Create the HttpResponse object with the appropriate PDF headers. response = HttpResponse(content_type='application/vnd.oasis.opendocument.text ') response['Content-Disposition'] = 'attachment; filename="render.odt"' context = {'aaa': pk} engine = Renderer() template = open('doc_template/ustyazi.odt', 'rb') #output = open('output.odt', 'wb') #output.write(engine.render(template, c=context )) response.write(engine.render(template, c=context )) return response
def __init__(self, template=None, context=None): """ Şablon ve şablon içinde kullanılacak değişkenler Usage: t = TRenderer(template=f, context={"name": "Cem",}) rendered_doc = t.render_document Args: template (file): file or file like object context (dict): context variables """ self.template = template self.context = context self.engine = Renderer()
def test_escape_elem_with_attributes(self): """A bug in _encode_escape_chars was preventing it from escaping LF and tabs inside text elements with tag attributes. See: https://github.com/christopher-ramirez/secretary/issues/39""" xml = '<text:span attr="value">This\nLF</text:span>' espected = '<text:span attr="value">This<text:line-break/>LF</text:span>' assert (Renderer._encode_escape_chars(xml) == espected)
def render(self, context): """Render the document using secretary.""" engine = Renderer() engine.environment.filters['format_date'] = format_date engine.environment.filters['format_datetime'] = format_datetime result = engine.render(self.template, **context) response = HttpResponse( content_type='application/vnd.oasis.opendocument.text; charset=UTF-8' ) response['Content-Disposition'] = 'inline; filename=' + self.filename with tempfile.NamedTemporaryFile() as output: output.write(result) output.flush() output = open(output.name, 'rb') response.write(output.read()) return response
class TRenderer(object): """Döküman renderer""" def __init__(self, template=None, context=None): """ Şablon ve şablon içinde kullanılacak değişkenler Usage: t = TRenderer(template=f, context={"name": "Cem",}) rendered_doc = t.render_document Args: template (file): file or file like object context (dict): context variables """ self.template = template self.context = context self.engine = Renderer() def render_document(self): """ Sablonu verilen degiskenler ile isleyip dondurur. Returns: (file): islenmis file like object """ try: return self.engine.render(self.template, **self.context) except SecretaryError as exception: current_app.logger.error(str(exception))
class RenderTestCase(TestCase): def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template) def test__unescape_entities(self): test_samples = { # test scaping of > '{{ "greater_than_1" if 1>0 }}' : '{{ "greater_than_1" if 1>0 }}', '{% a > b %}' : '{% a > b %}', '{{ a > b }}' : '{{ a > b }}', '{% a|filter > b %}' : '{% a|filter > b %}', '<node>{% a == b %}</node>{% else if a > b %}': '<node>{% a == b %}</node>{% else if a > b %}', # test scaping of < and " '{{ "lower_than_1" if 1<0 }}' : '{{ "lower_than_1" if 1<0 }}', '{% a < b %}' : '{% a < b %}', '{{ a < b }}' : '{{ a < b }}', '{% a|filter < b %}' : '{% a|filter < b %}', '<node>{% a == b %}</node>{% else if a < b %}': '<node>{% a == b %}</node>{% else if a < b %}', } for test, expect in test_samples.items(): assert self.engine._unescape_entities(test) == expect def _test_is_jinja_tag(self): assert self._is_jinja_tag('{{ foo }}')==True assert self._is_jinja_tag('{ foo }')==False def _test_is_block_tag(self): assert self._is_block_tag('{% if True %}')==True assert self._is_block_tag('{{ foo }}')==False assert self._is_block_tag('{ foo }')==False def test_create_test_node(self): assert self.engine.create_text_node(self.document, 'text').toxml() == 'text' def test_create_text_span_node(self): assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
def __init__(self, book, template, destination, filters=None): if not filters: filters = {} self.set_template(template) self.book = book self.set_destination(destination) self.engine = Secretary(markdown_extras=['fenced-code-blocks', 'footnotes', 'tables']) self.engine.environment.filters.update(filters)
def create_odt(self, first, filename): print("creating odt") odt_path = os.path.join(os.path.relpath("MyPatterns"), self.file_base + "_pattern.odt") foldsArr = [] for column in range(0, self.temp.size[0]): # add first page to get the correct page number, double for sheets instead of pages pagenum = column * 2 + first if column in self.final_pattern: # make the number a float with double precision, like 10.15 upper_corner = self.final_pattern[column][0] / 100.0 lower_corner = self.final_pattern[column][1] / 100.0 if SINGLE_PRECISION == True: upper_corner = ('%.1f' % round(upper_corner, 1)).rjust(7) lower_corner = ('%.1f' % round(lower_corner, 1)).rjust(7) else: upper_corner = ('%.2f' % upper_corner).rjust(6) lower_corner = ('%.2f' % lower_corner).rjust(6) foldsArr.append({ "pagenum": str(pagenum).rjust(6), "top": upper_corner, "bottom": lower_corner }) #else: #foldsArr.append({"pagenum": "4", "top": "", "bottom": ""}) engine = Renderer(media_path='.') template = open('template.odt', 'rb') output = open(odt_path, 'wb') output.write( engine.render(template, TITLE=self.file_base, image=os.path.join(os.path.relpath("MyPatterns"), self.file_base + "_sheets.png"), image_orig=os.path.join( os.path.relpath("MyPictures"), filename), folds=foldsArr))
def create_report(data): engine = Renderer() root = os.path.dirname(__file__) document = root + '/templates/bedjango/template.odt' DIRECTORY = root + '/templates/bedjango/tmp/' result = engine.render(document, data=data) tempfile.tempdir = DIRECTORY response = HttpResponse( content_type= 'application/vnd.oasis.opendocument.text; charset=UTF-8') response[ 'Content-Disposition'] = 'inline; filename=report_on_complaint_{}.odt'.format( now.strftime('%d%m%Y')) with tempfile.NamedTemporaryFile(delete=False) as output: output.write(result) output.flush() output = open(output.name, 'rb') response.write(output.read()) clear_folder(DIRECTORY) return response
class RenderTestCase(TestCase): def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template) def test__unescape_entities(self): test_samples = { # test scaping of > '{{ "greater_than_1" if 1>0 }}' : '{{ "greater_than_1" if 1>0 }}', '{% a > b %}' : '{% a > b %}', '{{ a > b }}' : '{{ a > b }}', '{% a|filter > b %}' : '{% a|filter > b %}', '<node>{% a == b %}</node>{% else if a > b %}': '<node>{% a == b %}</node>{% else if a > b %}', # test scaping of < and " '{{ "lower_than_1" if 1<0 }}' : '{{ "lower_than_1" if 1<0 }}', '{% a < b %}' : '{% a < b %}', '{{ a < b }}' : '{{ a < b }}', '{% a|filter < b %}' : '{% a|filter < b %}', '<node>{% a == b %}</node>{% else if a < b %}': '<node>{% a == b %}</node>{% else if a < b %}', } for test, expect in test_samples.items(): assert self.engine._unescape_entities(test) == expect def test__encode_escape_chars(self): test_samples = { '<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>', '<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>', '<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>', '<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>', '<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>', '<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>', '<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>', '<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>', } for test, expect in test_samples.items(): assert self.engine._encode_escape_chars(test) == expect def _test_is_jinja_tag(self): assert self._is_jinja_tag('{{ foo }}')==True assert self._is_jinja_tag('{ foo }')==False def _test_is_block_tag(self): assert self._is_block_tag('{% if True %}')==True assert self._is_block_tag('{{ foo }}')==False assert self._is_block_tag('{ foo }')==False def test_create_test_node(self): assert self.engine.create_text_node(self.document, 'text').toxml() == 'text' def test_create_text_span_node(self): assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
def renderx(x,user,projectname,data): root = os.path.dirname(__file__) base = os.path.join(root,'./static/users/%s/%s/' %(user,projectname)) jdata = os.path.join(base,'./json/data.json') # try: # r = requests.get(jsonpath) # addr = json.loads(r.text) # except : # return -1 template = os.path.join(base, './odt/'+x) engine = Renderer() engine.environment.filters['fnbarcode'] = fnbarcode result = engine.render(template,data=data) outf = os.path.join(base,'./out/') templatename = str(random.uniform(0,100))+x fname = os.path.join(outf,templatename) data = [] data.append(base) data.append(fname) data.append(templatename) output = open(fname, 'wb') output.write(result) return data
def render_template(self, template_file: str, rendered_file: str, values: Dict[str, Any], pdf: bool): logging.info( f"Rendering template {template_file} into {rendered_file} (PDF = {pdf})..." ) engine = Renderer() result = engine.render(template_file, **values) with open(rendered_file, "wb") as outf: outf.write(result) if pdf: soffice = self.get_soffice() saved_dir = os.getcwd() os.chdir(os.path.dirname(rendered_file)) cmd = f"{soffice} --headless --convert-to pdf '{os.path.basename(rendered_file)}'" out = subprocess.run([ soffice, "--headless", "--convert-to", "pdf", os.path.basename(rendered_file), ]) os.chdir(saved_dir) os.unlink(rendered_file)
class RenderTestCase(TestCase): def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template) def test__unescape_entities(self): test_samples = { '{{ "greater_than_1" if 1>0 }}': '{{ "greater_than_1" if 1>0 }}', '{{ "lower_than_1" if 1<0 }}': '{{ "lower_than_1" if 1<0 }}', '{{ if <text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if </text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if <text:s/> multiple_spaces }}': '{{ if multiple_spaces }}', } for test, expect in test_samples.items(): assert self.engine._unescape_entities(test) == expect def test__encode_escape_chars(self): test_samples = { '<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>', '<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>', '<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>', '<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>', '<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>', '<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>', '<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>', '<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>', } for test, expect in test_samples.items(): assert self.engine._encode_escape_chars(test) == expect def test_create_test_node(self): assert self.engine.create_text_node(self.document, 'text').toxml() == 'text' def test_create_text_span_node(self): assert self.engine.create_text_span_node( self.document, 'text').toxml() == '<text:span>text</text:span>'
def _open_template(self, template: TemplateModel, file_format=None) -> None: ''' Opens a template for document manipulation. The ´file_format´ argument is optional. ''' template_name = template.file if file_format: self.file_format = file_format else: templates = self._list_files( files_path=f'{self.pwd}/{self._templates_path}/{template_name}', file_formats=self.SUPPORTED_INPUT_FORMATS) template = templates[0] self.file_format = template.split('.')[-1] del templates, template self.template_name = template_name self.path = f'{self.pwd}/{self._templates_path}/{template_name}/{template_name}.{self.file_format}' if (self.file_format in self.ODF_FORMATS): # using relatorio # self._file = Template(source='', filepath=self.path) # using secretary self._file = Renderer() elif (self.file_format in self.OFFICE_FORMATS): self._file = DocxTemplate(self.path) else: raise TypeError( 'The file format is invalid or it is not supported.') try: self.info_path = f'{self.pwd}/{self._templates_path}/{template_name}/{template_name}.json' with open(self.info_path, 'r') as f: self.info = json.load(f) except Exception as e: raise e
class ODTRenderer(Renderer): def __init__(self, book, template, destination, filters=None): if not filters: filters = {} self.set_template(template) self.book = book self.set_destination(destination) self.engine = Secretary(markdown_extras=['fenced-code-blocks', 'footnotes', 'tables']) self.engine.environment.filters.update(filters) def set_template(self, template): if not os.path.isabs(template): current_path = os.path.dirname(os.path.abspath(__file__)) template = os.path.join(current_path, template) self.template = template def set_destination(self, destination): if not os.path.exists(destination): os.makedirs(destination) self.destination = destination def render(self): logger.debug('ODT Renderer Initialized') book_title = self.book.config.title or 'book' book_name = '%s.odt' % book_title book_path = os.path.join(self.destination, book_name) logger.debug(self.book.summary[0].content) result = self.engine.render(self.template, book=self.book, **self.book.config.variables) with open(book_path, 'wb') as f: f.write(result) logger.debug('ODT Renderer Finished') return True
class MixinReport(): """ Использует для рендера шаблонов С использование библиотеки secretary """ __engine = Renderer() template_name = '' # Имя шаблона для рендера context = {} # Данные # renderers def __renders(self): template = os.path.join(settings.BASE_DIR, 'static', self.template_name) result = self.__engine.render(template, **self.context) return result def get_report(self): """ Возваращет готовый шаблон """ response = HttpResponse(content_type='application/odt', content=self.__renders()) response['Content-Disposition'] = 'attachment; filename="renders.odt"' return response
class RenderTestCase(TestCase): def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template) def test__unescape_entities(self): test_samples = { '{{ "greater_than_1" if 1>0 }}': '{{ "greater_than_1" if 1>0 }}', '{{ "lower_than_1" if 1<0 }}': '{{ "lower_than_1" if 1<0 }}', '{{ if <text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if </text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if <text:s/> multiple_spaces }}': '{{ if multiple_spaces }}', } for test, expect in test_samples.items(): assert self.engine._unescape_entities(test) == expect def test__encode_escape_chars(self): test_samples = { '<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>', '<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>', '<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>', '<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>', '<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>', '<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>', '<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>', '<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>', } for test, expect in test_samples.items(): assert self.engine._encode_escape_chars(test) == expect def test_create_test_node(self): assert self.engine.create_text_node(self.document, 'text').toxml() == 'text' def test_create_text_span_node(self): assert self.engine.create_text_span_node(self.document, 'text').toxml() == '<text:span>text</text:span>'
# /usr/bin/activate import os import sys sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../../..'))) from secretary import Renderer if __name__ == '__main__': engine = Renderer(media_path='.') template = open('template.odt', 'rb') output = open('output.odt', 'wb') output.write(engine.render(template, image='writer.png')) print("Template rendering finished! Check output.odt file.")
class RenderTestCase(TestCase): def setUp(self): root = os.path.dirname(__file__) impl = getDOMImplementation() template = os.path.join(root, 'simple_template.odt') self.document = impl.createDocument(None, "some_tag", None) self.engine = Renderer() self.engine.render(template) def test__unescape_entities(self): test_samples = { # test scaping of > '{{ "greater_than_1" if 1>0 }}': '{{ "greater_than_1" if 1>0 }}', '{% a > b %}': '{% a > b %}', '{{ a > b }}': '{{ a > b }}', '{% a|filter > b %}': '{% a|filter > b %}', '<node>{% a == b %}</node>{% else if a > b %}': '<node>{% a == b %}</node>{% else if a > b %}', # test scaping of < and " '{{ "lower_than_1" if 1<0 }}': '{{ "lower_than_1" if 1<0 }}', '{% a < b %}': '{% a < b %}', '{{ a < b }}': '{{ a < b }}', '{% a|filter < b %}': '{% a|filter < b %}', '<node>{% a == b %}</node>{% else if a < b %}': '<node>{% a == b %}</node>{% else if a < b %}', # test scapig of multiple spaces, even encoded as <text:space> nodes '{{ if <text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if </text:s> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if <text:s/> multiple_spaces }}': '{{ if multiple_spaces }}', '{{ if <text:span/>[1,2,3]<text:span><</text:span>2 }}': '{{ if [1,2,3] < 2 }}', } for test, expect in test_samples.items(): assert self.engine._unescape_entities(test) == expect def test__encode_escape_chars(self): test_samples = { '<text:a>\n</text:a>': '<text:a><text:line-break/></text:a>', '<text:h>\n</text:h>': '<text:h><text:line-break/></text:h>', '<text:p>\n</text:p>': '<text:p><text:line-break/></text:p>', '<text:p>Hello\n</text:p>': '<text:p>Hello<text:line-break/></text:p>', '<text:p>Hello\nWorld\n!</text:p>': '<text:p>Hello<text:line-break/>World<text:line-break/>!</text:p>', '<text:ruby-base>\n</text:ruby-base>': '<text:ruby-base><text:line-break/></text:ruby-base>', '<text:meta>\u0009</text:meta>': '<text:meta><text:tab/></text:meta>', '<text:meta-field>\n</text:meta-field>': '<text:meta-field><text:line-break/></text:meta-field>', } for test, expect in test_samples.items(): assert self.engine._encode_escape_chars(test) == expect def test_create_test_node(self): assert self.engine.create_text_node(self.document, 'text').toxml() == 'text' def test_create_text_span_node(self): assert self.engine.create_text_span_node( self.document, 'text').toxml() == '<text:span>text</text:span>'
def executeProcess(self, folder): pto = self.fetchOne(folder.name) # print(pto) pto['dataMono'] = date.today().strftime('%d/%m/%Y') # Necessário para diminuir o tamanho dos campo no modelo: pto['freq'] = pto['freq_processada'] pto['mc'] = pto['meridiano_central'] pto['mascara'] = pto['mascara_elevacao'] pto['taxa'] = pto['taxa_gravacao'] pto['sigmaXY'] = pto['precisao_horizontal_esperada'] pto['sigmaZ'] = pto['precisao_vertical_esperada'] pto['m'] = 'Sim' if pto['materializado'] else 'Não' # por algum bug no odt/secretary não dá pra replicar imagem (no caso a assinatura, que está no rodapé) pto['signature'] = self.settings['signature'] pto['signature1'] = self.settings['signature'] pto['signature2'] = self.settings['signature'] pto['altitude_ortometrica'] = '{:.2f}'.format( pto['altitude_ortometrica']) pto['altitude_geometrica'] = '{:.2f}'.format( pto['altitude_geometrica']) pto['durRast'] = pto["fim_rastreio"] - pto["inicio_rastreio"] pto['photoCGEO'] = self.settings['pathImageCGEO'] try: pto['inicio_rastreio'] = pto['inicio_rastreio'].strftime( '%d/%m/%Y %H:%M:%S') pto['data_processamento'] = pto['data_processamento'].strftime( '%d/%m/%Y') except AttributeError: pass # Fotos do ponto photosPt = [ str(f) for f in Path(folder / '3_Foto_Rastreio').iterdir() if f.suffix in self.img_extensions ] for _idx in range(len(photosPt)): pto[f'photoPt{_idx + 1}'] = photosPt[_idx] # Não esquecer que as visões aéreas tem que ser geradas! pto['photoCroqui'] = [ str(f) for f in Path(folder / '4_Croqui').iterdir() if f.match('*.jpg') ][0] pto['photoAerView'] = str( Path(folder / '7_Imagens_Monografia' / f'{pto["cod_ponto"]}_AEREA.jpg')) pto['photoView1'] = str( Path(folder / '7_Imagens_Monografia' / f'{pto["cod_ponto"]}_MUNICIPIO.jpg')) pto['photoView2'] = str( Path(folder / '7_Imagens_Monografia' / f'{pto["cod_ponto"]}_ESTADO.jpg')) # pto['photoAerView'] = [str(f) for f in Path(self.settings['photoAerView']).iterdir() if f.match('{}*.jpg'.format(pto['cod_ponto']))][0] # pto['photoView1'] = [str(f) for f in Path(self.settings['photoView1']).iterdir() if f.match('{}*.jpg'.format(pto['cod_ponto']))][0] # pto['photoView2'] = [str(f) for f in Path(self.settings['photoView2']).iterdir() if f.match('{}*.jpg'.format(pto['cod_ponto']))][0] engine = Renderer() # Path do template result = engine.render(template='../modelo.odt', pto=pto) with open(folder / '{}.odt'.format(pto['cod_ponto']), 'wb') as output: output.write(result) # Gera o pdf _path_odt = str(Path(folder / pto['cod_ponto'])) subprocess.run([ f"{self.settings['pathLibreOffice']}", "--headless", "--convert-to", "pdf", f"{_path_odt}.odt" ]) # Transfere o pdf para a estrutura de pastas e deleta o odt Path.mkdir(folder / '8_Monografia', exist_ok=True) Path.replace( Path.cwd() / '{}.pdf'.format(pto['cod_ponto']), Path(folder / '8_Monografia' / '{}.pdf'.format(pto['cod_ponto']))) Path.unlink(Path(folder / '{}.odt'.format(pto['cod_ponto']))) print(f'Monografia do ponto {pto["cod_ponto"]} concluída.')
}, "richiedente": richiedente, "pi": { "desc": PI_desc, "nta": PI_nta }, "cs": CS_udp, "pat": { "desc": PAT_desc, "nta": PAT_nta }, } arcpy.AddMessage("PARAMS: %s" % str(params)) engine = Renderer() template2 = os.path.join(os.path.dirname(__file__), "SOMMARIO_template.odt") result2 = engine.render(template2, PI_def=PI_def, CS_def=CS_def, PAT_def=PAT_def) sommario_target = os.path.join(tempfile.mkdtemp(), "sommario.odt") with open(sommario_target, 'wb') as output: output.write(result2) output.flush() arcpy.AddMessage("SOMMARIO DESTINAZIONI: %s" % sommario_target) if not odt_target: odt_target = os.path.join(tempfile.mkdtemp(), "CDU.odt")
def execute(self, parameters, messages): """The source code of the tool.""" arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), "URB.pyt")) arcpy.gp.toolbox = os.path.join(os.path.dirname(__file__), "URB.pyt") # Local variables: contesto = parameters[0].valueAsText coordinate_catastali = parameters[1].valueAsText protocollo_numero = parameters[2].valueAsText protocollo_data = parameters[3].valueAsText richiedente = parameters[4].valueAsText odt_target = parameters[5].valueAsText #json = "" poligono = "" output_json = "" #facoltativo #output_testo_PI = "" #output_json__2_ = "" #output_testo_PAT = "" test = False PI_test = ''' [ {"note": null, "articolo": 0, "layer": 0, "definizione": "Area totale", "nome": "Area totale", "url": "", "gruppo": "cdu", "area": 3978.322295527004, "interno_pe": 0}, {"layer": 39, "definizione": "Area interessata da vincolo aeroportuale disciplinato dalla L. 4 febbraio 1963 n.58", "nome": "Perimetro ricognitivo aeroportuale", "url": "http://www.padovanet.it/allegati/C_1_Allegati_15388_Allegato_0.pdf", "gruppo": "perimetri", "area": 2974.954930029389, "note": null, "articolo": "", "interno_pe": null}, {"layer": 39, "definizione": "Area interessata da vincolo aeroportuale disciplinato dalla L. 4 febbraio 1963 n.58", "nome": "Perimetro ricognitivo aeroportuale", "url": "http://www.padovanet.it/allegati/C_1_Allegati_15388_Allegato_0.pdf", "gruppo": "perimetri", "area": 2974.954930029389, "note": null, "articolo": "", "interno_pe": null}, {"layer": 56, "definizione": "Zona di degrado %", "nome": "Perimetro zone di degrado", "url": "http://www.padovanet.it/allegati/C_1_Allegati_15388_Allegato_0.pdf#92", "gruppo": "centro_storico", "area": 1003.3676370044367, "note": null, "articolo": 41, "interno_pe": 0}, {"layer": 39, "definizione": "Area interessata da vincolo aeroportuale disciplinato dalla L. 4 febbraio 1963 n.58", "nome": "Perimetro ricognitivo aeroportuale", "url": "http://www.padovanet.it/allegati/C_1_Allegati_15388_Allegato_0.pdf", "gruppo": "perimetri", "area": 1003.3676370044367, "note": null, "articolo": "", "interno_pe": null}, {"layer": 39, "definizione": "Area interessata da vincolo aeroportuale disciplinato dalla L. 4 febbraio 1963 n.58", "nome": "Perimetro ricognitivo aeroportuale", "url": "http://www.padovanet.it/allegati/C_1_Allegati_15388_Allegato_0.pdf", "gruppo": "perimetri", "area": 1003.3676370044367, "note": null, "articolo": "", "interno_pe": null}, {"note": null, "articolo": "999.999", "layer": 999, "definizione": "Viabilità", "nome": "Area non zonizzata", "url": "", "gruppo": "cdu", "area": 3978.322295527004, "interno_pe": 0}] ''' CS_test = u''' [{"norma1": "UNITA' DI PIANO DELLA CLASSE E MODALITA' DI TIPO E - RISTRUTTURAZIONE EDILIZIA", "area": 3953.5930837093556, "despro1": "RESIDENZIALE, COMMERCIALE, DIREZIONALE, TURISTICA E ARTIGIANALE", "full_id": "0026500014", "despro": " "}] ''' PAT_test = u''' [ {"nta": 5.1, "layer": "b0101011_vincolo", "fc": "SIT.b0101011_Vincolo_r", "desc": "Vincolo sui beni culturali (D.Lgs. 42/2004, artt.10 e 12)"}, {"nta": "5.6.12", "layer": "b0105021_fascerispetto", "fc": "SIT.b0105021_FasceRispetto_r", "desc": "Servitù o Fasce di rispetto aeroportuale"}, {"nta": "5.5.1", "layer": "b0104011_centrostorico", "fc": "SIT.b0104011_CentroStorico_r", "desc": "Centro Storico (P.R.G., P.T.C.P. art.26 A N.T., P.T.R.C. art.24 N.T.)"}, {"nta": "5.6.8.1", "layer": "b0105051_centriabitati", "fc": "SIT.b0105051_CentriAbitati_r", "desc": "Centri Abitati"} ] ''' arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), "URB.pyt")) arcpy.gp.toolbox = os.path.join(os.path.dirname(__file__), "URB.pyt") if test: identificazione = "TEST" PI_def = json.loads(PI_test) CS_def = json.loads(CS_test) PAT_def = json.loads(PAT_test) arcpy.AddMessage(str(PAT_def)) for item in PAT_def: arcpy.AddMessage(item['desc']) else: poligono = contesto identificazione = "" if coordinate_catastali: CC_result = arcpy.gp.CC2FCtool(coordinate_catastali) arcpy.AddMessage("CC_result: %s" % CC_result.getOutput(0)) poligono = CC_result.getOutput(0) identificazione = u"così individuata nel Catasto Terreni: %s" % decodificaCatasto( coordinate_catastali) else: if not contesto: arcpy.AddError( "Deve essere specificato almeno un contesto, come layer o come coordinate catastali" ) exit(0) poligono = contesto # Process: CDU_PI PI_result = arcpy.gp.CDUPItool(poligono, output_json) arcpy.AddMessage("PI_result: %s" % PI_result.getOutput(0)) PI_def = json.loads(PI_result.getOutput(0)) checkInCS = False for defin in PI_def: if defin["layer"] == 55: checkInCS = True if checkInCS: # Process: CDU_CS CS_result = arcpy.gp.CDUCStool(poligono, output_json) arcpy.AddMessage("CS_result: %s" % CS_result.getOutput(0)) CS_def = json.loads(CS_result.getOutput(0)) else: CS_def = [] # Process: CDU_PAT PAT_result = arcpy.gp.CDUPATtool(poligono, output_json) arcpy.AddMessage("PAT_result: %s" % PAT_result.getOutput(0)) PAT_def = json.loads(PAT_result.getOutput(0)) PI_desc = '' PI_nta = '' dest_selection = [] for item in PI_def: arcpy.AddMessage("item: %s" % str(item)) if item["layer"] != 0 and not item["layer"] in dest_selection: dest_selection.append(item["layer"]) if '%' in item["definizione"]: definizione = item["definizione"].replace( '%', unicode(item["note"] or "")) else: definizione = item["definizione"] PI_desc += '%s, ' % definizione.upper() PI_nta += '%s, ' % str(item["articolo"]).upper() PI_desc = PI_desc[:-2] + "; " PI_nta = PI_nta[:-2] + "; " CS_udp = '' dest_selection = [] for item in CS_def: if not item["norma1"] + item["despro1"] in dest_selection: dest_selection.append(item["norma1"] + item["despro1"]) CS_udp += '%s con destinazione %s, ' % (item['norma1'], item["despro1"]) CS_udp = CS_udp[:-2] + "; " PAT_desc = '' PAT_nta = '' dest_selection = [] for item in PAT_def: arcpy.AddMessage("dest_selection: %s" % str(dest_selection)) if not item["desc"] in dest_selection: dest_selection.append(item["desc"]) PAT_desc += '%s, ' % item["desc"].upper() PAT_nta += '%s, ' % str(item["nta"]).upper() PAT_desc = PAT_desc[:-2] + "; " PAT_nta = PAT_nta[:-2] + "; " params = { "protocollo": { "numero": str(protocollo_numero), "data": str(protocollo_data) }, "richiedente": richiedente, "pi": { "desc": PI_desc, "nta": PI_nta }, "cs": CS_udp, "pat": { "desc": PAT_desc, "nta": PAT_nta }, } arcpy.AddMessage("PARAMS: %s" % str(params)) engine = Renderer() template2 = os.path.join(os.path.dirname(__file__), "SOMMARIO_template.odt") result2 = engine.render(template2, PI_def=PI_def, CS_def=CS_def, PAT_def=PAT_def) #sommario_target = os.path.join(tempfile.mkdtemp(),"sommario.odt") sommario_target = get_jobfile("output", "odt") with open(sommario_target, 'wb') as output: output.write(result2) output.flush() arcpy.AddMessage("SOMMARIO DESTINAZIONI: %s" % sommario_target) if not odt_target: #odt_target = os.path.join(tempfile.mkdtemp(),"CDU.odt") odt_target = get_jobfile("output", "odt") template1 = os.path.join(os.path.dirname(__file__), "CDU_template.odt") result1 = engine.render(template1, **params) with open(odt_target, 'wb') as output: output.write(result1) output.flush() arcpy.AddMessage("SOMMARIO DESTINAZIONI: %s" % odt_target) parameters[5].value = odt_target
from secretary import Renderer import barcode import os root = os.path.dirname(__file__) temp = os.path.join(root, './static/test_template2.odt') imgpath = "" engine = Renderer() # Configure custom application filters def square(barcod): ean = barcode.get('ean13', barcod) global imgpath imgpath = ean.save('image') # if imgpath: # print 'imgpath'-imgpath # engine.environment.filters['square'] = square # result = engine.render(temp,x=imgpath) # output = open('rendered_document.odt', 'wb') # output.write(result) # import os # import json # from secretary import Renderer # import requests # import barcode # from barcode.writer import ImageWriter # def fnbarcode(barcod):
def test(): engine = Renderer() print "hello"
def test_escape_xml_reserved_chars(self): ''' Should also escape minor and mayor signs ''' xml = '1 is > than 0 & -1 is <' expected = '1 is > than 0 & -1 is <' assert (Renderer.get_escaped_var_value(xml) == expected)
def test_encode_tab_char(self): xml = '<text:span>This\tTab</text:span>' espected = '<text:span>This<text:tab/>Tab</text:span>' assert (Renderer._encode_escape_chars(xml) == espected)
def setUp(self): self.engine = Renderer(markdown_extras=['fenced-code-blocks', 'footnotes', 'tables']) self.engine.template_images = {}
PAT_nta += '%s, ' % str(item["nta"]).upper() PAT_desc = PAT_desc[:-2] + "; " PAT_nta = PAT_nta[:-2] + "; " params = { "protocollo": { "numero": str(protocollo_numero), "data": str(protocollo_data) }, "richiedente": richiedente, "pi": { "desc": PI_desc, "nta": PI_nta }, "cs": CS_udp, "pat": { "desc": PAT_desc, "nta": PAT_nta }, } arcpy.AddMessage("PARAMS: %s" % str(params)) engine = Renderer() template = os.path.join(os.path.dirname(__file__), "CDU_template.odt") result = engine.render(template, **params) with open(odt_target, 'wb') as output: output.write(result) output.flush()
from secretary import Renderer import json, sys engine = Renderer() for line in sys.stdin: ctx = json.loads(line) template = 'template.odt' result = engine.render(template, **ctx) output = open(ctx.get('filepath'), 'wb') output.write(result) print json.dumps({'message': 'ok'})
def test_encode_linefeed_char(self): xml = '<text:span>This\nLF</text:span>' espected = '<text:span>This<text:line-break/>LF</text:span>' assert (Renderer._encode_escape_chars(xml) == espected)
class MarkdownFilterTestCase(TestCase): def setUp(self): self.engine = Renderer(markdown_extras=['fenced-code-blocks', 'footnotes', 'tables']) self.engine.template_images = {} def test_paragraphs(self): test_samples = { 'hello\n\n\nworld\n': 2, 'hello world': 1, } pattern = r'<text:p text:style-name="Standard">[a-z ]+</text:p>' for test, occurances in test_samples.items(): result = self.engine.markdown_filter(test) found = re.findall(pattern , result) assert len(found) == occurances def test_fenced_code_blocks(self): test = "```python\ndef test():\n pass\n```" result = self.engine.markdown_filter(test) assert not 'python' in result def test_code_blocks(self): test = "```\ndef test():\n pass\n```" result = self.engine.markdown_filter(test) assert 'codehilite' in result def test_code(self): test = "`test code`" result = self.engine.markdown_filter(test) assert 'codehilite' in result def test_code_blocks_indents(self): test_samples = { "```python\ndef test():\n if True:\n pass\n```": 3, "```\ndef test():\n pass\n```": 1, } for test, occurances in test_samples.items(): result = self.engine.markdown_filter(test) assert result.count('<text:tab/>') == occurances def test_new_line(self): test = "```python\ndef test():\n pass\n```" result = self.engine.markdown_filter(test) assert not '\n' in result def test_footnotes(self): tests = (("foo. [^1]\n bar. [^2] \n\n[^1]: referenced by foo.\n[^2]: referenced by bar\n", '<text:p text:style-name="Standard">foo. <text:note text:id="#fnref-1" text:note-class="footnote"><text:note-citation>0</text:note-citation><text:note-body><text:p>referenced by foo.\xa0</text:p></text:note-body></text:note><text:line-break/> bar. <text:note text:id="#fnref-2" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body><text:p>referenced by bar\xa0</text:p></text:note-body></text:note> </text:p>'), ("""# TEST foo. [^1]\n bar. [^2] \n\n[^1]: referenced by foo.\n[^2]: referenced by bar\n asdasdasd asd asd """, '<text:p text:style-name="Heading_20_1">TEST</text:p><text:line-break/><text:line-break/><text:p text:style-name="Standard">foo. <text:note text:id="#fnref-1" text:note-class="footnote"><text:note-citation>0</text:note-citation><text:note-body><text:p>referenced by foo.\xa0</text:p></text:note-body></text:note><text:line-break/> bar. <text:note text:id="#fnref-2" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body><text:p>referenced by bar\xa0</text:p></text:note-body></text:note> </text:p><text:line-break/><text:line-break/><text:p text:style-name="Standard">asdasdasd asd asd</text:p><text:line-break/><text:line-break/><text:line-break/>'), ) for text, expected in iter(tests): result = self.engine.markdown_filter(text) assert expected in result def test_tables(self): text = "<table><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></table>" expected = ('<table:table table:style-name="Table">' '<table:table-column table:number-columns-repeated="2"/>' '<table:table-row>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="Standard">1</text:p>' '</table:table-cell>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="Standard">2</text:p>' '</table:table-cell>' '</table:table-row>' '<table:table-row>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="Standard">3</text:p>' '</table:table-cell>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="Standard">4</text:p>' '</table:table-cell>' '</table:table-row>' '</table:table>') result = self.engine.markdown_filter(text) assert expected in result def test_tables_multiline(self): text = "<table><tr><td><p>1</p><p>2</p></td></tr></table>" expected = ('<table:table table:style-name="Table">' '<table:table-column table:number-columns-repeated="1"/>' '<table:table-row>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="Standard">1</text:p>' '<text:p text:style-name="Standard">2</text:p>' '</table:table-cell>' '</table:table-row>' '</table:table>') result = self.engine.markdown_filter(text) assert expected in result def test_tables_code(self): text = "<table><tr><td><code><span class='k'>1</span></code></td></tr></table>" expected = ('<table:table table:style-name="Table">' '<table:table-column table:number-columns-repeated="1"/>' '<table:table-row>' '<table:table-cell office:value-type="string">' '<text:p text:style-name="codehilite"><text:span text:style-name="k">1</text:span></text:p>' '</table:table-cell>' '</table:table-row>' '</table:table>') result = self.engine.markdown_filter(text) assert expected in result