From a109bf326b82e688769e5186b0a8cbd78474ca8f Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Wed, 9 Aug 2023 01:35:22 +0200
Subject: [PATCH] app: give applications a context object

---
 docs/badge/application-programming.rst        |  8 ++++----
 python_payload/apps/demo_cap_touch/main.py    |  9 +++++----
 python_payload/apps/demo_harmonic/__init__.py |  6 +++---
 python_payload/apps/demo_melodic/__init__.py  |  6 +++---
 python_payload/apps/demo_scroll/__init__.py   |  6 +++---
 python_payload/apps/demo_worms/__init__.py    |  6 +++---
 python_payload/apps/nick/__init__.py          |  6 +++---
 python_payload/apps/otamatone/__init__.py     |  8 ++++----
 python_payload/apps/simple_drums/__init__.py  |  6 +++---
 python_payload/st3m/application.py            | 17 ++++++++++++++---
 10 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/docs/badge/application-programming.rst b/docs/badge/application-programming.rst
index d8bc843116..36ebcc2f9a 100644
--- a/docs/badge/application-programming.rst
+++ b/docs/badge/application-programming.rst
@@ -357,7 +357,7 @@ Let's introduce the final class you should actually be using for application dev
 
 .. code-block:: python
 
-    from st3m.application import Application
+    from st3m.application import Application, ApplicationContext
     import st3m.run
 
     class SecondScreen(BaseView):
@@ -381,8 +381,8 @@ Let's introduce the final class you should actually be using for application dev
 
 
     class MyDemo(Application):
-        def __init__(self) -> None:
-            super().__init__(name="My demo")
+        def __init__(self, app_ctx: ApplicationContext) -> None:
+            super().__init__(app_ctx)
 
         def draw(self, ctx: Context) -> None:
             # Paint the background black
@@ -399,7 +399,7 @@ Let's introduce the final class you should actually be using for application dev
             if self.input.right_shoulder.middle.pressed:
                 self._view_manager.push(SecondScreen())
 
-    st3m.run.run_view(Example())
+    st3m.run.run_view(Example(ApplicationContext()))
 
 The `Application` class gives you the following extras:
 
diff --git a/python_payload/apps/demo_cap_touch/main.py b/python_payload/apps/demo_cap_touch/main.py
index 381d831b85..2819745974 100644
--- a/python_payload/apps/demo_cap_touch/main.py
+++ b/python_payload/apps/demo_cap_touch/main.py
@@ -1,4 +1,5 @@
-from st3m import application, logging
+from st3m import logging
+from st3m.application import Application, ApplicationContext
 from st3m.goose import List
 from st3m.input import InputState
 from ctx import Context
@@ -32,9 +33,9 @@ class Dot:
         ).fill()
 
 
