Improved formatting
This commit is contained in:
		| @@ -1,5 +1,5 @@ | ||||
| #!/usr/bin/python3 | ||||
| # -*- coding: utf-8 -*- | ||||
| #!python3 | ||||
|  | ||||
| """ | ||||
| Third party module template (inkycal-compatible module) | ||||
|  | ||||
| @@ -13,7 +13,6 @@ Copyright by aceisace | ||||
| from inkycal.modules.template import inkycal_module | ||||
| from inkycal.custom import * | ||||
|  | ||||
|  | ||||
| ############################################################################# | ||||
| #                           Built-in library imports (change as desired) | ||||
| ############################################################################# | ||||
| @@ -21,7 +20,6 @@ from inkycal.custom import * | ||||
| # Built-in libraries go here | ||||
| from random import shuffle | ||||
|  | ||||
|  | ||||
| ############################################################################# | ||||
| #                         External library imports (always use try-except) | ||||
| ############################################################################# | ||||
| @@ -30,11 +28,10 @@ from random import shuffle | ||||
| # use try...except ImportError to check if it has been installed | ||||
| # If it is not found, print a short message on how to install this dependency | ||||
| try: | ||||
|   import feedparser | ||||
|     import feedparser | ||||
| except ImportError: | ||||
|   print('feedparser is not installed! Please install with:') | ||||
|   print('pip3 install feedparser') | ||||
|  | ||||
|     print('feedparser is not installed! Please install with:') | ||||
|     print('pip3 install feedparser') | ||||
|  | ||||
| ############################################################################# | ||||
| #                         Filename + logging (do not remove) | ||||
| @@ -44,6 +41,7 @@ except ImportError: | ||||
| filename = os.path.basename(__file__).split('.py')[0] | ||||
| logger = logging.getLogger(filename) | ||||
|  | ||||
|  | ||||
| ############################################################################# | ||||
| #                         Class setup | ||||
| ############################################################################# | ||||
| @@ -52,181 +50,177 @@ logger = logging.getLogger(filename) | ||||
| # Avoid naming the class with too long names | ||||
|  | ||||
| class Simple(inkycal_module): | ||||
|   """ Simple Class | ||||
|   Once sentence describing what this module does, | ||||
|   e.g. Display hello world with your name! | ||||
|   """ | ||||
|     """ Simple Class | ||||
|     Once sentence describing what this module does, | ||||
|     e.g. Display hello world with your name! | ||||
|     """ | ||||
|  | ||||
|   # name is the name that will be shown on the web-ui | ||||
|   # may be same or different to the class name (Do not remove this) | ||||
|   name = "Simple - say hello world" | ||||
|     # name is the name that will be shown on the web-ui | ||||
|     # may be same or different to the class name (Do not remove this) | ||||
|     name = "Simple - say hello world" | ||||
|  | ||||
|   # create a dictionary containing variables which your module must have | ||||
|   # to run correctly, e.g. if your module needs an 'api-key' and a 'name': | ||||
|   requires = { | ||||
|     # A simple text input; users can choose what to enter by keyboard | ||||
|     'api_key': {"label" : "Please enter your api-key from some-website"}, | ||||
|     # create a dictionary containing variables which your module must have | ||||
|     # to run correctly, e.g. if your module needs an 'api-key' and a 'name': | ||||
|     requires = { | ||||
|         # A simple text input; users can choose what to enter by keyboard | ||||
|         'api_key': {"label": "Please enter your api-key from some-website"}, | ||||
|  | ||||
|     # A simple text input; users can choose what to enter by keyboard | ||||
|     'username': {"label": "Please enter a username"}, | ||||
|   } | ||||
|   # The format for the above is: |"key_name": {"Desription what this means"},| | ||||
|         # A simple text input; users can choose what to enter by keyboard | ||||
|         'username': {"label": "Please enter a username"}, | ||||
|     } | ||||
|     # The format for the above is: |"key_name": {"Desription what this means"},| | ||||
|  | ||||
|   # create a dictionary containing variables which your module optionally | ||||
|   # can have to run correctly, e.g. if your module needs has optional | ||||
|   # parameters like: 'api-key' and a 'name': | ||||
|     # create a dictionary containing variables which your module optionally | ||||
|     # can have to run correctly, e.g. if your module needs has optional | ||||
|     # parameters like: 'api-key' and a 'name': | ||||
|  | ||||
|   ######################################################################### | ||||
|   optional = { | ||||
|     ######################################################################### | ||||
|     optional = { | ||||
|  | ||||
|     # A simple text input with multiple values separated by a comma | ||||
|     'hobbies': {"label": "What is/are your hobbies? Separate multiple ones " | ||||
|                 "with a comma"}, | ||||
|         # A simple text input with multiple values separated by a comma | ||||
|         'hobbies': {"label": "What is/are your hobbies? Separate multiple ones " | ||||
|                              "with a comma"}, | ||||
|  | ||||
|     # A simple text input which should be a number | ||||
|     'age': {"label": "What is your age? Please enter a number"}, | ||||
|      | ||||
|     # A dropdown list variable. This will allow users to select something | ||||
|     # from the list in options. Instead of True/False, you can have | ||||
|     # strings, numbers and other datatypes. Add as many options as you need | ||||
|     'likes_inkycal': { | ||||
|                         "label": "Do you like Inkycal?", | ||||
|                         "options": [True, False], | ||||
|                      }, | ||||
|         # A simple text input which should be a number | ||||
|         'age': {"label": "What is your age? Please enter a number"}, | ||||
|  | ||||
|     # A dropdown list with a fallback value in case the user didn't select | ||||
|     # anything | ||||
|     'show_smiley': { | ||||
|                         "label": "Show a smiley next to your name?", | ||||
|                         "options": [True, False], | ||||
|                         "default": True, | ||||
|                      }, | ||||
|   } | ||||
|   ######################################################################## | ||||
|         # A dropdown list variable. This will allow users to select something | ||||
|         # from the list in options. Instead of True/False, you can have | ||||
|         # strings, numbers and other datatypes. Add as many options as you need | ||||
|         'likes_inkycal': { | ||||
|             "label": "Do you like Inkycal?", | ||||
|             "options": [True, False], | ||||
|         }, | ||||
|  | ||||
|   # Initialise the class (do not remove) | ||||
|   def __init__(self, config): | ||||
|     """Initialize your module module""" | ||||
|         # A dropdown list with a fallback value in case the user didn't select | ||||
|         # anything | ||||
|         'show_smiley': { | ||||
|             "label": "Show a smiley next to your name?", | ||||
|             "options": [True, False], | ||||
|             "default": True, | ||||
|         }, | ||||
|     } | ||||
|  | ||||
|     # Initialise this module via the inkycal_module template (required) | ||||
|     super().__init__(config) | ||||
|     ######################################################################## | ||||
|  | ||||
|     config = config['config'] | ||||
|     # Initialise the class (do not remove) | ||||
|     def __init__(self, config): | ||||
|         """Initialize your module module""" | ||||
|  | ||||
|     # Check if all required parameters are present | ||||
|     # remove this if your module has no required parameters | ||||
|     for param in self.requires: | ||||
|       if not param in config: | ||||
|         raise Exception('config is missing {}'.format(param)) | ||||
|         # Initialise this module via the inkycal_module template (required) | ||||
|         super().__init__(config) | ||||
|  | ||||
|     # the web-UI removes any blank space from the input | ||||
|     # It can only output strings or booleans, integers and lists need to be | ||||
|     # converted manually, e.g. | ||||
|         config = config['config'] | ||||
|  | ||||
|     # if you need a boolean (True/False), no conversion is needed: | ||||
|     self.show_smiley = config['show_smiley'] | ||||
|         # Check if all required parameters are present | ||||
|         # remove this if your module has no required parameters | ||||
|         for param in self.requires: | ||||
|             if not param in config: | ||||
|                 raise Exception('config is missing {}'.format(param)) | ||||
|  | ||||
|     # if you need a single word input, like the api-ley, no conversion is needed | ||||
|     self.api_key = config['api_key'] | ||||
|         # the web-UI removes any blank space from the input | ||||
|         # It can only output strings or booleans, integers and lists need to be | ||||
|         # converted manually, e.g. | ||||
|  | ||||
|     # if you need a integer (number) input, you have to convert this to a int | ||||
|     #-----------------------------------------------------------------------# | ||||
|     # bad example :/ | ||||
|     self.age = int( config["age"] ) | ||||
|     # Remember age was a optional parameter? What if no age was entered | ||||
|     # and there is no fallback value? Then the age would be None. | ||||
|     # This would cause crashing right here | ||||
|      | ||||
|     # good example :) | ||||
|     if config["age"] and isinstance(config["age"], str): | ||||
|       self.age = int( config["age"] ) | ||||
|     else: | ||||
|       self.age = 10 # just a joke, no offense | ||||
|     # -> Check if age was entered and if it's a string (entered via web-UI) | ||||
|     # If something was entered for age, convert it to a number | ||||
|     # The else statement is executed when nothing was entered for age | ||||
|     # You could assign a custom value now or print something. | ||||
|     #-----------------------------------------------------------------------# | ||||
|         # if you need a boolean (True/False), no conversion is needed: | ||||
|         self.show_smiley = config['show_smiley'] | ||||
|  | ||||
|     # if you need a list of words, you have to convert the string to a list | ||||
|     #-----------------------------------------------------------------------# | ||||
|     # good example :) | ||||
|     if config["hobbies"] and isinstance(config["hobbies"], str): | ||||
|       self.hobbies = config["age"].split(",") | ||||
|       # split splits the string on each comma -> gives a list | ||||
|       # even if a single value was entered, it will be converted to a list | ||||
|     else: | ||||
|       self.hobbies = [] # empty list if nothing was entered by user | ||||
|     #-----------------------------------------------------------------------# | ||||
|         # if you need a single word input, like the api-ley, no conversion is needed | ||||
|         self.api_key = config['api_key'] | ||||
|  | ||||
|     # give an OK message | ||||
|     print(f'{filename} loaded') | ||||
|         # if you need a integer (number) input, you have to convert this to a int | ||||
|         # -----------------------------------------------------------------------# | ||||
|         # bad example :/ | ||||
|         self.age = int(config["age"]) | ||||
|         # Remember age was a optional parameter? What if no age was entered | ||||
|         # and there is no fallback value? Then the age would be None. | ||||
|         # This would cause crashing right here | ||||
|  | ||||
| ############################################################################# | ||||
| #                 Validation of module specific parameters   (optional)     # | ||||
| ############################################################################# | ||||
|         # good example :) | ||||
|         if config["age"] and isinstance(config["age"], str): | ||||
|             self.age = int(config["age"]) | ||||
|         else: | ||||
|             self.age = 10  # just a joke, no offense | ||||
|         # -> Check if age was entered and if it's a string (entered via web-UI) | ||||
|         # If something was entered for age, convert it to a number | ||||
|         # The else statement is executed when nothing was entered for age | ||||
|         # You could assign a custom value now or print something. | ||||
|         # -----------------------------------------------------------------------# | ||||
|  | ||||
|   def _validate(self): | ||||
|     """Validate module-specific parameters""" | ||||
|     # Check the type of module-specific parameters | ||||
|     # This function is optional, but useful for debugging. | ||||
|         # if you need a list of words, you have to convert the string to a list | ||||
|         # -----------------------------------------------------------------------# | ||||
|         # good example :) | ||||
|         if config["hobbies"] and isinstance(config["hobbies"], str): | ||||
|             self.hobbies = config["age"].split(",") | ||||
|             # split splits the string on each comma -> gives a list | ||||
|             # even if a single value was entered, it will be converted to a list | ||||
|         else: | ||||
|             self.hobbies = []  # empty list if nothing was entered by user | ||||
|         # -----------------------------------------------------------------------# | ||||
|  | ||||
|     # Here, we are checking if do_something (from init) is True/False | ||||
|     if not isinstance(self.age, int): | ||||
|       print(f"age has to be a number, but given value is {self.age}") | ||||
|         # give an OK message | ||||
|         print(f'{filename} loaded') | ||||
|  | ||||
|     ############################################################################# | ||||
|     #                 Validation of module specific parameters   (optional)     # | ||||
|     ############################################################################# | ||||
|  | ||||
| ############################################################################# | ||||
| #                       Generating the image                                # | ||||
| ############################################################################# | ||||
|     def _validate(self): | ||||
|         """Validate module-specific parameters""" | ||||
|         # Check the type of module-specific parameters | ||||
|         # This function is optional, but useful for debugging. | ||||
|  | ||||
|   def generate_image(self): | ||||
|     """Generate image for this module""" | ||||
|         # Here, we are checking if do_something (from init) is True/False | ||||
|         if not isinstance(self.age, int): | ||||
|             print(f"age has to be a number, but given value is {self.age}") | ||||
|  | ||||
|     # Define new image size with respect to padding | ||||
|     im_width = int(self.width - (2 * self.padding_left)) | ||||
|     im_height = int(self.height - (2 * self.padding_top)) | ||||
|     im_size = im_width, im_height | ||||
|     logger.info('image size: {} x {} px'.format(im_width, im_height)) | ||||
|     ############################################################################# | ||||
|     #                       Generating the image                                # | ||||
|     ############################################################################# | ||||
|  | ||||
|     # Use logger.info(), logger.debug(), logger.warning() to display | ||||
|     # useful information for the developer | ||||
|     logger.info('image size: {} x {} px'.format(im_width, im_height)) | ||||
|     def generate_image(self): | ||||
|         """Generate image for this module""" | ||||
|  | ||||
|     # Create an image for black pixels and one for coloured pixels (required) | ||||
|     im_black = Image.new('RGB', size = im_size, color = 'white') | ||||
|     im_colour = Image.new('RGB', size = im_size, color = 'white') | ||||
|         # Define new image size with respect to padding | ||||
|         im_width = int(self.width - (2 * self.padding_left)) | ||||
|         im_height = int(self.height - (2 * self.padding_top)) | ||||
|         im_size = im_width, im_height | ||||
|         logger.info('image size: {} x {} px'.format(im_width, im_height)) | ||||
|  | ||||
|     ################################################################# | ||||
|         # Use logger.info(), logger.debug(), logger.warning() to display | ||||
|         # useful information for the developer | ||||
|         logger.info('image size: {} x {} px'.format(im_width, im_height)) | ||||
|  | ||||
|     #                    Your code goes here                        # | ||||
|      | ||||
|     # Write/Draw something on the image | ||||
|         # Create an image for black pixels and one for coloured pixels (required) | ||||
|         im_black = Image.new('RGB', size=im_size, color='white') | ||||
|         im_colour = Image.new('RGB', size=im_size, color='white') | ||||
|  | ||||
|     #   You can use these custom functions to help you create the image: | ||||
|     # - write()               -> write text on the image | ||||
|     # - get_fonts()           -> see which fonts are available | ||||
|     # - get_system_tz()       -> Get the system's current timezone | ||||
|     # - auto_fontsize()       -> Scale the fontsize to the provided height | ||||
|     # - textwrap()            -> Split a paragraph into smaller lines | ||||
|     # - internet_available()  -> Check if internet is available | ||||
|     # - draw_border()         -> Draw a border around the specified area | ||||
|         ################################################################# | ||||
|  | ||||
|     # If these aren't enough, take a look at python Pillow (imaging library)'s | ||||
|     # documentation. | ||||
|         #                    Your code goes here                        # | ||||
|  | ||||
|      | ||||
|     ################################################################# | ||||
|         # Write/Draw something on the image | ||||
|  | ||||
|     # return the images ready for the display | ||||
|     return im_black, im_colour | ||||
|         #   You can use these custom functions to help you create the image: | ||||
|         # - write()               -> write text on the image | ||||
|         # - get_fonts()           -> see which fonts are available | ||||
|         # - get_system_tz()       -> Get the system's current timezone | ||||
|         # - auto_fontsize()       -> Scale the fontsize to the provided height | ||||
|         # - textwrap()            -> Split a paragraph into smaller lines | ||||
|         # - internet_available()  -> Check if internet is available | ||||
|         # - draw_border()         -> Draw a border around the specified area | ||||
|  | ||||
|         # If these aren't enough, take a look at python Pillow (imaging library)'s | ||||
|         # documentation. | ||||
|  | ||||
|         ################################################################# | ||||
|  | ||||
|         # return the images ready for the display | ||||
|         return im_black, im_colour | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|   print('running {0} in standalone mode'.format(filename)) | ||||
|  | ||||
|  | ||||
|  | ||||
|     print('running {0} in standalone mode'.format(filename)) | ||||
|  | ||||
| ################################################################################ | ||||
| # Last steps | ||||
| @@ -248,4 +242,3 @@ if __name__ == '__main__': | ||||
| # How do I now import my module? | ||||
| # from inkycal.modules import Class | ||||
| # Where Class is the name of the class inside your module (e.g. Simple) | ||||
|    | ||||
|   | ||||
		Reference in New Issue
	
	Block a user