comment parameter support was added for some formats.

packages
Иванов Денис 4 years ago
parent 63daadb9d3
commit 5f675b461a

@ -11,13 +11,18 @@ from pyparsing import originalTextFor, OneOrMore, Word, alphanums, Literal,\
class BINDFormat(BaseFormat):
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False,
comment_symbol=''):
processing_methods = []
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._comments_processing = True
self._join_before = join_before
self._comment_symbol = comment_symbol
self._format = 'bind'
self._last_comments_list = []
@ -101,10 +106,20 @@ class BINDFormat(BaseFormat):
# Для парсинга комментариев.
python_style_comment = originalTextFor(Literal('#') + restOfLine)
comments = (cppStyleComment |
python_style_comment).setParseAction(
if not self._comment_symbol:
comments = (cppStyleComment |
python_style_comment).setParseAction(
self._create_comment_list
)
)
else:
custom_style_comment = originalTextFor(
Literal(self._comment_symbol) + restOfLine
)
comments = (cppStyleComment |
python_style_comment |
custom_style_comment).setParseAction(
self._create_comment_list
)
# Для парсинга директивы include.
include_line = (Optional(action_symbols, default='')('action')

@ -9,8 +9,10 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class CompizFormat(BaseFormat):
_initialized = False
_comment_symbol = ''
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str, ignore_comments=False,
join_before=False, comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_section_line,
self._parse_parameter_line,
@ -19,6 +21,7 @@ class CompizFormat(BaseFormat):
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._comments_processing = True
self._join_before = join_before
self._need_finish = True
self._format = 'compiz'
@ -26,8 +29,8 @@ class CompizFormat(BaseFormat):
self._current_section_name = ''
self._last_comments_list = []
if not self._initialized:
self._initialize_parser()
if not self._initialized or comment_symbol != self._comment_symbol:
self._initialize_parser(comment_symbol)
if document_text == '':
self._document_dictionary = OrderedDict()
@ -36,7 +39,7 @@ class CompizFormat(BaseFormat):
self._lines_to_dictionary(document_lines)
@classmethod
def _initialize_parser(cls):
def _initialize_parser(cls, comment_symbol):
section_name = originalTextFor(
OneOrMore(Word(alphanums+'_'))
)
@ -66,11 +69,21 @@ class CompizFormat(BaseFormat):
+ parameter_name('name')
+ restOfLine.suppress())('parameter_name')
cls._comment_line = originalTextFor(
Literal('#')
+ ZeroOrMore(Word(printables
+ pyparsing_unicode.alphanums))
)('comment')
if not comment_symbol:
cls._comment_line = originalTextFor(
Literal('#')
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums))
)('comment')
else:
cls._comment_line = originalTextFor(
(Literal('#') | Literal(comment_symbol))
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums))
)('comment')
cls._initialized = True
def _parse_section_line(self, line):

@ -5,7 +5,7 @@ from os import path
class DiffFormat():
def __init__(self, document_text: str):
def __init__(self, document_text: str, comment_symbol=''):
self._patch_text = document_text
self._root_path = ''
self._last_level = 0

@ -12,9 +12,10 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class DovecotFormat(BaseFormat):
_initialized = False
_comment_symbol = ''
def __init__(self, document_text: str, ignore_comments=False,
join_before=False):
join_before=False, comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_section_start_line,
self._parse_include_line,
@ -24,8 +25,8 @@ class DovecotFormat(BaseFormat):
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._need_finish = True
self._comments_processing = True
self._need_finish = True
self._join_before = join_before
self._format = 'dovecot'
@ -33,8 +34,8 @@ class DovecotFormat(BaseFormat):
self._current_section_name = ''
self._last_comments_list = []
if not self._initialized:
self._initialize_parser()
if not self._initialized or comment_symbol != self._comment_symbol:
self._initialize_parser(comment_symbol)
if document_text == '':
self._document_dictionary = OrderedDict()
@ -43,19 +44,28 @@ class DovecotFormat(BaseFormat):
self._lines_to_dictionary(document_lines)
@classmethod
def _initialize_parser(cls):
def _initialize_parser(cls, comment_symbol=''):
# Знаки пунктуации и действий.
left_brace = Literal('{')
right_brace = Literal('}')
action_symbols = (Literal('!') | Literal('-'))
cls._comment_line_parser = originalTextFor(
Literal('#')
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums)
)
)('comment')
if not comment_symbol:
cls._comment_line_parser = originalTextFor(
Literal('#')
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums)
)
)('comment')
else:
cls._comment_line_parser = originalTextFor(
(Literal('#') | Literal(comment_symbol))
+ ZeroOrMore(Word(
printables
+ pyparsing_unicode.alphanums)
)
)('comment')
# Для парсинга строк с началом секций.
section = Word(alphas, alphanums+'-_', excludeChars='{}')