-class CapTouchDemo(application.Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+class CapTouchDemo(Application):
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         self.dots: List[Dot] = []
         self.last_calib = None
         # self.ui_autocalib = ui.IconLabel("Autocalib done", size=30)
diff --git a/python_payload/apps/demo_harmonic/__init__.py b/python_payload/apps/demo_harmonic/__init__.py
index 7eccb43333..3367c9f6f9 100644
--- a/python_payload/apps/demo_harmonic/__init__.py
+++ b/python_payload/apps/demo_harmonic/__init__.py
@@ -17,12 +17,12 @@ chords = [
     [3, 7, 10, 14, 15],
 ]
 
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 
 
 class HarmonicApp(Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
 
         self.color_intensity = 0.0
         self.chord_index = 0
diff --git a/python_payload/apps/demo_melodic/__init__.py b/python_payload/apps/demo_melodic/__init__.py
index 20b02e8930..4bf3d9f977 100644
--- a/python_payload/apps/demo_melodic/__init__.py
+++ b/python_payload/apps/demo_melodic/__init__.py
@@ -83,13 +83,13 @@ def foreground() -> None:
     adjust_playing_field_to_octave()
 
 
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 
 
 # TODO(q3k): properly port this app
 class MelodicApp(Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         init()
 
     def draw(self, ctx: Context) -> None:
diff --git a/python_payload/apps/demo_scroll/__init__.py b/python_payload/apps/demo_scroll/__init__.py
index ccaabab9fd..05910414dd 100644
--- a/python_payload/apps/demo_scroll/__init__.py
+++ b/python_payload/apps/demo_scroll/__init__.py
@@ -5,7 +5,7 @@ import math
 
 # flow3r imports
 from st3m import InputState
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 from st3m.property import BLUE, WHITE
 from st3m.goose import Optional
 from st3m.utils import xy_from_polar, tau
@@ -16,8 +16,8 @@ from ctx import Context
 class ScrollDemo(Application):
     PETAL_NO = 8
 
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         self.scroll = CapScrollController()
 
     def draw(self, ctx: Context) -> None:
diff --git a/python_payload/apps/demo_worms/__init__.py b/python_payload/apps/demo_worms/__init__.py
index c2f72ff3e2..b2591fb03c 100644
--- a/python_payload/apps/demo_worms/__init__.py
+++ b/python_payload/apps/demo_worms/__init__.py
@@ -5,7 +5,7 @@ import math
 
 # flow3r imports
 from st3m import InputState
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 from st3m.property import BLUE, WHITE
 from st3m.goose import Optional
 from st3m.utils import xy_from_polar
@@ -18,8 +18,8 @@ tau = 2 * math.pi
 
 # Subclass Application
 class AppWorms(Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
 
         # HACK: we work against double buffering by keeping note of how many
         # times on_draw got called.
diff --git a/python_payload/apps/nick/__init__.py b/python_payload/apps/nick/__init__.py
index 620a0bea69..167f0da4a8 100644
--- a/python_payload/apps/nick/__init__.py
+++ b/python_payload/apps/nick/__init__.py
@@ -1,4 +1,4 @@
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 from st3m.property import PUSH_RED, GO_GREEN, BLACK
 from st3m.goose import Dict, Any
 from st3m.input import InputState
@@ -48,8 +48,8 @@ class Configuration:
 
 
 class NickApp(Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         self._scale = 1.0
         self._led = 0.0
         self._phase = 0.0
diff --git a/python_payload/apps/otamatone/__init__.py b/python_payload/apps/otamatone/__init__.py
index 81986763c7..c666f93e5e 100644
--- a/python_payload/apps/otamatone/__init__.py
+++ b/python_payload/apps/otamatone/__init__.py
@@ -5,7 +5,7 @@ import math
 import leds
 
 from st3m import InputState, Responder
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 from st3m.property import BLUE, WHITE
 from st3m.goose import Optional
 from st3m.utils import xy_from_polar, tau
@@ -77,8 +77,8 @@ class Otamatone(Application):
 
     PETAL_NO = 3
 
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         self._ts = 0
         self._blob = Blob()
 
@@ -136,4 +136,4 @@ class Otamatone(Application):
 if __name__ == "__main__":
     from st3m.run import run_view
 
-    run_view(Otamatone("otamatone"))
+    run_view(Otamatone(ApplicationContext()))
diff --git a/python_payload/apps/simple_drums/__init__.py b/python_payload/apps/simple_drums/__init__.py
index 3abfe34ba6..c5f7a1ef1d 100644
--- a/python_payload/apps/simple_drums/__init__.py
+++ b/python_payload/apps/simple_drums/__init__.py
@@ -3,7 +3,7 @@ import hardware
 import captouch
 import leds
 
-from st3m.application import Application
+from st3m.application import Application, ApplicationContext
 from st3m.input import InputState
 from st3m.goose import Tuple
 from ctx import Context
@@ -37,8 +37,8 @@ class Dot:
 
 
 class SimpleDrums(Application):
-    def __init__(self, name: str) -> None:
-        super().__init__(name)
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
         # ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
         self.blm = bl00mbox.Channel()
         self.seq = self.blm.new(bl00mbox.patches.step_sequencer)
diff --git a/python_payload/st3m/application.py b/python_payload/st3m/application.py
index 576e7cfe54..fc3b515d09 100644
--- a/python_payload/st3m/application.py
+++ b/python_payload/st3m/application.py
@@ -18,9 +18,20 @@ import sys
 log = Log(__name__)
 
 
+class ApplicationContext:
+    """
+    Container for future application context.
+
+    Envisioned are: path to the bundle, bundle data,
+    path to a data directory, etc...
+    """
+
+    pass
+
+
 class Application(BaseView):
-    def __init__(self, name: str = __name__) -> None:
-        self._name = name
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        self._app_ctx = app_ctx
         super().__init__()
 
     def on_exit(self) -> None:
@@ -152,7 +163,7 @@ class BundleMetadata:
             log.info(f"Loaded {self.name} module: {m}")
             klass = getattr(m, class_entry)
             log.info(f"Loaded {self.name} class: {klass}")
-            inst = klass(package_name)
+            inst = klass(ApplicationContext())
             log.info(f"Instantiated {self.name} class: {inst}")
             return inst  # type: ignore
         except Exception as e:
-- 
GitLab