Seed code for yarf
[ta/yarf.git] / src / yarf / restfulargs.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 import argparse
17 import sys
18
19 import yarf.config_defaults as config_defaults
20 import yarf.restfullogger as restfullogger
21
22 from yarf.iniloader import INILoader
23 from yarf.exceptions import ConfigError
24
25
26 def exception_handler(func):
27     def exception_wrapper(*args, **kwargs):
28         try:
29             restlogger = restfullogger.get_logger()
30             restlogger.debug("calling {}".format(func.__name__))
31             ret = func(*args, **kwargs)
32             return ret
33         except Exception as error:
34             restlogger.info("Exception from function {} (error: {})".format(func.__name__, str(error)))
35             if isinstance(error, ConfigError):
36                 raise error
37             else:
38                 raise ConfigError(str(error))
39     return exception_wrapper
40
41
42 class RestConfig(object):
43     __restinstance = None
44
45     def __new__(cls):
46         if RestConfig.__restinstance is None:
47             RestConfig.__restinstance = object.__new__(cls)
48         return RestConfig.__restinstance
49
50     def __init__(self):
51         self.default_section = config_defaults.default_section
52         self.config_default = config_defaults.config_defaults
53         self.default_config_file = config_defaults.default_config_file
54
55         self.config = None
56         self.config_file = None
57
58     @exception_handler
59     def parse(self, args=sys.argv[1:]):
60         parser = argparse.ArgumentParser(description='Restful server')
61         parser.add_argument('--config',
62                             type=str,
63                             default=self.default_config_file,
64                             help="Configuration file",
65                             dest='config_file')
66
67         args = parser.parse_args(args)
68         self.config_file = args.config_file
69         self.config = INILoader(self.config_file, defaults=self.config_default, defaultsection=self.default_section)
70
71     @exception_handler
72     def get_port(self):
73         return self.config.get('port', type_of_value=int)
74
75     @exception_handler
76     def get_ip(self):
77         return self.config.get('ip_address')
78
79     @exception_handler
80     def use_ssl(self):
81         return self.config.get('use_ssl', type_of_value=bool)
82
83     @exception_handler
84     def get_private_key(self):
85         if self.use_ssl():
86             return self.config.get('ssl_private_key')
87         return None
88
89     @exception_handler
90     def get_certificate(self):
91         if self.use_ssl():
92             return self.config.get('ssl_certificate')
93         return None
94
95     @exception_handler
96     def get_handler_dir(self):
97         return self.config.get('handler_directory')
98
99     def get_section(self, section, format='list'):
100         return self.config.get_section(section, format=format)
101
102     def get_debug(self):
103         return self.config.get('debug', type_of_value=bool)
104
105     @exception_handler
106     def get_passthrough_errors(self):
107         return self.config.get('passthrough_errors', type_of_value=bool)
108
109     @exception_handler
110     def is_threaded(self):
111         return self.config.get('threaded', type_of_value=bool)
112
113     @exception_handler
114     def get_auth_method(self):
115         return self.config.get('auth_method')
116
117
118 def get_config():
119     return RestConfig()