Minor code improvements

This commit is contained in:
aceisace 2022-04-10 06:35:08 +02:00
parent f452135b42
commit 5cd04bda04
16 changed files with 84 additions and 95 deletions

View File

@ -1 +1,2 @@
from .functions import *
from .inkycal_exceptions import *

View File

@ -243,7 +243,7 @@ def internet_available():
Returned output can be used to add a check for internet availability:
>>> if internet_available() == True:
>>> if internet_available():
>>> #...do something that requires internet connectivity
"""

View File

@ -0,0 +1,16 @@
#!python3
"""
Inkycal custom Exceptions
"""
class SettingsFileNotFoundError(Exception):
def __init__(self, message="Inkycal could not find a settings.json file. Please check the settings-file path"):
self.message = message
super().__init__(self.message)
class NetworkNotReachableError(Exception):
def __init__(self, message="Inkycal could not establish a connection to the web"):
self.message = message
super().__init__(self.message)

View File

@ -109,9 +109,7 @@ class Inkycal:
self.settings = settings
except FileNotFoundError:
print('No settings file found in given path\n'
'Please double check your settings_path')
return
raise SettingsFileNotFoundError
else:
try:
@ -120,8 +118,7 @@ class Inkycal:
self.settings = settings
except FileNotFoundError:
print('No settings file found in /boot')
return
raise SettingsFileNotFoundError
# Option to use epaper image optimisation, reduces colours
self.optimize = True

View File

@ -115,7 +115,7 @@ class Simple(inkycal_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:
if param not in config:
raise Exception('config is missing {}'.format(param))
# the web-UI removes any blank space from the input

View File

@ -4,6 +4,15 @@
iCalendar (parsing) module for Inky-Calendar Project
Copyright by aceisace
"""
import urllib
import arrow
from urllib.request import urlopen
import logging
import time
import os
import recurring_ical_events
from icalendar import Calendar
""" ---info about iCalendars---
all day events start at midnight, ending at midnight of the next day
@ -13,24 +22,6 @@ Copyright by aceisace
local timezone. Converting all-day events to local timezone is a problem!
"""
import arrow
from urllib.request import urlopen
import logging
import time
import os
try:
import recurring_ical_events
except ModuleNotFoundError:
print('recurring-ical-events library could not be found.')
print('Please install this with: pip3 install recurring-ical-events')
try:
from icalendar import Calendar, Event
except ModuleNotFoundError:
print('icalendar library could not be found. Please install this with:')
print('pip3 install icalendar')
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
@ -49,20 +40,6 @@ class iCalendar:
add username and password to access protected files
"""
if type(url) == list:
if (username == None) and (password == None):
ical = [Calendar.from_ical(str(urlopen(_).read().decode()))
for _ in url]
else:
ical = [auth_ical(each_url, username, password) for each_url in url]
elif type(url) == str:
if (username == None) and (password == None):
ical = [Calendar.from_ical(str(urlopen(url).read().decode()))]
else:
ical = [auth_ical(url, username, password)]
else:
raise Exception(f"Input: '{url}' is not a string or list!")
def auth_ical(url, uname, passwd):
"""Authorisation helper for protected ical files"""
@ -74,6 +51,20 @@ class iCalendar:
ical = Calendar.from_ical(str(opener.open(url).read().decode()))
return ical
if type(url) == list:
if (username is None) and (password is None):
ical = [Calendar.from_ical(str(urlopen(_).read().decode()))
for _ in url]
else:
ical = [auth_ical(each_url, username, password) for each_url in url]
elif type(url) == str:
if (username is None) and (password is None):
ical = [Calendar.from_ical(str(urlopen(url).read().decode()))]
else:
ical = [auth_ical(url, username, password)]
else:
raise Exception(f"Input: '{url}' is not a string or list!")
# Add the parsed icalendar/s to the self.icalendars list
if ical: self.icalendars += ical
logger.info('loaded iCalendars from URLs')
@ -106,7 +97,7 @@ class iCalendar:
Returns a list of events sorted by date
"""
if type(timeline_start) == arrow.arrow.Arrow:
if timezone == None:
if timezone is None:
timezone = 'UTC'
t_start = timeline_start
t_end = timeline_end

View File

