Skip to content

Commit 44860cb

Browse files
authored
Add flake8 and clean up code (#6)
* Add flake8 config * Apply flake8 suggestions * Apply black formatting
1 parent d28940d commit 44860cb

File tree

8 files changed

+74
-38
lines changed

8 files changed

+74
-38
lines changed

poetry.lock

Lines changed: 50 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ uvicorn = "^0.32.1"
2020

2121
[tool.poetry.group.dev.dependencies]
2222
black = "^24.10.0"
23+
flake8 = "^7.1.1"
2324
isort = "^5.13.2"
2425

2526
[build-system]
2627
requires = ["poetry-core"]
2728
build-backend = "poetry.core.masonry.api"
29+
30+
[tool.black]
31+
line-length = 100
32+
33+
[tool.isort]
34+
profile = "black"

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = E501, W503
3+
max-line-length = 100

src/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class MagInkDashConfig:
2626
LAT: float = float(os.getenv("LAT", 34.118333))
2727
LNG: float = float(os.getenv("LNG", -118.300333))
2828
WEATHER_UNITS: WeatherUnits = WeatherUnits[os.getenv("WEATHER_UNITS", "metric")]
29-
NUM_DAYS_IN_TEMPLATE: int = (
30-
5 # Not configurable because it's hard-coded in the HTML template
31-
)
29+
NUM_DAYS_IN_TEMPLATE: int = 5 # Not configurable because it's hard-coded in the HTML template
3230

3331
def get_config():
3432
global _current_config

src/ics_cal/ics.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def get_events(self, ics_url, calStartDatetime, calEndDatetime, displayTZ, numDa
3838

3939
for event in eventList:
4040
if event["isMultiday"]:
41-
numDays = (
42-
event["endDatetime"].date() - event["startDatetime"].date()
43-
).days
41+
numDays = (event["endDatetime"].date() - event["startDatetime"].date()).days
4442
for day in range(0, numDays):
4543
calDict.setdefault(
4644
event["startDatetime"].date() + dt.timedelta(days=day), []

src/ics_cal/icshelper.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ def retrieve_events(self, ics_url, startDatetime, endDatetime, localTZ):
2828
if response.ok:
2929
cal = Calendar(response.text)
3030
else:
31-
self.logger.error(
32-
f"Received an error when downloading ICS: {response.text}"
33-
)
31+
self.logger.error(f"Received an error when downloading ICS: {response.text}")
3432
sys.exit(1)
3533

3634
if not cal.events:

src/main.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
retrieve the information. So feel free to change up the code and amend it to your needs.
77
"""
88

9+
910
import datetime as dt
1011
import tempfile
1112
import time
@@ -55,11 +56,8 @@ def get_image() -> FileResponse:
5556
)
5657

5758
currTime = dt.datetime.now(pytz.timezone(cfg.DISPLAY_TZ))
58-
currDate = currTime.date()
5959
calStartDatetime = currTime.replace(hour=0, minute=0, second=0, microsecond=0)
60-
calEndDatetime = calStartDatetime + dt.timedelta(
61-
days=cfg.NUM_CAL_DAYS_TO_QUERY, seconds=-1
62-
)
60+
calEndDatetime = calStartDatetime + dt.timedelta(days=cfg.NUM_CAL_DAYS_TO_QUERY, seconds=-1)
6361

6462
events = calModule.get_events(
6563
cfg.ICS_URL,
@@ -73,16 +71,12 @@ def get_image() -> FileResponse:
7371
) # sort by date so we can later take the first N days
7472

7573
end_time = time.time()
76-
logger.info(
77-
f"Completed data retrieval in {round(end_time - start_time, 3)} seconds."
78-
)
74+
logger.info(f"Completed data retrieval in {round(end_time - start_time, 3)} seconds.")
7975

8076
# TODO: delete=False leads to accumulating temporary files in /tmp but is currently needed because the FileResponse is async.
81-
with tempfile.NamedTemporaryFile(
82-
suffix=".png", delete_on_close=False, delete=False
83-
) as tf:
77+
with tempfile.NamedTemporaryFile(suffix=".png", delete_on_close=False, delete=False) as tf:
8478
start_time = time.time()
85-
logger.info(f"Generating image...")
79+
logger.info("Generating image...")
8680

8781
renderService = RenderHelper(cfg)
8882
renderService.process_inputs(
@@ -103,7 +97,7 @@ def get_image() -> FileResponse:
10397

10498

10599
if __name__ == "__main__":
106-
logger.info(f"Starting web server...")
100+
logger.info("Starting web server...")
107101
config = uvicorn.Config(app, host="127.0.0.1", port=5000, log_level="debug")
108102
server = uvicorn.Server(config)
109103
server.run()

src/render/render.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from time import sleep
1414

1515
import structlog
16-
from PIL import Image
1716
from selenium import webdriver
1817
from selenium.webdriver.chrome.options import Options
1918
from selenium.webdriver.common.by import By
@@ -36,12 +35,8 @@ def set_viewport_size(self, driver):
3635
inner_height = int(html.get_attribute("clientHeight"))
3736

3837
# "Internal width you want to set+Set "outer frame width" to window size
39-
target_width = self.cfg.IMAGE_WIDTH + (
40-
current_window_size["width"] - inner_width
41-
)
42-
target_height = self.cfg.IMAGE_HEIGHT + (
43-
current_window_size["height"] - inner_height
44-
)
38+
target_width = self.cfg.IMAGE_WIDTH + (current_window_size["width"] - inner_width)
39+
target_height = self.cfg.IMAGE_HEIGHT + (current_window_size["height"] - inner_height)
4540

4641
driver.set_window_rect(width=target_width, height=target_height)
4742

@@ -60,9 +55,7 @@ def get_screenshot(self, path_to_server_image):
6055
sleep(1)
6156
driver.get_screenshot_as_file(self.currPath + "/dashboard.png")
6257
driver.get_screenshot_as_file(path_to_server_image)
63-
self.logger.debug(
64-
f"Screenshot captured and saved to file {path_to_server_image}."
65-
)
58+
self.logger.debug(f"Screenshot captured and saved to file {path_to_server_image}.")
6659
driver.close()
6760

6861
def get_short_time(self, datetimeObj, is24hour=False):
@@ -120,9 +113,7 @@ def process_inputs(
120113

121114
if len(cal_events_days) == 0:
122115
cal_events_days.append("Today")
123-
cal_events_list.append(
124-
'<div class="event"><span class="event-time">None</span></div>'
125-
)
116+
cal_events_list.append('<div class="event"><span class="event-time">None</span></div>')
126117

127118
self.extend_list(cal_events_days, self.cfg.NUM_DAYS_IN_TEMPLATE, "")
128119
self.extend_list(cal_events_list, self.cfg.NUM_DAYS_IN_TEMPLATE, "")
@@ -146,9 +137,7 @@ def process_inputs(
146137
events_dayafter3=cal_events_list[3],
147138
events_dayafter4=cal_events_list[4],
148139
# I'm choosing to show the forecast for the next hour instead of the current weather
149-
current_weather_text=string.capwords(
150-
current_weather["weather"][0]["description"]
151-
),
140+
current_weather_text=string.capwords(current_weather["weather"][0]["description"]),
152141
current_weather_id=current_weather["weather"][0]["id"],
153142
current_weather_temp=round(current_weather["temp"]),
154143
today_weather_id=daily_forecast[0]["weather"][0]["id"],

0 commit comments

Comments
 (0)