SUIT + Python, New interface

jredford@lehman.com
Wed, 30 Mar 94 16:01:12 -0500

After taking a look at the recently posted Python interface for SUIT,
I wrote a more object based python interface. At the moment it is
'quite usable' but 'rather incomplete'. It provides most of the
functionality you would need to generate a quick & pretty interface,
but does not include access to the lower level GP calls. I will likely
write a integrated interface for those in the future.

This interface can perhaps best be described by a little sample code:

import suit

def print_data(o):
o.name()
o.classname()

suit.init('sample')
a = suit.object('Press Me','button')
a.callback(print_data)

def cur_val(o):
o.current_value

a = suit.object('a radio','radio buttons')
a.callback(cur_val)
a.add('button one') # Add a button to the radio
a.add('button two')
a.add('button three')
a.current_value = 'button one' # Display this button as pressed

a = suit.object('list','scrollable list')
a.list = ('besuit', 'cosuitor', 'countersuit', 'demisuit') # And so on

suit.beginDisplay()
while 1:
suit.checkAndProcessInput()

Well, upon actually looking at this, I guess it isnt so obvious what
is going on, but hopefully you get the idea.

The functions I have to date implemeted are:

beginDisplay() Begin displaying suit objects
checkAndProcessInput(*time) Wait an event and handle it, or time out
getObjectByName(name) Return a suit object by its suit name
init(progname) Initialize suit
object(name,classname) Create a new suit object
objects() Return a list of all the suit objects

and all objects have these methods:

callback(*func) Set or return the call back function
classname() What is the name of the class of this object
delete() Remove this object from the 'all objects' list.
locked(name,*l) Is this attribute locked, set to l
name() What is the suit name of this object
newattr(name,type,value) Add a new attribute to the object
perm(*name,*p) Is this object/attribute permanent, set to p
properties() Return a tuple of tuples of the object's properties

Additionally, all properties can be referenced as attributes of the
python object refering to the suit object.

a = suit.getObjectByName('some object')
a.label = 'Hello World!'

This would set the label property of the object named 'some object'.

a.foreground_color = ('blue',1)

If a property exists in a parent object but not the object itself, it
will be instantiated in the object at assignment.

a.newattr('my list','SUIT_enum',('a',('a','b','c','d')))
a.my_list
'a'
a.my_list = 'c'

Properties that do not exist in parents must be added with newattr, as
there is no other way to determine the type of the attribute.

The folowing code can be used to dump out the state of SUIT in a
manner similar to the .sui file, such that this can be executed to
restore the state of the application.

import suit
import regsub

ok_types = ("boolean", "double", "GP_color", "int", "SUIT_object", "SUIT_springiness", "SUIT_enum", "SUIT_textList", "text", "viewport")

def dump():
all_objects = suit.objects()
for obj in all_objects:
if obj.perm():
if obj.name() == 'ROOT':
print "object = suit.getObjectByName('"+obj.name()+"')"
else:
print "object = suit.object('"+obj.name()+"','"+obj.classname()+"')"
for prop in obj.properties()[0]:
if obj.perm(prop[0]):
prop_name = regsub.gsub(' ','_',prop[0])
prop_type = prop[1]
if prop_type in ok_types:
if prop_type == "SUIT_enum":
prop_value = eval('obj.'+prop_name)[0]
else:
prop_value = eval('obj.'+prop_name)
print 'object.'+prop_name+' = '+repr(prop_value)
print

This generates output like:

object = suit.getObjectByName('ROOT')
object.animated = 0
object.background_color = ('grey', 0)
object.border_color = ('grey', 1)
object.border_raised = 1
object.border_type = 'motif'
object.border_width = 2
object.clip_to_viewport = 1
object.default_object_height = 80
object.default_object_width = 80
object.draw_border_on_inside = 0
object.foreground_color = ('black', 1)
object.has_background = 1
object.has_border = 1
object.margin = 5
object.show_temporary_properties = 0
object.shrink_to_fit = 0
object.springiness = (1, 1, 1, 1, 1, 1)
object.viewport = ((0, 0), (959, 599))
object.visible = 1

object = suit.object('list','scrollable list')
object.active_display = 'bulletin board'
object.current_row = -1
object.current_value = ''
object.label = ''
object.list = ('besuit', 'cosuitor', 'countersuit', 'demisuit')
object.viewport = ((322, -42), (521, 158))

object = suit.object('Press Me','button')
object.active_display = 'standard'
object.disabled = 0
object.label = 'Press Me'
object.viewport = ((235, 372), (294, 396))

You would save this to foosui.py, and then

import suit
suit.init('foo')
import foosui
suit.beginDisplay()

To restore the graphical display of the application.

Note that the current release does not save all information that is
needed, specifically call backs are not saved, and must be
re-registered:

suit.getObjectByName('my button').callback(my_callback)

So, since I am not at a site you can FTP to, people interested in
checking this code out can send me email asking for a copy of it.

Comments & suggestions as to what people would like to see added are
welcome.

--
John Redford (AKA GArrow) | 3,600 hours of tape.
jredford@lehman.com       | 5 cans of Scotchguard.