Implementation of interface (template) for all modules
- Correct setup of logging - all inkycal-modules inherit from the given template - Added basic, optional validation - more code cleanups - fixed a few minor bugs
This commit is contained in:
		| @@ -1,4 +1,2 @@ | ||||
| from .settings_parser import settings | ||||
| print('loaded settings') | ||||
| from .layout import layout | ||||
| print('loaded layout') | ||||
|   | ||||
| @@ -6,22 +6,21 @@ Copyright by aceisace | ||||
| """ | ||||
|  | ||||
| import logging | ||||
| import os | ||||
|  | ||||
| filename = os.path.basename(__file__).split('.py')[0] | ||||
| logger = logging.getLogger(filename) | ||||
| logger.setLevel(level=logging.INFO) | ||||
|  | ||||
| class layout: | ||||
|   """Page layout handling""" | ||||
|  | ||||
|   logger = logging.getLogger(__name__) | ||||
|   logging.basicConfig(level=logging.DEBUG) | ||||
|  | ||||
|   def __init__(self, model=None, width=None, height=None, | ||||
|                supports_colour=False): | ||||
|     """Initialize parameters for specified epaper model | ||||
|     Use model parameter to specify display OR | ||||
|     Crate a custom display with given width and height""" | ||||
|  | ||||
|     self.background_colour = 'white' | ||||
|     self.text_colour = 'black' | ||||
|  | ||||
|     if (model != None) and (width == None) and (height == None): | ||||
|       display_dimensions = { | ||||
|       'epd_7_in_5_v2_colour': (800, 400), | ||||
| @@ -78,15 +77,15 @@ class layout: | ||||
|       self.bottom_section_height = (self.display_height - | ||||
|         self.top_section_height - self.middle_section_height) | ||||
|  | ||||
|     logging.debug('top-section size: {} x {} px'.format( | ||||
|     logger.debug('top-section size: {} x {} px'.format( | ||||
|       self.top_section_width, self.top_section_height)) | ||||
|     logging.debug('middle-section size: {} x {} px'.format( | ||||
|     logger.debug('middle-section size: {} x {} px'.format( | ||||
|       self.middle_section_width, self.middle_section_height)) | ||||
|     logging.debug('bottom-section size: {} x {} px'.format( | ||||
|     logger.debug('bottom-section size: {} x {} px'.format( | ||||
|       self.bottom_section_width, self.bottom_section_height)) | ||||
|  | ||||
|  | ||||
|   def get_section_size(self, section): | ||||
|   def get_size(self, section): | ||||
|     """Enter top/middle/bottom to get the size of the section as a tuple: | ||||
|     (width, height)""" | ||||
|  | ||||
|   | ||||
| @@ -6,6 +6,7 @@ Copyright by aceisace | ||||
| """ | ||||
| import json | ||||
| import os | ||||
| from inkycal.config.layout import layout | ||||
|  | ||||
| class settings: | ||||
|   """Load and validate settings from the settings file""" | ||||
| @@ -25,7 +26,6 @@ class settings: | ||||
|   def __init__(self, settings_file_path): | ||||
|     """Load settings from path (folder or settings.json file)""" | ||||
|     try: | ||||
|       # If | ||||
|       if settings_file_path.endswith('settings.json'): | ||||
|         folder = settings_file_path.split('/settings.json')[0] | ||||
|       else: | ||||
| @@ -39,14 +39,23 @@ class settings: | ||||
|     except FileNotFoundError: | ||||
|       print('No settings file found in specified location') | ||||
|  | ||||
|     # Validate the settings | ||||
|     self._validate() | ||||
|  | ||||
|     # Get the height-percentages of the modules | ||||
|     heights = [_['height']/100 for _ in self._settings['panels']] | ||||
|  | ||||
|     self.layout = layout(model=self.model) | ||||
|     self.layout.create_sections(top_section= heights[0], | ||||
|                            middle_section=heights[1], | ||||
|                            bottom_section=heights[2]) | ||||
|  | ||||
|   def _validate(self): | ||||
|     """Validate the basic config""" | ||||
|     settings = self._settings | ||||
|  | ||||
|     required =  ['language', 'units', 'hours', 'model', 'calibration_hours', | ||||
|             'display_orientation'] | ||||
|     required =  ['language', 'units', 'hours', 'model', 'calibration_hours'] | ||||
|             #'display_orientation'] | ||||
|  | ||||
|     # Check if all required settings exist | ||||
|     for param in required: | ||||
| @@ -60,7 +69,7 @@ class settings: | ||||
|     self.hours = settings['hours'] | ||||
|     self.model = settings['model'] | ||||
|     self.calibration_hours = settings['calibration_hours'] | ||||
|     self.display_orientation = settings['display_orientation'] | ||||
|     #self.display_orientation = settings['display_orientation'] | ||||
|  | ||||
|     # Validate the parameters | ||||
|     if (not isinstance(self.language, str) or self.language not in | ||||
| @@ -87,30 +96,31 @@ class settings: | ||||
|       print('calibration_hours not supported, switching to fallback, [0,12,18]') | ||||
|       self.calibration_hours = [0,12,18] | ||||
|  | ||||
|     if (not isinstance(self.display_orientation, str) or self.display_orientation not in | ||||
|         self._supported_display_orientation): | ||||
|       print('display orientation not supported, switching to fallback, normal') | ||||
|       self.display_orientation = 'normal' | ||||
| ##    if (not isinstance(self.display_orientation, str) or self.display_orientation not in | ||||
| ##        self._supported_display_orientation): | ||||
| ##      print('display orientation not supported, switching to fallback, normal') | ||||
| ##      self.display_orientation = 'normal' | ||||
|  | ||||
|     print('Settings file loaded') | ||||
|     print('Settings file OK!') | ||||
|  | ||||
|   def _active_modules(self): | ||||
|   def active_modules(self): | ||||
|     modules = [section['type'] for section in self._settings['panels']] | ||||
|     return modules | ||||
|  | ||||
|   def get_config(self, module_name): | ||||
|     """Ge the config of this module""" | ||||
|     if module_name not in self._active_modules(): | ||||
|     """Ge the config of this module (size, config)""" | ||||
|     if module_name not in self.active_modules(): | ||||
|       print('No config is available for this module') | ||||
|     else: | ||||
|       for section in self._settings['panels']: | ||||
|         if section['type'] == module_name: | ||||
|           config = section['config'] | ||||
|     return config | ||||
|           size = self.layout.get_size(self.get_position(module_name)) | ||||
|     return {'size':size, 'config':config} | ||||
|  | ||||
|   def get_position(self, module_name): | ||||
|     """Get the position of this module's image on the display""" | ||||
|     if module_name not in self._active_modules(): | ||||
|     if module_name not in self.active_modules(): | ||||
|       print('No position is available for this module') | ||||
|     else: | ||||
|       for section in self._settings['panels']: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user