@ -9,7 +9,7 @@ images.
Copyright by aceisace
"""
from PIL import Image, ImageOps, ImageColor
from PIL import Image
import requests
import numpy
import os
@ -134,12 +134,12 @@ class Inkyimage:
image = self.image
if layout == 'horizontal':
if (image.height > image.width):
if image.height > image.width:
logger.info('image width greater than image height, flipping')
image = image.rotate(90, expand=True)
elif layout == 'vertical':
if (image.width > image.height):
if image.width > image.height:
logger.info('image width greater than image height, flipping')
image = image.rotate(90, expand=True)
else:
@ -242,7 +242,7 @@ class Inkyimage:
if self._image_loaded():
image = self.image.convert('RGB')
else:
logger.error('No image loaded')
raise FileNotFoundError
if palette == 'bwr':
# black-white-red palette

View File

@ -5,12 +5,11 @@ Agenda module for Inky-Calendar Project
Copyright by aceisace
"""
from inkycal.modules.template import inkycal_module
import arrow
from inkycal.custom import *
from inkycal.modules.ical_parser import iCalendar
import calendar as cal
import arrow
from inkycal.modules.template import inkycal_module
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
@ -58,9 +57,8 @@ class Agenda(inkycal_module):
# Check if all required parameters are present
for param in self.requires:
if not param in config:
if param not in config:
raise Exception(f'config is missing {param}')
logger.exception(f'config is missing "{param}"')
# module specific parameters
self.date_format = config['date_format']
@ -184,7 +182,7 @@ class Agenda(inkycal_module):
title = _['title']
# Check if item is a date
if not 'end' in _:
if 'end' not in _:
ImageDraw.Draw(im_colour).line(
(0, line_pos[cursor][1], im_width, line_pos[cursor][1]),
fill='black')
@ -199,7 +197,7 @@ class Agenda(inkycal_module):
time = _['begin'].format(self.time_format)
# Check if event is all day, if not, add the time
if parser.all_day(_) == False:
if not parser.all_day(_):
write(im_black, (x_time, line_pos[cursor][1]),
(time_width, line_height), time,
font=self.font, alignment='left')

View File

@ -108,7 +108,7 @@ class Calendar(inkycal_module):
logger.debug(f"month_name_height: {month_name_height}")
logger.debug(f"weekdays_height: {weekdays_height}")
if self.show_events == True:
if self.show_events:
logger.debug("Allocating space for events")
calendar_height = int(im_height * 0.6)
events_height = im_height - month_name_height - weekdays_height - calendar_height
@ -207,7 +207,7 @@ class Calendar(inkycal_module):
im_colour.paste(icon, current_day_pos, icon)
# If events should be loaded and shown...
if self.show_events == True:
if self.show_events:
# If this month requires 5 instead of 6 rows, increase event section height
if len(cal.monthcalendar(now.year, now.month)) == 5:
@ -277,7 +277,7 @@ class Calendar(inkycal_module):
self._upcoming_events = upcoming_events
# delete events which won't be able to fit (more events than lines)
upcoming_events[:max_event_lines]
upcoming_events = upcoming_events[:max_event_lines]
# Check if any events were found in the given timerange
if upcoming_events:
@ -315,7 +315,7 @@ class Calendar(inkycal_module):
date, font=self.font, alignment='left')
# Check if event is all day
if parser.all_day(event) == True:
if parser.all_day(event):
write(im_black, (date_width, event_lines[cursor][1]),
(event_width_l, line_height), name, font=self.font,
alignment='left')

View File

