Methods for initialization of parsers is class methods now.

packages
Иванов Денис 4 years ago
parent fce2667231
commit e82bd4cd4d

@ -181,7 +181,7 @@ class BaseFormat():
pass pass
def _is_ready_to_update(self): def _is_ready_to_update(self):
is_ready, self._match = self._ready_to_update, False is_ready, self._ready_to_update = self._ready_to_update, False
return is_ready return is_ready
def _is_match(self): def _is_match(self):

@ -8,6 +8,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class CompizFormat(BaseFormat): class CompizFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_section_line, self._parse_section_line,
@ -24,7 +26,8 @@ class CompizFormat(BaseFormat):
self._current_section_name = '' self._current_section_name = ''
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -32,17 +35,18 @@ class CompizFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
section_name = originalTextFor( section_name = originalTextFor(
OneOrMore(Word(alphanums+'_')) OneOrMore(Word(alphanums+'_'))
) )
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
self._section_line = (Literal('[').suppress() cls._section_line = (Literal('[').suppress()
+ Optional(action_symbols, default='')('action') + Optional(action_symbols, default='')('action')
+ section_name('name') + section_name('name')
+ Literal(']').suppress())('section_name') + Literal(']').suppress())('section_name')
parameter_name = originalTextFor( parameter_name = originalTextFor(
OneOrMore(Word(printables, OneOrMore(Word(printables,
@ -51,22 +55,23 @@ class CompizFormat(BaseFormat):
parameter_value = Word(printables) parameter_value = Word(printables)
self._parameter_line = (Group(Optional(action_symbols, cls._parameter_line = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name('name')
)('parameter_name')
+ Literal('=').suppress()
+ parameter_value('parameter_value'))
self._parameter_to_delete = (action_symbols('action')
+ parameter_name('name') + parameter_name('name')
+ restOfLine.suppress())('parameter_name') )('parameter_name')
+ Literal('=').suppress()
+ parameter_value('parameter_value'))
cls._parameter_to_delete = (action_symbols('action')
+ parameter_name('name')
+ restOfLine.suppress())('parameter_name')
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))
)('comment') )('comment')
cls._initialized = True
def _parse_section_line(self, line): def _parse_section_line(self, line):
try: try:

@ -11,6 +11,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class DovecotFormat(BaseFormat): class DovecotFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_section_start_line, self._parse_section_start_line,
@ -29,7 +31,8 @@ class DovecotFormat(BaseFormat):
self._current_section_name = '' self._current_section_name = ''
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -37,52 +40,53 @@ class DovecotFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
# Знаки пунктуации и действий. # Знаки пунктуации и действий.
left_brace = Literal('{') left_brace = Literal('{')
right_brace = Literal('}') right_brace = Literal('}')
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
self._comment_line_parser = originalTextFor( cls._comment_line_parser = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word( + ZeroOrMore(Word(
printables printables
+ pyparsing_unicode.alphanums) + pyparsing_unicode.alphanums)
) )
)('comment') )('comment')
# Для парсинга строк с началом секций. # Для парсинга строк с началом секций.
section = Word(alphas, alphanums+'-_', excludeChars='{}') section = Word(alphas, alphanums+'-_', excludeChars='{}')
section_name = Word(printables, excludeChars='{}') section_name = Word(printables, excludeChars='{}')
self._section_start_parser = Group(Optional(action_symbols, cls._section_start_parser = Group(Optional(action_symbols,
default='')('action') default='')('action')
+ section + section
+ Optional(section_name) + Optional(section_name)
+ left_brace.suppress())('name') + left_brace.suppress())('name')
# Для парсинга строк, указывающих конец секций. # Для парсинга строк, указывающих конец секций.
self._section_end_parser = lineStart() + right_brace + lineEnd() cls._section_end_parser = lineStart() + right_brace + lineEnd()
# Для парсинга строк, содержащих параметры. # Для парсинга строк, содержащих параметры.
parameter_name = Word(alphas, alphanums+'_-', excludeChars='{}=') parameter_name = Word(alphas, alphanums+'_-', excludeChars='{}=')
parameter_value = OneOrMore(Word(printables)) parameter_value = OneOrMore(Word(printables))
self._parameter_line_parser = (Group(Optional(action_symbols, cls._parameter_line_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name)('name') + parameter_name)('name')
+ Literal('=').suppress() + Literal('=').suppress()
+ originalTextFor( + originalTextFor(
parameter_value parameter_value
)('value')) )('value'))
# Для парсинга строк с параметрами, подлежащими удалению. # Для парсинга строк с параметрами, подлежащими удалению.
self._parameter_to_delete_parser = (action_symbols('action') cls._parameter_to_delete_parser = (action_symbols('action')
+ parameter_name + parameter_name
+ Optional(Literal('=')).suppress() + Optional(Literal('=')).suppress()
) )
# Для парсинга строк, содержащих директиву !include. # Для парсинга строк, содержащих директиву !include.
include = Keyword('!include') | Keyword('!include_try') include = Keyword('!include') | Keyword('!include_try')
@ -93,8 +97,9 @@ class DovecotFormat(BaseFormat):
include_line_to_delete = (action_symbols('action') + include('keyword') include_line_to_delete = (action_symbols('action') + include('keyword')
+ Word(printables)('value')) + Word(printables)('value'))
self._include_line_parser = (include_line_plain | cls._include_line_parser = (include_line_plain |
include_line_to_delete) include_line_to_delete)
cls._initialized = True
def _parse_section_start_line(self, line): def _parse_section_start_line(self, line):
try: try:

@ -4,10 +4,12 @@ from .base_format import BaseFormat
from collections import OrderedDict from collections import OrderedDict
from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
OneOrMore, alphanums, ParseException, restOfLine,\ OneOrMore, alphanums, ParseException, restOfLine,\
pyparsing_unicode, Group, Optional Group, Optional, Regex
class KDEFormat(BaseFormat): class KDEFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_section_line, self._parse_section_line,
@ -24,7 +26,8 @@ class KDEFormat(BaseFormat):
self._current_section_name = '' self._current_section_name = ''
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -32,7 +35,8 @@ class KDEFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
section_name_part_content = originalTextFor((OneOrMore( section_name_part_content = originalTextFor((OneOrMore(
Word(alphanums+':')))) Word(alphanums+':'))))
@ -41,37 +45,35 @@ class KDEFormat(BaseFormat):
+ section_name_part_content + section_name_part_content
+ Literal(']').suppress()) + Literal(']').suppress())
self._section_line = (Literal('[').suppress() cls._section_line = (Literal('[').suppress()
+ Optional(action_symbols, default='')('action') + Optional(action_symbols, default='')('action')
+ section_name_part_content + section_name_part_content
+ Literal(']').suppress() + Literal(']').suppress()
+ ZeroOrMore(section_name_part))('section_name') + ZeroOrMore(section_name_part))('section_name')
parameter_name = originalTextFor( parameter_name = originalTextFor(
OneOrMore(Word(printables, OneOrMore(Word(printables,
excludeChars='=')) excludeChars='='))
)('parameter_name') )('parameter_name')
parameter_value = originalTextFor(OneOrMore(Word( parameter_value = originalTextFor(OneOrMore(
pyparsing_unicode.alphanums Regex(r'[^\s]+')
+ printables)) ))('parameter_value')
)('parameter_value')
cls._parameter_line = (Group(Optional(action_symbols,
self._parameter_line = (Group(Optional(action_symbols, default='')('action')
default='')('action') + parameter_name)('parameter_name')
+ parameter_name)('parameter_name') + Literal('=').suppress()
+ Literal('=').suppress() + parameter_value('parameter_value'))
+ parameter_value('parameter_value'))
cls._parameter_to_delete = (action_symbols('action')
self._parameter_to_delete = (action_symbols('action') + parameter_name('name')
+ parameter_name('name') + restOfLine.suppress())('parameter_name')
+ restOfLine.suppress())('parameter_name')
cls._comment_line = originalTextFor(
self._comment_line = originalTextFor( Literal('#') + restOfLine
Literal('#')
+ ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums))
)('comment') )('comment')
cls._initialized = True
def _parse_section_line(self, line): def _parse_section_line(self, line):
try: try:
@ -107,7 +109,7 @@ class KDEFormat(BaseFormat):
self._match = True self._match = True
key_value = tuple(parsing_result.parameter_name.asList()) key_value = tuple(parsing_result.parameter_name.asList())
parameter_value = parsing_result.parameter_value parameter_value = parsing_result.parameter_value.strip()
parameter_value = self._last_comments_list + [parameter_value] parameter_value = self._last_comments_list + [parameter_value]
self._last_comments_list = [] self._last_comments_list = []
@ -138,7 +140,7 @@ class KDEFormat(BaseFormat):
self._match = True self._match = True
if not self._ignore_comments: if not self._ignore_comments:
self._last_comments_list.append(parsing_result.comment) self._last_comments_list.append(parsing_result.comment.strip())
except ParseException: except ParseException:
return return