@ -7,7 +7,7 @@ import json
class JSONFormat(BaseFormat):
def __init__(self, document_text: str, ignore_comments=False,
join_before=False):
join_before=False, comment_symbol=''):
processing_methods = []
super().__init__(processing_methods)
self._ignore_comments = ignore_comments

@ -10,7 +10,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class KDEFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str, ignore_comments=False,
join_before=False, comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_section_line,
self._parse_parameter_line,
@ -19,6 +20,7 @@ class KDEFormat(BaseFormat):
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._comments_processing = True
self._join_before = join_before
self._need_finish = True
self._format = 'kde'

@ -10,13 +10,15 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class KernelFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str, ignore_comments=False,
join_before=False, comment_symbol=''):
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._join_before = join_before
self._comments_processing = True
self._format = 'kernel'

@ -11,7 +11,10 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class LDAPFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_type_line,
self._parse_access_line,
@ -27,6 +30,7 @@ class LDAPFormat(BaseFormat):
super().__init__(processing_methods)
self._ignore_comments = ignore_comments
self._comments_processing = True
self._join_before = join_before
self._need_finish = True
self._format = 'ldap'

@ -10,7 +10,10 @@ from pyparsing import Word, Literal, printables, originalTextFor, ZeroOrMore,\
class OpenRCFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_parameter_line,
self._parse_to_delete_line]

@ -10,7 +10,8 @@ except ImportError:
class PatchFormat(BaseFormat):
def __init__(self, document_text: str, multiline=False, dotall=False):
def __init__(self, document_text: str, multiline=False, dotall=False,
comment_symbol=''):
processing_methods = OrderedDict()
super().__init__(processing_methods)
self._format = 'patch'

@ -10,7 +10,10 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class PostfixFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_parameter_line,
self._parse_to_delete_line]

@ -10,7 +10,10 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\
class ProcmailFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=True,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_parameter_line,
self._parse_to_delete_line]

@ -11,7 +11,10 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\
class ProFTPDFormat(BaseFormat):
_initialized = False
def __init__(self, document_text: str, ignore_comments=False):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_section_start_line,
self._parse_section_end_line,

