You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.8 KiB

# vim: fileencoding=utf-8
#
from .base_format import BaseFormat
from collections import OrderedDict
from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
ZeroOrMore, OneOrMore, ParseException, restOfLine,\
pyparsing_unicode, Group, Optional
class KernelFormat(BaseFormat):
def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line,
self._parse_parameter_line,
self._parse_to_delete_line]
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._comments_processing = True
self._format = 'kernel'
self._last_comments_list = []
self._initialize_parser()
if document_text == '':
self._document_dictionary = OrderedDict()
else:
documentLines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(documentLines)
def _initialize_parser(self):
parameter_name = Word(alphanums+'_')('parameter_name')
parameter_value = originalTextFor(
OneOrMore(Word(printables))
)('parameter_value')
action_symbols = (Literal('!') | Literal('-'))
self._parameter_line = (Group(Optional(action_symbols,
default='')('action')
+ parameter_name('name')
)('parameter_name')
+ Literal('=').suppress()
+ parameter_value('parameter_value'))
self._parameter_to_delete = (Group(action_symbols('action')
+ parameter_name('name')
)('parameter_name')
+ restOfLine.suppress())
self._comment_line = originalTextFor(
Literal('#').suppress()
+ ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums))
)('Comment')
def _parse_parameter_line(self, line):
try:
self._item_to_add = OrderedDict()
parsing_result = self._parameter_line.parseString(line)
key_value = tuple(parsing_result.parameter_name.asList())
parameter_value = parsing_result.parameter_value
parameter_value = self._last_comments_list + [parameter_value]
self._last_comments_list = []
self._item_to_add[key_value] = parameter_value
self._ready_to_update = True
self._match = True
except ParseException:
return
def _parse_to_delete_line(self, line):
try:
self._item_to_add = OrderedDict()
parsing_result = self._parameter_to_delete.parseString(line)
if parsing_result.action == '-':
return
key_value = (parsing_result.parameter_name.action,
parsing_result.parameter_name.name.upper())
parameter_value = self._last_comments_list
self._last_comments_list = []
self._item_to_add[key_value] = parameter_value
self._ready_to_update = True
self._match = True
except ParseException:
return
def _parse_comment_line(self, line):
try:
result = self._comment_line.parseString(line)
self._match = True
if not self._ignore_comments:
self._last_comments_list.append(result.Comment)
except ParseException:
return