@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class KernelFormat(BaseFormat): class KernelFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_parameter_line, self._parse_parameter_line,
@ -19,7 +21,8 @@ class KernelFormat(BaseFormat):
self._format = 'kernel' self._format = 'kernel'
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -27,7 +30,8 @@ class KernelFormat(BaseFormat):
documentLines = self._get_list_of_logic_lines(document_text) documentLines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(documentLines) self._lines_to_dictionary(documentLines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
parameter_name = Word(alphanums+'_')('parameter_name') parameter_name = Word(alphanums+'_')('parameter_name')
parameter_value = originalTextFor( parameter_value = originalTextFor(
@ -36,23 +40,24 @@ class KernelFormat(BaseFormat):
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
self._parameter_line = (Group(Optional(action_symbols, cls._parameter_line = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name('name') + parameter_name('name')
)('parameter_name') )('parameter_name')
+ Literal('=').suppress() + Literal('=').suppress()
+ parameter_value('parameter_value')) + parameter_value('parameter_value'))
self._parameter_to_delete = (Group(action_symbols('action') cls._parameter_to_delete = (Group(action_symbols('action')
+ parameter_name('name') + parameter_name('name')
)('parameter_name') )('parameter_name')
+ restOfLine.suppress()) + restOfLine.suppress())
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#').suppress() Literal('#').suppress()
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))
)('Comment') )('Comment')
cls._initialized = True
def _parse_parameter_line(self, line): def _parse_parameter_line(self, line):
try: try:

