2016-01-24 09:15:14 +01:00
|
|
|
import json
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class File(object):
|
|
|
|
|
"Holds controller state in the file"
|
|
|
|
|
def __init__(self, filename=None):
|
|
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
|
|
def present(self):
|
2016-10-08 18:20:50 +02:00
|
|
|
return os.path.isfile(self.filename)
|
|
|
|
|
|
2016-01-24 09:15:14 +01:00
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
|
if self.filename is None:
|
|
|
|
|
raise ClassNotReadyException("Filename not set!")
|
|
|
|
|
with open(self.filename) as input_file:
|
|
|
|
|
self.data = json.load(input_file)
|
|
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
if self.filename is None:
|
|
|
|
|
raise ClassNotReadyException("Filename not set!")
|
|
|
|
|
if self.data is None:
|
|
|
|
|
raise ClassNotReadyException("Data not loaded!")
|
|
|
|
|
|
|
|
|
|
with open(self.filename, 'w') as out_file:
|
|
|
|
|
json.dump(self.data, out_file)
|