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