@ -9,6 +9,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class LDAPFormat(BaseFormat): class LDAPFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_type_line, self._parse_type_line,
@ -36,7 +38,8 @@ class LDAPFormat(BaseFormat):
self._current_type = ('', 'global') self._current_type = ('', 'global')
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -44,8 +47,9 @@ class LDAPFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
self._comment_line = originalTextFor( def _initialize_parser(cls):
cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))
@ -64,10 +68,10 @@ class LDAPFormat(BaseFormat):
OneOrMore(Word(printables)) OneOrMore(Word(printables))
)('value') )('value')
self._not_unique_parser = (Optional(action_symbols, cls._not_unique_parser = (Optional(action_symbols,
default='')('action') default='')('action')
+ not_unique_directives + not_unique_directives
+ not_unique_value + restOfLine.suppress()) + not_unique_value + restOfLine.suppress())
# Для выделения областей global, backend и database. # Для выделения областей global, backend и database.
type_sections_keywords = originalTextFor( type_sections_keywords = originalTextFor(
@ -77,10 +81,10 @@ class LDAPFormat(BaseFormat):
type_value = originalTextFor(Word(alphanums)) type_value = originalTextFor(Word(alphanums))
self._type_line = (Optional(action_symbols, default='')('action') cls._type_line = (Optional(action_symbols, default='')('action')
+ type_sections_keywords + type_sections_keywords
+ type_value + type_value
+ restOfLine.suppress()) + restOfLine.suppress())
# Для парсинга конструкции syncrepl rid=<replica ID> <parameters> # Для парсинга конструкции syncrepl rid=<replica ID> <parameters>
content_without_spaces = Word(printables, excludeChars='"') content_without_spaces = Word(printables, excludeChars='"')
@ -105,35 +109,35 @@ class LDAPFormat(BaseFormat):
+ assignment.suppress() + assignment.suppress()
+ Word(nums))('replicaID') + Word(nums))('replicaID')
self._syncrepl_line_parser = (Group(Optional(action_symbols, cls._syncrepl_line_parser = (Group(Optional(action_symbols,
default='')('action')
+ Keyword('syncrepl')
+ syncrepl_replica_id)('name')
+ values('Values')
+ restOfLine.suppress())
cls._syncrepl_value_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ Keyword('syncrepl') + originalTextFor(
+ syncrepl_replica_id)('name')
+ values('Values')
+ restOfLine.suppress())
self._syncrepl_value_parser = (Group(Optional(action_symbols,
default='')('action')
+ originalTextFor(
Word( Word(
printables, printables,
excludeChars='"=' excludeChars='"='
) )
))('name') ))('name')
+ assignment.suppress() + assignment.suppress()
+ originalTextFor( + originalTextFor(
OneOrMore( OneOrMore(
Word(printables) Word(printables)
) )
)('value')) )('value'))
self._syncrepl_line_to_delete_parser = (Group(Optional( cls._syncrepl_line_to_delete_parser = (Group(Optional(
action_symbols, action_symbols,
default='' default=''
)('action') )('action')
+ Keyword('syncrepl') + Keyword('syncrepl')
+ syncrepl_replica_id)('name') + syncrepl_replica_id)('name')
+ restOfLine.suppress()) + restOfLine.suppress())
# Для парсинга конструкции # Для парсинга конструкции
# access to <what> by <who>|<access level>|<control> # access to <what> by <who>|<access level>|<control>
@ -144,12 +148,12 @@ class LDAPFormat(BaseFormat):
content_without_spaces | content_without_spaces |
content_with_spaces) content_with_spaces)
self._access_line_parser = (Group(Optional(action_symbols, cls._access_line_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ access_keyword + access_keyword
+ value)('name') + value)('name')
+ Keyword('by').suppress() + Keyword('by').suppress()
+ delimitedList( + delimitedList(
originalTextFor(value + originalTextFor(value +
SkipTo( SkipTo(
Keyword('by'), Keyword('by'),
@ -157,52 +161,53 @@ class LDAPFormat(BaseFormat):
restOfLine restOfLine
), ),
delim='by' delim='by'
)('Values')) )('Values'))
self._access_value_parser = (Group(Optional(action_symbols, cls._access_value_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ originalTextFor(value))('name') + originalTextFor(value))('name')
+ originalTextFor( + originalTextFor(
Optional(Word(alphanums)) Optional(Word(alphanums))
)('value')) )('value'))
self._access_line_to_delete_parser = (Group(action_symbols('action') cls._access_line_to_delete_parser = (Group(action_symbols('action')
+ access_keyword + access_keyword
+ value + value
+ restOfLine.suppress())('name')) + restOfLine.suppress())('name'))
# Для парсинга строк с директивами index. # Для парсинга строк с директивами index.
self._index_line_parser = (Group(Optional(action_symbols, cls._index_line_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ Keyword('index') + Keyword('index')
+ originalTextFor(Word(printables)) + originalTextFor(Word(printables))
)('name') )('name')
+ originalTextFor( + originalTextFor(
OneOrMore(Word(printables)) OneOrMore(Word(printables))
)('value')) )('value'))
self._index_line_to_delete_parser = (Group(action_symbols('action') cls._index_line_to_delete_parser = (Group(action_symbols('action')
+ Keyword('index') + Keyword('index')
+ originalTextFor( + originalTextFor(
Word(printables) Word(printables)
))('name')) ))('name'))
# Для парсинга остальных директив. # Для парсинга остальных директив.
self._directive_line_parser = (Group(Optional(action_symbols, cls._directive_line_parser = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ originalTextFor( + originalTextFor(
Word(printables) Word(printables)
))('name') ))('name')
+ originalTextFor( + originalTextFor(
OneOrMore(Word( OneOrMore(Word(
printables printables
) )
))('value')) ))('value'))
self._directive_line_to_delete_parser = (action_symbols('action') cls._directive_line_to_delete_parser = (action_symbols('action')
+ originalTextFor( + originalTextFor(
Word(printables) Word(printables)
))('name') ))('name')
cls._initialized = True
def _get_list_of_logic_lines(self, text): def _get_list_of_logic_lines(self, text):
list_of_lines = [] list_of_lines = []
@ -354,7 +359,8 @@ class LDAPFormat(BaseFormat):
parsing_result = self._syncrepl_line_parser.parseString(line) parsing_result = self._syncrepl_line_parser.parseString(line)
self._match = True self._match = True
values = [value.strip() for value in parsing_result.Values.asList()] values = [value.strip() for value in
parsing_result.Values.asList()]
parameter_name = tuple(parsing_result.name.asList()) parameter_name = tuple(parsing_result.name.asList())
value_dictionary = OrderedDict() value_dictionary = OrderedDict()
@ -431,7 +437,9 @@ class LDAPFormat(BaseFormat):
Аргументы: line -- строка, которую нужно распарсить. Аргументы: line -- строка, которую нужно распарсить.
''' '''
try: try:
parsing_result = self._index_line_to_delete_parser.parseString(line) parsing_result = self._index_line_to_delete_parser.parseString(
line
)
self._match = True self._match = True
parameter_name = tuple(parsing_result.name.asList()) parameter_name = tuple(parsing_result.name.asList())