@ -11,11 +11,7 @@ from inkycal.custom import *
from random import shuffle
try:
import feedparser
except ImportError:
print('feedparser is not installed! Please install with:')
print('pip3 install feedparser')
import feedparser
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
@ -54,7 +50,7 @@ class Feeds(inkycal_module):
# Check if all required parameters are present
for param in self.requires:
if not param in config:
if param not in config:
raise Exception(f'config is missing {param}')
# required parameters
@ -89,10 +85,10 @@ class Feeds(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
else:
raise Exception('Network could not be reached :/')
raise NetworkNotReachableError
# Set some parameters for formatting feeds
line_spacing = 1
@ -119,7 +115,7 @@ class Feeds(inkycal_module):
self._parsed_feeds = parsed_feeds
# Shuffle the list to prevent showing the same content
if self.shuffle_feeds == True:
if self.shuffle_feeds:
shuffle(parsed_feeds)
# Trim down the list to the max number of lines

View File

@ -88,7 +88,7 @@ class Inkyimage(inkycal_module):
im.remove_alpha()
# if autoflip was enabled, flip the image
if self.autoflip == True:
if self.autoflip:
im.autoflip(self.orientation)
# resize the image so it can fit on the epaper

View File

@ -49,10 +49,10 @@ class Jokes(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
else:
raise Exception('Network could not be reached :/')
raise NetworkNotReachableError
# Set some parameters for formatting feeds
line_spacing = 1

View File

@ -58,7 +58,7 @@ class Inkyserver(inkycal_module):
# required parameters
for param in self.requires:
if not param in config:
if param not in config:
raise Exception(f'config is missing {param}')
# optional parameters

View File

@ -97,7 +97,7 @@ class Slideshow(inkycal_module):
return somelist[1:] + somelist[:1]
# Switch to the next image if this is not the first run
if self._first_run == True:
if self._first_run:
self._first_run = False
else:
self.images = rotate(self.images)
@ -115,7 +115,7 @@ class Slideshow(inkycal_module):
im.remove_alpha()
# if autoflip was enabled, flip the image
if self.autoflip == True:
if self.autoflip:
im.autoflip(self.orientation)
# resize the image so it can fit on the epaper

View File

@ -8,11 +8,7 @@ Copyright by aceisace
from inkycal.modules.template import inkycal_module
from inkycal.custom import *
try:
import todoist
except ImportError:
print('todoist is not installed! Please install with:')
print('pip3 install todoist-python')
import todoist
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
@ -20,7 +16,7 @@ logger = logging.getLogger(filename)
class Todoist(inkycal_module):
"""Todoist api class
parses todo's from api-key
parses todos from api-key
"""
name = "Todoist API - show your todos from todoist"
@ -47,7 +43,7 @@ class Todoist(inkycal_module):
# Check if all required parameters are present
for param in self.requires:
if not param in config:
if param not in config:
raise Exception(f'config is missing {param}')
# module specific parameters
@ -84,11 +80,11 @@ class Todoist(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
self._api.sync()
else:
raise Exception('Network could not be reached :/')
raise NetworkNotReachableError
# Set some parameters for formatting todos
line_spacing = 1
@ -134,7 +130,7 @@ class Todoist(inkycal_module):
simplified = [
{
'name': task['content'],
'due': task['due']['string'] if task['due'] != None else "",
'due': task['due']['string'] if task['due'] is not None else "",
'priority': task['priority'],
'project': all_projects[task['project_id']] if task['project_id'] in all_projects else "deleted"
}
@ -170,13 +166,13 @@ class Todoist(inkycal_module):
if cursor < len(line_positions):
line_x, line_y = line_positions[cursor]
# Add todo project name
# Add todos project name
write(
im_colour, line_positions[cursor],
(project_width, line_height),
todo['project'], font=self.font, alignment='left')
# Add todo due if not empty
# Add todos due if not empty
if todo['due'] != "":
write(
im_black,
@ -184,7 +180,7 @@ class Todoist(inkycal_module):
(due_width, line_height),
todo['due'], font=self.font, alignment='left')
# Add todo name
# Add todos name
write(
im_black,
(line_x + project_width + due_width, line_y),

View File

@ -10,13 +10,8 @@ from inkycal.custom import *
import math, decimal
import arrow
from locale import getdefaultlocale as sys_locale
try:
from pyowm.owm import OWM
except ImportError:
print('pyowm is not installed! Please install with:')
print('pip3 install pyowm')
from pyowm.owm import OWM
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
@ -123,11 +118,10 @@ class Weather(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
else:
logger.exception('Network could not be reached :(')
raise
raise NetworkNotReachableError
def get_moon_phase():
"""Calculate the current (approximate) moon phase"""
@ -426,11 +420,11 @@ class Weather(inkycal_module):
sunset = sunset_raw.format('H:mm')
# Format the windspeed to user preference
if self.use_beaufort == True:
if self.use_beaufort:
logger.debug("using beaufort for wind")
wind = str(weather.wind(unit='beaufort')['speed'])
elif self.use_beaufort == False:
else:
if self.units == 'metric':
logging.debug('getting windspeed in metric unit')