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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 import yarf.config_defaults as config_defaults
20 import yarf.restfullogger as restfullogger
22 from yarf.iniloader import INILoader
23 from yarf.exceptions import ConfigError
26 def exception_handler(func):
27 def exception_wrapper(*args, **kwargs):
29 restlogger = restfullogger.get_logger()
30 restlogger.debug("calling {}".format(func.__name__))
31 ret = func(*args, **kwargs)
33 except Exception as error:
34 restlogger.info("Exception from function {} (error: {})".format(func.__name__, str(error)))
35 if isinstance(error, ConfigError):
38 raise ConfigError(str(error))
39 return exception_wrapper
42 class RestConfig(object):
46 if RestConfig.__restinstance is None:
47 RestConfig.__restinstance = object.__new__(cls)
48 return RestConfig.__restinstance
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
56 self.config_file = None
59 def parse(self, args=sys.argv[1:]):
60 parser = argparse.ArgumentParser(description='Restful server')
61 parser.add_argument('--config',
63 default=self.default_config_file,
64 help="Configuration file",
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)
73 return self.config.get('port', type_of_value=int)
77 return self.config.get('ip_address')
81 return self.config.get('use_ssl', type_of_value=bool)
84 def get_private_key(self):
86 return self.config.get('ssl_private_key')
90 def get_certificate(self):
92 return self.config.get('ssl_certificate')
96 def get_handler_dir(self):
97 return self.config.get('handler_directory')
99 def get_section(self, section, format='list'):
100 return self.config.get_section(section, format=format)
103 return self.config.get('debug', type_of_value=bool)
106 def get_passthrough_errors(self):
107 return self.config.get('passthrough_errors', type_of_value=bool)
110 def is_threaded(self):
111 return self.config.get('threaded', type_of_value=bool)
114 def get_auth_method(self):
115 return self.config.get('auth_method')