@ -8,6 +8,8 @@ from pyparsing import Word, Literal, printables, originalTextFor, ZeroOrMore,\
class OpenRCFormat(BaseFormat): class OpenRCFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_parameter_line, self._parse_parameter_line,
@ -19,7 +21,8 @@ class OpenRCFormat(BaseFormat):
self._format = 'openrc' self._format = 'openrc'
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -27,29 +30,31 @@ class OpenRCFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
parameter_name = Word(printables, excludeChars='=') parameter_name = Word(printables, excludeChars='=')
parameter_value = originalTextFor(OneOrMore(Word(printables))) parameter_value = originalTextFor(OneOrMore(Word(printables)))
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
self._parameter_line = (Group(Optional(action_symbols, cls._parameter_line = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name('name'))('parameter_name') + parameter_name('name'))('parameter_name')
+ Literal('=').suppress() + Literal('=').suppress()
+ parameter_value('parameter_value')) + parameter_value('parameter_value'))
self._parameter_to_delete = (Group(action_symbols('action') cls._parameter_to_delete = (Group(action_symbols('action')
+ parameter_name('name') + parameter_name('name')
)('parameter_name') )('parameter_name')
+ restOfLine.suppress()) + restOfLine.suppress())
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))
)('comment') )('comment')
cls._initialized = True
def _parse_parameter_line(self, line): def _parse_parameter_line(self, line):
try: try:

