refactor: do not process .py file manually
Import the driver as a module in `Display.get_display_size()`.
This commit is contained in:
		| @@ -3,12 +3,15 @@ Inkycal ePaper driving functions | |||||||
| Copyright by aceisace | Copyright by aceisace | ||||||
| """ | """ | ||||||
| import os | import os | ||||||
|  | import logging | ||||||
| from importlib import import_module | from importlib import import_module | ||||||
| from PIL import Image | from PIL import Image | ||||||
|  |  | ||||||
| from inkycal.custom import top_level | from inkycal.custom import top_level | ||||||
| import glob | import glob | ||||||
|  |  | ||||||
|  | def import_driver(model): | ||||||
|  |     return import_module(f'inkycal.display.drivers.{model}') | ||||||
|  |  | ||||||
| class Display: | class Display: | ||||||
|     """Display class for inkycal |     """Display class for inkycal | ||||||
| @@ -30,8 +33,7 @@ class Display: | |||||||
|             self.supports_colour = False |             self.supports_colour = False | ||||||
|  |  | ||||||
|         try: |         try: | ||||||
|             driver_path = f'inkycal.display.drivers.{epaper_model}' |             driver = import_driver(epaper_model) | ||||||
|             driver = import_module(driver_path) |  | ||||||
|             self._epaper = driver.EPD() |             self._epaper = driver.EPD() | ||||||
|             self.model_name = epaper_model |             self.model_name = epaper_model | ||||||
|  |  | ||||||
| @@ -168,26 +170,12 @@ class Display: | |||||||
|  |  | ||||||
|         >>> Display.get_display_size('model_name') |         >>> Display.get_display_size('model_name') | ||||||
|         """ |         """ | ||||||
|         if not isinstance(model_name, str): |         try: | ||||||
|             print('model_name should be a string') |             driver = import_driver(model_name) | ||||||
|             return |             return driver.EPD_WIDTH, driver.EPD_HEIGHT | ||||||
|         else: |         except Exception as e: | ||||||
|             driver_files = top_level + '/inkycal/display/drivers/*.py' |             logging.error(f'Failed to load driver for ${model_name}. Check spelling?') | ||||||
|             drivers = glob.glob(driver_files) |             raise e; | ||||||
|             drivers = [i.split('/')[-1].split('.')[0] for i in drivers] |  | ||||||
|             drivers.remove('__init__') |  | ||||||
|             drivers.remove('epdconfig') |  | ||||||
|             if model_name not in drivers: |  | ||||||
|                 print('This model name was not found. Please double check your spellings') |  | ||||||
|                 return |  | ||||||
|             else: |  | ||||||
|                 with open(top_level + '/inkycal/display/drivers/' + model_name + '.py') as file: |  | ||||||
|                     for line in file: |  | ||||||
|                         if 'EPD_WIDTH=' in line.replace(" ", ""): |  | ||||||
|                             width = int(line.rstrip().replace(" ", "").split('=')[-1]) |  | ||||||
|                         if 'EPD_HEIGHT=' in line.replace(" ", ""): |  | ||||||
|                             height = int(line.rstrip().replace(" ", "").split('=')[-1]) |  | ||||||
|                 return width, height |  | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|     def get_display_names(cls) -> list: |     def get_display_names(cls) -> list: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user