File: tflang.py

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (116 lines) | stat: -rw-r--r-- 4,099 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#############################################################################
# Copyright (c) 2015-2016 Balabit
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################

from __future__ import print_function, absolute_import
from .completerlang import CompleterLang
from .commandlinelexer import CommandLineLexer
from .getoptlexer import GetoptLexer


class TemplateFunctionLang(CompleterLang):
    tokens = []
    tokens_base = [
        "COMMAND", "ARG", "OPT"
    ]
    known_commands = ("format-json", "echo")
    known_options = (
        # value-pairs
        "--scope", "--exclude", "--key", "--rekey", "--pair",
        "--shift", "--add-prefix", "--replace-prefix", "--replace")

    def p_template_func(self, p):
        '''template_func : tf_format_json
                         | tf_echo
                         | tf_generic'''

    def p_tf_format_json(self, p):
        '''tf_format_json : COMMAND_FORMAT_JSON tf_format_json_args'''

    def p_tf_format_json_args(self, p):
        '''tf_format_json_args : tf_format_json_arg tf_format_json_args
                               | '''

    def p_tf_format_json_arg(self, p):
        '''tf_format_json_arg  : OPT__SCOPE value_pairs_scope
                               | OPT__EXCLUDE ARG
                               | OPT__KEY name_value_name
                               | OPT__REKEY ARG
                               | OPT__PAIR name_value_pair
                               | OPT__SHIFT ARG
                               | OPT__ADD_PREFIX ARG
                               | OPT__REPLACE_PREFIX ARG
                               | OPT__REPLACE ARG
                               | OPT
                               | ARG'''

    def p_name_value_name(self, p):
        '''name_value_name : ARG'''

    def p_name_value_pair(self, p):
        '''name_value_pair : ARG'''

    def p_value_pairs_scope(self, p):
        '''value_pairs_scope : ARG'''

    def p_tf_echo(self, p):
        '''tf_echo : COMMAND_ECHO templates'''

    def p_tf_generic(self, p):
        '''tf_generic : COMMAND args'''

    def p_args(self, p):
        '''args : ARG args
                |
        '''

    def p_templates(self, p):
        '''templates : template templates
                     | '''

    def p_template(self, p):
        '''template : ARG'''

    def p_ipaddress(self, p):
        '''ipaddress : ARG'''

    def _initialize_rules(self):
        tokens = list(self.tokens_base)
        self._convert_known_commands_to_tokens(tokens)
        self._convert_known_options_to_tokens(tokens)
        self.tokens = tokens

    def _construct_lexer(self):
        return GetoptLexer(CommandLineLexer(),
                           known_commands=self.known_commands,
                           known_options=self.known_options)

    @staticmethod
    def _tokenize_argument(arg):
        return arg.upper().replace('-', '_')

    def _convert_known_commands_to_tokens(self, tokens):
        for command in self.known_commands:
            tokens.append(f"COMMAND_{self._tokenize_argument(command)}")

    def _convert_known_options_to_tokens(self, tokens):
        for option in self.known_options:
            tokens.append(f"OPT{self._tokenize_argument(option)}")