@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class PostfixFormat(BaseFormat): class PostfixFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_parameter_line, self._parse_parameter_line,
@ -19,7 +21,8 @@ class PostfixFormat(BaseFormat):
self._format = 'postfix' self._format = 'postfix'
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -27,7 +30,8 @@ class PostfixFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
parameter_name = Word(alphanums+'_') parameter_name = Word(alphanums+'_')
parameter_value = originalTextFor(OneOrMore(Word(printables))) parameter_value = originalTextFor(OneOrMore(Word(printables)))
@ -36,17 +40,17 @@ class PostfixFormat(BaseFormat):
assignment = Literal('=') assignment = Literal('=')
self._parameter_line = (Group(Optional(action_symbols, cls._parameter_line = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name)('parameter_name') + parameter_name)('parameter_name')
+ assignment.suppress() + assignment.suppress()
+ parameter_value('parameter_value')) + parameter_value('parameter_value'))
self._parameter_to_delete = (action_symbols('action') cls._parameter_to_delete = (action_symbols('action')
+ parameter_name('Name') + parameter_name('Name')
)('parameter_name') )('parameter_name')
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))

@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class ProcmailFormat(BaseFormat): class ProcmailFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_parameter_line, self._parse_parameter_line,
@ -19,7 +21,8 @@ class ProcmailFormat(BaseFormat):
self._format = 'procmail' self._format = 'procmail'
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -27,32 +30,34 @@ class ProcmailFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
parameter_name = Word(alphanums+'_.') parameter_name = Word(alphanums+'_.')
parameter_value = originalTextFor(OneOrMore(Word(printables))) parameter_value = originalTextFor(OneOrMore(Word(printables)))
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
self._parameter_line = (Group(Optional(action_symbols, cls._parameter_line = (Group(Optional(action_symbols,
default='')('action') default='')('action')
+ parameter_name('Name') + parameter_name('Name')
)('parameter_name') )('parameter_name')
+ Literal('=').suppress() + Literal('=').suppress()
+ parameter_value('parameter_value')) + parameter_value('parameter_value'))
self._parameter_to_delete = (Group(action_symbols('action') cls._parameter_to_delete = (Group(action_symbols('action')
+ parameter_name('Name') + parameter_name('Name')
)('parameter_name') )('parameter_name')
+ restOfLine.suppress()) + restOfLine.suppress())
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore( + ZeroOrMore(
Word(printables Word(printables
+ pyparsing_unicode.alphanums) + pyparsing_unicode.alphanums)
) )
)('comment') )('comment')
cls._initialized = True
def _parse_parameter_line(self, line): def _parse_parameter_line(self, line):
try: try:

