From 3d236bd02db7276b54461bcfbbcd90b642f96b19 Mon Sep 17 00:00:00 2001 From: Josef Date: Thu, 9 Jan 2020 14:41:51 +0800 Subject: [PATCH] Closes #23 custom serialisation changed config load/save path to dynamic --- notiframe/Notiframe.py | 173 ++++++++++++++++++++++++++++------------- notiframe/config.toml | 7 -- tests/test_manager.py | 10 +-- 3 files changed, 124 insertions(+), 66 deletions(-) delete mode 100644 notiframe/config.toml diff --git a/notiframe/Notiframe.py b/notiframe/Notiframe.py index 363444a..6f28515 100644 --- a/notiframe/Notiframe.py +++ b/notiframe/Notiframe.py @@ -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__)) @@ -11,53 +11,42 @@ mydir = os.path.dirname(os.path.abspath(__file__)) # Classes # class Manager(): - # Manager class to manage a notiframe session. + # Manager class to manage a notiframe session. 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 + def add_device(self, name, resolution): # add parameters for Device class constructor self.devices.append(Device(name, resolution)) - + def remove_device(self, dev_id): for dev in self.devices: if dev.device_id == dev_id: self.devices.remove(dev) break - - def import_config_file(self): - #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 = convert(uni_config) - restored_data = restore(config) - return restored_data - except: - return None - - def export_config_file(self): - #take tuple containing all layouts and dump as toml - path = os.path.join(mydir, 'config.toml') - preserved_data = preserve(self.devices) + + def import_config_file(self, path): + # get toml config file, convert to named tuple + # return named tuple containing all layouts + config = toml.load(path) + #config = convert(uni_config) + self.devices = [Device.load(device_data) for device_data in config['devices']] + + def export_config_file(self, path): + # take tuple containing all layouts and dump as toml + 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): + # def update_config(self, new_config_tuple): # new_device_list = [] # for dev in devices: - # conf_dev = Conf_device(dev.name, dev.resolution, dev.device_id) + # conf_dev = Conf_device(dev.name, dev.resolution, dev.device_id) # new_device_list.append(conf_dev) - # -# + # + # # self.export_config_file(new_config_tuple) # #self.update_layout() update devices layouts that need to be updated @@ -68,41 +57,117 @@ 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): - #attributes + """ + 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 + # widgets container self.widgets = [] - #generated + # 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 diff --git a/notiframe/config.toml b/notiframe/config.toml deleted file mode 100644 index 4bd7b55..0000000 --- a/notiframe/config.toml +++ /dev/null @@ -1,7 +0,0 @@ -[[devices]] -name = "test" -resolution = [ 1, 2,] -device_id = "1f85b632-4457-4097-859a-03ae24cd3ad7" -widgets = [] -"<_jam>" = "Device" - diff --git a/tests/test_manager.py b/tests/test_manager.py index c1de780..e67b37d 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -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' -