|
From: Kevin A. <al...@se...> - 2001-10-18 18:16:50
|
Resource files are an old idea, but they are not widely used in the Python
community. My own experience with resources is based largely on how Mac
applications used the resource fork. PythonCard attempts to isolate the
window, widget layout, and menu descriptions from the source code by putting
them in a separate .rsrc.py file. This allows the layout to be modified
independently of the source code and logic of the application. It also
simplifies supporting multiple layouts for different OS platforms or doing
international versions of an application. Finally, it means that a
non-programmer can handle the layout since they do not need to write any
Python code.
The resource file typically shares the same base name as the main
application, so the minimal sample has two files:
minimal.py
minimal.rsrc.py
The resource file has a .py extension because the resource file is actually
a valid Python dictionary and so it is often easiest to edit and validate
the resource file using a Python-aware editor. The files are plain text.
They are not in XML format since a separate program would be necessary to
validate the XML and I feel that XML is less human-readable than the
equivelant nested dictionary/list done using Python syntax. Binary data
necessary for an application such as images and sound files are stored
separately. Images necessary for the application widgets are referenced via
'image' or 'file' attributes in the resource file.
There is currently a circular type of reference between the main source app
and the resource file. When an application like 'minimal.py' starts,
'.rsrc.py' is appended to the name of the application and passed to the
PythonCardApp class to load and start the real application launch. This is
shown by the code below, which is common to all the samples.
if __name__ == '__main__':
base, ext = os.path.splitext(sys.argv[0])
filename = base + ".rsrc.py"
configOptions()
app = PythonCardPrototype.model.PythonCardApp(filename)
app.MainLoop()
The resource file such as minimal.rsrc.py has a reference back to the source
file as shown below
...
{ 'type':'Background',
'file':'minimal.py',
'classname':'Minimal',
...
Only the base part of the name (e.g. 'minimal') is actually used since the
framework will match both a .py and .pyw file. The circular reference is
actually bad design and should probably be changed. One obvious change would
be to simply remove the 'file' attribute, since like the code above, it
could be determined at runtime as long as we follow the convention of a
common base name for a resource file and the main source that goes with it.
The full organization of the resource file is defined in spec.py. When a
resource is read in (see res.py) it is validated with spec.py, default
arguments are initialized and any undefined component types in the resource
will cause a runtime error. The resource dictionary is converted to an
object that supports dot notation automatically and most places in the
framework where a resource argument is used, it is valid to pass in a
dictionary instead.
The resourceEditor sample is a simplistic editor for the .rsrc.py files. It
is not a full-featured layout editor and you still need to edit the .rsrc.py
file by hand to change anything other than the widget layout and attributes.
However, it is possible to have a fairly short edit/run cycle by keeping the
layout of your app open in the resourceEditor sample and the source code
open in your favorite editor/IDE and then launching the app from the
command-line or a directory browser (Explorer) as you make changes.
There is currently no support for doing a PythonCard application without a
resource file. You can define all of the Background components (widgets) in
source code, but the resource is required to define the basic app and
background window attributes and menus. I'll describe dynamic widget
creation in another message.
ka
|