@ -9,6 +9,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class ProFTPDFormat(BaseFormat): class ProFTPDFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False): def __init__(self, document_text: str, ignore_comments=False):
processing_methods = [self._parse_comment_line, processing_methods = [self._parse_comment_line,
self._parse_section_start_line, self._parse_section_start_line,
@ -30,7 +32,8 @@ class ProFTPDFormat(BaseFormat):
self._actions_stack = [] self._actions_stack = []
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -38,7 +41,8 @@ class ProFTPDFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
left_angle_bracket = Literal('<') left_angle_bracket = Literal('<')
right_angle_bracket = Literal('>') right_angle_bracket = Literal('>')
slash = Literal('/') slash = Literal('/')
@ -52,31 +56,31 @@ class ProFTPDFormat(BaseFormat):
directive_value = Word(printables) directive_value = Word(printables)
self._section_start_parser = (left_angle_bracket.suppress() cls._section_start_parser = (left_angle_bracket.suppress()
+ Optional(action_symbols, + Optional(action_symbols,
default='')('action') default='')('action')
+ Group(directive('name') + Group(directive('name')
+ originalTextFor( + originalTextFor(
OneOrMore(section_value) OneOrMore(section_value)
)('value') )('value')
)('directive') )('directive')
+ right_angle_bracket.suppress()) + right_angle_bracket.suppress())
self._section_end_parser = (left_angle_bracket.suppress() cls._section_end_parser = (left_angle_bracket.suppress()
+ slash.suppress() + slash.suppress()
+ directive('directive') + directive('directive')
+ right_angle_bracket.suppress()) + right_angle_bracket.suppress())
self._plain_directive_parser = (Optional(action_symbols)('action') cls._plain_directive_parser = (Optional(action_symbols)('action')
+ Group(directive('name') + Group(directive('name')
+ originalTextFor( + originalTextFor(
OneOrMore(directive_value) OneOrMore(directive_value)
)('value') )('value')
)('directive') )('directive')
) )
self._delete_plain_directive_parser = (action_symbols('action') cls._delete_plain_directive_parser = (action_symbols('action')
+ directive('directive')) + directive('directive'))
single_key_directive = (Keyword('AllowAll') | single_key_directive = (Keyword('AllowAll') |
Keyword('DenyAll') | Keyword('DenyAll') |
@ -87,8 +91,8 @@ class ProFTPDFormat(BaseFormat):
Keyword('CwdRatioMsg') | Keyword('CwdRatioMsg') |
Keyword('FileRatioErrMsg')) Keyword('FileRatioErrMsg'))
self._single_key_directive_parser = (Optional(action_symbols)('action') cls._single_key_directive_parser = (Optional(action_symbols)('action')
+ single_key_directive( + single_key_directive(
'directive' 'directive'
)) ))
@ -138,26 +142,26 @@ class ProFTPDFormat(BaseFormat):
Keyword('ModuleControlsACLs') | Keyword('ModuleControlsACLs') |
Keyword('ControlsACLs')) Keyword('ControlsACLs'))
self._double_key_directive_parser = (Optional(action_symbols)('action') cls._double_key_directive_parser = (Optional(action_symbols)('action')
+ Group((double_key_directive + Group((double_key_directive
+ directive_value + directive_value
)('name') )('name')
+ originalTextFor( + originalTextFor(
ZeroOrMore( ZeroOrMore(
directive_value directive_value
) )
)('value') )('value')
)('directive') )('directive')
) )
self._delete_double_key_directive_parser = (action_symbols('action') cls._delete_double_key_directive_parser = (action_symbols('action')
+ Group( + Group(
( (
double_key_directive double_key_directive
+ directive_value + directive_value
)('name') )('name')
)('directive') )('directive')
) )
full_key_directive = (Keyword('AllowClass') | full_key_directive = (Keyword('AllowClass') |
Keyword('AllowGroup') | Keyword('AllowGroup') |
@ -176,19 +180,20 @@ class ProFTPDFormat(BaseFormat):
Keyword('TimeoutSession') | Keyword('TimeoutSession') |
Keyword('UserAlias')) Keyword('UserAlias'))
self._full_key_directive_parser = (Optional(action_symbols)('action') cls._full_key_directive_parser = (Optional(action_symbols)('action')
+ Group(full_key_directive + Group(full_key_directive
+ OneOrMore( + OneOrMore(
directive_value directive_value
) )
)('directive') )('directive')
) )
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
Literal('#') Literal('#')
+ ZeroOrMore(Word(printables + ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums)) + pyparsing_unicode.alphanums))
)('comment') )('comment')
cls._initialized = True
def _parse_section_start_line(self, line): def _parse_section_start_line(self, line):
try: try:

