| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | import abc | 
					
						
							|  |  |  | from inkycal.custom import * | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class inkycal_module(metaclass=abc.ABCMeta): | 
					
						
							| 
									
										
										
										
											2020-12-06 23:26:17 +01:00
										 |  |  |   """Generic base class for inkycal modules""" | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @classmethod | 
					
						
							|  |  |  |   def __subclasshook__(cls, subclass): | 
					
						
							|  |  |  |     return (hasattr(subclass, 'generate_image') and | 
					
						
							|  |  |  |       callable(subclass.generate_image) or | 
					
						
							|  |  |  |       NotImplemented) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  |   def __init__(self, config): | 
					
						
							|  |  |  |     """Initialize module with given config""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     # Initializes base module | 
					
						
							|  |  |  |     # sets properties shared amongst all sections | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  |     self.config = conf = config['config'] | 
					
						
							|  |  |  |     self.width, self.height = conf['size'] | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  |     self.padding_left = self.padding_right = conf["padding_x"] | 
					
						
							|  |  |  |     self.padding_top = self.padding_bottom = conf['padding_y'] | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 11:53:48 +01:00
										 |  |  |     self.fontsize = conf["fontsize"] | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |     self.font = ImageFont.truetype( | 
					
						
							| 
									
										
										
										
											2020-11-21 16:22:15 +01:00
										 |  |  |       fonts['NotoSansUI-Regular'], size = self.fontsize) | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def set(self, help=False, **kwargs): | 
					
						
							|  |  |  |     """Set attributes of class, e.g. class.set(key=value)
 | 
					
						
							|  |  |  |     see that can be changed by setting help to True | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     lst = dir(self).copy() | 
					
						
							|  |  |  |     options = [_ for _ in lst if not _.startswith('_')] | 
					
						
							|  |  |  |     if 'logger' in options: options.remove('logger') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if help == True: | 
					
						
							|  |  |  |       print('The following can be configured:') | 
					
						
							|  |  |  |       print(options) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for key, value in kwargs.items(): | 
					
						
							|  |  |  |       if key in options: | 
					
						
							| 
									
										
										
										
											2020-06-13 15:13:53 +02:00
										 |  |  |         if key == 'fontsize': | 
					
						
							|  |  |  |           self.font  = ImageFont.truetype(self.font.path, value) | 
					
						
							|  |  |  |           self.fontsize = value | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |           setattr(self, key, value) | 
					
						
							| 
									
										
										
										
											2020-11-29 14:51:19 +01:00
										 |  |  |           print(f"set '{key}' to '{value}'") | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |       else: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:51:19 +01:00
										 |  |  |         print(f'{key} does not exist') | 
					
						
							| 
									
										
										
										
											2020-05-23 01:45:40 +02:00
										 |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Check if validation has been implemented | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |       self._validate() | 
					
						
							|  |  |  |     except AttributeError: | 
					
						
							|  |  |  |       print('no validation implemented') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @abc.abstractmethod | 
					
						
							|  |  |  |   def generate_image(self): | 
					
						
							|  |  |  |     # Generate image for this module with specified parameters | 
					
						
							|  |  |  |     raise NotImplementedError( | 
					
						
							|  |  |  |       'The developers were too lazy to implement this function') | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @classmethod | 
					
						
							|  |  |  |   def get_config(cls): | 
					
						
							|  |  |  |     # Do not change | 
					
						
							| 
									
										
										
										
											2020-11-12 21:20:12 +01:00
										 |  |  |     # Get the config of this module for the web-ui | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  |     try: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if hasattr(cls, 'requires'): | 
					
						
							|  |  |  |         for each in cls.requires: | 
					
						
							|  |  |  |           if not "label" in cls.requires[each]: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:51:19 +01:00
										 |  |  |             raise Exception(f"no label found for {each}") | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if hasattr(cls, 'optional'): | 
					
						
							|  |  |  |         for each in cls.optional: | 
					
						
							|  |  |  |           if not "label" in cls.optional[each]: | 
					
						
							| 
									
										
										
										
											2020-11-29 14:51:19 +01:00
										 |  |  |             raise Exception(f"no label found for {each}") | 
					
						
							| 
									
										
										
										
											2020-11-09 17:51:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       conf = { | 
					
						
							|  |  |  |         "name": cls.__name__, | 
					
						
							|  |  |  |         "name_str": cls.name, | 
					
						
							|  |  |  |         "requires": cls.requires if hasattr(cls, 'requires') else {}, | 
					
						
							|  |  |  |         "optional": cls.optional if hasattr(cls, 'optional') else {}, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       return conf | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |       raise Exception( | 
					
						
							|  |  |  |         'Ohoh, something went wrong while trying to get the config of this module') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:20:12 +01:00
										 |  |  | 
 |