forked from tuxintrouble/sigbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appconfig.py
93 lines (71 loc) · 2.65 KB
/
appconfig.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
#!/usr/local/bin/python3
#
# This file is part of the SigBit project
# https://github.com/tuxintrouble/sigbit
# Author: Sebastian Stetter, DJ5SE
# License: GNU GENERAL PUBLIC LICENSE Version 3
#
# configuration handling for SigBit application
import configparser, os, sys
from appdirs import AppDirs
import serial.tools.list_ports
DEBUG=False
def debug(caller,txt):
if DEBUG:
print(txt)
class AppConfig():
def __init__(self, appname, author, version = None):
dirs = AppDirs(appname, author, version)
self.configdir = dirs.user_config_dir
self.config_file_name = os.path.join(self.configdir,"config.ini")
debug(self,self.config_file_name)
self.config = configparser.ConfigParser()
first_serial_port = ''
if len(serial.tools.list_ports.comports()) > 0:
first_serial_port = serial.tools.list_ports.comports()[0][0]
self.config['DEFAULT'] = {
'server_url' : '49.12.102.144',
'server_port' : '7373',
'keyer_speed' : '18',
'serial_port' : first_serial_port,
'sidetone_freq' : '550',
'autoreconnect' : 'True',
'decode_cw' : 'True'
}
self.load()
def load(self):
if os.path.exists(self.config_file_name):
try:
self.config.read(self.config_file_name)
except Exception as err:
print(err)
else:
#see if we have that path, if not create it
if not os.path.exists(self.configdir):
try:
os.makedirs(self.configdir)
debug(self.configdir, " created.")
except Exception as err:
print(err)
#copy defaulf configuration
#self.config['USER'] = self.config['DEFAULT']
try:
with open(self.config_file_name, 'w') as configfile:
self.config.write(configfile)
debug("configuration saved to ",self.config_file_name)
except Exception as err:
print(err)
def save(self):
try:
with open(self.config_file_name, 'w') as configfile:
self.config.write(configfile)
except Exception as err:
print(err)
def get(self, key):
return self.config['DEFAULT'].get(key)
def getint(self, key):
return self.config['DEFAULT'].getint(key)
def getboolean(self,key):
return self.config['DEFAULT'].getboolean(key)
def set(self, key, val):
self.config['DEFAULT'][key] = str(val)