Hello App: HelloApp.py

Index Home Sample App > Hello App: HelloApp.py

Contents

sys.path.insert(1, mae_python_path)
class HelloApp(UserDevice):

HelloApp.py

# HelloApp.py - class declaration for Hello app

#

# Copyright (c) 2024 HanoverSoft

# Written by Anthony V. Edwards

# 820 Churchill Drive, Chapel Hill, NC  27517

# ave@hanoversoft.net

#

# We handle new session initialization, menu callbacks, and user input responses here.

import os

import sys

mae_python_path= '/usr/mae/python'

if 'MAE_PYTHON' in os.environ:

    mae_python_path= os.environ['MAE_PYTHON']

sys.path.insert(1, mae_python_path)

from UserDevice import UserDevice

from HashArray import HashArray

from XMLData import XMLData

from User import User   # using the User class to access to MAE User database table

class HelloApp(UserDevice):

    This class holds user session state for Hello app.

        Note that our super class is UserDevice(), so we can call its methods via self.

    def init(self, id= 0):

        Constructor for an instance of a user interacting with us.

            We are a simple program, so there's not much to initialize.

            @param id - user id

        super().init()    # initialize our super class, UserDevice

        self.id= id             # our record id - user id - used by the Cache class to manage multiple HelloApp instances

        self.params= XMLData()  # the initial arguments provided to us to control this program's behavior (not used)

    def valid(self) -> bool:

        Determine if the current instance is valid.

            If setDevice() has been called with a valid UserDevice, then HelloApp is valid.

            @return true if this instance is valid, false otherwise

        return self.getUserId() > 0

    # GET methods

    def getId(self) -> int:

        Get this HelloApp instance's id

            @return current instance (user id)

        return self.id

    def getDevice(self):

        Get the UserDevice instance to talk to the user.

            (UserDevice is our super class.)

        return super()

    # SET mehtods

    def setDevice(self, newdevice):

        Record the UserDevice used to communicate with the user so we can use it again later (and not have to pass it as a parameter everywhere).

            @param newdevice - the value of UserDevice to communicate with this user

            @return the current HelloApp instance for further use

        super().fromString(newdevice.toString())

        return self

    def clear(self):

        Clear this instance of HelloApp

        self.setUserId(0)

        self.params.clear()

    def userstart(self, param):

        The first time the user connects to our app, this is called.

            @param param - inbound variables from UserDevice.run() (if any)

        # save the starting params for the instance, once launched

        self.params= param

        # Create a tab/window and display our prompt there

        self.drawPromptWindow()

    def drawPromptWindow(self, defaultName: str= ):

        Create a 'Prompt' window and display our question in it.

            The layout of the question comes from /usr/mae/html/record/helloPrompt.html.

            @see UserDevice

        # Create a tab/window and display our prompt there

        promptwin= self.setWindow('Prompt') # super class UserDevice::setWindow()

        values= HashArray()

        promptwin.record('helloPrompt', values)

        # Fill in the text box with a name

        if defaultName:

            promptwin.settext(defaultName, 'helloPrompt_name')

    def inputName(self, name: str):

        The user has typed something into the name box and we can now process it.

            @param name - the user text

        # UserDevice::

        self.setRegion('helloPrompt_greeting');

        # UserDevice::

        self.writeln('Hello, '+name+'!');

    def menu_Display_FromDB(self):

        Menu Display->From DB selected.  So, query the user's name from the DB, then say hello.

            @see User

        userId= self.getUserId()  # e.g. UserDevice::getUserId()

        if userId == 0:

            # this should never happen! A user must be authorized before they can get through.

            self.attention('Who are you?')  # e.g. UserDevice::attention()

        else:

            # Get the user record from the data store

            user= User(userId)

            # Display the greeting to the user by name

            self.inputName(user.getName())

    def menu_Display_Clear(self):

        The user selected the Display->Clear menu options.

            So, clear the display of anything output so far.

        # Clear the windows

        self.unsetWindow('Prompt')      # e.g. UserDevice::unsetWindow()

        self.unsetWindow('Help');

        # Redraw the prompt window

        self.drawPromptWindow()

    def menu_Display_Help(self):

        The user selected the Display->Help menu options.

            So, display some help (in a new tab).

        helpwin= self.setWindow('Help')  # e.g. UserDevice::setWindow()

        helpwin.clear()

        helpwin.writeln('This is a simple MAE app that asks for a name.')

        helpwin.writeln('Upon submitting the name, the program greets you with a Hello.')

        helpwin.writeln('(Click on the Prompt tab to return to that prompt.)')

    def menu_Display_Quit(self):

        The user selected the Display->Quit menu options.

            So, quit.

        # If we have any user state to save, save it out

        self.save()

        # Exit out back to the starting app for this user

        self.quit();        # e.g. UserDevice::quit()

    def save(self) -> bool:

        Save out our user session data.  For now, do nothing.

            @return true if data save out, false if not

        return False

    def toHashArray(self):

        Convert our session data to a HashArray (key/value pairs). For now, do nothing.

            @return key/value pairs of our session data

        return HashArray()

    @staticmethod

    def cache(id: int):

        Pull our session data from an external cache, such as directly from the data store.  For now, do nothing.

            @param id - id of the record to retrieve (user id)

            @return a ready instance of HelloApp based upon the last saved state

        return HelloApp()