Code cleanup + removing obsolete files
This commit is contained in:
		| @@ -1,474 +0,0 @@ | ||||
| from inkycal import Settings, Layout | ||||
| from inkycal.custom import * | ||||
|  | ||||
| #from os.path import exists | ||||
| import os | ||||
| import traceback | ||||
| import logging | ||||
| import arrow | ||||
| import time | ||||
|  | ||||
| try: | ||||
|   from PIL import Image | ||||
| except ImportError: | ||||
|   print('Pillow is not installed! Please install with:') | ||||
|   print('pip3 install Pillow') | ||||
|  | ||||
| try: | ||||
|   import numpy | ||||
| except ImportError: | ||||
|   print('numpy is not installed! Please install with:') | ||||
|   print('pip3 install numpy') | ||||
|  | ||||
| logger = logging.getLogger('inkycal') | ||||
| logger.setLevel(level=logging.ERROR) | ||||
|  | ||||
| class Inkycal: | ||||
|   """Inkycal main class""" | ||||
|  | ||||
|   def __init__(self, settings_path, render=True): | ||||
|     """Initialise Inkycal | ||||
|     settings_path = str -> location/folder of settings file | ||||
|     render = bool -> show something on the ePaper? | ||||
|     """ | ||||
|     self._release = '2.0.0beta' | ||||
|  | ||||
|     # Check if render is boolean | ||||
|     if not isinstance(render, bool): | ||||
|       raise Exception('render must be True or False, not "{}"'.format(render)) | ||||
|     self.render = render | ||||
|  | ||||
|     # Init settings class | ||||
|     self.Settings = Settings(settings_path) | ||||
|  | ||||
|     # Check if display support colour | ||||
|     self.supports_colour = self.Settings.Layout.supports_colour | ||||
|  | ||||
|     # Option to flip image upside down | ||||
|     if self.Settings.display_orientation == 'normal': | ||||
|       self.upside_down = False | ||||
|  | ||||
|     elif self.Settings.display_orientation == 'upside_down': | ||||
|       self.upside_down = True | ||||
|  | ||||
|     # Option to use epaper image optimisation | ||||
|     self.optimize = True | ||||
|  | ||||
|     # Load drivers if image should be rendered | ||||
|     if self.render == True: | ||||
|  | ||||
|       # Get model and check if colour can be rendered | ||||
|       model= self.Settings.model | ||||
|  | ||||
|       # Init Display class | ||||
|       from inkycal.display import Display | ||||
|       self.Display = Display(model) | ||||
|  | ||||
|       # get calibration hours | ||||
|       self._calibration_hours = self.Settings.calibration_hours | ||||
|  | ||||
|       # set a check for calibration | ||||
|       self._calibration_state = False | ||||
|  | ||||
|     # load+validate settings file. Import and setup specified modules | ||||
|     self.active_modules = self.Settings.active_modules() | ||||
|     for module in self.active_modules: | ||||
|       try: | ||||
|         loader = 'from inkycal.modules import {0}'.format(module) | ||||
|         module_data = self.Settings.get_config(module) | ||||
|         size, conf = module_data['size'], module_data['config'] | ||||
|         setup = 'self.{} = {}(size, conf)'.format(module, module) | ||||
|         exec(loader) | ||||
|         exec(setup) | ||||
|         logger.debug(('{}: size: {}, config: {}'.format(module, size, conf))) | ||||
|  | ||||
|       # If a module was not found, print an error message | ||||
|       except ImportError: | ||||
|         print( | ||||
|           'Could not find module: "{}". Please try to import manually.'.format( | ||||
|           module)) | ||||
|  | ||||
|     # Give an OK message | ||||
|     print('loaded inkycal') | ||||
|  | ||||
|   def countdown(self, interval_mins=None): | ||||
|     """Returns the remaining time in seconds until next display update""" | ||||
|  | ||||
|     # Validate update interval | ||||
|     allowed_intervals = [10, 15, 20, 30, 60] | ||||
|  | ||||
|     # Check if empty, if empty, use value from settings file | ||||
|     if interval_mins == None: | ||||
|       interval_mins = self.Settings.update_interval | ||||
|  | ||||
|     # Check if integer | ||||
|     if not isinstance(interval_mins, int): | ||||
|       raise Exception('Update interval must be an integer -> 60') | ||||
|  | ||||
|     # Check if value is supported | ||||
|     if interval_mins not in allowed_intervals: | ||||
|       raise Exception('Update interval is {}, but should be one of: {}'.format( | ||||
|         interval_mins, allowed_intervals)) | ||||
|  | ||||
|     # Find out at which minutes the update should happen | ||||
|     now = arrow.now() | ||||
|     update_timings = [(60 - int(interval_mins)*updates) for updates in | ||||
|                       range(60//int(interval_mins))][::-1] | ||||
|  | ||||
|     # Calculate time in mins until next update | ||||
|     minutes = [_ for _ in update_timings if _>= now.minute][0] - now.minute | ||||
|  | ||||
|     # Print the remaining time in mins until next update | ||||
|     print('{0} Minutes left until next refresh'.format(minutes)) | ||||
|  | ||||
|     # Calculate time in seconds until next update | ||||
|     remaining_time = minutes*60 + (60 - now.second) | ||||
|  | ||||
|     # Return seconds until next update | ||||
|     return remaining_time | ||||
|  | ||||
|   def test(self): | ||||
|     """Inkycal test run. | ||||
|     Generates images for each module, one by one and prints OK if no | ||||
|     problems were found.""" | ||||
|     print('You are running inkycal v{}'.format(self._release)) | ||||
|  | ||||
|  | ||||
|     print('Running inkycal test-run for {} ePaper'.format( | ||||
|       self.Settings.model)) | ||||
|  | ||||
|     if self.upside_down == True: | ||||
|       print('upside-down mode active') | ||||
|  | ||||
|     for module in self.active_modules: | ||||
|       generate_im = 'self.{0}.generate_image()'.format(module) | ||||
|       print('generating image for {} module...'.format(module), end = '') | ||||
|       try: | ||||
|         exec(generate_im) | ||||
|         print('OK!') | ||||
|       except Exception as Error: | ||||
|         print('Error!') | ||||
|         print(traceback.format_exc()) | ||||
|  | ||||
|   def run(self): | ||||
|     """Runs the main inykcal program nonstop (cannot be stopped anymore!) | ||||
|     Will show something on the display if render was set to True""" | ||||
|  | ||||
|     # TODO: printing traceback on display (or at least a smaller message?) | ||||
|     # Calibration | ||||
|  | ||||
|     # Get the time of initial run | ||||
|     runtime = arrow.now() | ||||
|  | ||||
|     # Function to flip images upside down | ||||
|     upside_down = lambda image: image.rotate(180, expand=True) | ||||
|  | ||||
|     # Count the number of times without any errors | ||||
|     counter = 1 | ||||
|  | ||||
|     # Calculate the max. fontsize for info-section | ||||
|     if self.Settings.info_section == True: | ||||
|       info_section_height = round(self.Settings.Layout.display_height* (1/95) ) | ||||
|       self.font = auto_fontsize(ImageFont.truetype( | ||||
|         fonts['NotoSans-SemiCondensed']), info_section_height) | ||||
|  | ||||
|     while True: | ||||
|       print('Generating images for all modules...') | ||||
|       for module in self.active_modules: | ||||
|         generate_im = 'self.{0}.generate_image()'.format(module) | ||||
|         try: | ||||
|           exec(generate_im) | ||||
|         except Exception as Error: | ||||
|           print('Error!') | ||||
|           message = traceback.format_exc() | ||||
|           print(message) | ||||
|           counter = 0 | ||||
|       print('OK') | ||||
|  | ||||
|       # Assemble image from each module | ||||
|       self._assemble() | ||||
|  | ||||
|       # Check if image should be rendered | ||||
|       if self.render == True: | ||||
|         Display = self.Display | ||||
|  | ||||
|         self._calibration_check() | ||||
|  | ||||
|         if self.supports_colour == True: | ||||
|           im_black = Image.open(images+'canvas.png') | ||||
|           im_colour = Image.open(images+'canvas_colour.png') | ||||
|  | ||||
|           # Flip the image by 180° if required | ||||
|           if self.upside_down == True: | ||||
|             im_black = upside_down(im_black) | ||||
|             im_colour = upside_down(im_colour) | ||||
|  | ||||
|           # render the image on the display | ||||
|           Display.render(im_black, im_colour) | ||||
|  | ||||
|         # Part for black-white ePapers | ||||
|         elif self.supports_colour == False: | ||||
|  | ||||
|           im_black = self._merge_bands() | ||||
|  | ||||
|           # Flip the image by 180° if required | ||||
|           if self.upside_down == True: | ||||
|             im_black = upside_down(im_black) | ||||
|  | ||||
|           Display.render(im_black) | ||||
|  | ||||
|       print('\ninkycal has been running without any errors for', end = ' ') | ||||
|       print('{} display updates'.format(counter)) | ||||
|       print('Programm started {}'.format(runtime.humanize())) | ||||
|  | ||||
|       counter += 1 | ||||
|  | ||||
|       sleep_time = self.countdown() | ||||
|       time.sleep(sleep_time) | ||||
|  | ||||
|   def _merge_bands(self): | ||||
|     """Merges black and coloured bands for black-white ePapers | ||||
|     returns the merged image | ||||
|     """ | ||||
|  | ||||
|     im_path = images | ||||
|  | ||||
|     im1_path, im2_path = images+'canvas.png', images+'canvas_colour.png' | ||||
|  | ||||
|     # If there is an image for black and colour, merge them | ||||
|     if os.path.exists(im1_path) and os.path.exists(im2_path): | ||||
|  | ||||
|       im1 = Image.open(im1_path).convert('RGBA') | ||||
|       im2 = Image.open(im2_path).convert('RGBA') | ||||
|  | ||||
|       def clear_white(img): | ||||
|         """Replace all white pixels from image with transparent pixels | ||||
|         """ | ||||
|         x = numpy.asarray(img.convert('RGBA')).copy() | ||||
|         x[:, :, 3] = (255 * (x[:, :, :3] != 255).any(axis=2)).astype(numpy.uint8) | ||||
|         return Image.fromarray(x) | ||||
|  | ||||
|       im2 = clear_white(im2) | ||||
|       im1.paste(im2, (0,0), im2) | ||||
|  | ||||
|     # If there is no image for the coloured-band, return the bw-image | ||||
|     elif os.path.exists(im1_path) and not os.path.exists(im2_path): | ||||
|       im1 = Image.open(im1_name).convert('RGBA') | ||||
|  | ||||
|     return im1 | ||||
|  | ||||
|  | ||||
|   def _assemble(self): | ||||
|     """Assmebles all sub-images to a single image""" | ||||
|  | ||||
|     # Create an empty canvas with the size of the display | ||||
|     width, height = self.Settings.Layout.display_size | ||||
|      | ||||
|     if self.Settings.info_section == True: | ||||
|       height = round(height * ((1/95)*100) ) | ||||
|  | ||||
|     im_black = Image.new('RGB', (width, height), color = 'white') | ||||
|     im_colour = Image.new('RGB', (width ,height), color = 'white') | ||||
|  | ||||
|     # Set cursor for y-axis | ||||
|     im1_cursor = 0 | ||||
|     im2_cursor = 0 | ||||
|  | ||||
|     for module in self.active_modules: | ||||
|  | ||||
|       im1_path = images+module+'.png' | ||||
|       im2_path = images+module+'_colour.png' | ||||
|  | ||||
|       # Check if there is an image for the black band | ||||
|       if os.path.exists(im1_path): | ||||
|  | ||||
|         # Get actual size of image | ||||
|         im1 = Image.open(im1_path).convert('RGBA') | ||||
|         im1_size = im1.size | ||||
|  | ||||
|         # Get the size of the section | ||||
|         section_size = self.Settings.get_config(module)['size'] | ||||
|         # Calculate coordinates to center the image | ||||
|         x = int( (section_size[0] - im1_size[0]) /2) | ||||
|  | ||||
|         # If this is the first module, use the y-offset | ||||
|         if im1_cursor == 0: | ||||
|           y = int( (section_size[1]-im1_size[1]) /2) | ||||
|         else: | ||||
|           y = im1_cursor + int( (section_size[1]-im1_size[1]) /2) | ||||
|  | ||||
|         # center the image in the section space | ||||
|         im_black.paste(im1, (x,y), im1) | ||||
|  | ||||
|         # Shift the y-axis cursor at the beginning of next section | ||||
|         im1_cursor += section_size[1] | ||||
|  | ||||
|       # Check if there is an image for the coloured band | ||||
|       if os.path.exists(im2_path): | ||||
|  | ||||
|         # Get actual size of image | ||||
|         im2 = Image.open(im2_path).convert('RGBA') | ||||
|         im2_size = im2.size | ||||
|  | ||||
|         # Get the size of the section | ||||
|         section_size = self.Settings.get_config(module)['size'] | ||||
|  | ||||
|         # Calculate coordinates to center the image | ||||
|         x = int( (section_size[0]-im2_size[0]) /2) | ||||
|  | ||||
|         # If this is the first module, use the y-offset | ||||
|         if im2_cursor == 0: | ||||
|           y = int( (section_size[1]-im2_size[1]) /2) | ||||
|         else: | ||||
|           y = im2_cursor + int( (section_size[1]-im2_size[1]) /2) | ||||
|  | ||||
|         # center the image in the section space | ||||
|         im_colour.paste(im2, (x,y), im2) | ||||
|  | ||||
|         # Shift the y-axis cursor at the beginning of next section | ||||
|         im2_cursor += section_size[1] | ||||
|  | ||||
|     # Show an info section if specified by the settings file | ||||
|     now = arrow.now() | ||||
|     stamp = 'last update: {}'.format(now.format('D MMM @ HH:mm', locale = | ||||
|                                                 self.Settings.language)) | ||||
|     if self.Settings.info_section == True: | ||||
|       write(im_black, (0, im1_cursor), (width, height-im1_cursor), | ||||
|             stamp, font = self.font) | ||||
|  | ||||
|  | ||||
|     # optimize the image by mapping colours to pure black and white | ||||
|     if self.optimize == True: | ||||
|       self._optimize_im(im_black).save(images+'canvas.png', 'PNG') | ||||
|       self._optimize_im(im_colour).save(images+'canvas_colour.png', 'PNG') | ||||
|     else: | ||||
|       im_black.save(images+'canvas.png', 'PNG') | ||||
|       im_colour.save(images+'canvas_colour.png', 'PNG') | ||||
|  | ||||
|   def _optimize_im(self, image, threshold=220): | ||||
|     """Optimize the image for rendering on ePaper displays""" | ||||
|  | ||||
|     buffer = numpy.array(image.convert('RGB')) | ||||
|     red, green = buffer[:, :, 0], buffer[:, :, 1] | ||||
|     # grey->black | ||||
|     buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0,0,0] | ||||
|     image = Image.fromarray(buffer) | ||||
|     return image | ||||
|  | ||||
|   def calibrate(self): | ||||
|     """Calibrate the ePaper display to prevent burn-ins (ghosting) | ||||
|     use this command to manually calibrate the display""" | ||||
|  | ||||
|     self.Display.calibrate() | ||||
|  | ||||
|   def _calibration_check(self): | ||||
|     """Calibration sheduler | ||||
|     uses calibration hours from settings file to check if calibration is due""" | ||||
|     now = arrow.now() | ||||
|     print('hour:', now.hour, 'hours:', self._calibration_hours) | ||||
|     print('state:', self._calibration_state) | ||||
|     if now.hour in self._calibration_hours and self._calibration_state == False: | ||||
|       self.calibrate() | ||||
|       self._calibration_state = True | ||||
|     else: | ||||
|       self._calibration_state = False | ||||
|  | ||||
|  | ||||
|   def _check_for_updates(self): | ||||
|     """Check if a new update is available for inkycal""" | ||||
|  | ||||
|     raise NotImplementedError('Tha developer were too lazy to implement this..') | ||||
|  | ||||
|  | ||||
|   @staticmethod | ||||
|   def _add_module(filepath_module, classname): | ||||
|     """Add a third party module to inkycal | ||||
|     filepath_module = the full path of your module. The file should be in /modules! | ||||
|     classname = the name of your class inside the module | ||||
|     """ | ||||
|  | ||||
|     # Path for modules | ||||
|     _module_path = 'inkycal/modules/' | ||||
|  | ||||
|     # Check if the filepath is a string | ||||
|     if not isinstance(filepath_module, str): | ||||
|       raise ValueError('filepath has to be a string!') | ||||
|  | ||||
|     # Check if the classname is a string | ||||
|     if not isinstance(classname, str): | ||||
|       raise ValueError('classname has to be a string!') | ||||
|  | ||||
|     # TODO: | ||||
|     # Ensure only third-party modules are deleted as built-in modules | ||||
|     # should not be deleted | ||||
|  | ||||
|     # Check if module is inside the modules folder | ||||
|     if not _module_path in filepath_module: | ||||
|       raise Exception('Your module should be in', _module_path) | ||||
|  | ||||
|     # Get the name of the third-party module file without extension (.py) | ||||
|     filename = filepath_module.split('.py')[0].split('/')[-1] | ||||
|  | ||||
|     # Check if filename or classname is in the current module init file | ||||
|     with open('modules/__init__.py', mode ='r') as module_init: | ||||
|       content = module_init.read().splitlines() | ||||
|  | ||||
|     for line in content: | ||||
|       if (filename or clasname) in line: | ||||
|         raise Exception( | ||||
|           'A module with this filename or classname already exists') | ||||
|  | ||||
|     # Check if filename or classname is in the current inkycal init file | ||||
|     with open('__init__.py', mode ='r') as inkycal_init: | ||||
|       content = inkycal_init.read().splitlines() | ||||
|  | ||||
|     for line in content: | ||||
|       if (filename or clasname) in line: | ||||
|         raise Exception( | ||||
|           'A module with this filename or classname already exists') | ||||
|  | ||||
|     # If all checks have passed, add the module in the module init file | ||||
|     with open('modules/__init__.py', mode='a') as module_init: | ||||
|       module_init.write('from .{} import {}'.format(filename, classname)) | ||||
|  | ||||
|     # If all checks have passed, add the module in the inkycal init file | ||||
|     with open('__init__.py', mode ='a') as inkycal_init: | ||||
|       inkycal_init.write('# Added by module adder \n') | ||||
|       inkycal_init.write('import inkycal.modules.{}'.format(filename)) | ||||
|  | ||||
|     print('Your module {} has been added successfully! Hooray!'.format( | ||||
|       classname)) | ||||
|  | ||||
|   @staticmethod | ||||
|   def _remove_module(classname, remove_file = True): | ||||
|     """Removes a third-party module from inkycal | ||||
|     Input the classname of the file you want to remove  | ||||
|     """ | ||||
|  | ||||
|     # Check if filename or classname is in the current module init file | ||||
|     with open('modules/__init__.py', mode ='r') as module_init: | ||||
|       content = module_init.read().splitlines() | ||||
|  | ||||
|     with open('modules/__init__.py', mode ='w') as module_init: | ||||
|       for line in content: | ||||
|         if not classname in line: | ||||
|           module_init.write(line+'\n') | ||||
|         else: | ||||
|           filename = line.split(' ')[1].split('.')[1] | ||||
|  | ||||
|     # Check if filename or classname is in the current inkycal init file | ||||
|     with open('__init__.py', mode ='r') as inkycal_init: | ||||
|       content = inkycal_init.read().splitlines() | ||||
|  | ||||
|     with open('__init__.py', mode ='w') as inkycal_init: | ||||
|       for line in content: | ||||
|         if not filename in line: | ||||
|           inkycal_init.write(line+'\n') | ||||
|  | ||||
|     # remove the file of the third party module if it exists and remove_file | ||||
|     # was set to True (default) | ||||
|     if os.path.exists('modules/{}.py'.format(filename)) and remove_file == True: | ||||
|       os.remove('modules/{}.py'.format(filename)) | ||||
|  | ||||
|     print('The module {} has been removed successfully'.format(classname)) | ||||
|  | ||||
							
								
								
									
										474
									
								
								inkycal/old.py
									
									
									
									
									
								
							
							
						
						
									
										474
									
								
								inkycal/old.py
									
									
									
									
									
								
							| @@ -1,474 +0,0 @@ | ||||
| from inkycal import Settings, Layout | ||||
| from inkycal.custom import * | ||||
|  | ||||
| #from os.path import exists | ||||
| import os | ||||
| import traceback | ||||
| import logging | ||||
| import arrow | ||||
| import time | ||||
|  | ||||
| try: | ||||
|   from PIL import Image | ||||
| except ImportError: | ||||
|   print('Pillow is not installed! Please install with:') | ||||
|   print('pip3 install Pillow') | ||||
|  | ||||
| try: | ||||
|   import numpy | ||||
| except ImportError: | ||||
|   print('numpy is not installed! Please install with:') | ||||
|   print('pip3 install numpy') | ||||
|  | ||||
| logger = logging.getLogger('inkycal') | ||||
| logger.setLevel(level=logging.ERROR) | ||||
|  | ||||
| class Inkycal: | ||||
|   """Inkycal main class""" | ||||
|  | ||||
|   def __init__(self, settings_path, render=True): | ||||
|     """initialise class | ||||
|     settings_path = str -> location/folder of settings file | ||||
|     render = bool -> show something on the ePaper? | ||||
|     """ | ||||
|     self._release = '2.0.0beta' | ||||
|  | ||||
|     # Check if render is boolean | ||||
|     if not isinstance(render, bool): | ||||
|       raise Exception('render must be True or False, not "{}"'.format(render)) | ||||
|     self.render = render | ||||
|  | ||||
|     # Init settings class | ||||
|     self.Settings = Settings(settings_path) | ||||
|  | ||||
|     # Check if display support colour | ||||
|     self.supports_colour = self.Settings.Layout.supports_colour | ||||
|  | ||||
|     # Option to flip image upside down | ||||
|     if self.Settings.display_orientation == 'normal': | ||||
|       self.upside_down = False | ||||
|  | ||||
|     elif self.Settings.display_orientation == 'upside_down': | ||||
|       self.upside_down = True | ||||
|  | ||||
|     # Option to use epaper image optimisation | ||||
|     self.optimize = True | ||||
|  | ||||
|     # Load drivers if image should be rendered | ||||
|     if self.render == True: | ||||
|  | ||||
|       # Get model and check if colour can be rendered | ||||
|       model= self.Settings.model | ||||
|  | ||||
|       # Init Display class | ||||
|       from inkycal.display import Display | ||||
|       self.Display = Display(model) | ||||
|  | ||||
|       # get calibration hours | ||||
|       self._calibration_hours = self.Settings.calibration_hours | ||||
|  | ||||
|       # set a check for calibration | ||||
|       self._calibration_state = False | ||||
|  | ||||
|     # load+validate settings file. Import and setup specified modules | ||||
|     self.active_modules = self.Settings.active_modules() | ||||
|     for module in self.active_modules: | ||||
|       try: | ||||
|         loader = 'from inkycal.modules import {0}'.format(module) | ||||
|         module_data = self.Settings.get_config(module) | ||||
|         size, conf = module_data['size'], module_data['config'] | ||||
|         setup = 'self.{} = {}(size, conf)'.format(module, module) | ||||
|         exec(loader) | ||||
|         exec(setup) | ||||
|         logger.debug(('{}: size: {}, config: {}'.format(module, size, conf))) | ||||
|  | ||||
|       # If a module was not found, print an error message | ||||
|       except ImportError: | ||||
|         print( | ||||
|           'Could not find module: "{}". Please try to import manually.'.format( | ||||
|           module)) | ||||
|  | ||||
|     # Give an OK message | ||||
|     print('loaded inkycal') | ||||
|  | ||||
|   def countdown(self, interval_mins=None): | ||||
|     """Returns the remaining time in seconds until next display update""" | ||||
|  | ||||
|     # Validate update interval | ||||
|     allowed_intervals = [10, 15, 20, 30, 60] | ||||
|  | ||||
|     # Check if empty, if empty, use value from settings file | ||||
|     if interval_mins == None: | ||||
|       interval_mins = self.Settings.update_interval | ||||
|  | ||||
|     # Check if integer | ||||
|     if not isinstance(interval_mins, int): | ||||
|       raise Exception('Update interval must be an integer -> 60') | ||||
|  | ||||
|     # Check if value is supported | ||||
|     if interval_mins not in allowed_intervals: | ||||
|       raise Exception('Update interval is {}, but should be one of: {}'.format( | ||||
|         interval_mins, allowed_intervals)) | ||||
|  | ||||
|     # Find out at which minutes the update should happen | ||||
|     now = arrow.now() | ||||
|     update_timings = [(60 - int(interval_mins)*updates) for updates in | ||||
|                       range(60//int(interval_mins))][::-1] | ||||
|  | ||||
|     # Calculate time in mins until next update | ||||
|     minutes = [_ for _ in update_timings if _>= now.minute][0] - now.minute | ||||
|  | ||||
|     # Print the remaining time in mins until next update | ||||
|     print('{0} Minutes left until next refresh'.format(minutes)) | ||||
|  | ||||
|     # Calculate time in seconds until next update | ||||
|     remaining_time = minutes*60 + (60 - now.second) | ||||
|  | ||||
|     # Return seconds until next update | ||||
|     return remaining_time | ||||
|  | ||||
|   def test(self): | ||||
|     """Inkycal test run. | ||||
|     Generates images for each module, one by one and prints OK if no | ||||
|     problems were found.""" | ||||
|     print('You are running inkycal v{}'.format(self._release)) | ||||
|  | ||||
|  | ||||
|     print('Running inkycal test-run for {} ePaper'.format( | ||||
|       self.Settings.model)) | ||||
|  | ||||
|     if self.upside_down == True: | ||||
|       print('upside-down mode active') | ||||
|  | ||||
|     for module in self.active_modules: | ||||
|       generate_im = 'self.{0}.generate_image()'.format(module) | ||||
|       print('generating image for {} module...'.format(module), end = '') | ||||
|       try: | ||||
|         exec(generate_im) | ||||
|         print('OK!') | ||||
|       except Exception as Error: | ||||
|         print('Error!') | ||||
|         print(traceback.format_exc()) | ||||
|  | ||||
|   def run(self): | ||||
|     """Runs the main inykcal program nonstop (cannot be stopped anymore!) | ||||
|     Will show something on the display if render was set to True""" | ||||
|  | ||||
|     # TODO: printing traceback on display (or at least a smaller message?) | ||||
|     # Calibration | ||||
|  | ||||
|     # Get the time of initial run | ||||
|     runtime = arrow.now() | ||||
|  | ||||
|     # Function to flip images upside down | ||||
|     upside_down = lambda image: image.rotate(180, expand=True) | ||||
|  | ||||
|     # Count the number of times without any errors | ||||
|     counter = 1 | ||||
|  | ||||
|     # Calculate the max. fontsize for info-section | ||||
|     if self.Settings.info_section == True: | ||||
|       info_section_height = round(self.Settings.Layout.display_height* (1/95) ) | ||||
|       self.font = auto_fontsize(ImageFont.truetype( | ||||
|         fonts['NotoSans-SemiCondensed']), info_section_height) | ||||
|  | ||||
|     while True: | ||||
|       print('Generating images for all modules...') | ||||
|       for module in self.active_modules: | ||||
|         generate_im = 'self.{0}.generate_image()'.format(module) | ||||
|         try: | ||||
|           exec(generate_im) | ||||
|         except Exception as Error: | ||||
|           print('Error!') | ||||
|           message = traceback.format_exc() | ||||
|           print(message) | ||||
|           counter = 0 | ||||
|       print('OK') | ||||
|  | ||||
|       # Assemble image from each module | ||||
|       self._assemble() | ||||
|  | ||||
|       # Check if image should be rendered | ||||
|       if self.render == True: | ||||
|         Display = self.Display | ||||
|  | ||||
|         self._calibration_check() | ||||
|  | ||||
|         if self.supports_colour == True: | ||||
|           im_black = Image.open(images+'canvas.png') | ||||
|           im_colour = Image.open(images+'canvas_colour.png') | ||||
|  | ||||
|           # Flip the image by 180° if required | ||||
|           if self.upside_down == True: | ||||
|             im_black = upside_down(im_black) | ||||
|             im_colour = upside_down(im_colour) | ||||
|  | ||||
|           # render the image on the display | ||||
|           Display.render(im_black, im_colour) | ||||
|  | ||||
|         # Part for black-white ePapers | ||||
|         elif self.supports_colour == False: | ||||
|  | ||||
|           im_black = self._merge_bands() | ||||
|  | ||||
|           # Flip the image by 180° if required | ||||
|           if self.upside_down == True: | ||||
|             im_black = upside_down(im_black) | ||||
|  | ||||
|           Display.render(im_black) | ||||
|  | ||||
|       print('\ninkycal has been running without any errors for', end = ' ') | ||||
|       print('{} display updates'.format(counter)) | ||||
|       print('Programm started {}'.format(runtime.humanize())) | ||||
|  | ||||
|       counter += 1 | ||||
|  | ||||
|       sleep_time = self.countdown() | ||||
|       time.sleep(sleep_time) | ||||
|  | ||||
|   def _merge_bands(self): | ||||
|     """Merges black and coloured bands for black-white ePapers | ||||
|     returns the merged image | ||||
|     """ | ||||
|  | ||||
|     im_path = images | ||||
|  | ||||
|     im1_path, im2_path = images+'canvas.png', images+'canvas_colour.png' | ||||
|  | ||||
|     # If there is an image for black and colour, merge them | ||||
|     if os.path.exists(im1_path) and os.path.exists(im2_path): | ||||
|  | ||||
|       im1 = Image.open(im1_path).convert('RGBA') | ||||
|       im2 = Image.open(im2_path).convert('RGBA') | ||||
|  | ||||
|       def clear_white(img): | ||||
|         """Replace all white pixels from image with transparent pixels | ||||
|         """ | ||||
|         x = numpy.asarray(img.convert('RGBA')).copy() | ||||
|         x[:, :, 3] = (255 * (x[:, :, :3] != 255).any(axis=2)).astype(numpy.uint8) | ||||
|         return Image.fromarray(x) | ||||
|  | ||||
|       im2 = clear_white(im2) | ||||
|       im1.paste(im2, (0,0), im2) | ||||
|  | ||||
|     # If there is no image for the coloured-band, return the bw-image | ||||
|     elif os.path.exists(im1_path) and not os.path.exists(im2_path): | ||||
|       im1 = Image.open(im1_name).convert('RGBA') | ||||
|  | ||||
|     return im1 | ||||
|  | ||||
|  | ||||
|   def _assemble(self): | ||||
|     """Assmebles all sub-images to a single image""" | ||||
|  | ||||
|     # Create an empty canvas with the size of the display | ||||
|     width, height = self.Settings.Layout.display_size | ||||
|      | ||||
|     if self.Settings.info_section == True: | ||||
|       height = round(height * ((1/95)*100) ) | ||||
|  | ||||
|     im_black = Image.new('RGB', (width, height), color = 'white') | ||||
|     im_colour = Image.new('RGB', (width ,height), color = 'white') | ||||
|  | ||||
|     # Set cursor for y-axis | ||||
|     im1_cursor = 0 | ||||
|     im2_cursor = 0 | ||||
|  | ||||
|     for module in self.active_modules: | ||||
|  | ||||
|       im1_path = images+module+'.png' | ||||
|       im2_path = images+module+'_colour.png' | ||||
|  | ||||
|       # Check if there is an image for the black band | ||||
|       if os.path.exists(im1_path): | ||||
|  | ||||
|         # Get actual size of image | ||||
|         im1 = Image.open(im1_path).convert('RGBA') | ||||
|         im1_size = im1.size | ||||
|  | ||||
|         # Get the size of the section | ||||
|         section_size = self.Settings.get_config(module)['size'] | ||||
|         # Calculate coordinates to center the image | ||||
|         x = int( (section_size[0] - im1_size[0]) /2) | ||||
|  | ||||
|         # If this is the first module, use the y-offset | ||||
|         if im1_cursor == 0: | ||||
|           y = int( (section_size[1]-im1_size[1]) /2) | ||||
|         else: | ||||
|           y = im1_cursor + int( (section_size[1]-im1_size[1]) /2) | ||||
|  | ||||
|         # center the image in the section space | ||||
|         im_black.paste(im1, (x,y), im1) | ||||
|  | ||||
|         # Shift the y-axis cursor at the beginning of next section | ||||
|         im1_cursor += section_size[1] | ||||
|  | ||||
|       # Check if there is an image for the coloured band | ||||
|       if os.path.exists(im2_path): | ||||
|  | ||||
|         # Get actual size of image | ||||
|         im2 = Image.open(im2_path).convert('RGBA') | ||||
|         im2_size = im2.size | ||||
|  | ||||
|         # Get the size of the section | ||||
|         section_size = self.Settings.get_config(module)['size'] | ||||
|  | ||||
|         # Calculate coordinates to center the image | ||||
|         x = int( (section_size[0]-im2_size[0]) /2) | ||||
|  | ||||
|         # If this is the first module, use the y-offset | ||||
|         if im2_cursor == 0: | ||||
|           y = int( (section_size[1]-im2_size[1]) /2) | ||||
|         else: | ||||
|           y = im2_cursor + int( (section_size[1]-im2_size[1]) /2) | ||||
|  | ||||
|         # center the image in the section space | ||||
|         im_colour.paste(im2, (x,y), im2) | ||||
|  | ||||
|         # Shift the y-axis cursor at the beginning of next section | ||||
|         im2_cursor += section_size[1] | ||||
|  | ||||
|     # Show an info section if specified by the settings file | ||||
|     now = arrow.now() | ||||
|     stamp = 'last update: {}'.format(now.format('D MMM @ HH:mm', locale = | ||||
|                                                 self.Settings.language)) | ||||
|     if self.Settings.info_section == True: | ||||
|       write(im_black, (0, im1_cursor), (width, height-im1_cursor), | ||||
|             stamp, font = self.font) | ||||
|  | ||||
|  | ||||
|     # optimize the image by mapping colours to pure black and white | ||||
|     if self.optimize == True: | ||||
|       self._optimize_im(im_black).save(images+'canvas.png', 'PNG') | ||||
|       self._optimize_im(im_colour).save(images+'canvas_colour.png', 'PNG') | ||||
|     else: | ||||
|       im_black.save(images+'canvas.png', 'PNG') | ||||
|       im_colour.save(images+'canvas_colour.png', 'PNG') | ||||
|  | ||||
|   def _optimize_im(self, image, threshold=220): | ||||
|     """Optimize the image for rendering on ePaper displays""" | ||||
|  | ||||
|     buffer = numpy.array(image.convert('RGB')) | ||||
|     red, green = buffer[:, :, 0], buffer[:, :, 1] | ||||
|     # grey->black | ||||
|     buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0,0,0] | ||||
|     image = Image.fromarray(buffer) | ||||
|     return image | ||||
|  | ||||
|   def calibrate(self): | ||||
|     """Calibrate the ePaper display to prevent burn-ins (ghosting) | ||||
|     use this command to manually calibrate the display""" | ||||
|  | ||||
|     self.Display.calibrate() | ||||
|  | ||||
|   def _calibration_check(self): | ||||
|     """Calibration sheduler | ||||
|     uses calibration hours from settings file to check if calibration is due""" | ||||
|     now = arrow.now() | ||||
|     print('hour:', now.hour, 'hours:', self._calibration_hours) | ||||
|     print('state:', self._calibration_state) | ||||
|     if now.hour in self._calibration_hours and self._calibration_state == False: | ||||
|       self.calibrate() | ||||
|       self._calibration_state = True | ||||
|     else: | ||||
|       self._calibration_state = False | ||||
|  | ||||
|  | ||||
|   def _check_for_updates(self): | ||||
|     """Check if a new update is available for inkycal""" | ||||
|  | ||||
|     raise NotImplementedError('Tha developer were too lazy to implement this..') | ||||
|  | ||||
|  | ||||
|   @staticmethod | ||||
|   def _add_module(filepath_module, classname): | ||||
|     """Add a third party module to inkycal | ||||
|     filepath_module = the full path of your module. The file should be in /modules! | ||||
|     classname = the name of your class inside the module | ||||
|     """ | ||||
|  | ||||
|     # Path for modules | ||||
|     _module_path = 'inkycal/modules/' | ||||
|  | ||||
|     # Check if the filepath is a string | ||||
|     if not isinstance(filepath_module, str): | ||||
|       raise ValueError('filepath has to be a string!') | ||||
|  | ||||
|     # Check if the classname is a string | ||||
|     if not isinstance(classname, str): | ||||
|       raise ValueError('classname has to be a string!') | ||||
|  | ||||
|     # TODO: | ||||
|     # Ensure only third-party modules are deleted as built-in modules | ||||
|     # should not be deleted | ||||
|  | ||||
|     # Check if module is inside the modules folder | ||||
|     if not _module_path in filepath_module: | ||||
|       raise Exception('Your module should be in', _module_path) | ||||
|  | ||||
|     # Get the name of the third-party module file without extension (.py) | ||||
|     filename = filepath_module.split('.py')[0].split('/')[-1] | ||||
|  | ||||
|     # Check if filename or classname is in the current module init file | ||||
|     with open('modules/__init__.py', mode ='r') as module_init: | ||||
|       content = module_init.read().splitlines() | ||||
|  | ||||
|     for line in content: | ||||
|       if (filename or clasname) in line: | ||||
|         raise Exception( | ||||
|           'A module with this filename or classname already exists') | ||||
|  | ||||
|     # Check if filename or classname is in the current inkycal init file | ||||
|     with open('__init__.py', mode ='r') as inkycal_init: | ||||
|       content = inkycal_init.read().splitlines() | ||||
|  | ||||
|     for line in content: | ||||
|       if (filename or clasname) in line: | ||||
|         raise Exception( | ||||
|           'A module with this filename or classname already exists') | ||||
|  | ||||
|     # If all checks have passed, add the module in the module init file | ||||
|     with open('modules/__init__.py', mode='a') as module_init: | ||||
|       module_init.write('from .{} import {}'.format(filename, classname)) | ||||
|  | ||||
|     # If all checks have passed, add the module in the inkycal init file | ||||
|     with open('__init__.py', mode ='a') as inkycal_init: | ||||
|       inkycal_init.write('# Added by module adder \n') | ||||
|       inkycal_init.write('import inkycal.modules.{}'.format(filename)) | ||||
|  | ||||
|     print('Your module {} has been added successfully! Hooray!'.format( | ||||
|       classname)) | ||||
|  | ||||
|   @staticmethod | ||||
|   def _remove_module(classname, remove_file = True): | ||||
|     """Removes a third-party module from inkycal | ||||
|     Input the classname of the file you want to remove  | ||||
|     """ | ||||
|  | ||||
|     # Check if filename or classname is in the current module init file | ||||
|     with open('modules/__init__.py', mode ='r') as module_init: | ||||
|       content = module_init.read().splitlines() | ||||
|  | ||||
|     with open('modules/__init__.py', mode ='w') as module_init: | ||||
|       for line in content: | ||||
|         if not classname in line: | ||||
|           module_init.write(line+'\n') | ||||
|         else: | ||||
|           filename = line.split(' ')[1].split('.')[1] | ||||
|  | ||||
|     # Check if filename or classname is in the current inkycal init file | ||||
|     with open('__init__.py', mode ='r') as inkycal_init: | ||||
|       content = inkycal_init.read().splitlines() | ||||
|  | ||||
|     with open('__init__.py', mode ='w') as inkycal_init: | ||||
|       for line in content: | ||||
|         if not filename in line: | ||||
|           inkycal_init.write(line+'\n') | ||||
|  | ||||
|     # remove the file of the third party module if it exists and remove_file | ||||
|     # was set to True (default) | ||||
|     if os.path.exists('modules/{}.py'.format(filename)) and remove_file == True: | ||||
|       os.remove('modules/{}.py'.format(filename)) | ||||
|  | ||||
|     print('The module {} has been removed successfully'.format(classname)) | ||||
|  | ||||
| @@ -1,12 +1,6 @@ | ||||
| from flask_wtf import FlaskForm | ||||
| from wtforms import BooleanField | ||||
|  | ||||
| #from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField | ||||
| #from wtforms.validators import DataRequired | ||||
|  | ||||
|  | ||||
| class LoginForm(FlaskForm): | ||||
|     #username = StringField('api-key', validators=[DataRequired()]) | ||||
|     #modules = SelectField(u'modules', choices = [(_[0], _[1]) for _ in modules]) | ||||
|     remember_me = BooleanField('Show info section') | ||||
|     #submit = SubmitField('Sign In') | ||||
|   remember_me = BooleanField('Show info section') | ||||
|  | ||||
|   | ||||
| @@ -1,445 +0,0 @@ | ||||
| {% extends "base.html" %} | ||||
|  | ||||
| <!-- Main container --> | ||||
| {% block content %} | ||||
|  | ||||
| <!-- Wrap everything in a container--> | ||||
| <div class="container"> | ||||
|  | ||||
| <!-- heading --> | ||||
| <h3>Inkycal-Setup v.2.0.0 BETA</h3> | ||||
|  | ||||
| <!-- project link--> | ||||
| <div class="alert alert-light" role="alert"> | ||||
|   <a href="https://github.com/aceisace/Inky-Calendar">For Inkycal Project of ace innovation laboratory - aceinnolab.com - by aceisace</a><br> | ||||
| </div> | ||||
|  | ||||
| <!-- Inkycal logo --> | ||||
| <img class="img-fluid" src="https://github.com/aceisace/Inky-Calendar/blob/dev_ver2_0/Gallery/logo.png?raw=true" alt="Inkycal Logo"> | ||||
|  | ||||
| <br><br> | ||||
|  | ||||
| <!-- Instructions --> | ||||
| <div class="alert alert-primary" role="alert"> | ||||
|     <h4 class="alert-heading">Instructions</h4> | ||||
|     Insert your personal details and preferences and click on 'Generate'.<br> | ||||
|     Copy the downloaded file to the Raspberry Pi.<br> | ||||
|     The location does not matter, however, you need to know the path to this file.<br> | ||||
|     <hr> | ||||
|     <p class="mb-0">If no value is filled in for any of the row, the default value will be used.</p> | ||||
| </div> | ||||
|  | ||||
| <!-- Main form --> | ||||
| <form class="needs-validation" method="post" novalidate> | ||||
|     {{ form.hidden_tag() }} | ||||
|  | ||||
|     <h4> General settings </h4> | ||||
|  | ||||
|     <!-- group E-Paper settings in a single row--> | ||||
|     <div class="form-row"> | ||||
|  | ||||
|     <!-- model selection start--> | ||||
|     <div class="col"> | ||||
|         <label for="model">Model</label> | ||||
|         <select class="form-control" id="model" name="model"> | ||||
|  | ||||
|             <option value="9_in_7">                     9.7" ePaper                         </option> | ||||
|  | ||||
|             <option value="epd_7_in_5_v3_colour">       7.5" v3 (880x528px) colour          </option> | ||||
|             <option value="epd_7_in_5_v3" selected>     7.5" v3 (880x528px) black-white     </option> | ||||
|  | ||||
|             <option value="epd_7_in_5_v2_colour">       7.5" v2 (800x400px) colour          </option> | ||||
|             <option value="epd_7_in_5_v2">              7.5" v2 (800x400px) black-white     </option> | ||||
|  | ||||
|             <option value="epd_7_in_5_colour">          7.5" v1 (600x384px) colour          </option> | ||||
|             <option value="epd_7_in_5">                 7.5" v1 (600x384px) black-white     </option> | ||||
|  | ||||
|             <option value="epd_5_in_83_colour">         5.83" colour                        </option> | ||||
|             <option value="epd_5_in_83">                5.83" black-white                   </option> | ||||
|  | ||||
|             <option value="epd_4_in_2_colour">          4.2" colour                         </option> | ||||
|             <option value="epd_4_in_2">                 4.2" black-white                    </option> | ||||
|         </select> | ||||
|     </div> | ||||
|  | ||||
|  | ||||
|     <!-- Update interval start--> | ||||
|     <div class="col"> | ||||
|         <label>Update interval</label><br> | ||||
|         <select class="form-control" id="update_interval" name="update_interval"> | ||||
|             <option value=60 checked>           every 60 minutes                        </option> | ||||
|             <option value=30>                   every 30 minutes                        </option> | ||||
|             <option value=20>                   every 20 minutes                        </option> | ||||
|             <option value=15>                   every 15 minutes                        </option> | ||||
|             <option value=10>                   every 10 minutes                        </option> | ||||
|         </select> | ||||
|     </div> | ||||
|  | ||||
|     <!-- Update interval end--> | ||||
|  | ||||
|  | ||||
|     <!-- Orientation start --> | ||||
|     <div class="col"> | ||||
|         <label>Orientation</label><br> | ||||
|  | ||||
|         <select class="form-control" id="orientation" name="orientation"> | ||||
|             <option value=0 checked>            Flex cable left                         </option> | ||||
|             <option value=180>                  Flex cable right                        </option> | ||||
|         </select> | ||||
|  | ||||
|     </div> | ||||
|  | ||||
|     </div><br> <!-- row end --> | ||||
|  | ||||
|     <!-- Calibration start --> | ||||
|     <div class="form-group"> | ||||
|         <label>When should the display be calibrated? (Leave blank if you're unsure)</label> | ||||
|  | ||||
|         <!-- Info about calibration (collapsible info)--> | ||||
|         <details> | ||||
|  | ||||
|             <summary>Info about calibration</summary> | ||||
|             <blockquote class="blockquote"> | ||||
|                 Calibration is a way to retain nice colours on ePaper displays. It works by flushing colours a few times on the entire display. | ||||
|                 Please choose 3 hours in 24-hour format (0-24) to specify at which hours calibration should be executed. | ||||
|                 Please also note that it takes around 10-20 minutes to calibrate, so best to choose hours when you won't be looking at Inkycal. | ||||
|             </blockquote> | ||||
|  | ||||
|         </details> | ||||
|  | ||||
|  | ||||
|         <!-- Calibration hours input fields--> | ||||
|         <div class="form-row"> | ||||
|             <div class="col"> | ||||
|                 <input type="number" class="form-control" name="calibration_hour_1" value=0 min=0 max=24> | ||||
|             </div> | ||||
|  | ||||
|             <div class="col"> | ||||
|                 <input type="number" class="form-control" name="calibration_hour_2" value=12 min=0 max=24> | ||||
|                 </div> | ||||
|  | ||||
|             <div class="col"> | ||||
|                 <input type="number" class="form-control" name="calibration_hour_3" value=18 min=0 max=24> | ||||
|             </div> | ||||
|         </div> | ||||
|         <!-- Calibration hours input end--> | ||||
|     </div> | ||||
|     <!-- Calibration end--> | ||||
|  | ||||
|     <!-- Info section --> | ||||
|     <div class="form-group"> | ||||
|         <div class="form-check"> | ||||
|             <input type="checkbox" class="form-check-input" id="info_section" name="info_section"> | ||||
|             <label class="form-check-label" for="info_section">Show info section? (shows time of last display-update)</label> | ||||
|         </div> | ||||
|     </div> | ||||
|  | ||||
|     <h4> Common module settings </h4> | ||||
|     <div class="form-row"> | ||||
|         <!-- language selection- shared by all modules --> | ||||
|         <div class="col"> | ||||
|             <label for="language">Language</label> | ||||
|             <select class="form-control" id="language" name="language"> | ||||
|  | ||||
|                 <option value="en" selected>    English                     </option> | ||||
|                 <option value="de">             German                      </option> | ||||
|                 <option value="ru">             Russian                     </option> | ||||
|                 <option value="it">             Italian                     </option> | ||||
|                 <option value="es">             Spanish                     </option> | ||||
|                 <option value="fr">             French                      </option> | ||||
|                 <option value="el">             Greek                       </option> | ||||
|                 <option value="sv">             Swedish                     </option> | ||||
|                 <option value="nl">             Dutch                       </option> | ||||
|                 <option value="pl">             Polish                      </option> | ||||
|                 <option value="ua">             Ukrainian                   </option> | ||||
|                 <option value="nb">             Norwegian                   </option> | ||||
|                 <option value="vi">             Vietnamese                  </option> | ||||
|                 <option value="zh-tw">          Chinese-Taiwanese           </option> | ||||
|                 <option value="zh">             Chinese                     </option> | ||||
|                 <option value="ja">             Japanese                    </option> | ||||
|                 <option value="ko">             Korean                      </option> | ||||
|  | ||||
|             </select> | ||||
|         </div> | ||||
|  | ||||
|  | ||||
|         <!--fontsize selection - shared by all modules--> | ||||
|         <div class="col"> | ||||
|             <label for="fontsize">Fontsize</label> | ||||
|             <input type="number" class="form-control" name="fontsize" placeholder=12 value=12 min=0 max=30> | ||||
|         </div> | ||||
|  | ||||
|         <!--padding-top-bottom - shared by all modules--> | ||||
|         <div class="col"> | ||||
|             <label for="padding_y">Padding top/bottom (in pixels) </label> | ||||
|             <input type="number" class="form-control" name="padding_y" placeholder=10 value=10 min=0 max=30> | ||||
|         </div> | ||||
|  | ||||
|         <!--padding-left-right - shared by all modules--> | ||||
|         <div class="col"> | ||||
|             <label for="padding_x">Padding right/left (in pixels) </label> | ||||
|             <input type="number" class="form-control" name="padding_x" placeholder=10 value=10 min=0 max=30> | ||||
|         </div> | ||||
|  | ||||
|     </div><br> | ||||
|  | ||||
|  | ||||
|     <!--Create templates for modules with their respective config for later use--> | ||||
|     {% for module in conf %} | ||||
|     <template id={{ module["name"] }} > | ||||
|         <div class="card"><div class="card-header">{{ module["name_str"] }} config</div> | ||||
|             <div class="card-body"> | ||||
|  | ||||
|                 {% if module['requires'] != {} %} | ||||
|                     <h5 class="card-title">Required config</h5> | ||||
|                 {% endif %} | ||||
|  | ||||
|                 {% for key in module["requires"] %} | ||||
|                     {% if 'options' in module["requires"][key] %} | ||||
|                         <label for={{key}}>{{module["requires"][key]["label"]}} *</label> | ||||
|  | ||||
|                         <select class="form-control" id={{key}} name={{ module["name"] }}_{{key}} required> | ||||
|                         {% for option in module["requires"][key]['options'] %} | ||||
|                             <option value={{option}}> {{option}} </option> | ||||
|                         {% endfor %} | ||||
|                         </select> | ||||
|  | ||||
|                         <div class="invalid-feedback">Sorry, but this field should not be empty</div> | ||||
|                         <div class="valid-feedback"> Looks good! </div> | ||||
|                     {% endif %} | ||||
|  | ||||
|                     {% if not 'options' in module["requires"][key] %} | ||||
|                         <label for={{key}}>{{module["requires"][key]["label"]}} *</label> | ||||
|                         <input type="text" class="form-control" id={{key}} name={{ module["name"] }}_{{key}} required> | ||||
|                         <div class="invalid-feedback">Sorry, but this field should not be empty</div> | ||||
|                         <div class="valid-feedback"> Looks good! </div> | ||||
|                     {% endif %} | ||||
|                     <br> | ||||
|                 {% endfor %} | ||||
|  | ||||
|  | ||||
|                 {% if module['optional'] != {} %} | ||||
|                     <h5 class="card-title">Optional config</h5> | ||||
|                 {% endif %} | ||||
|  | ||||
|                 {% for key in module["optional"] %} | ||||
|  | ||||
|                     {% if 'options' in module["optional"][key] %} | ||||
|                         <label for={{key}}>{{module["optional"][key]["label"]}}</label> | ||||
|  | ||||
|                         <select class="form-control" id={{key}} name={{ module["name"] }}_{{key}}> | ||||
|                         {% for option in module["optional"][key]['options'] %} | ||||
|                             <option value={{option}}> {{option}} </option> | ||||
|                         {% endfor %} | ||||
|                         </select> | ||||
|  | ||||
|                         <div class="invalid-feedback">Sorry, but this field should not be empty</div> | ||||
|                         <div class="valid-feedback"> Looks good! </div> | ||||
|                     {% endif %} | ||||
|  | ||||
|                     {% if not 'options' in module["optional"][key] %} | ||||
|                         <label for={{key}}>{{module["optional"][key]["label"]}}</label> | ||||
|                         <input type="text" class="form-control" id={{key}} name={{ module["name"] }}_{{key}}> | ||||
|                     {% endif %} | ||||
|                 {% endfor %} | ||||
|  | ||||
|             </div> | ||||
|         </div> | ||||
|     </template> | ||||
|     {% endfor %} | ||||
|  | ||||
|     <h4> Modules config </h4> | ||||
|  | ||||
|     <div class="alert alert-primary" role="alert">Fields marked with an asterisk(*) are required</div> | ||||
|  | ||||
|     <!-- module 1 selection --> | ||||
|     <div class="form-row"> | ||||
|         <div class="col-md-10"> | ||||
|         <label for="module1">Top section module</label> | ||||
|             <select class="form-control" id="module1" name="module1"> | ||||
|                 <option value="None" checked>Empty</option> | ||||
|                 {% for module in conf%} | ||||
|                     <option value={{ module['name'] }} > {{module['name_str'] }} </option> | ||||
|                 {% endfor %} | ||||
|             </select> | ||||
|         </div> | ||||
|  | ||||
|         <div class="col-md-2"> | ||||
|             <label for="module1_height">Height in percent</label> | ||||
|             <input type="number" class="form-control" name="module1_height" value=10 placeholder=10 min=0 max=100> | ||||
|         </div> | ||||
|  | ||||
|     </div><br> | ||||
|  | ||||
|     <!-- placeholder div --> | ||||
|     <div id="module1_conf"></div> | ||||
|  | ||||
|  | ||||
|     <!-- module 2 selection --> | ||||
|     <div class="form-row"> | ||||
|         <div class="col-md-10"> | ||||
|         <label for="module2">Middle section module</label> | ||||
|             <select class="form-control" id="module2" name="module2"> | ||||
|                 <option value="None" checked>Empty</option> | ||||
|                 {% for module in conf%} | ||||
|                     <option value={{ module['name'] }} > {{module['name_str'] }} </option> | ||||
|                 {% endfor %} | ||||
|             </select> | ||||
|         </div> | ||||
|         <div class="col-md-2"> | ||||
|             <label for="module2_height">Height in percent</label> | ||||
|             <input type="number" class="form-control" name="module2_height" value=65 placeholder=65 min=0 max=100> | ||||
|         </div> | ||||
|     </div><br> | ||||
|  | ||||
|     <!-- placeholder div --> | ||||
|     <div id="module2_conf"></div> | ||||
|  | ||||
|  | ||||
|     <!-- module 3 selection --> | ||||
|     <div class="form-row"> | ||||
|         <div class="col-md-10"> | ||||
|         <label for="module3">Bottom section module</label> | ||||
|             <select class="form-control" id="module3" name="module3"> | ||||
|                 <option value="None" checked>Empty</option> | ||||
|                 {% for module in conf%} | ||||
|                     <option value={{ module['name'] }} > {{module['name_str'] }} </option> | ||||
|                 {% endfor %} | ||||
|             </select> | ||||
|         </div> | ||||
|         <div class="col-md-2"> | ||||
|             <label for="module3_height">Height in percent</label> | ||||
|             <input type="number" class="form-control" name="module3_height" value=25 placeholder=25 min=0 max=100> | ||||
|         </div> | ||||
|     </div><br> | ||||
|  | ||||
|     <!-- placeholder div --> | ||||
|     <div id="module3_conf"></div> | ||||
|  | ||||
|  | ||||
|     <!--Show config of selected modules--> | ||||
|     <script> | ||||
|         $(document).ready(function(){ | ||||
|  | ||||
|             $("#module1").change(function(){ | ||||
|                 $(this).find("option:selected").each(function(){ | ||||
|                     var module1_selection = $(this).attr("value"); | ||||
|                     console.log("Module 1 selected to: "+ module1_selection); | ||||
|                     if(module1_selection != "None"){ | ||||
|  | ||||
|                         // reset module 1 config (avoid showing duplicates) | ||||
|                         $("#module1_conf").replaceWith('<div id="module1_conf"></div>'); | ||||
|  | ||||
|                         // add and render the config for the selected module | ||||
|                         var module1_template = document.querySelector("#"+module1_selection); | ||||
|                         var clone = document.importNode(module1_template.content, true); | ||||
|                         $("#module1_conf").append(clone); | ||||
|  | ||||
|                         // With the selected module name known, we can replace the name tag of that module's config for unique id's | ||||
|                         // This allows having multiple modules running with different configs for each instance | ||||
|                         $("#module1_conf input").each(function(i) { | ||||
|                             //console.log($(this).attr('name', $(this).attr('name').replace(module1_selection, "module1"))); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module1_selection, "module1")); | ||||
|                         }); | ||||
|                         $("#module1_conf select").each(function(i) { | ||||
|                             //console.log($(this).attr('name', $(this).attr('name').replace(module1_selection, "module1"))); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module1_selection, "module1")); | ||||
|                         }); | ||||
|                     } else { | ||||
|                         // revert to empty section | ||||
|                         $("#module1_conf").replaceWith('<div id="module1_conf"></div>'); | ||||
|                     } | ||||
|                 }); | ||||
|             }).change(); | ||||
|  | ||||
|             $("#module2").change(function(){ | ||||
|                 $(this).find("option:selected").each(function(){ | ||||
|                     var module2_selection = $(this).attr("value"); | ||||
|                     console.log("Module 2 selected to: "+ module2_selection); | ||||
|                     if(module2_selection != "None"){ | ||||
|  | ||||
|                         // reset module 2 config (avoid showing duplicates) | ||||
|                         $("#module2_conf").replaceWith('<div id="module2_conf"></div>'); | ||||
|  | ||||
|                         // add and render the config for the selected module | ||||
|                         var module2_template = document.querySelector("#"+module2_selection); | ||||
|                         var clone = document.importNode(module2_template.content, true); | ||||
|                         $("#module2_conf").append(clone); | ||||
|  | ||||
|                         // With the selected module name known, we can replace the name tag of that module's config for unique id's | ||||
|                         // This allows having multiple modules running with different configs for each instance | ||||
|                         $("#module2_conf input").each(function(i) { | ||||
|                             //console.log( $(this).attr('name').replace(module2_selection, "module2")); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module2_selection, "module2")); | ||||
|                         }); | ||||
|                         $("#module2_conf select").each(function(i) { | ||||
|                             //console.log($(this).attr('name', $(this).attr('name').replace(module2_selection, "module2"))); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module2_selection, "module2")); | ||||
|                         }); | ||||
|                     } else { | ||||
|                         // revert to empty section | ||||
|                         $("#module2_conf").replaceWith('<div id="module2_conf"></div>'); | ||||
|                     } | ||||
|                 }); | ||||
|             }).change(); | ||||
|  | ||||
|             $("#module3").change(function(){ | ||||
|                 $(this).find("option:selected").each(function(){ | ||||
|                     var module3_selection = $(this).attr("value"); | ||||
|                     console.log("Module 3 selected to: "+ module3_selection); | ||||
|                     if(module3_selection != "None"){ | ||||
|  | ||||
|                         // reset module 3 config (avoid showing duplicates) | ||||
|                         $("#module3_conf").replaceWith('<div id="module3_conf"></div>'); | ||||
|  | ||||
|                         // add and render the config for the selected module | ||||
|                         var module3_template = document.querySelector("#"+module3_selection); | ||||
|                         var clone = document.importNode(module3_template.content, true); | ||||
|                         $("#module3_conf").append(clone); | ||||
|  | ||||
|                         // With the selected module name known, we can replace the name tag of that module's config for unique id's | ||||
|                         // This allows having multiple modules running with different configs for each instance | ||||
|                         $("#module3_conf input").each(function(i) { | ||||
|                             //console.log( $(this).attr('name').replace(module3_selection, "module3")); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module3_selection, "module3")); | ||||
|                         }); | ||||
|                         $("#module3_conf select").each(function(i) { | ||||
|                             //console.log($(this).attr('name', $(this).attr('name').replace(module3_selection, "module3"))); | ||||
|                             $(this).attr('name', $(this).attr('name').replace(module3_selection, "module3")); | ||||
|                         }); | ||||
|                     } else { | ||||
|                         // revert to empty section | ||||
|                         $("#module3_conf").replaceWith('<div id="module3_conf"></div>'); | ||||
|                     } | ||||
|                 }); | ||||
|             }).change(); | ||||
|         }); | ||||
|     </script> | ||||
|  | ||||
|     <script> | ||||
|     (function() { | ||||
|       'use strict'; | ||||
|       window.addEventListener('load', function() { | ||||
|         var forms = document.getElementsByClassName('needs-validation'); | ||||
|         var validation = Array.prototype.filter.call(forms, function(form) { | ||||
|           form.addEventListener('submit', function(event) { | ||||
|             if (form.checkValidity() === false) { | ||||
|               event.preventDefault(); | ||||
|               event.stopPropagation(); | ||||
|             } | ||||
|             form.classList.add('was-validated'); | ||||
|           }, false); | ||||
|         }); | ||||
|       }, false); | ||||
|     })(); | ||||
|     </script> | ||||
|  | ||||
|     <br> | ||||
|     <div class="form-group"> | ||||
|         <button class="btn btn-primary" type="submit">Generate settings file</button> | ||||
|     </div> | ||||
|  | ||||
| </form> | ||||
|  | ||||
| </div> | ||||
| {% endblock %} | ||||
		Reference in New Issue
	
	Block a user