-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
86 lines (63 loc) · 2.52 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
import configparser
import os
import sys
import requests
config = configparser.ConfigParser()
# generate default config, if no config file exists
if not os.path.exists('config.ini'):
print("No config file found. Generating default config...")
config['general'] = {'default_backlight': '1',
'lcd_lines': '4',
'lcd_columns': '20'
}
config['weather'] = {'api_key': 'insert your openweathermap.org api key here',
'location': 'linz',
'refresh_interval': '300'
}
config['webserver'] = {'enabled': 'false',
'port': '5000'}
config['button'] = {'enabled': 'false',
'pin': '0'}
config['lightsensor'] = {'enabled': 'false',
'pin': '0'}
with open('config.ini', 'w') as configfile:
config.write(configfile)
print("Default config file generated. Please edit config.ini and restart the program.")
sys.exit(1)
config.read('config.ini')
def testWeatherApi():
params = {"q": getWeatherLocation(), "units": "metric", "appid": getWeatherApiKey(), "lang": "de"}
try:
request = requests.get(url = "https://api.openweathermap.org/data/2.5/weather", params = params)
return request.status_code == 200
except:
return False
# read config file and create methods for getting the values
try:
config.read('config.ini')
def getDefaultBacklight():
return int(config['general']['default_backlight'])
def getLcdLines():
return int(config['general']['lcd_lines'])
def getLcdColumns():
return int(config['general']['lcd_columns'])
def getWeatherApiKey():
return config['weather']['api_key']
def getWeatherLocation():
return config['weather']['location']
def getWeatherRefreshInterval():
return int(config['weather']['refresh_interval'])
def getServerEnabled():
return config['webserver']['enabled'].lower()
def getServerPort():
return config['webserver']['port']
def getButtonEnabled():
return config['button']['enabled'].lower()
def getButtonPin():
return config['button']['pin']
def getLightSensorEnabled():
return config['lightsensor']['enabled'].lower()
def getLightSensorPin():
return config['lightsensor']['pin']
except:
print("An error occured while reading or writing the config file")