ZPUI code snippets

From ZeroPhone
Jump to: navigation, search

!!! This page has been moved to ZPUI documentation! !!! This page will no longer exit after some point in the future.

This page contains some useful code snippets you can use in your ZPUI apps.

Minimal ZPUI app structure


menu_name = "Skeleton app"

i = None #Input device
o = None #Output device

def init_app(input, output):
    #Gets called when app is loaded
    global i, o
    i = input; o = output

def callback():
    #Gets called when app is selected from menu

app/__init__.py (empty file):


Reading data from a config file that is stored in the app folder

from helpers import read_config, local_path_gen
config_filename = "config.json"

local_path = local_path_gen(__name__)
config = read_config(local_path(config_filename))

"read_config" function assumes your config is in JSON format, so you might want to use some other library if you're using some other format.

Restoring config file to defaults if reading it failed

from helpers import read_or_create_config, local_path_gen
default_config = '{"your":"default", "config":"to_use"}' #has to be a string
config_filename = "config.json"

local_path = local_path_gen(__name__)
config = read_or_create_config(local_path(config_filename), default_config, menu_name+" app")