Closes #23 custom serialisation

changed config load/save path to dynamic
master
Josef Dabrowski 6 years ago
parent 00a15f0c81
commit 3d236bd02d

@ -3,7 +3,7 @@ from collections import namedtuple
import uuid
import os
import toml
from preserve import preservable, preserve, restore
#from preserve import preservable, preserve, restore
mydir = os.path.dirname(os.path.abspath(__file__))
@ -15,8 +15,7 @@ class Manager():
def __init__(self):
self.manager_id = gen_id()
self.devices = []
self.config = self.import_config_file()
self.construct_from_config()
#widget class register list - updated with available widget classes
def add_device(self, name, resolution): # add parameters for Device class constructor
self.devices.append(Device(name, resolution))
@ -27,29 +26,19 @@ class Manager():
self.devices.remove(dev)
break
def import_config_file(self):
def import_config_file(self, path):
# get toml config file, convert to named tuple
# return named tuple containing all layouts
try:
config = toml.load(os.path.join(mydir, 'config.toml'))
config = toml.load(path)
#config = convert(uni_config)
restored_data = restore(config)
return restored_data
except:
return None
self.devices = [Device.load(device_data) for device_data in config['devices']]
def export_config_file(self):
def export_config_file(self, path):
# take tuple containing all layouts and dump as toml
path = os.path.join(mydir, 'config.toml')
preserved_data = preserve(self.devices)
preserved_data = [device.save() for device in self.devices]
with open(path, "w+") as config_file:
config_file.write(toml.dumps({'devices': preserved_data}))
def construct_from_config(self):
try:
self.devices = self.config['devices']
except:
return None
# def update_config(self, new_config_tuple):
# new_device_list = []
@ -68,18 +57,40 @@ class Manager():
device.layout = {}
@preservable
class Device():
# Device class to contain a device's properties, layout and widgets
def __init__(self, name, resolution, dev_id=None):
"""
Device class to contain a device's properties, layout and widgets
"""
@staticmethod
def load(saved_state):
"""
"""
new_device = Device(
name=saved_state['name'], resolution=saved_state['resolution'], device_id=saved_state['device_id'])
for widget_state in saved_state['widgets']:
new_device.add_widget(Widget.load(widget_state))
return new_device
def save(self):
"""
Returns a dict with saved state of the device
"""
return_state = {}
return_state['name'] = self.name
return_state['resolution'] = self.resolution
return_state['device_id'] = self.device_id
return_state['widgets'] = [widget.save() for widget in self.widgets]
return return_state
def __init__(self, name, resolution, device_id=None):
# attributes
self.name = name
self.resolution = resolution
if(dev_id is None):
if(device_id is None):
self.device_id = gen_id()
else:
self.device_id = dev_id
self.device_id = device_id
# widgets container
self.widgets = []
@ -87,22 +98,76 @@ class Device():
# generated
self.device_image = None
#@preservable
#class Widget():
def add_widget(self, widget):
self.widgets.append(widget)
class Widget():
"""
test
"""
@staticmethod
def load(saved_state):
"""
test
"""
widget_classes = {'Widget': Widget, 'BasicTextWidget': BasicTextWidget} # this list maintained in the module or manager...
new_widget_class = widget_classes[saved_state['w_type']]
new_widget = new_widget_class(
position=saved_state['position'], dimensions=saved_state['dimensions'], widg_id=saved_state['widget_id'])
return new_widget
def save(self):
"""
Returns a dict with saved state of the widget
"""
return_state = {}
return_state['w_type'] = 'Widget'
return_state['position'] = self.position
return_state['dimensions'] = self.dimensions
return_state['widget_id'] = self.widget_id
return return_state
def render(self):
"""
returns image of widget
"""
pass
def __init__(self, position, dimensions, widget_id=None):
if(widget_id is None):
self.widget_id = gen_id()
else:
self.widget_id = widget_id
self.position = position # xy grid space location to draw the top left corner in
self.dimensions = dimensions # xy in terms of grid spaces
class BasicTextWidget(Widget):
"""
test
"""
@staticmethod
def load(saved_state):
"""
test
"""
new_widget = BasicTextWidget(
position=saved_state['position'], dimensions=saved_state['dimensions'], widg_id=saved_state['widget_id'], text=saved_state['text'])
return new_widget
def save(self):
return_state = super().save()
return_state['text'] = self.text
return_state['w_type'] = 'BasicTextWidget'
return return_state
def __init__(self, position, dimensions, widg_id, text):
super().__init__(position, dimensions, widg_id)
self.text = text
# Global Functions
#
def gen_id():
return str(uuid.uuid4())
def convert(input):
if isinstance(input, dict):
return dict((convert(key), convert(value)) for key, value in input.iteritems())
elif isinstance(input, list):
return [convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input

@ -1,7 +0,0 @@
[[devices]]
name = "test"
resolution = [ 1, 2,]
device_id = "1f85b632-4457-4097-859a-03ae24cd3ad7"
widgets = []
"<_jam>" = "Device"

@ -1,16 +1,16 @@
from notiframe import Notiframe
import os
import toml
mydir = os.path.dirname(os.path.abspath(__file__))
def test_config_dumpload():
path = os.path.join(mydir, '../notiframe/config.toml')
os.remove(path)
def test_config_dumpload(tmp_path):
path = os.path.join(tmp_path, 'config.toml')
mgr = Notiframe.Manager()
mgr.add_device('test', [1,2])
mgr.export_config_file()
mgr.export_config_file(path)
mgr = Notiframe.Manager()
mgr.import_config_file(path)
assert mgr.devices[0].name == 'test'

Loading…
Cancel
Save