forked from pklaus/python-inwx-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.py
111 lines (100 loc) · 4.62 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
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
#!/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 scripts. ######
###### It is not needed if you just want to use the inwx class... ######
import sys
import ConfigParser
from os.path import expanduser
def get_account_data(print_errors = False, config_file = 'python-inwx-xmlrpc.cfg', config_section = 'live'):
"""
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')
except Exception, err:
message = 'Error: Please make sure your config file %s contains the section %s with the entries "url", "username" and "password".' % (config_file, config_section)
if print_errors:
print message
sys.exit(2)
else:
raise NameError(message)
return (api_url, username, password)
def get_domain_update(print_errors=False, config_file = 'python-inwx-xmlrpc.cfg', config_section = 'automatic_nameserver_entry_update'):
config = open_config_file(print_errors, config_file)
try:
domain = config.get(config_section, 'domain')
subdomain = config.get(config_section, 'subdomain')
default_ip = config.get(config_section, 'default_ip')
except Exception, err:
message = 'Error: Please make sure your config file %s contains the section %s with the entries "domain", "subdomain" and "default_ip".' % (config_file, config_section)
if print_errors:
print message
sys.exit(2)
else:
raise NameError(message)
return domain, subdomain, default_ip
def get_invoices_folder(print_errors=False, config_file = 'python-inwx-xmlrpc.cfg', config_section = 'invoices_folder'):
config = open_config_file(print_errors, config_file)
try:
invoices_folder = expanduser(config.get(config_section, 'invoices_folder'))
except:
message = 'Error: Please make sure your config file %s contains the section %s with the entry "invoices_folder".' % (config_file, config_section)
if print_errors:
print message
sys.exit(2)
else:
raise NameError(message)
return invoices_folder
def get_nsbackup_files(print_errors=False, config_file = 'python-inwx-xmlrpc.cfg', config_section = 'nameserver_backup'):
config = open_config_file(print_errors, config_file)
backup_files = dict()
try:
backup_files['json_backup_file'] = expanduser(config.get(config_section, 'json_backup_file'))
except:
pass
try:
backup_files['pickle_backup_file'] = expanduser(config.get(config_section, 'pickle_backup_file'))
except:
pass
if len(backup_files) == 0 :
message = 'Error: Please make sure your config file %s contains the section %s with the entries "json_backup_file" or "pickle_backup_file".' % (config_file, config_section)
if print_errors:
print message
sys.exit(2)
else:
raise NameError(message)
return backup_files
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