Implementation of interface (template) for all modules
- Correct setup of logging - all inkycal-modules inherit from the given template - Added basic, optional validation - more code cleanups - fixed a few minor bugs
This commit is contained in:
@@ -4,46 +4,43 @@
|
||||
Weather module for Inky-Calendar software.
|
||||
Copyright by aceisace
|
||||
"""
|
||||
|
||||
from inkycal.modules.template import inkycal_module
|
||||
from inkycal.custom import *
|
||||
|
||||
import math, decimal
|
||||
import arrow
|
||||
from locale import getdefaultlocale as sys_locale
|
||||
|
||||
try:
|
||||
import pyowm
|
||||
except ImportError:
|
||||
print('pyowm is not installed! Please install with:')
|
||||
print('pip3 install pyowm')
|
||||
|
||||
filename = os.path.basename(__file__).split('.py')[0]
|
||||
logger = logging.getLogger(filename)
|
||||
logger.setLevel(level=logging.INFO)
|
||||
|
||||
# Debug Data (not for production use!)
|
||||
config = {'api_key': 'secret', 'location': 'Stuttgart, DE'}
|
||||
size = (384,80)
|
||||
|
||||
class weather:
|
||||
class weather(inkycal_module):
|
||||
"""weather class
|
||||
parses weather details from openweathermap
|
||||
"""
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
def __init__(self, section_size, section_config):
|
||||
"""Initialize inkycal_weather module"""
|
||||
self.name = os.path.basename(__file__).split('.py')[0]
|
||||
self.config = section_config
|
||||
self.width, self.height = section_size
|
||||
self.background_colour = 'white'
|
||||
self.font_colour = 'black'
|
||||
self.fontsize = 12
|
||||
self.font = ImageFont.truetype(fonts['NotoSans-SemiCondensed'],
|
||||
size = self.fontsize)
|
||||
self.padding_x = 0.02
|
||||
self.padding_y = 0.05
|
||||
|
||||
super().__init__(section_size, section_config)
|
||||
|
||||
# Weather-specfic options
|
||||
self.owm = pyowm.OWM(config['api_key'])
|
||||
# Module specific parameters
|
||||
required = ['api_key','location']
|
||||
for param in required:
|
||||
if not param in section_config:
|
||||
raise Exception('config is missing {}'.format(param))
|
||||
|
||||
# module name
|
||||
self.name = filename
|
||||
|
||||
# module specific parameters
|
||||
self.owm = pyowm.OWM(self.config['api_key'])
|
||||
self.units = 'metric' # metric # imperial
|
||||
self.hour_format = '24' # 12 #24
|
||||
self.timezone = get_system_tz()
|
||||
@@ -54,52 +51,25 @@ class weather:
|
||||
self.locale = sys_locale()[0]
|
||||
self.weatherfont = ImageFont.truetype(fonts['weathericons-regular-webfont'],
|
||||
size = self.fontsize)
|
||||
|
||||
# give an OK message
|
||||
print('{0} loaded'.format(self.name))
|
||||
|
||||
def set(self, **kwargs):
|
||||
"""Manually set some parameters of this module"""
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if key in self.__dict__:
|
||||
setattr(self, key, value)
|
||||
else:
|
||||
print('{0} does not exist'.format(key))
|
||||
pass
|
||||
|
||||
def get(self, **kwargs):
|
||||
"""Manually get some parameters of this module"""
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if key in self.__dict__:
|
||||
getattr(self, key, value)
|
||||
else:
|
||||
print('{0} does not exist'.format(key))
|
||||
pass
|
||||
|
||||
|
||||
def get_options(self):
|
||||
"""Get all options which can be changed"""
|
||||
|
||||
return self.__dict__
|
||||
|
||||
|
||||
def generate_image(self):
|
||||
"""Generate image for this module"""
|
||||
|
||||
# Define new image size with respect to padding
|
||||
im_width = int(self.width - (self.width * 2 * self.padding_x))
|
||||
im_height = int(self.height - (self.height * 2 * self.padding_y))
|
||||
im_width = int(self.width - (self.width * 2 * self.margin_x))
|
||||
im_height = int(self.height - (self.height * 2 * self.margin_y))
|
||||
im_size = im_width, im_height
|
||||
logging.info('image size: {} x {} px'.format(im_width, im_height))
|
||||
logger.info('image size: {} x {} px'.format(im_width, im_height))
|
||||
|
||||
# Create an image for black pixels and one for coloured pixels
|
||||
im_black = Image.new('RGB', size = im_size, color = self.background_colour)
|
||||
im_black = Image.new('RGB', size = im_size, color = 'white')
|
||||
im_colour = Image.new('RGB', size = im_size, color = 'white')
|
||||
|
||||
# Check if internet is available
|
||||
if internet_available() == True:
|
||||
logging.info('Connection test passed')
|
||||
logger.info('Connection test passed')
|
||||
else:
|
||||
raise Exception('Network could not be reached :(')
|
||||
|
||||
@@ -353,7 +323,7 @@ class weather:
|
||||
}
|
||||
|
||||
for key,val in fc_data.items():
|
||||
logging.info((key,val))
|
||||
logger.info((key,val))
|
||||
|
||||
# Get some current weather details
|
||||
temperature = '{}°'.format(weather.get_temperature(unit=temp_unit)['temp'])
|
||||
@@ -450,16 +420,16 @@ class weather:
|
||||
draw_border(im_black, (col6, row1), (col_width, im_height))
|
||||
draw_border(im_black, (col7, row1), (col_width, im_height))
|
||||
|
||||
##############################################################################
|
||||
# Error Handling
|
||||
##############################################################################
|
||||
|
||||
# Save image of black and colour channel in image-folder
|
||||
# Save image of black and colour channel in image-folder
|
||||
im_black.save(images+self.name+'.png', "PNG")
|
||||
im_colour.save(images+self.name+'_colour.png', "PNG")
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('running {0} in standalone mode'.format(
|
||||
os.path.basename(__file__).split('.py')[0]))
|
||||
a = weather(size, config)
|
||||
a.generate_image()
|
||||
print('running {0} in standalone mode'.format(filename))
|
||||
|
||||
|
||||
##config = {'api_key': 'secret', 'location': 'Stuttgart, DE'}
|
||||
##size = (384,80)
|
||||
##a = weather(size, config)
|
||||
##a.generate_image()
|
||||
# Debug Data (not for production use!)
|
||||
|
Reference in New Issue
Block a user