-
Notifications
You must be signed in to change notification settings - Fork 10
/
configuration.py
67 lines (59 loc) · 2.63 KB
/
configuration.py
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
#!/usr/bin/env python
# -*- encoding: UTF8 -*-
# author: Philipp Klaus, philipp.klaus →AT→ gmail.com
# This file is part of python-inwx-xmlrpc.
#
# python-inwx-xmlrpc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-inwx-xmlrpc 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 python-inwx-xmlrpc. If not, see <http://www.gnu.org/licenses/>.
###########################################################################
###### This file helps to handle the INI-style .cfg files. ######
###### You only need it if you want to test the example script. ######
###### It is not needed if you just want to use the inwx class. ######
import sys
if sys.version_info.major == 3:
import configparser
else:
import ConfigParser as configparser
from os.path import expanduser
def get_account_data(print_errors = False, config_file = 'conf.cfg', config_section = 'ote'):
"""
boolean print_errors: Print errors to stdout instead of raising an exception.
string config_file: The name of the configuration file.
string config_section: The name of the section to read in the configuration file.
"""
config = open_config_file(print_errors, config_file)
try:
api_url = config.get(config_section, 'url')
username = config.get(config_section, 'username')
password = config.get(config_section, 'password')
shared_secret = config.get(config_section, 'shared_secret')
except Exception as err:
message = 'Error: Please make sure your config file %s contains the section %s with the entries "url", "username", "password" and "shared_secret".' % (config_file, config_section)
if print_errors:
print(message)
sys.exit(2)
else:
raise NameError(message)
return (api_url, username, password, shared_secret)
def open_config_file(print_errors, config_file):
config = configparser.ConfigParser()
try:
if config.read(config_file) == []: raise Exception()
except:
message = "Error: Please make sure you adopted the config file: %s" % config_file
if print_errors:
print(message)
sys.exit(2)
else:
raise NameError(message)
return config