@ -2,12 +2,13 @@
# #
from .base_format import BaseFormat from .base_format import BaseFormat
from collections import OrderedDict from collections import OrderedDict
from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ from pyparsing import originalTextFor, Literal, Word, printables, OneOrMore,\
OneOrMore, alphanums, ParseException, pyparsing_unicode,\ alphanums, ParseException, Optional, Group, restOfLine
Optional, Group
class SambaFormat(BaseFormat): class SambaFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, def __init__(self, document_text: str,
ignore_comments=False, ignore_comments=False,
join_before=False): join_before=False):
@ -29,7 +30,8 @@ class SambaFormat(BaseFormat):
self._format = 'samba' self._format = 'samba'
self._last_comments_list = [] self._last_comments_list = []
self._initialize_parser() if not self._initialized:
self._initialize_parser()
if document_text == '': if document_text == '':
self._document_dictionary = OrderedDict() self._document_dictionary = OrderedDict()
@ -37,7 +39,8 @@ class SambaFormat(BaseFormat):
document_lines = self._get_list_of_logic_lines(document_text) document_lines = self._get_list_of_logic_lines(document_text)
self._lines_to_dictionary(document_lines) self._lines_to_dictionary(document_lines)
def _initialize_parser(self): @classmethod
def _initialize_parser(cls):
action_symbols = (Literal('!') | Literal('-')) action_symbols = (Literal('!') | Literal('-'))
comment_symbols = (Literal('#') | Literal(';')) comment_symbols = (Literal('#') | Literal(';'))
@ -46,10 +49,10 @@ class SambaFormat(BaseFormat):
section_name = originalTextFor(OneOrMore(Word(alphanums+'_'))) section_name = originalTextFor(OneOrMore(Word(alphanums+'_')))
self._section_line = (Literal('[').suppress() cls._section_line = (Literal('[').suppress()
+ Optional(action_symbols, default='')('action') + Optional(action_symbols, default='')('action')
+ section_name('name') + Literal(']').suppress() + section_name('name') + Literal(']').suppress()
)('section_name') )('section_name')
parameter_name = originalTextFor( parameter_name = originalTextFor(
OneOrMore(Word(printables, OneOrMore(Word(printables,
@ -60,23 +63,21 @@ class SambaFormat(BaseFormat):
OneOrMore(Word(printables)) OneOrMore(Word(printables))
) )
self._parameter_line = (Group(Optional(action_symbols('action'), cls._parameter_line = (Group(Optional(action_symbols('action'),
default='') default='')
+ parameter_name('name')
)('parameter_name')
+ assignment_symbol.suppress()
+ parameter_value('parameter_value'))
self._parameter_to_delete = (action_symbols('action')
+ parameter_name('name') + parameter_name('name')
)('parameter_name') )('parameter_name')
+ assignment_symbol.suppress()
+ parameter_value('parameter_value'))
cls._parameter_to_delete = (action_symbols('action')
+ parameter_name('name')
)('parameter_name')
self._comment_line = originalTextFor( cls._comment_line = originalTextFor(
comment_symbols comment_symbols + restOfLine
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums))
)('comment') )('comment')
cls._initialized = True
def _parse_section_line(self, line): def _parse_section_line(self, line):
try: try:
@ -144,7 +145,7 @@ class SambaFormat(BaseFormat):
self._match = True self._match = True
if not self._ignore_comments: if not self._ignore_comments:
self._last_comments_list.append(parsing_result.comment) self._last_comments_list.append(parsing_result.comment.strip())
except ParseException: except ParseException:
return return

Binary file not shown.

@ -195,11 +195,11 @@ class TestParsingMethods:
section_2_content = OrderedDict({('', 'alignment'): ['132'], section_2_content = OrderedDict({('', 'alignment'): ['132'],
('', 'panelVisibility'): ['1']}) ('', 'panelVisibility'): ['1']})
section3Content = OrderedDict({('', 'Exec'): ['konversation -qwindowtitle %c %u']}) section_3_content = OrderedDict({('', 'Exec'): ['konversation -qwindowtitle %c %u']})
result = OrderedDict({('', 'PlasmaViews', 'Panel 69', 'Horizontal2048'): section_1_content, result = OrderedDict({('', 'PlasmaViews', 'Panel 69', 'Horizontal2048'): section_1_content,
('!', 'PlasmaViews', 'Panel 69'): section_2_content, ('!', 'PlasmaViews', 'Panel 69'): section_2_content,
('-', 'Desktop Entry'): section3Content}) ('-', 'Desktop Entry'): section_3_content})
kde_object = KDEFormat(document_text) kde_object = KDEFormat(document_text)
assert kde_object._document_dictionary == result assert kde_object._document_dictionary == result

Loading…
Cancel
Save