apache-ignite
93 строки · 2.9 Кб
1# Licensed to the Apache Software Foundation (ASF) under one or more
2# contributor license agreements. See the NOTICE file distributed with
3# this work for additional information regarding copyright ownership.
4# The ASF licenses this file to You under the Apache License, Version 2.0
5# (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""
17This module contains ignite config classes and utilities.
18"""
19import os
20
21from jinja2 import FileSystemLoader, Environment
22
23IGNITE_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")
24ZK_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "zk", "templates")
25DEFAULT_IGNITE_CONF = "ignite.xml.j2"
26DEFAULT_THIN_CLIENT_CONF = "thin_client_config.xml.j2"
27DEFAULT_LOG4J2_CONF = "log4j2.xml.j2"
28
29TEMPLATE_PATHES = [IGNITE_TEMPLATE_PATH, ZK_TEMPLATE_PATH]
30
31
32class ConfigTemplate:
33"""
34Basic configuration.
35"""
36def __init__(self, path):
37env = Environment(loader=FileSystemLoader(searchpath=TEMPLATE_PATHES))
38env.filters["snake_to_camel"] = snake_to_camel
39
40self.template = env.get_template(path)
41self.default_params = {}
42
43def render(self, **kwargs):
44"""
45Render configuration.
46"""
47kwargs.update(self.default_params)
48unfiltered = self.template.render(**kwargs)
49
50return '\n'.join(filter(lambda line: line.strip(), unfiltered.split('\n')))
51
52
53def snake_to_camel(snake_name):
54"""
55Custom jinja2 filter to convert named from smake to camel format
56:param snake_name: name in snake format
57:return: name in camel format
58"""
59components = snake_name.split('_')
60return components[0] + ''.join(x.title() for x in components[1:])
61
62
63class IgniteServerConfigTemplate(ConfigTemplate):
64"""
65Ignite server node configuration.
66"""
67def __init__(self, path=DEFAULT_IGNITE_CONF):
68super().__init__(path)
69
70
71class IgniteClientConfigTemplate(ConfigTemplate):
72"""
73Ignite client node configuration.
74"""
75def __init__(self, path=DEFAULT_IGNITE_CONF):
76super().__init__(path)
77self.default_params.update(client_mode=True)
78
79
80class IgniteThinClientConfigTemplate(ConfigTemplate):
81"""
82Ignite client node configuration.
83"""
84def __init__(self, path=DEFAULT_THIN_CLIENT_CONF):
85super().__init__(path)
86
87
88class IgniteLoggerConfigTemplate(ConfigTemplate):
89"""
90Ignite logger configuration.
91"""
92def __init__(self):
93super().__init__(DEFAULT_LOG4J2_CONF)
94