Skip to content

Commit

Permalink
Code gardening.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchilvers committed Feb 21, 2024
1 parent b1f8cd8 commit 256e8ae
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 76 deletions.
43 changes: 21 additions & 22 deletions examples/farmyard/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import invent
from invent.ui import Button, Image, from_datastore
from invent.ui import from_datastore

invent.set_media_root(".")
invent.datastore.update({
"number_of_honks": 0,
"number_of_oinks": 0,
})


# User interface
farmyard_app = invent.App(
# User interface.
invent.ui.App(
name="Loosey Goosey",
content = [
invent.Page(
content=[
invent.ui.Page(
name="Honk",
content = [
content=[
invent.ui.Image(
invent.media.images.goose.png,
channel="honk"
),
invent.ui.Button(
name="button honk",
label="HONK!",
label="HONK!!!",
channel="honk"
),
invent.ui.Button(
Expand All @@ -35,9 +29,9 @@
)
],
),
invent.Page(
invent.ui.Page(
name="Oink",
content = [
content=[
invent.ui.Image(
invent.media.images.pig.png,
channel="oink"
Expand All @@ -62,7 +56,13 @@
)


# Handlers (stacks of blocks)
# Datastore.
invent.datastore.update(number_of_honks=0)
if "number_of_oinks" not in invent.datastore:
invent.datastore.update(number_of_oinks=0)


# Code (stacks of blocks).
def make_honk(message):
invent.datastore["number_of_honks"] = invent.datastore["number_of_honks"] + 1
invent.play_sound(invent.media.sounds.honk.mp3)
Expand All @@ -79,12 +79,11 @@ def move_page(message):
elif message.button == "to_pig":
invent.show_page("Oink")


# Pubsub (???)
invent.subscribe(make_honk, to_channel="honk", when=["press", "touch"])
invent.subscribe(make_oink, to_channel="oink", when=["press", "touch"])
invent.subscribe(move_page, to_channel="navigate", when=["press",])
# Channels.
invent.subscribe(make_honk, to_channel="honk", when_subject=["press", "touch"])
invent.subscribe(make_oink, to_channel="oink", when_subject=["press", "touch"])
invent.subscribe(move_page, to_channel="navigate", when_subject=["press",])


# GO!
farmyard_app.go()
invent.go()
8 changes: 2 additions & 6 deletions examples/farmyard/pyscript.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
[files]
#
# Project.
#
"./translations.json"="./translations.json"
#
# Invent.
#
"{INVENT}" = "/src/invent"
"{INVENT_TO}" = "./invent"
"{INVENT}/__init__.py"="{INVENT_TO}/__init__.py"
"{INVENT}/__about__.py"="{INVENT_TO}/__about__.py"
"{INVENT}/app.py"="{INVENT_TO}/app.py"
"{INVENT}/channels.py"="{INVENT_TO}/channels.py"
"{INVENT}/datastore.py"="{INVENT_TO}/datastore.py"
"{INVENT}/i18n.py"="{INVENT_TO}/i18n.py"
"{INVENT}/media.py"="{INVENT_TO}/media.py"
"{INVENT}/page.py"="{INVENT_TO}/page.py"
"{INVENT}/utils.py"="{INVENT_TO}/utils.py"
"{INVENT}/ui/__init__.py"="{INVENT_TO}/ui/__init__.py"
"{INVENT}/ui/app.py"="{INVENT_TO}/ui/app.py"
"{INVENT}/ui/core.py"="{INVENT_TO}/ui/core.py"
"{INVENT}/ui/page.py"="{INVENT_TO}/ui/page.py"
"{INVENT}/ui/utils.py"="{INVENT_TO}/ui/utils.py"
"{INVENT}/ui/widgets/__init__.py"="{INVENT_TO}/ui/widgets/__init__.py"
"{INVENT}/ui/widgets/button.py"="{INVENT_TO}/ui/widgets/button.py"
Expand Down
14 changes: 0 additions & 14 deletions examples/farmyard/translations.json

This file was deleted.

11 changes: 7 additions & 4 deletions src/invent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import sys
from .channels import Message, subscribe, publish, unsubscribe
from .datastore import DataStore
from .page import Page
from .app import App
from .i18n import _, load_translations
from .media import Media, set_media_root, get_media_root
from .ui.app import App
from .utils import play_sound, show_page

__all__ = [
Expand All @@ -34,8 +33,6 @@
"unsubscribe",
"DataStore",
"datastore",
"App",
"Page",
"_",
"load_translations",
"Media",
Expand All @@ -45,6 +42,7 @@
"play_sound",
"show_page",
"is_micropython",
"go"
]


Expand All @@ -58,3 +56,8 @@

#: A flag to show if MicroPython is the current Python interpreter.
is_micropython = "MicroPython" in sys.version


#: Start the app.
def go():
App.app().go()
20 changes: 10 additions & 10 deletions src/invent/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __str__(self):
return result


def subscribe(handler, to_channel, when):
def subscribe(handler, to_channel, when_subject):
"""
Subscribe an event handler to a channel[s] to handle when a certain sort of
message[s] is received (identified by subject).
Expand All @@ -69,14 +69,14 @@ def subscribe(handler, to_channel, when):
to_channel = [
to_channel,
]
if isinstance(when, str):
when = [
when,
if isinstance(when_subject, str):
when_subject = [
when_subject,
]
for channel in to_channel:
if channel not in _channels:
_channels[channel] = {}
for name in when:
for name in when_subject:
message_handlers = _channels[channel].get(name, set())
message_handlers.add(handler)
_channels[channel][name] = message_handlers
Expand Down Expand Up @@ -104,7 +104,7 @@ def publish(message, to_channel):
handler(message)


def unsubscribe(handler, from_channel, when):
def unsubscribe(handler, from_channel, when_subject):
"""
Unsubscribe a handler from a channel[s] to stop it handling when a certain
message[s] is received (identified by subject).
Expand All @@ -120,14 +120,14 @@ def unsubscribe(handler, from_channel, when):
from_channel = [
from_channel,
]
if isinstance(when, str):
when = [
when,
if isinstance(when_subject, str):
when_subject = [
when_subject,
]
for channel in from_channel:
channel_info = _channels.get(channel)
if channel_info:
for name in when:
for name in when_subject:
if name in channel_info and handler in channel_info[name]:
channel_info[name].remove(handler)
else:
Expand Down
2 changes: 1 addition & 1 deletion src/invent/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def load_translations(translations="./translations.json"):
with open(translations, "r") as tr:
__translations = json.load(tr)
except Exception as ex:
window.console.error(str(ex))
window.console.warn(str(ex))
for language in window.navigator.languages: # pragma: no cover
if language in __translations:
set_language(language)
Expand Down
2 changes: 1 addition & 1 deletion src/invent/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


#: The URL root from which the Media class builds the full path.
__root__ = ""
__root__ = "."


def set_media_root(root):
Expand Down
6 changes: 5 additions & 1 deletion src/invent/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
limitations under the License.
"""

from .utils import random_id, sanitize
from .app import App
from .core import Widget, Container, Column, Row, from_datastore
from .page import Page
from .utils import random_id, sanitize
from .widgets.button import Button
from .widgets.image import Image
from .widgets.textbox import TextBox
Expand All @@ -29,6 +31,8 @@
__all__ = [
"random_id",
"sanitize",
"App",
"Page",
"Widget",
"Container",
"Column",
Expand Down
7 changes: 6 additions & 1 deletion src/invent/app.py → src/invent/ui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"""

from pyscript import document
from .i18n import load_translations, _

import invent
from ..i18n import load_translations, _


__all__ = [
Expand All @@ -41,6 +43,7 @@ class App:
def __init__(
self,
name,
media_root=".",
icon=None,
description=None,
author=None,
Expand All @@ -59,6 +62,8 @@ def __init__(
self.content = content or []
self._current_page = None

invent.set_media_root(media_root)

@classmethod
def app(cls):
global __app__
Expand Down
2 changes: 1 addition & 1 deletion src/invent/ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def reactor(message, key=value.key):
obj.render()

# Subscribe to store events for the specified key.
invent.subscribe(reactor, to_channel="store-data", when=value.key)
invent.subscribe(reactor, to_channel="store-data", when_subject=value.key)
# Update value to the actual value from the datastore.
value = invent.datastore.get(value.key, self.default_value)
# Set the value in the widget.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/invent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Utility functions.
"""

from invent import App
from invent.ui import App
from pyscript import window


Expand Down
6 changes: 3 additions & 3 deletions static/mpy-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"{INVENT_TO}": "./invent",
"{INVENT}/channels.py": "{INVENT_TO}/channels.py",
"{INVENT}/datastore.py": "{INVENT_TO}/datastore.py",
"{INVENT}/app.py": "{INVENT_TO}/app.py",
"{INVENT}/page.py": "{INVENT_TO}/page.py",
"{INVENT}/i18n.py": "{INVENT_TO}/i18n.py",
"{INVENT}/media.py": "{INVENT_TO}/media.py",
"{INVENT}/utils.py": "{INVENT_TO}/utils.py",
"{INVENT}/__init__.py": "{INVENT_TO}/__init__.py",
"{INVENT}/__about__.py": "{INVENT_TO}/__about__.py",
"{INVENT}/ui/__init__.py": "{INVENT_TO}/ui/__init__.py",
"{INVENT}/ui/utils.py": "{INVENT_TO}/ui/utils.py",
"{INVENT}/ui/app.py": "{INVENT_TO}/ui/app.py",
"{INVENT}/ui/core.py": "{INVENT_TO}/ui/core.py",
"{INVENT}/ui/page.py": "{INVENT_TO}/ui/page.py",
"{INVENT}/ui/utils.py": "{INVENT_TO}/ui/utils.py",
"{INVENT}/ui/widgets/__init__.py": "{INVENT_TO}/ui/widgets/__init__.py",
"{INVENT}/ui/widgets/button.py": "{INVENT_TO}/ui/widgets/button.py",
"{INVENT}/ui/widgets/image.py": "{INVENT_TO}/ui/widgets/image.py",
Expand Down
Loading

0 comments on commit 256e8ae

Please sign in to comment.