-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_handler.py
38 lines (30 loc) · 1004 Bytes
/
config_handler.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
# -*- coding: utf-8 -*-
# pylint: disable=C0111
import json
import time
import os
class ConfigHandler:
def __init__(self):
self.file_location = self.get_user_preferences('Plant_Type')
self.load(self.file_location)
self.disable_reload = False
def load(self, config_file):
with open(config_file) as file:
self.data = json.load(file)
self.last_loaded = time.time()
def reload_if_modified(self):
if not self.disable_reload and os.path.getmtime(
self.file_location) > self.last_loaded:
self.load(self.file_location)
def __getitem__(self, key):
try:
return self.data[key]
except KeyError:
return None
def run(self):
self.reload_if_modified()
return time.time() + 5
def get_user_preferences(self, config_line):
with open('user_preferences.json') as file:
config = json.load(file)
return config[config_line]