ZPUI code snippets

From ZeroPhone
(Redirected from PyLCI code snippets)
Jump to: navigation, search

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

Minimal ZPUI app structure

app/main.py:

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
    pass

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")