|  |  | @ -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__)) | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -15,8 +15,7 @@ class Manager(): | 
			
		
	
		
		
			
				
					
					|  |  |  |     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)) | 
			
		
	
	
		
		
			
				
					|  |  | @ -27,29 +26,19 @@ class Manager(): | 
			
		
	
		
		
			
				
					
					|  |  |  |                 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) | 
			
		
	
		
		
			
				
					
					|  |  |  |             restored_data = restore(config) |  |  |  |         self.devices = [Device.load(device_data) for device_data in config['devices']] | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             return restored_data |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         except: |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             return None |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     def export_config_file(self): |  |  |  |     def export_config_file(self, path): | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         # take tuple containing all layouts and dump as toml |  |  |  |         # take tuple containing all layouts and dump as toml | 
			
		
	
		
		
			
				
					
					|  |  |  |         path = os.path.join(mydir, 'config.toml')  |  |  |  |         preserved_data = [device.save() for device in self.devices] | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         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 = [] | 
			
		
	
	
		
		
			
				
					|  |  | @ -68,18 +57,40 @@ 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 | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     """ | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     @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 |  |  |  |         # 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 = [] | 
			
		
	
	
		
		
			
				
					|  |  | @ -87,22 +98,76 @@ class Device(): | 
			
		
	
		
		
			
				
					
					|  |  |  |         # 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 |  |  |  |  | 
			
		
	
	
		
		
			
				
					|  |  | 
 |