@ -11,7 +11,8 @@ class SambaFormat(BaseFormat):
def __init__(self, document_text: str,
ignore_comments=False,
join_before=False):
join_before=False,
comment_symbol=''):
processing_methods = [self._parse_comment_line,
self._parse_section_line,
self._parse_parameter_line,

@ -140,6 +140,70 @@ acl "dns_servers" {
bind_object = BINDFormat(document_text)
assert bind_object._document_dictionary == result
def test_if_comment_parameter_is_set_for_template__format_object_will_parse_comments_with_comment_symbol_from_this_parameter(self):
document_text = '''
@ Comment 1
!pid-file "/run/named/named.pid";
/*
* A very big comment.
* Still here...
* The pure giant of the comment kind.
*/
-disable-empty-zone "10.in-addr.arpa";
@ Comment 2
@ Comment 3
acl "dns_servers" {
!127.0.0.1;
// Comment 4
10.0.1.3;
10.1.0.3;
};
-options {
!response-policy {
/*
* This comment is very important.
* And I have no idea, why this
* comment is so important.
*/
zone "rpz.zone";
};
!recursion yes;
}
'''
acl_section = OrderedDict({'#': ['@ Comment 2', '@ Comment 3'],
('!', '127.0.0.1'): [''],
('', '10.0.1.3'): ['// Comment 4', ''],
('', '10.1.0.3'): ['']})
response_section = OrderedDict({('', 'zone'):
['/*',
'* This comment is very important.',
'* And I have no idea, why this',
'* comment is so important.',
'*/',
'"rpz.zone"']})
options_section = OrderedDict({('!', 'response-policy'):
response_section,
('!', 'recursion'): ['yes']})
result = OrderedDict({('!', 'pid-file'):
['@ Comment 1',
'"/run/named/named.pid"'],
('-', 'disable-empty-zone'):
['/*',
'* A very big comment.',
'* Still here...',
'* The pure giant of the comment kind.',
'*/',
'"10.in-addr.arpa"'],
('', 'acl', '"dns_servers"'): acl_section,
('-', 'options'): options_section})
bind_object = BINDFormat(document_text, comment_symbol='@')
assert bind_object._document_dictionary == result
def test_if_parameters_and_blocks_in_input_document_has_some_comments__the_comments_will_be_collected_in_the_list_of_parameter_value_or_with_special_key_in_block_dictionary(self):
document_text = '''
// Comment 1

@ -139,7 +139,46 @@ class TestParsingMethods:
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_the_IgnoreComments_flag_is_set__the_parser_ignores_all_comments(self):
def test_if_comment_parameter_is_set_for_template__format_object_will_parse_comments_with_comment_symbol_from_this_parameter(self):
document_text = '''
# Comment
[Added Associations]
@ Comment1
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
@ Comment2
# Comment3
application/rtf=libreoffice-writer.desktop;
[Other Section]
@Comment
!application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section_1 = OrderedDict({'#': ['# Comment'],
('', 'application/illustrator'):
['@ Comment1',
'zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['@ Comment2',
'# Comment3',
'libreoffice-writer.desktop;']})
section_2 = OrderedDict({('!', 'application/vnd.oasis.opendocument.spreadsheet'):
['@Comment',
"calculate-calc.desktop;"]})
result = OrderedDict({('', 'Added Associations'): section_1,
('', 'Other Section'): section_2})
compiz_object = CompizFormat(document_text, comment_symbol='@')
assert compiz_object._document_dictionary == result
def test_if_the_ignore_comments_flag_is_set__the_parser_ignores_all_comments(self):
document_text = '''
# Comment
[Added Associations]

@ -362,63 +362,3 @@ database bdb
result_text = result_file.read()
assert ldap_original_object.get_document_text() == result_text
def test_make_template(self):
document_1 = '''
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/nis.schema
access to attrs=userPassword
by dn="cn=ldapadmin,dc=calculate" write
by dn="ou=Samba,ou=Services,dc=calculate" write
by dn="ou=Mail,ou=Services,dc=calculate" read
by dn="ou=Ftp,ou=Services,dc=calculate" read
by dn="ou=Replication,ou=LDAP,ou=Services,dc=calculate" write
by self read
by * auth
backend bdb
suffix "dc=example"
modulepath /usr/lib/openldap
database bdb
include /etc/openldap/replication.conf
suffix "dc=example, dc=calculate"
cachesize 10000
sizelimit unlimited
'''
document_2 = '''
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
access to attrs=userPassword
by dn="cn=ldapadmin,dc=calculate" write
by dn="ou=Samba,ou=Services,dc=calculate" write
by dn="ou=Unix,ou=Services,dc=calculate" write
by dn="ou=Jabber,ou=Services,dc=calculate" read
by dn="ou=Replication,ou=LDAP,ou=Services,dc=calculate" write
by self read
by * auth
backend bdb
suffix "dc=example"
rootdn "cn=ldaproot,dc=calculate"
modulepath /usr/lib/openldap
database bdb
include /etc/openldap/replication.conf
suffix "dc=example, dc=calculate"
checkpoint 1024 5
sizelimit limited
'''
document_1_object = LDAPFormat(document_1)
document_2_object = LDAPFormat(document_2)
template = document_1_object.make_template(document_2_object)
print('Template:')
print(template.get_document_text())
document_1_object.join_template(template)
print('Joining result:')
print(document_1_object.get_document_text())
assert False
# assert document_1_object.get_document_text() == document_2

Loading…
Cancel
Save