From e82bd4cd4d2759d347d3b6dc3e50db3228507d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=81?= Date: Tue, 18 Feb 2020 09:02:01 +0300 Subject: [PATCH] Methods for initialization of parsers is class methods now. --- calculate/templates/format/base_format.py | 2 +- calculate/templates/format/compiz_format.py | 37 +++-- calculate/templates/format/dovecot_format.py | 61 ++++---- calculate/templates/format/kde_format.py | 60 ++++---- calculate/templates/format/kernel_format.py | 31 ++-- calculate/templates/format/ldap_format.py | 140 +++++++++--------- calculate/templates/format/openrc_format.py | 29 ++-- calculate/templates/format/postfix_format.py | 26 ++-- calculate/templates/format/procmail_format.py | 33 +++-- calculate/templates/format/proftpd_format.py | 101 +++++++------ calculate/templates/format/samba_format.py | 47 +++--- calculate/vars/.datavars.py.swp | Bin 12288 -> 0 bytes tests/format/test_kde.py | 4 +- 13 files changed, 308 insertions(+), 263 deletions(-) delete mode 100644 calculate/vars/.datavars.py.swp diff --git a/calculate/templates/format/base_format.py b/calculate/templates/format/base_format.py index 891d759..704459a 100644 --- a/calculate/templates/format/base_format.py +++ b/calculate/templates/format/base_format.py @@ -181,7 +181,7 @@ class BaseFormat(): pass 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 def _is_match(self): diff --git a/calculate/templates/format/compiz_format.py b/calculate/templates/format/compiz_format.py index 736b8fe..c13fcac 100644 --- a/calculate/templates/format/compiz_format.py +++ b/calculate/templates/format/compiz_format.py @@ -8,6 +8,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ class CompizFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_section_line, @@ -24,7 +26,8 @@ class CompizFormat(BaseFormat): self._current_section_name = '' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -32,17 +35,18 @@ class CompizFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): section_name = originalTextFor( OneOrMore(Word(alphanums+'_')) ) action_symbols = (Literal('!') | Literal('-')) - self._section_line = (Literal('[').suppress() - + Optional(action_symbols, default='')('action') - + section_name('name') - + Literal(']').suppress())('section_name') + cls._section_line = (Literal('[').suppress() + + Optional(action_symbols, default='')('action') + + section_name('name') + + Literal(']').suppress())('section_name') parameter_name = originalTextFor( OneOrMore(Word(printables, @@ -51,22 +55,23 @@ class CompizFormat(BaseFormat): parameter_value = Word(printables) - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name('name') - )('parameter_name') - + Literal('=').suppress() - + parameter_value('parameter_value')) - - self._parameter_to_delete = (action_symbols('action') + cls._parameter_line = (Group(Optional(action_symbols, + default='')('action') + 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('#') + ZeroOrMore(Word(printables + pyparsing_unicode.alphanums)) )('comment') + cls._initialized = True def _parse_section_line(self, line): try: diff --git a/calculate/templates/format/dovecot_format.py b/calculate/templates/format/dovecot_format.py index f4397ed..315e2d5 100644 --- a/calculate/templates/format/dovecot_format.py +++ b/calculate/templates/format/dovecot_format.py @@ -11,6 +11,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ class DovecotFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_section_start_line, @@ -29,7 +31,8 @@ class DovecotFormat(BaseFormat): self._current_section_name = '' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -37,52 +40,53 @@ class DovecotFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): # Знаки пунктуации и действий. left_brace = Literal('{') right_brace = Literal('}') action_symbols = (Literal('!') | Literal('-')) - self._comment_line_parser = originalTextFor( - Literal('#') - + ZeroOrMore(Word( - printables - + pyparsing_unicode.alphanums) - ) - )('comment') + cls._comment_line_parser = originalTextFor( + Literal('#') + + ZeroOrMore(Word( + printables + + pyparsing_unicode.alphanums) + ) + )('comment') # Для парсинга строк с началом секций. section = Word(alphas, alphanums+'-_', excludeChars='{}') section_name = Word(printables, excludeChars='{}') - self._section_start_parser = Group(Optional(action_symbols, - default='')('action') - + section - + Optional(section_name) - + left_brace.suppress())('name') + cls._section_start_parser = Group(Optional(action_symbols, + default='')('action') + + section + + Optional(section_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_value = OneOrMore(Word(printables)) - self._parameter_line_parser = (Group(Optional(action_symbols, - default='')('action') - + parameter_name)('name') - + Literal('=').suppress() - + originalTextFor( - parameter_value - )('value')) + cls._parameter_line_parser = (Group(Optional(action_symbols, + default='')('action') + + parameter_name)('name') + + Literal('=').suppress() + + originalTextFor( + parameter_value + )('value')) # Для парсинга строк с параметрами, подлежащими удалению. - self._parameter_to_delete_parser = (action_symbols('action') - + parameter_name - + Optional(Literal('=')).suppress() - ) + cls._parameter_to_delete_parser = (action_symbols('action') + + parameter_name + + Optional(Literal('=')).suppress() + ) # Для парсинга строк, содержащих директиву !include. include = Keyword('!include') | Keyword('!include_try') @@ -93,8 +97,9 @@ class DovecotFormat(BaseFormat): include_line_to_delete = (action_symbols('action') + include('keyword') + Word(printables)('value')) - self._include_line_parser = (include_line_plain | - include_line_to_delete) + cls._include_line_parser = (include_line_plain | + include_line_to_delete) + cls._initialized = True def _parse_section_start_line(self, line): try: diff --git a/calculate/templates/format/kde_format.py b/calculate/templates/format/kde_format.py index 34292eb..e434b77 100644 --- a/calculate/templates/format/kde_format.py +++ b/calculate/templates/format/kde_format.py @@ -4,10 +4,12 @@ from .base_format import BaseFormat from collections import OrderedDict from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ OneOrMore, alphanums, ParseException, restOfLine,\ - pyparsing_unicode, Group, Optional + Group, Optional, Regex class KDEFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_section_line, @@ -24,7 +26,8 @@ class KDEFormat(BaseFormat): self._current_section_name = '' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -32,7 +35,8 @@ class KDEFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): action_symbols = (Literal('!') | Literal('-')) section_name_part_content = originalTextFor((OneOrMore( Word(alphanums+':')))) @@ -41,37 +45,35 @@ class KDEFormat(BaseFormat): + section_name_part_content + Literal(']').suppress()) - self._section_line = (Literal('[').suppress() - + Optional(action_symbols, default='')('action') - + section_name_part_content - + Literal(']').suppress() - + ZeroOrMore(section_name_part))('section_name') + cls._section_line = (Literal('[').suppress() + + Optional(action_symbols, default='')('action') + + section_name_part_content + + Literal(']').suppress() + + ZeroOrMore(section_name_part))('section_name') parameter_name = originalTextFor( OneOrMore(Word(printables, excludeChars='=')) )('parameter_name') - parameter_value = originalTextFor(OneOrMore(Word( - pyparsing_unicode.alphanums - + printables)) - )('parameter_value') - - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name)('parameter_name') - + Literal('=').suppress() - + parameter_value('parameter_value')) - - self._parameter_to_delete = (action_symbols('action') - + parameter_name('name') - + restOfLine.suppress())('parameter_name') - - self._comment_line = originalTextFor( - Literal('#') - + ZeroOrMore(Word(printables - + pyparsing_unicode.alphanums)) + parameter_value = originalTextFor(OneOrMore( + Regex(r'[^\s]+') + ))('parameter_value') + + cls._parameter_line = (Group(Optional(action_symbols, + default='')('action') + + parameter_name)('parameter_name') + + Literal('=').suppress() + + parameter_value('parameter_value')) + + cls._parameter_to_delete = (action_symbols('action') + + parameter_name('name') + + restOfLine.suppress())('parameter_name') + + cls._comment_line = originalTextFor( + Literal('#') + restOfLine )('comment') + cls._initialized = True def _parse_section_line(self, line): try: @@ -107,7 +109,7 @@ class KDEFormat(BaseFormat): self._match = True 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] self._last_comments_list = [] @@ -138,7 +140,7 @@ class KDEFormat(BaseFormat): self._match = True if not self._ignore_comments: - self._last_comments_list.append(parsing_result.comment) + self._last_comments_list.append(parsing_result.comment.strip()) except ParseException: return diff --git a/calculate/templates/format/kernel_format.py b/calculate/templates/format/kernel_format.py index 6c75d57..2d9977d 100644 --- a/calculate/templates/format/kernel_format.py +++ b/calculate/templates/format/kernel_format.py @@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\ class KernelFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_parameter_line, @@ -19,7 +21,8 @@ class KernelFormat(BaseFormat): self._format = 'kernel' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -27,7 +30,8 @@ class KernelFormat(BaseFormat): documentLines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(documentLines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): parameter_name = Word(alphanums+'_')('parameter_name') parameter_value = originalTextFor( @@ -36,23 +40,24 @@ class KernelFormat(BaseFormat): action_symbols = (Literal('!') | Literal('-')) - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name('name') - )('parameter_name') - + Literal('=').suppress() - + parameter_value('parameter_value')) + cls._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()) + cls._parameter_to_delete = (Group(action_symbols('action') + + parameter_name('name') + )('parameter_name') + + restOfLine.suppress()) - self._comment_line = originalTextFor( + cls._comment_line = originalTextFor( Literal('#').suppress() + ZeroOrMore(Word(printables + pyparsing_unicode.alphanums)) )('Comment') + cls._initialized = True def _parse_parameter_line(self, line): try: diff --git a/calculate/templates/format/ldap_format.py b/calculate/templates/format/ldap_format.py index 0568d4a..6de9e38 100644 --- a/calculate/templates/format/ldap_format.py +++ b/calculate/templates/format/ldap_format.py @@ -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= 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 by || @@ -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()) diff --git a/calculate/templates/format/openrc_format.py b/calculate/templates/format/openrc_format.py index 040243c..a0e47de 100644 --- a/calculate/templates/format/openrc_format.py +++ b/calculate/templates/format/openrc_format.py @@ -8,6 +8,8 @@ from pyparsing import Word, Literal, printables, originalTextFor, ZeroOrMore,\ class OpenRCFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_parameter_line, @@ -19,7 +21,8 @@ class OpenRCFormat(BaseFormat): self._format = 'openrc' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -27,29 +30,31 @@ class OpenRCFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): parameter_name = Word(printables, excludeChars='=') parameter_value = originalTextFor(OneOrMore(Word(printables))) action_symbols = (Literal('!') | Literal('-')) - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name('name'))('parameter_name') - + Literal('=').suppress() - + parameter_value('parameter_value')) + cls._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()) + cls._parameter_to_delete = (Group(action_symbols('action') + + parameter_name('name') + )('parameter_name') + + restOfLine.suppress()) - self._comment_line = originalTextFor( + cls._comment_line = originalTextFor( Literal('#') + ZeroOrMore(Word(printables + pyparsing_unicode.alphanums)) )('comment') + cls._initialized = True def _parse_parameter_line(self, line): try: diff --git a/calculate/templates/format/postfix_format.py b/calculate/templates/format/postfix_format.py index 758a20f..2ffd57e 100644 --- a/calculate/templates/format/postfix_format.py +++ b/calculate/templates/format/postfix_format.py @@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\ class PostfixFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_parameter_line, @@ -19,7 +21,8 @@ class PostfixFormat(BaseFormat): self._format = 'postfix' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -27,7 +30,8 @@ class PostfixFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): parameter_name = Word(alphanums+'_') parameter_value = originalTextFor(OneOrMore(Word(printables))) @@ -36,17 +40,17 @@ class PostfixFormat(BaseFormat): assignment = Literal('=') - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name)('parameter_name') - + assignment.suppress() - + parameter_value('parameter_value')) + cls._parameter_line = (Group(Optional(action_symbols, + default='')('action') + + parameter_name)('parameter_name') + + assignment.suppress() + + parameter_value('parameter_value')) - self._parameter_to_delete = (action_symbols('action') - + parameter_name('Name') - )('parameter_name') + cls._parameter_to_delete = (action_symbols('action') + + parameter_name('Name') + )('parameter_name') - self._comment_line = originalTextFor( + cls._comment_line = originalTextFor( Literal('#') + ZeroOrMore(Word(printables + pyparsing_unicode.alphanums)) diff --git a/calculate/templates/format/procmail_format.py b/calculate/templates/format/procmail_format.py index 873c6f5..577ebae 100644 --- a/calculate/templates/format/procmail_format.py +++ b/calculate/templates/format/procmail_format.py @@ -8,6 +8,8 @@ from pyparsing import Word, Literal, alphanums, printables, originalTextFor,\ class ProcmailFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_parameter_line, @@ -19,7 +21,8 @@ class ProcmailFormat(BaseFormat): self._format = 'procmail' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -27,32 +30,34 @@ class ProcmailFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): parameter_name = Word(alphanums+'_.') parameter_value = originalTextFor(OneOrMore(Word(printables))) action_symbols = (Literal('!') | Literal('-')) - self._parameter_line = (Group(Optional(action_symbols, - default='')('action') - + parameter_name('Name') - )('parameter_name') - + Literal('=').suppress() - + parameter_value('parameter_value')) + cls._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()) + cls._parameter_to_delete = (Group(action_symbols('action') + + parameter_name('Name') + )('parameter_name') + + restOfLine.suppress()) - self._comment_line = originalTextFor( + cls._comment_line = originalTextFor( Literal('#') + ZeroOrMore( Word(printables + pyparsing_unicode.alphanums) ) - )('comment') + )('comment') + cls._initialized = True def _parse_parameter_line(self, line): try: diff --git a/calculate/templates/format/proftpd_format.py b/calculate/templates/format/proftpd_format.py index 029bdc8..7133a4b 100644 --- a/calculate/templates/format/proftpd_format.py +++ b/calculate/templates/format/proftpd_format.py @@ -9,6 +9,8 @@ from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ class ProFTPDFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False): processing_methods = [self._parse_comment_line, self._parse_section_start_line, @@ -30,7 +32,8 @@ class ProFTPDFormat(BaseFormat): self._actions_stack = [] self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -38,7 +41,8 @@ class ProFTPDFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): left_angle_bracket = Literal('<') right_angle_bracket = Literal('>') slash = Literal('/') @@ -52,31 +56,31 @@ class ProFTPDFormat(BaseFormat): directive_value = Word(printables) - self._section_start_parser = (left_angle_bracket.suppress() - + Optional(action_symbols, - default='')('action') - + Group(directive('name') - + originalTextFor( - OneOrMore(section_value) - )('value') - )('directive') - + right_angle_bracket.suppress()) - - self._section_end_parser = (left_angle_bracket.suppress() - + slash.suppress() - + directive('directive') - + right_angle_bracket.suppress()) - - self._plain_directive_parser = (Optional(action_symbols)('action') - + Group(directive('name') - + originalTextFor( - OneOrMore(directive_value) - )('value') - )('directive') - ) - - self._delete_plain_directive_parser = (action_symbols('action') - + directive('directive')) + cls._section_start_parser = (left_angle_bracket.suppress() + + Optional(action_symbols, + default='')('action') + + Group(directive('name') + + originalTextFor( + OneOrMore(section_value) + )('value') + )('directive') + + right_angle_bracket.suppress()) + + cls._section_end_parser = (left_angle_bracket.suppress() + + slash.suppress() + + directive('directive') + + right_angle_bracket.suppress()) + + cls._plain_directive_parser = (Optional(action_symbols)('action') + + Group(directive('name') + + originalTextFor( + OneOrMore(directive_value) + )('value') + )('directive') + ) + + cls._delete_plain_directive_parser = (action_symbols('action') + + directive('directive')) single_key_directive = (Keyword('AllowAll') | Keyword('DenyAll') | @@ -87,8 +91,8 @@ class ProFTPDFormat(BaseFormat): Keyword('CwdRatioMsg') | Keyword('FileRatioErrMsg')) - self._single_key_directive_parser = (Optional(action_symbols)('action') - + single_key_directive( + cls._single_key_directive_parser = (Optional(action_symbols)('action') + + single_key_directive( 'directive' )) @@ -138,26 +142,26 @@ class ProFTPDFormat(BaseFormat): Keyword('ModuleControlsACLs') | Keyword('ControlsACLs')) - self._double_key_directive_parser = (Optional(action_symbols)('action') - + Group((double_key_directive - + directive_value - )('name') - + originalTextFor( + cls._double_key_directive_parser = (Optional(action_symbols)('action') + + Group((double_key_directive + + directive_value + )('name') + + originalTextFor( ZeroOrMore( directive_value ) )('value') - )('directive') - ) + )('directive') + ) - self._delete_double_key_directive_parser = (action_symbols('action') - + Group( + cls._delete_double_key_directive_parser = (action_symbols('action') + + Group( ( double_key_directive + directive_value )('name') - )('directive') - ) + )('directive') + ) full_key_directive = (Keyword('AllowClass') | Keyword('AllowGroup') | @@ -176,19 +180,20 @@ class ProFTPDFormat(BaseFormat): Keyword('TimeoutSession') | Keyword('UserAlias')) - self._full_key_directive_parser = (Optional(action_symbols)('action') - + Group(full_key_directive - + OneOrMore( - directive_value - ) - )('directive') - ) + cls._full_key_directive_parser = (Optional(action_symbols)('action') + + Group(full_key_directive + + OneOrMore( + directive_value + ) + )('directive') + ) - self._comment_line = originalTextFor( + cls._comment_line = originalTextFor( Literal('#') + ZeroOrMore(Word(printables + pyparsing_unicode.alphanums)) )('comment') + cls._initialized = True def _parse_section_start_line(self, line): try: diff --git a/calculate/templates/format/samba_format.py b/calculate/templates/format/samba_format.py index ed1e2dd..1b9aebe 100644 --- a/calculate/templates/format/samba_format.py +++ b/calculate/templates/format/samba_format.py @@ -2,12 +2,13 @@ # from .base_format import BaseFormat from collections import OrderedDict -from pyparsing import originalTextFor, Literal, ZeroOrMore, Word, printables,\ - OneOrMore, alphanums, ParseException, pyparsing_unicode,\ - Optional, Group +from pyparsing import originalTextFor, Literal, Word, printables, OneOrMore,\ + alphanums, ParseException, Optional, Group, restOfLine class SambaFormat(BaseFormat): + _initialized = False + def __init__(self, document_text: str, ignore_comments=False, join_before=False): @@ -29,7 +30,8 @@ class SambaFormat(BaseFormat): self._format = 'samba' self._last_comments_list = [] - self._initialize_parser() + if not self._initialized: + self._initialize_parser() if document_text == '': self._document_dictionary = OrderedDict() @@ -37,7 +39,8 @@ class SambaFormat(BaseFormat): document_lines = self._get_list_of_logic_lines(document_text) self._lines_to_dictionary(document_lines) - def _initialize_parser(self): + @classmethod + def _initialize_parser(cls): action_symbols = (Literal('!') | Literal('-')) comment_symbols = (Literal('#') | Literal(';')) @@ -46,10 +49,10 @@ class SambaFormat(BaseFormat): section_name = originalTextFor(OneOrMore(Word(alphanums+'_'))) - self._section_line = (Literal('[').suppress() - + Optional(action_symbols, default='')('action') - + section_name('name') + Literal(']').suppress() - )('section_name') + cls._section_line = (Literal('[').suppress() + + Optional(action_symbols, default='')('action') + + section_name('name') + Literal(']').suppress() + )('section_name') parameter_name = originalTextFor( OneOrMore(Word(printables, @@ -60,23 +63,21 @@ class SambaFormat(BaseFormat): OneOrMore(Word(printables)) ) - self._parameter_line = (Group(Optional(action_symbols('action'), - default='') - + parameter_name('name') - )('parameter_name') - + assignment_symbol.suppress() - + parameter_value('parameter_value')) - - self._parameter_to_delete = (action_symbols('action') + cls._parameter_line = (Group(Optional(action_symbols('action'), + default='') + parameter_name('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( - comment_symbols - + ZeroOrMore(Word( - printables - + pyparsing_unicode.alphanums)) + cls._comment_line = originalTextFor( + comment_symbols + restOfLine )('comment') + cls._initialized = True def _parse_section_line(self, line): try: @@ -144,7 +145,7 @@ class SambaFormat(BaseFormat): self._match = True if not self._ignore_comments: - self._last_comments_list.append(parsing_result.comment) + self._last_comments_list.append(parsing_result.comment.strip()) except ParseException: return diff --git a/calculate/vars/.datavars.py.swp b/calculate/vars/.datavars.py.swp deleted file mode 100644 index c03b12faef8b3fe9afa85f3396232f6cef781edc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&ubJ(6vxXS2X)tF<;2;v_Q!PA~Xj3y=~gOKx5eOVEYg0g$wKVZMF)7>+U1OuKlui%@ip7&nWd!MQv z2vcJxMt{5@2dt9nTa^gxHHoVC*ZA-ta$J$R66;*0>Y}3FBK9S5Pa$qwP*p>tualLNfC(@GCcp%k025#WOn?b6fiIANT@&KnULpS4hvf15fBygfe+Py57y1W! z4ZVaGpeN8Q^bq<9Iu8v&gV1+S4`dDqu?{Ul&!Ac8A#@Zv4DE(?LGSkq@dgT@8stFd zpg{;fRf^-#Zs^swLi`Cep$X_bWI_GV0ciajAs#_jAq(0I?SbB)CaUW#)y9npFaajO z1egF5U;<3wDh=Bg3fnY3@Hs2nM`-Y0819I@l^-XW2C zQ$*fy^R*Frvbe5Vg|Ens)a$zRaUB)mmTKxKj>;eSV$M$Q4{Y*m5jx>(uN>)E(foa~ zRQRRWUQ4~jBy~TOxu4WtF^<~n{tbG(wER`=yJe+amOcpTQ zhU(0(cxz}=@+ca2Cp(WN7tK6UolEW_q^c% zC!dwH?zCd6f(l(l{BuD|Qp;1=NZi+s`sg5TIc1`>fDC^Hw`HUa>_oY>#pNRve>Sy6 wY*!0utf0(%SGSx1UdYXhCqUKCla5{8IF_T?_Z|+co(lXh#uraMqL*L%50<<{?*IS* diff --git a/tests/format/test_kde.py b/tests/format/test_kde.py index a2b8636..7ad1eef 100644 --- a/tests/format/test_kde.py +++ b/tests/format/test_kde.py @@ -195,11 +195,11 @@ class TestParsingMethods: section_2_content = OrderedDict({('', 'alignment'): ['132'], ('', '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, ('!', 'PlasmaViews', 'Panel 69'): section_2_content, - ('-', 'Desktop Entry'): section3Content}) + ('-', 'Desktop Entry'): section_3_content}) kde_object = KDEFormat(document_text) assert kde_object._document_dictionary == result