forked from sanchezfauste/SuitePY
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.py
130 lines (108 loc) · 4.12 KB
/
config.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#######################################################################
# Suite PY is a simple Python client for SuiteCRM API.
# Copyright (C) 2017-2018 BTACTIC, SCCL
# Copyright (C) 2017-2018 Marc Sanchez Fauste
# This program 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.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#######################################################################
import configparser
import os.path
class Config:
"""
This class is used to read from a file the access credentials of a SuiteCRM API.
This avoids the need of hard-code the credentials in the code.
"""
def __init__(self, config_file="suitepy.ini"):
"""
Creates a Config instance loading settings from specified file.
:param str config_file: file from which the configuration will be read.
"""
if os.path.isabs(config_file):
abs_path = config_file
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
abs_path = os.path.join(base_dir, config_file)
if os.path.isfile(abs_path):
print("Loading config from file: " + abs_path)
self._load_config_file(abs_path)
else:
print("Creating new config file on: " + abs_path)
print("Please edit config file and rerun the application.")
self._create_config_file(abs_path)
exit(0)
def _load_config_file(self, config_file):
config = configparser.ConfigParser()
config.read(config_file)
self._url = config.get("SuiteCRM API Credentials", "url")
self._username = config.get("SuiteCRM API Credentials", "username")
self._password = config.get("SuiteCRM API Credentials", "password")
self._application_name = config.get(
"SuiteCRM API Credentials",
"application_name"
)
self._verify_ssl = bool(config.get(
"SuiteCRM API Credentials", "verify_ssl"))
@staticmethod
def _create_config_file(config_file):
config_file = open(config_file, "w")
config = configparser.ConfigParser()
config.add_section("SuiteCRM API Credentials")
config.set(
"SuiteCRM API Credentials",
"url",
"https://example.org/service/v4_1/rest.php"
)
config.set("SuiteCRM API Credentials", "username", "api")
config.set("SuiteCRM API Credentials", "password", "123456")
config.set("SuiteCRM API Credentials", "application_name", "SuitePY")
config.set("SuiteCRM API Credentials", "verify_ssl", True)
config.write(config_file)
config_file.close()
@property
def url(self):
"""
Get SuiteCRM REST API URL.
:return: SuiteCRM REST API URL
:rtype: str
"""
return self._url
@property
def username(self):
"""
Get login username.
:return: login username.
:rtype: str
"""
return self._username
@property
def password(self):
"""
Get login password.
:return: login password.
:rtype: str
"""
return self._password
@property
def application_name(self):
"""
Get application name used when login to SuiteCRM API.
:return: application name.
:rtype: str
"""
return self._application_name
@property
def verify_ssl(self):
"""
Specifies whether the SSL certificate should be verified.
:return: True if SSL certificate must be verified, False otherwise.
:rtype: bool
"""
return self._verify_ssl