logger best practices
This commit is contained in:
		| @@ -11,7 +11,7 @@ from urllib.request import urlopen | |||||||
| import os | import os | ||||||
| import time | import time | ||||||
|  |  | ||||||
| logs = logging.getLogger('inkycal_custom') | logs = logging.getLogger(__name__) | ||||||
| logs.setLevel(level=logging.INFO) | logs.setLevel(level=logging.INFO) | ||||||
|  |  | ||||||
| # Get the path to the Inkycal folder | # Get the path to the Inkycal folder | ||||||
| @@ -26,14 +26,14 @@ image_folder = top_level + '/image_folder/' | |||||||
| fonts = {} | fonts = {} | ||||||
|  |  | ||||||
| for path, dirs, files in os.walk(fonts_location): | for path, dirs, files in os.walk(fonts_location): | ||||||
|     for filename in files: |     for _ in files: | ||||||
|         if filename.endswith('.otf'): |         if _.endswith('.otf'): | ||||||
|             name = filename.split('.otf')[0] |             name = _.split('.otf')[0] | ||||||
|             fonts[name] = os.path.join(path, filename) |             fonts[name] = os.path.join(path, _) | ||||||
|  |  | ||||||
|         if filename.endswith('.ttf'): |         if _.endswith('.ttf'): | ||||||
|             name = filename.split('.ttf')[0] |             name = _.split('.ttf')[0] | ||||||
|             fonts[name] = os.path.join(path, filename) |             fonts[name] = os.path.join(path, _) | ||||||
|  |  | ||||||
| available_fonts = [key for key, values in fonts.items()] | available_fonts = [key for key, values in fonts.items()] | ||||||
|  |  | ||||||
| @@ -206,6 +206,7 @@ def text_wrap(text, font=None, max_width=None): | |||||||
|     Uses a Font object for more accurate calculations. |     Uses a Font object for more accurate calculations. | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
|  |       - text -> Text as a string | ||||||
|       - font: A PIL font object which is used to calculate the size. |       - font: A PIL font object which is used to calculate the size. | ||||||
|       - max_width: int-> a width in pixels defining the maximum width before |       - max_width: int-> a width in pixels defining the maximum width before | ||||||
|         splitting the text into the next chunk. |         splitting the text into the next chunk. | ||||||
|   | |||||||
| @@ -32,8 +32,7 @@ import logging | |||||||
| import sys | import sys | ||||||
| import time | import time | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class RaspberryPi: | class RaspberryPi: | ||||||
|   | |||||||
| @@ -71,8 +71,7 @@ else: | |||||||
| # Show less logging for PIL module | # Show less logging for PIL module | ||||||
| logging.getLogger("PIL").setLevel(logging.WARNING) | logging.getLogger("PIL").setLevel(logging.WARNING) | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| # TODO: autostart -> supervisor? | # TODO: autostart -> supervisor? | ||||||
|   | |||||||
| @@ -38,8 +38,7 @@ except ImportError: | |||||||
| ############################################################################# | ############################################################################# | ||||||
|  |  | ||||||
| # Get the name of this file, set up logging for this filename | # Get the name of this file, set up logging for this filename | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| ############################################################################# | ############################################################################# | ||||||
| @@ -159,7 +158,7 @@ class Simple(inkycal_module): | |||||||
|         # -----------------------------------------------------------------------# |         # -----------------------------------------------------------------------# | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     ############################################################################# |     ############################################################################# | ||||||
|     #                 Validation of module specific parameters   (optional)     # |     #                 Validation of module specific parameters   (optional)     # | ||||||
| @@ -220,7 +219,7 @@ class Simple(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print('running {0} in standalone mode'.format(filename)) |     print('running {0} in standalone mode'.format(__name__)) | ||||||
|  |  | ||||||
| ################################################################################ | ################################################################################ | ||||||
| # Last steps | # Last steps | ||||||
|   | |||||||
| @@ -22,8 +22,7 @@ from icalendar import Calendar | |||||||
|   local timezone. Converting all-day events to local timezone is a problem! |   local timezone. Converting all-day events to local timezone is a problem! | ||||||
| """ | """ | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class iCalendar: | class iCalendar: | ||||||
| @@ -203,4 +202,4 @@ class iCalendar: | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone mode') |     print(f'running {__name__} in standalone mode') | ||||||
|   | |||||||
| @@ -15,8 +15,7 @@ import requests | |||||||
|  |  | ||||||
| from PIL import Image | from PIL import Image | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Inkyimage: | class Inkyimage: | ||||||
| @@ -30,7 +29,7 @@ class Inkyimage: | |||||||
|         self.image = image |         self.image = image | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         logger.info(f'{filename} loaded') |         logger.info(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def load(self, path): |     def load(self, path): | ||||||
|         """loads an image from a URL or filepath. |         """loads an image from a URL or filepath. | ||||||
| @@ -330,4 +329,4 @@ class Inkyimage: | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|   | |||||||
| @@ -11,8 +11,7 @@ from inkycal.custom import * | |||||||
| from inkycal.modules.ical_parser import iCalendar | from inkycal.modules.ical_parser import iCalendar | ||||||
| from inkycal.modules.template import inkycal_module | from inkycal.modules.template import inkycal_module | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Agenda(inkycal_module): | class Agenda(inkycal_module): | ||||||
| @@ -81,7 +80,7 @@ class Agenda(inkycal_module): | |||||||
|         self.timezone = get_system_tz() |         self.timezone = get_system_tz() | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -228,4 +227,4 @@ class Agenda(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone mode') |     print(f'running {__name__} in standalone mode') | ||||||
|   | |||||||
| @@ -10,8 +10,7 @@ from inkycal.custom import * | |||||||
| import calendar as cal | import calendar as cal | ||||||
| import arrow | import arrow | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Calendar(inkycal_module): | class Calendar(inkycal_module): | ||||||
| @@ -86,7 +85,7 @@ class Calendar(inkycal_module): | |||||||
|             fonts['NotoSans-SemiCondensed'], size=self.fontsize) |             fonts['NotoSans-SemiCondensed'], size=self.fontsize) | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -341,4 +340,4 @@ class Calendar(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone mode') |     print(f'running {__name__} in standalone mode') | ||||||
|   | |||||||
| @@ -13,8 +13,7 @@ from random import shuffle | |||||||
|  |  | ||||||
| import feedparser | import feedparser | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Feeds(inkycal_module): | class Feeds(inkycal_module): | ||||||
| @@ -63,7 +62,7 @@ class Feeds(inkycal_module): | |||||||
|         self.shuffle_feeds = config["shuffle_feeds"] |         self.shuffle_feeds = config["shuffle_feeds"] | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def _validate(self): |     def _validate(self): | ||||||
|         """Validate module-specific parameters""" |         """Validate module-specific parameters""" | ||||||
| @@ -155,4 +154,4 @@ class Feeds(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|   | |||||||
| @@ -10,8 +10,7 @@ from inkycal.custom import * | |||||||
|  |  | ||||||
| from inkycal.modules.inky_image import Inkyimage as Images | from inkycal.modules.inky_image import Inkyimage as Images | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Inkyimage(inkycal_module): | class Inkyimage(inkycal_module): | ||||||
| @@ -66,7 +65,7 @@ class Inkyimage(inkycal_module): | |||||||
|         self.orientation = config['orientation'] |         self.orientation = config['orientation'] | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -105,4 +104,4 @@ class Inkyimage(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|   | |||||||
| @@ -14,8 +14,7 @@ import requests | |||||||
| # Show less logging for request module | # Show less logging for request module | ||||||
| logging.getLogger("urllib3").setLevel(logging.WARNING) | logging.getLogger("urllib3").setLevel(logging.WARNING) | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Jokes(inkycal_module): | class Jokes(inkycal_module): | ||||||
| @@ -33,7 +32,7 @@ class Jokes(inkycal_module): | |||||||
|         config = config['config'] |         config = config['config'] | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -101,4 +100,4 @@ class Jokes(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|   | |||||||
| @@ -13,8 +13,7 @@ from inkycal.custom import * | |||||||
|  |  | ||||||
| from inkycal.modules.inky_image import Inkyimage as Images | from inkycal.modules.inky_image import Inkyimage as Images | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Inkyserver(inkycal_module): | class Inkyserver(inkycal_module): | ||||||
| @@ -73,7 +72,7 @@ class Inkyserver(inkycal_module): | |||||||
|             self.path_body = config['path_body'] |             self.path_body = config['path_body'] | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -122,7 +121,7 @@ class Inkyserver(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|  |  | ||||||
| ##  'https://inkycal.robertsirre.nl/panel/calendar/{model}?width={width}&height={height}' | ##  'https://inkycal.robertsirre.nl/panel/calendar/{model}?width={width}&height={height}' | ||||||
| ##path = path.replace('{model}', model).replace('{width}',str(display_width)).replace('{height}',str(display_height)) | ##path = path.replace('{model}', model).replace('{width}',str(display_width)).replace('{height}',str(display_height)) | ||||||
|   | |||||||
| @@ -12,8 +12,7 @@ from inkycal.custom import * | |||||||
| # PIL has a class named Image, use alias for Inkyimage -> Images | # PIL has a class named Image, use alias for Inkyimage -> Images | ||||||
| from inkycal.modules.inky_image import Inkyimage as Images | from inkycal.modules.inky_image import Inkyimage as Images | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Slideshow(inkycal_module): | class Slideshow(inkycal_module): | ||||||
| @@ -80,7 +79,7 @@ class Slideshow(inkycal_module): | |||||||
|         self._first_run = True |         self._first_run = True | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f'{filename} loaded') |         print(f'{__name__} loaded') | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -132,4 +131,4 @@ class Slideshow(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone/debug mode') |     print(f'running {__name__} in standalone/debug mode') | ||||||
|   | |||||||
| @@ -8,13 +8,13 @@ Copyright by aceisace | |||||||
| from inkycal.modules.template import inkycal_module | from inkycal.modules.template import inkycal_module | ||||||
| from inkycal.custom import * | from inkycal.custom import * | ||||||
|  |  | ||||||
| import math, decimal | import math | ||||||
|  | import decimal | ||||||
| import arrow | import arrow | ||||||
|  |  | ||||||
| from pyowm.owm import OWM | from pyowm.owm import OWM | ||||||
|  |  | ||||||
| filename = os.path.basename(__file__).split('.py')[0] | logger = logging.getLogger(__name__) | ||||||
| logger = logging.getLogger(filename) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class Weather(inkycal_module): | class Weather(inkycal_module): | ||||||
| @@ -102,7 +102,7 @@ class Weather(inkycal_module): | |||||||
|             fonts['weathericons-regular-webfont'], size=self.fontsize) |             fonts['weathericons-regular-webfont'], size=self.fontsize) | ||||||
|  |  | ||||||
|         # give an OK message |         # give an OK message | ||||||
|         print(f"{filename} loaded") |         print(f"{__name__} loaded") | ||||||
|  |  | ||||||
|     def generate_image(self): |     def generate_image(self): | ||||||
|         """Generate image for this module""" |         """Generate image for this module""" | ||||||
| @@ -505,4 +505,4 @@ class Weather(inkycal_module): | |||||||
|  |  | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     print(f'running {filename} in standalone mode') |     print(f'running {__name__} in standalone mode') | ||||||
|   | |||||||
							
								
								
									
										0
									
								
								inkycal/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								inkycal/tests/__init__.py
									
									
									
									
									
										Normal file
									
								
							| @@ -80,7 +80,7 @@ class module_test(unittest.TestCase): | |||||||
|             module = Module(test) |             module = Module(test) | ||||||
|             im_black, im_colour = module.generate_image() |             im_black, im_colour = module.generate_image() | ||||||
|             print('OK') |             print('OK') | ||||||
|             if use_preview == True and environment == 'Raspberry': |             if use_preview and environment == 'Raspberry': | ||||||
|                 preview(merge(im_black, im_colour)) |                 preview(merge(im_black, im_colour)) | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -181,7 +181,7 @@ class module_test(unittest.TestCase): | |||||||
|                 module = Module(test) |                 module = Module(test) | ||||||
|                 im_black, im_colour = module.generate_image() |                 im_black, im_colour = module.generate_image() | ||||||
|                 print('OK') |                 print('OK') | ||||||
|                 if use_preview == True and environment == 'Raspberry': |                 if use_preview and environment == 'Raspberry': | ||||||
|                     preview(merge(im_black, im_colour)) |                     preview(merge(im_black, im_colour)) | ||||||
|         else: |         else: | ||||||
|             print('No key given, omitted testing') |             print('No key given, omitted testing') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user