Skip to content

Commit e09293d

Browse files
authored
Add notifications and sounds to schema (#1064)
* Add notifications and sounds to schema Add the ability to specific app notifications and sounds in the schema. Here is an example: ```starlark def get_schema(): return schema.Schema( version = "1", notifications = [ schema.Notification( id = "notificationid", name = "Notification", desc = "A Notification", icon = "notification", sounds = [ schema.Sound( title = "Ding!", file = ding_mp3, ), ], ), ], ) ``` This change makes the `Schema` and `SchemaJSON` fields of `Applet` public, so that embedders can inspect the schema and access the sound files that are referenced by `schema.Sound`. * Add Bazel build files to gitignore Bazel files are generated as part of Tidbyt's internal development process but are not used for the open source build.
1 parent 0d1fdc4 commit e09293d

File tree

14 files changed

+549
-104
lines changed

14 files changed

+549
-104
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ dist/
1818
# Dependency directories
1919
node_modules
2020
src/go
21+
22+
# build files from monorepo
23+
BUILD.bazel

cmd/community/validateicons.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/spf13/cobra"
12+
1213
"tidbyt.dev/pixlet/icons"
1314
"tidbyt.dev/pixlet/runtime"
1415
"tidbyt.dev/pixlet/schema"
@@ -55,12 +56,12 @@ func ValidateIcons(cmd *cobra.Command, args []string) error {
5556
}
5657

5758
s := schema.Schema{}
58-
schemaStr := applet.GetSchema()
59-
if schemaStr == "" {
59+
js := applet.SchemaJSON
60+
if len(js) == 0 {
6061
return nil
6162
}
6263

63-
err = json.Unmarshal([]byte(schemaStr), &s)
64+
err = json.Unmarshal(js, &s)
6465
if err != nil {
6566
return fmt.Errorf("failed to load schema: %w", err)
6667
}

runtime/applet.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"tidbyt.dev/pixlet/render"
3131
"tidbyt.dev/pixlet/runtime/modules/animation_runtime"
32+
"tidbyt.dev/pixlet/runtime/modules/file"
3233
"tidbyt.dev/pixlet/runtime/modules/hmac"
3334
"tidbyt.dev/pixlet/runtime/modules/humanize"
3435
"tidbyt.dev/pixlet/runtime/modules/qrcode"
@@ -61,12 +62,12 @@ type Applet struct {
6162

6263
globals map[string]starlark.StringDict
6364

64-
mainFile string
65-
mainFun *starlark.Function
66-
65+
mainFile string
66+
mainFun *starlark.Function
6767
schemaFile string
68-
schema *schema.Schema
69-
schemaJSON []byte
68+
69+
Schema *schema.Schema
70+
SchemaJSON []byte
7071
}
7172

7273
func WithModuleLoader(loader ModuleLoader) AppletOption {
@@ -189,7 +190,7 @@ func (a *Applet) RunWithConfig(ctx context.Context, config map[string]string) (r
189190
// CallSchemaHandler calls a schema handler, passing it a single
190191
// string parameter and returning a single string value.
191192
func (app *Applet) CallSchemaHandler(ctx context.Context, handlerName, parameter string) (result string, err error) {
192-
handler, found := app.schema.Handlers[handlerName]
193+
handler, found := app.Schema.Handlers[handlerName]
193194
if !found {
194195
return "", fmt.Errorf("no exported handler named '%s'", handlerName)
195196
}
@@ -238,11 +239,6 @@ func (app *Applet) CallSchemaHandler(ctx context.Context, handlerName, parameter
238239
return "", fmt.Errorf("a very unexpected error happened for handler \"%s\"", handlerName)
239240
}
240241

241-
// GetSchema returns the config for the applet.
242-
func (app *Applet) GetSchema() string {
243-
return string(app.schemaJSON)
244-
}
245-
246242
// RunTests runs all test functions that are defined in the applet source.
247243
func (app *Applet) RunTests(t *testing.T) {
248244
app.initializers = append(app.initializers, func(thread *starlark.Thread) *starlark.Thread {
@@ -439,23 +435,23 @@ func (a *Applet) ensureLoaded(fsys fs.FS, pathToLoad string, currentlyLoading ..
439435
return fmt.Errorf("calling schema function for %s: %w", a.ID, err)
440436
}
441437

442-
a.schema, err = schema.FromStarlark(schemaVal, globals)
438+
a.Schema, err = schema.FromStarlark(schemaVal, globals)
443439
if err != nil {
444440
return fmt.Errorf("parsing schema for %s: %w", a.ID, err)
445441
}
446442

447-
a.schemaJSON, err = json.Marshal(a.schema)
443+
a.SchemaJSON, err = json.Marshal(a.Schema)
448444
if err != nil {
449445
return fmt.Errorf("serializing schema to JSON for %s: %w", a.ID, err)
450446
}
451447
}
452448

453449
default:
454450
a.globals[pathToLoad] = starlark.StringDict{
455-
"file": File{
456-
fsys: fsys,
457-
path: pathToLoad,
458-
}.Struct(),
451+
"file": &file.File{
452+
FS: fsys,
453+
Path: pathToLoad,
454+
},
459455
}
460456
}
461457

runtime/applet_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
1515
"go.starlark.net/starlark"
16+
1617
"tidbyt.dev/pixlet/schema"
1718
)
1819

@@ -199,9 +200,8 @@ def get_schema():
199200
require.NoError(t, err)
200201
require.NotNil(t, app)
201202

202-
jsonSchema := app.GetSchema()
203203
var s schema.Schema
204-
json.Unmarshal([]byte(jsonSchema), &s)
204+
json.Unmarshal(app.SchemaJSON, &s)
205205
assert.Equal(t, "1", s.Version)
206206

207207
roots, err := app.Run(context.Background())
@@ -374,7 +374,7 @@ func TestZIPModule(t *testing.T) {
374374
// https://go.dev/src/archive/zip/example_test.go
375375
buf := new(bytes.Buffer)
376376
w := zip.NewWriter(buf)
377-
var files = []struct {
377+
files := []struct {
378378
Name, Body string
379379
}{
380380
{"readme.txt", "This archive contains some text files."},
@@ -422,4 +422,35 @@ def main(config):
422422
}, printedText)
423423
}
424424

425+
func TestReadFile(t *testing.T) {
426+
src := `
427+
load("hello.txt", hello = "file")
428+
429+
def assert_eq(message, actual, expected):
430+
if not expected == actual:
431+
fail(message, "-", "expected", expected, "actual", actual)
432+
433+
def test_readall():
434+
assert_eq("readall", hello.readall(), "hello world")
435+
436+
def test_readall_binary():
437+
assert_eq("readall_binary", hello.readall("rb"), b"hello world")
438+
439+
def main():
440+
pass
441+
442+
`
443+
444+
helloTxt := `hello world`
445+
446+
vfs := &fstest.MapFS{
447+
"main.star": {Data: []byte(src)},
448+
"hello.txt": {Data: []byte(helloTxt)},
449+
}
450+
451+
app, err := NewAppletFromFS("test_read_file", vfs)
452+
require.NoError(t, err)
453+
app.RunTests(t)
454+
}
455+
425456
// TODO: test Screens, especially Screens.Render()

runtime/file_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

runtime/file.go renamed to runtime/modules/file/file.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
package runtime
1+
package file
22

33
import (
44
"fmt"
55
"io"
66
"io/fs"
77

8+
"github.com/mitchellh/hashstructure/v2"
89
"go.starlark.net/starlark"
910
"go.starlark.net/starlarkstruct"
1011
)
1112

1213
type File struct {
13-
fsys fs.FS
14-
path string
14+
FS fs.FS
15+
Path string
1516
}
1617

17-
func (f File) Struct() *starlarkstruct.Struct {
18-
return starlarkstruct.FromStringDict(starlark.String("File"), starlark.StringDict{
19-
"path": starlark.String(f.path),
20-
"readall": starlark.NewBuiltin("readall", f.readall),
21-
})
22-
}
23-
24-
func (f File) readall(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
18+
func (f *File) readall(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
2519
var mode starlark.String
2620
if err := starlark.UnpackArgs("readall", args, kwargs, "mode?", &mode); err != nil {
2721
return nil, err
@@ -36,7 +30,7 @@ func (f File) readall(thread *starlark.Thread, _ *starlark.Builtin, args starlar
3630
return r.read(thread, nil, nil, nil)
3731
}
3832

39-
func (f File) reader(mode string) (*Reader, error) {
33+
func (f *File) reader(mode string) (*Reader, error) {
4034
var binaryMode bool
4135
switch mode {
4236
case "", "r", "rt":
@@ -49,13 +43,41 @@ func (f File) reader(mode string) (*Reader, error) {
4943
return nil, fmt.Errorf("unsupported mode: %s", mode)
5044
}
5145

52-
fl, err := f.fsys.Open(f.path)
46+
fl, err := f.FS.Open(f.Path)
5347
if err != nil {
5448
return nil, err
5549
}
5650
return &Reader{fl, binaryMode}, nil
5751
}
5852

53+
func (f *File) AttrNames() []string {
54+
return []string{"path", "readall"}
55+
}
56+
57+
func (f *File) Attr(name string) (starlark.Value, error) {
58+
switch name {
59+
60+
case "path":
61+
return starlark.String(f.Path), nil
62+
63+
case "readall":
64+
return starlark.NewBuiltin("readall", f.readall), nil
65+
66+
default:
67+
return nil, nil
68+
}
69+
}
70+
71+
func (f *File) String() string { return "File(...)" }
72+
func (f *File) Type() string { return "File" }
73+
func (f *File) Freeze() {}
74+
func (f *File) Truth() starlark.Bool { return true }
75+
76+
func (f *File) Hash() (uint32, error) {
77+
sum, err := hashstructure.Hash(f, hashstructure.FormatV2, nil)
78+
return uint32(sum), err
79+
}
80+
5981
type Reader struct {
6082
io.ReadCloser
6183
binaryMode bool

0 commit comments

Comments
 (0)