Иванов Денис 3 years ago
commit 97e479a8ca

@ -39,6 +39,7 @@ from calculate.variables.datavars import (
FloatType,
ListType
)
from calculate.utils.fs import readFile
from calculate.variables.loader import Datavars
import calculate.templates.template_filters as template_filters
@ -970,6 +971,7 @@ class CalculateExtension(Extension):
self.package_atom_parser = PackageAtomParser(chroot_path=chroot_path)
self.environment.globals.update({'pkg': self.pkg})
self.environment.globals.update({'grep': self.grep})
self._datavars = datavars_module
self.parameters_processor = parameters_processor
@ -1472,6 +1474,36 @@ class CalculateExtension(Extension):
return Version()
return package.version
@contextfunction
def grep(self, context, fname, regpattern) -> str:
'''
Метод реализующий функция grep
'''
try:
reg = re.compile(regpattern)
except re.error:
raise TemplateSyntaxError(_("Wrong regular expression"))
# TODO: добавить получение домашней директории пользователя
#if fname[0] == "~":
# # Получаем директорию пользователя
# fname = os.path.join(self.homeDir,
# fname.partition("/")[2], "")[:-1]
# TODO: учитывать также root_path
fname = os.path.join(
self.parameters_processor.chroot_path,
fname.lstrip("/"))
fileContent = readFile(fname)
if not fileContent:
return ""
match_data = reg.search(fileContent)
if match_data:
md_groups = match_data.groups()
if md_groups:
return md_groups[0] or ""
else:
return match_data.group()
else:
return ""
class TemplateEngine:
def __init__(self, directory_path=None,

@ -262,3 +262,48 @@ parameter_2 = {{ vars_1.var_1 }}'''
text = template_engine.template_text
assert text == output_text
@pytest.mark.parametrize('case',
[
{
"name": "pattern not found",
"grep": "'/test_grep', 'QWERTY'",
"result": ""
},
{
"name": "file not found",
"grep": "'/test_grep_not_found', 'QWERTY'",
"result": ""
},
{
"name": "pattern found",
"grep": "'/test_grep', 'NOSTATUSLINE=true'",
"result": "NOSTATUSLINE=true"
},
{
"name": "regexp found",
"grep": "'/test_grep', 'NOSTATUS.*'",
"result": "NOSTATUSLINE=true"
},
{
"name": "group regexp found",
"grep": "'/test_grep', 'NOSTATUSLINE=(.*)'",
"result": "true"
},
],
ids=lambda x:x["name"])
def test_grep(self, case):
datavars_module = Variables({'vars_1':
Variables({'var_1':
'test-category/test-package',
'var_2': 1.2})})
input_template = '''{% calculate name = 'filename', force -%}
{{ grep(''' + case['grep'] + ''') }}'''
output_text = case["result"]
template_engine = TemplateEngine(appends_set=APPENDS_SET,
datavars_module=datavars_module,
chroot_path=CHROOT_PATH)
template_engine.process_template_from_string(input_template, FILE)
text = template_engine.template_text
assert text == output_text

@ -0,0 +1,10 @@
#------------------------------------------------------------------------------
# Modified Calculate Utilities 3.6.7.23
# Processed template files:
# /var/db/repos/calculate/profiles/templates/3.6/2_ac_install_merge/app-portage/eix/50-calculate
# For modify this file, create /run/calculate/mount/install/etc/eixrc/50-calculate.clt template.
#------------------------------------------------------------------------------
COLORSCHEME1="true"
NOSTATUSLINE=true
EIX_LIMIT=0
EIX_LIMIT_COMPACT=0
Loading…
Cancel
Save