-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_file.py
58 lines (49 loc) · 1.88 KB
/
config_file.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
"""
MQTT Client GUI - Config File Operations
Author: Sahin MERSIN - electrocoder <[email protected]>
"""
import os
import configparser
class ConfigFile:
def __init__(self):
self.config = configparser.ConfigParser()
basedir = os.path.dirname(__file__)
self.file_name = os.path.join(basedir, "config_file.ini")
if os.path.exists(self.file_name):
self.config.read(self.file_name)
def read_sections(self):
self.config.read(self.file_name)
return self.config.sections()
def create_file(self, name, broker, port, username, password):
self.config.add_section(name)
self.config.set(name, 'broker', broker)
self.config.set(name, 'port', port)
self.config.set(name, 'username', username)
self.config.set(name, 'password', password)
self.config.set(name, 'topics', '#,')
with open(self.file_name, 'w') as configfile:
self.config.write(configfile)
return True
def read_broker(self, name):
self.config.read(self.file_name)
broker = self.config[name]["broker"]
port = self.config[name]["port"]
username = self.config[name]["username"]
password = self.config[name]["password"]
return name, broker, port, username, password
def read_topics(self, name):
self.config.read(self.file_name)
return self.config[name]["topics"]
def create_topic(self, name, topic):
con = self.config[name]
con['topics'] += topic + ","
with open(self.file_name, 'w') as configfile:
self.config.write(configfile)
return topic
def delete(self, name):
self.config.read(self.file_name)
if self.config.remove_section(name):
with open(self.file_name, 'w') as configfile:
self.config.write(configfile)
return True
return False