Template executor checks if the template is empty now and checks original file for changes using MD5. Format 'raw' is added. fixed #10
parent
f42ebc0075
commit
3834b7e168
@ -0,0 +1,52 @@
|
||||
# vim: fileencoding=utf-8
|
||||
#
|
||||
from .base_format import Format
|
||||
from ..template_engine import ParametersContainer
|
||||
|
||||
|
||||
class RawFormat(Format):
|
||||
'''Класс формата raw.'''
|
||||
FORMAT = 'raw'
|
||||
EXECUTABLE = False
|
||||
FORMAT_PARAMETERS = {'comment'}
|
||||
|
||||
def __init__(self, document_text: str,
|
||||
template_path,
|
||||
ignore_comments=False,
|
||||
join_before=False,
|
||||
add_header=False,
|
||||
already_changed=False,
|
||||
parameters=ParametersContainer()):
|
||||
self.comment_symbol = parameters.comment or "#"
|
||||
self._before = join_before
|
||||
|
||||
if add_header and not ignore_comments:
|
||||
self.header, self._document_text =\
|
||||
self._get_header_and_document_text(
|
||||
document_text,
|
||||
template_path,
|
||||
already_changed=already_changed)
|
||||
else:
|
||||
self.header = ''
|
||||
self._document_text = document_text
|
||||
|
||||
@property
|
||||
def document_text(self):
|
||||
return '{}{}'.format(self.header, self._document_text)
|
||||
|
||||
def join_template(self, template):
|
||||
if self._before:
|
||||
self._document_text = '{0}{1}{2}'.format(
|
||||
template._document_text,
|
||||
"" if template._document_text.
|
||||
endswith("\n") else "\n",
|
||||
self._document_text)
|
||||
else:
|
||||
self._document_text = '{0}{1}{2}'.format(
|
||||
self._document_text,
|
||||
"" if template._document_text.
|
||||
endswith("\n") else "\n",
|
||||
template._document_text)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.document_text)
|
@ -0,0 +1,137 @@
|
||||
import pytest
|
||||
from collections import OrderedDict
|
||||
from calculate.templates.format.raw_format import RawFormat
|
||||
|
||||
|
||||
@pytest.mark.raw
|
||||
class TestParsingMethods:
|
||||
def test_first(self):
|
||||
original = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
template = '''parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
output = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;
|
||||
parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
raw_document_1 = RawFormat(original, 'path/to/template')
|
||||
raw_document_2 = RawFormat(template, 'path/to/template')
|
||||
raw_document_1.join_template(raw_document_2)
|
||||
|
||||
assert raw_document_1.document_text == output
|
||||
|
||||
def test_second(self):
|
||||
document_1 = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
document_2 = '''parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
output = '''parameter-4 = d;
|
||||
parameter-5 = e;
|
||||
parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
raw_document_1 = RawFormat(document_1, 'path/to/template',
|
||||
join_before=True)
|
||||
raw_document_2 = RawFormat(document_2, 'path/to/template')
|
||||
raw_document_1.join_template(raw_document_2)
|
||||
|
||||
assert raw_document_1.document_text == output
|
||||
|
||||
def test_first_with_comment(self):
|
||||
document_1 = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
document_2 = '''parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
output = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# path/to/template
|
||||
#-------------------------------------------------------------------------------
|
||||
parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;
|
||||
parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
raw_document_1 = RawFormat(document_1, 'path/to/template',
|
||||
add_header=True)
|
||||
raw_document_2 = RawFormat(document_2, 'path/to/template')
|
||||
raw_document_1.join_template(raw_document_2)
|
||||
|
||||
assert raw_document_1.document_text == output
|
||||
|
||||
def test_second_with_comment(self):
|
||||
document_1 = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
document_2 = '''parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
output = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# path/to/template
|
||||
#-------------------------------------------------------------------------------
|
||||
parameter-4 = d;
|
||||
parameter-5 = e;
|
||||
parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
raw_document_1 = RawFormat(document_1, 'path/to/template',
|
||||
join_before=True, add_header=True)
|
||||
raw_document_2 = RawFormat(document_2, 'path/to/template')
|
||||
raw_document_1.join_template(raw_document_2)
|
||||
|
||||
assert raw_document_1.document_text == output
|
||||
|
||||
def test_third_with_comment(self):
|
||||
original_text = '''parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;'''
|
||||
|
||||
template_text_1 = '''parameter-4 = d;
|
||||
parameter-5 = e;'''
|
||||
|
||||
template_text_2 = '''parameter-6 = f;'''
|
||||
|
||||
output = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# path/to/template_1
|
||||
# path/to/template_2
|
||||
#-------------------------------------------------------------------------------
|
||||
parameter-1 = a;
|
||||
parameter-2 = b;
|
||||
parameter-3 = c;
|
||||
parameter-4 = d;
|
||||
parameter-5 = e;
|
||||
parameter-6 = f;'''
|
||||
|
||||
original_1 = RawFormat(original_text, 'path/to/template_1',
|
||||
add_header=True,
|
||||
already_changed=False)
|
||||
template_1 = RawFormat(template_text_1, 'path/to/template_1')
|
||||
template_2 = RawFormat(template_text_2, 'path/to/template_2')
|
||||
|
||||
original_1.join_template(template_1)
|
||||
original_2 = RawFormat(original_1.document_text, 'path/to/template_2',
|
||||
add_header=True,
|
||||
already_changed=True)
|
||||
|
||||
original_2.join_template(template_2)
|
||||
|
||||
assert original_2.document_text == output
|
After Width: | Height: | Size: 3.0 KiB |
@ -0,0 +1 @@
|
||||
{% calculate action = "install", append = "skip", path = "/etc" %}
|
@ -0,0 +1,2 @@
|
||||
{% calculate append = "join", format = "raw", source = "/etc/img.gif",
|
||||
name = "copy.gif", package = "test-category/test-package" -%}
|
Loading…
Reference in new issue