|
|
|
@ -1,15 +1,24 @@
|
|
|
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from collections import namedtuple
|
|
|
|
import uuid
|
|
|
|
import uuid
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
|
|
|
|
import toml
|
|
|
|
|
|
|
|
from preserve import preservable, preserve, restore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mydir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
test_mode = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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.devices = []
|
|
|
|
|
|
|
|
self.manager_id = gen_id()
|
|
|
|
self.manager_id = gen_id()
|
|
|
|
|
|
|
|
self.devices = []
|
|
|
|
|
|
|
|
self.config = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.import_config_file()
|
|
|
|
|
|
|
|
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))
|
|
|
|
@ -19,15 +28,67 @@ class Manager():
|
|
|
|
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):
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
self.config = restored_data
|
|
|
|
|
|
|
|
self.devices = self.config['devices']
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
with open(path, "w+") as config_file:
|
|
|
|
|
|
|
|
config_file.write(toml.dumps({'devices': preserved_data}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
# new_device_list.append(conf_dev)
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# self.export_config_file(new_config_tuple)
|
|
|
|
|
|
|
|
# #self.update_layout() update devices layouts that need to be updated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_layout(self, new_layout, device):
|
|
|
|
|
|
|
|
device.layout = new_layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_layout(self, device):
|
|
|
|
|
|
|
|
device.layout = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def construct_from_config(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
for confdev in self.config.devices:
|
|
|
|
|
|
|
|
dev = Device(confdev.name, confdev.resolution, confdev.device_id)
|
|
|
|
|
|
|
|
#for cwidg in dev.widgets: ###add code to construct widgets as well
|
|
|
|
|
|
|
|
self.devices.append(dev)
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@preservable
|
|
|
|
class Device():
|
|
|
|
class Device():
|
|
|
|
# Device class to contain a device's properties, layout and widgets
|
|
|
|
# Device class to contain a device's properties, layout and widgets
|
|
|
|
def __init__(self, name, resolution):
|
|
|
|
def __init__(self, name, resolution, dev_id=None):
|
|
|
|
|
|
|
|
#attributes
|
|
|
|
self.name = name
|
|
|
|
self.name = name
|
|
|
|
self.resolution = resolution
|
|
|
|
self.resolution = resolution
|
|
|
|
self.device_id = gen_id()
|
|
|
|
if(dev_id is None):
|
|
|
|
|
|
|
|
self.device_id = gen_id()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.device_id = dev_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#widgets container
|
|
|
|
self.widgets = []
|
|
|
|
self.widgets = []
|
|
|
|
self.layout = Layout()
|
|
|
|
|
|
|
|
|
|
|
|
#generated
|
|
|
|
self.device_image = None
|
|
|
|
self.device_image = None
|
|
|
|
|
|
|
|
|
|
|
|
class Layout():
|
|
|
|
class Layout():
|
|
|
|
@ -35,78 +96,18 @@ class Layout():
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
self.layout = {}
|
|
|
|
self.layout = {}
|
|
|
|
|
|
|
|
|
|
|
|
def import_file(self):
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_to_file(self):
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_layout(self, new_layout):
|
|
|
|
|
|
|
|
self.layout = new_layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clear_layout(self):
|
|
|
|
|
|
|
|
self.layout = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Test():
|
|
|
|
|
|
|
|
# Test class to test other classes and their functions.
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
#instantiate manager class
|
|
|
|
|
|
|
|
self.manager = Manager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_manager(self):
|
|
|
|
|
|
|
|
manager_uuid = self.manager_uuid()
|
|
|
|
|
|
|
|
add_device = self.add_device()
|
|
|
|
|
|
|
|
device_uuid = self.device_uuid()
|
|
|
|
|
|
|
|
remove_device = self.remove_device()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("manager id is valid: " + str(manager_uuid))
|
|
|
|
|
|
|
|
print("device id is valid: " + str(device_uuid))
|
|
|
|
|
|
|
|
print("added device: " + str(add_device))
|
|
|
|
|
|
|
|
print("removed device: " + str(remove_device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Note: Do function wrapper here for tests functions, to say "test_function_name" success/failure
|
|
|
|
|
|
|
|
def manager_uuid(self):
|
|
|
|
|
|
|
|
uuid_test = self.manager.manager_id
|
|
|
|
|
|
|
|
return self.is_valid_uuid(uuid_test)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def device_uuid(self):
|
|
|
|
|
|
|
|
dev = self.manager.devices[0]
|
|
|
|
|
|
|
|
uuid_test = dev.device_id
|
|
|
|
|
|
|
|
return self.is_valid_uuid(uuid_test)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test if manager class's gen_id function produces a valid uuid
|
|
|
|
|
|
|
|
def is_valid_uuid(self, val):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
uuid.UUID(str(val))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_device(self):
|
|
|
|
|
|
|
|
len1 = len(self.manager.devices)
|
|
|
|
|
|
|
|
self.manager.add_device("test", (1080, 1920))
|
|
|
|
|
|
|
|
len2 = len(self.manager.devices)
|
|
|
|
|
|
|
|
if len2 == len1 + 1:
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_device(self):
|
|
|
|
|
|
|
|
id = self.manager.devices[0].device_id
|
|
|
|
|
|
|
|
len1 = len(self.manager.devices)
|
|
|
|
|
|
|
|
self.manager.remove_device(id)
|
|
|
|
|
|
|
|
len2 = len(self.manager.devices)
|
|
|
|
|
|
|
|
if len2 == len1 - 1:
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Global Functions
|
|
|
|
# Global Functions
|
|
|
|
#
|
|
|
|
#
|
|
|
|
def gen_id():
|
|
|
|
def gen_id():
|
|
|
|
return uuid.uuid4()
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert(input):
|
|
|
|
test = Test()
|
|
|
|
if isinstance(input, dict):
|
|
|
|
test.test_manager()
|
|
|
|
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
|
|
|
|
|