|
|
|
@ -3,7 +3,7 @@ from collections import namedtuple
|
|
|
|
import uuid
|
|
|
|
import uuid
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import toml
|
|
|
|
import toml
|
|
|
|
from preserve import preservable, preserve, restore
|
|
|
|
#from preserve import preservable, preserve, restore
|
|
|
|
|
|
|
|
|
|
|
|
mydir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
mydir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
@ -11,53 +11,42 @@ mydir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
# Classes
|
|
|
|
# Classes
|
|
|
|
#
|
|
|
|
#
|
|
|
|
class Manager():
|
|
|
|
class Manager():
|
|
|
|
# Manager class to manage a notiframe session.
|
|
|
|
# Manager class to manage a notiframe session.
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
self.manager_id = gen_id()
|
|
|
|
self.manager_id = gen_id()
|
|
|
|
self.devices = []
|
|
|
|
self.devices = []
|
|
|
|
self.config = self.import_config_file()
|
|
|
|
#widget class register list - updated with available widget classes
|
|
|
|
self.construct_from_config()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
self.devices.append(Device(name, resolution))
|
|
|
|
|
|
|
|
|
|
|
|
def remove_device(self, dev_id):
|
|
|
|
def remove_device(self, dev_id):
|
|
|
|
for dev in self.devices:
|
|
|
|
for dev in self.devices:
|
|
|
|
if dev.device_id == dev_id:
|
|
|
|
if dev.device_id == dev_id:
|
|
|
|
self.devices.remove(dev)
|
|
|
|
self.devices.remove(dev)
|
|
|
|
break
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
def import_config_file(self):
|
|
|
|
def import_config_file(self, path):
|
|
|
|
#get toml config file, convert to named tuple
|
|
|
|
# get toml config file, convert to named tuple
|
|
|
|
#return named tuple containing all layouts
|
|
|
|
# return named tuple containing all layouts
|
|
|
|
try:
|
|
|
|
config = toml.load(path)
|
|
|
|
config = toml.load(os.path.join(mydir, 'config.toml'))
|
|
|
|
#config = convert(uni_config)
|
|
|
|
#config = convert(uni_config)
|
|
|
|
self.devices = [Device.load(device_data) for device_data in config['devices']]
|
|
|
|
restored_data = restore(config)
|
|
|
|
|
|
|
|
return restored_data
|
|
|
|
def export_config_file(self, path):
|
|
|
|
except:
|
|
|
|
# take tuple containing all layouts and dump as toml
|
|
|
|
return None
|
|
|
|
preserved_data = [device.save() for device in self.devices]
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
with open(path, "w+") as config_file:
|
|
|
|
with open(path, "w+") as config_file:
|
|
|
|
config_file.write(toml.dumps({'devices': preserved_data}))
|
|
|
|
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 = []
|
|
|
|
# new_device_list = []
|
|
|
|
# for dev in devices:
|
|
|
|
# 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)
|
|
|
|
# new_device_list.append(conf_dev)
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# self.export_config_file(new_config_tuple)
|
|
|
|
# self.export_config_file(new_config_tuple)
|
|
|
|
# #self.update_layout() update devices layouts that need to be updated
|
|
|
|
# #self.update_layout() update devices layouts that need to be updated
|
|
|
|
|
|
|
|
|
|
|
|
@ -68,41 +57,117 @@ class Manager():
|
|
|
|
device.layout = {}
|
|
|
|
device.layout = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@preservable
|
|
|
|
|
|
|
|
class Device():
|
|
|
|
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
|
|
|
|
#attributes
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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.name = name
|
|
|
|
self.resolution = resolution
|
|
|
|
self.resolution = resolution
|
|
|
|
if(dev_id is None):
|
|
|
|
if(device_id is None):
|
|
|
|
self.device_id = gen_id()
|
|
|
|
self.device_id = gen_id()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.device_id = dev_id
|
|
|
|
self.device_id = device_id
|
|
|
|
|
|
|
|
|
|
|
|
#widgets container
|
|
|
|
# widgets container
|
|
|
|
self.widgets = []
|
|
|
|
self.widgets = []
|
|
|
|
|
|
|
|
|
|
|
|
#generated
|
|
|
|
# generated
|
|
|
|
self.device_image = None
|
|
|
|
self.device_image = None
|
|
|
|
|
|
|
|
|
|
|
|
#@preservable
|
|
|
|
def add_widget(self, widget):
|
|
|
|
#class 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
|
|
|
|
# Global Functions
|
|
|
|
#
|
|
|
|
#
|
|
|
|
def gen_id():
|
|
|
|
def gen_id():
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
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
|
|
|
|
|
|
|
|
|