#!/usr/bin/env python3 from collections import namedtuple import uuid import os import toml from preserve import preservable, preserve, restore mydir = os.path.dirname(os.path.abspath(__file__)) # Classes # class Manager(): # 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() 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) 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(): # Device class to contain a device's properties, layout and widgets def __init__(self, name, resolution, dev_id=None): #attributes self.name = name self.resolution = resolution if(dev_id is None): self.device_id = gen_id() else: self.device_id = dev_id #widgets container self.widgets = [] #generated self.device_image = None class Layout(): # Layout class to contain a layout and functions to manipulate that layout. def __init__(self): self.layout = {} # 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