Skip to content

Commit 56292b0

Browse files
committed
adapt to package resources.
1 parent 2f30e5b commit 56292b0

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT

Magic_AI_Storybook/magic_ai_storybook/make_shortcut.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def main():
4646
Comment=Run {APP_TITLE}
4747
Terminal={"true" if RUN_IN_TERMINAL else "false"}
4848
Name={APP_TITLE}
49-
Exec=sudo python {APP_PATH}
49+
Exec=sudo /root/.local/bin/magicbook-story
5050
Type=Application
5151
Icon={APP_ICON}
5252
"""

Magic_AI_Storybook/magic_ai_storybook/story.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import threading
66
import sys
77
import os
8+
import importlib.resource
9+
810
import re
911
import time
1012
import argparse
@@ -21,12 +23,10 @@
2123
from rpi_backlight import Backlight
2224
from adafruit_led_animation.animation.pulse import Pulse
2325

24-
from listener import Listener
26+
from .listener import Listener
2527

2628
# Base Path is the folder the script resides in
27-
BASE_PATH = os.path.dirname(sys.argv[0])
28-
if BASE_PATH != "":
29-
BASE_PATH += "/"
29+
BASE_PATH = importlib.resource.files(__package__)
3030

3131
# General Settings
3232
STORY_WORD_LENGTH = 800
@@ -59,13 +59,13 @@
5959
BUTTON_NEW_IMAGE = "button_new.png"
6060

6161
# Asset Paths
62-
IMAGES_PATH = BASE_PATH + "images/"
63-
FONTS_PATH = BASE_PATH + "fonts/"
62+
IMAGES_PATH = BASE_PATH.joinpath("images/")
63+
FONTS_PATH = BASE_PATH.joinpath("fonts/")
6464

6565
# Font Path & Size
66-
TITLE_FONT = (FONTS_PATH + "Desdemona Black Regular.otf", 48)
66+
TITLE_FONT = (FONTS_PATH.joinpath("Desdemona Black Regular.otf"), 48)
6767
TITLE_COLOR = (0, 0, 0)
68-
TEXT_FONT = (FONTS_PATH + "times new roman.ttf", 24)
68+
TEXT_FONT = (FONTS_PATH.joinpath("times new roman.ttf"), 24)
6969
TEXT_COLOR = (0, 0, 0)
7070

7171
# Delays Settings
@@ -262,9 +262,9 @@ def start(self):
262262
self._load_font("text", TEXT_FONT)
263263

264264
# Add buttons
265-
back_button_image = pygame.image.load(IMAGES_PATH + BUTTON_BACK_IMAGE)
266-
next_button_image = pygame.image.load(IMAGES_PATH + BUTTON_NEXT_IMAGE)
267-
new_button_image = pygame.image.load(IMAGES_PATH + BUTTON_NEW_IMAGE)
265+
back_button_image = pygame.image.load(IMAGES_PATH.joinpath(BUTTON_BACK_IMAGE))
266+
next_button_image = pygame.image.load(IMAGES_PATH.joinpath(BUTTON_NEXT_IMAGE))
267+
new_button_image = pygame.image.load(IMAGES_PATH.joinpath(BUTTON_NEW_IMAGE))
268268
button_spacing = (
269269
self.width
270270
- (
@@ -407,7 +407,7 @@ def _rotate_mouse_pos(self, point):
407407

408408
def _load_image(self, name, filename):
409409
try:
410-
image = pygame.image.load(IMAGES_PATH + filename)
410+
image = pygame.image.load(IMAGES_PATH.joinpath(filename))
411411
self.images[name] = image
412412
except pygame.error:
413413
pass
@@ -771,7 +771,8 @@ def parse_args():
771771
return parser.parse_args()
772772

773773

774-
def main(args):
774+
def main():
775+
args = parse_args()
775776
book = Book(args.rotation)
776777
try:
777778
book.start()
@@ -790,4 +791,4 @@ def main(args):
790791

791792

792793
if __name__ == "__main__":
793-
main(parse_args())
794+
main()

Magic_AI_Storybook/pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dependencies = [
2020
"adafruit-circuitpython-neopixel>=6.3.9",
2121
"rpi-backlight>=2.6.0",
2222
]
23+
24+
[project.scripts]
25+
"magicbook-story" = "magic_ai_storybook:story.run"
26+
2327
[build-system]
2428
requires = ["setuptools>=61", "wheel"]
2529
build-backend = "setuptools.build_meta"

0 commit comments

Comments
 (0)