first draft of preparsing

This commit is contained in:
vky 2022-02-21 19:59:56 +01:00
parent 5d9cb6d093
commit d67d169e15
2 changed files with 20 additions and 14 deletions

View File

@ -6,6 +6,7 @@ from io import StringIO
from bootstraparse.modules import pathresolver as pr from bootstraparse.modules import pathresolver as pr
from bootstraparse.modules import environment from bootstraparse.modules import environment
from bootstraparse.modules import syntax
import rich import rich
from rich.tree import Tree from rich.tree import Tree
@ -13,12 +14,12 @@ from rich.tree import Tree
# list of regexps # list of regexps
_rgx_import_file = regex.compile(r'::( ?\< ?(?P<file_name>[\w\-._/]+) ?\>[ \s]*)+') _rgx_import_file = regex.compile(r'::( ?\< ?(?P<file_name>[\w\-._/]+) ?\>[ \s]*)+')
_rgx_shortcut_alias = r'@\[(?P<alias_name>[\w\-._/]+)\]' # _rgx_shortcut_alias = r'@\[(?P<alias_name>[\w\-._/]+)\]'
_rgx_shortcut_image = r'@{(?P<image_name>[\w\-._/]+)}' # _rgx_shortcut_image = r'@{(?P<image_name>[\w\-._/]+)}'
_rgx_shortcut_class = r'({(?P<class>[^}]+)})?' # _rgx_shortcut_class = r'({(?P<class>[^}]+)})?'
_rgx_shortcut_variable = r'(\[(?P<variables>[^\]]+)\])?' # _rgx_shortcut_variable = r'(\[(?P<variables>[^\]]+)\])?'
_rgx_alias = regex.compile(f'({_rgx_shortcut_alias}{_rgx_shortcut_class}{_rgx_shortcut_variable} ?)+') # _rgx_alias = regex.compile(f'({_rgx_shortcut_alias}{_rgx_shortcut_class}{_rgx_shortcut_variable} ?)+')
_rgx_image = regex.compile(f'({_rgx_shortcut_image}{_rgx_shortcut_class}{_rgx_shortcut_variable} ?)+') # _rgx_image = regex.compile(f'({_rgx_shortcut_image}{_rgx_shortcut_class}{_rgx_shortcut_variable} ?)+')
class PreParser: class PreParser:
@ -136,16 +137,21 @@ class PreParser:
:return: a table of occurrences and their lines :return: a table of occurrences and their lines
""" """
line_count = 0 line_count = 0
alias_list = [] temp_file = StringIO()
image_list = []
for line in self.readlines(): for line in self.readlines():
results = regex.match(_rgx_alias, line) results = syntax.alias.parse_string(line)
if results: if results:
for e in results.captures('alias_name', 'class', 'variables'): temp_file.writelines(self.get_alias_from_config(results.alias_name))
alias_list += [(e, line_count)] else:
results = syntax.inage.parse_string(line)
if results:
temp_file.writelines(self.get_picture_from_config(results.image_name))
else:
temp_file.writelines(line)
line_count += 1 line_count += 1
def get_shortcut_from_config(self, shortcut):
def get_alias_from_config(self, shortcut):
""" """
Fetches shortcut paths from aliases.yaml Fetches shortcut paths from aliases.yaml
:return: the html to insert as a string :return: the html to insert as a string

View File

@ -13,7 +13,7 @@ var = '[' + pp.Group(assignation + pp.ZeroOrMore(pp.Suppress(r',') + assignation
# specific elements # specific elements
image_element = '@{' + pp.common.identifier('image_name') + '}' image_element = '@{' + pp.common.identifier('image_name') + '}'
alias_element = '@[' + pp.common.identifier('image_name') + ']' alias_element = '@[' + pp.common.identifier('alias_name') + ']'
expression = pp.Word(pp.alphanums + r'=+-_\'",;: ') expression = pp.Word(pp.alphanums + r'=+-_\'",;: ')
html_insert = '{' + expression('html_insert') + '}' html_insert = '{' + expression('html_insert') + '}'
@ -31,7 +31,7 @@ if __name__ == '__main__': # pragma: no cover
saucisse = alias.parse_string(test_string) saucisse = alias.parse_string(test_string)
# rich.inspect(final.parse_string(test_string)) # rich.inspect(final.parse_string(test_string))
rich.inspect(saucisse.vars) rich.inspect(saucisse)
for e in saucisse.vars: for e in saucisse.vars:
print(f'{e.var_name} equals "{e.var_value}"') print(f'{e.var_name} equals "{e.var_value}"')