1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: resources |
---|
3 | # |
---|
4 | # This file contains routines to parse settings from the "resources" |
---|
5 | # file specified by the middleware. This file is located via an |
---|
6 | # environment variable as $SESSIONDIR/resources. If this file |
---|
7 | # exists, it is used to load settings that affect the rest of the |
---|
8 | # application. It looks something like this: |
---|
9 | # |
---|
10 | # application_name "My Tool" |
---|
11 | # results_directory /tmp |
---|
12 | # filexfer_port 6593 |
---|
13 | # |
---|
14 | # ====================================================================== |
---|
15 | # AUTHOR: Michael McLennan, Purdue University |
---|
16 | # Copyright (c) 2004-2005 Purdue Research Foundation |
---|
17 | # |
---|
18 | # See the file "license.terms" for information on usage and |
---|
19 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
20 | # ====================================================================== |
---|
21 | package require Itcl |
---|
22 | |
---|
23 | namespace eval Rappture { # forward declaration } |
---|
24 | namespace eval Rappture::resources { |
---|
25 | # |
---|
26 | # Set up a safe interpreter for loading filexfer options... |
---|
27 | # |
---|
28 | variable optionParser [interp create -safe] |
---|
29 | foreach cmd [$optionParser eval {info commands}] { |
---|
30 | $optionParser hide $cmd |
---|
31 | } |
---|
32 | # this lets us ignore unrecognized commands in the file: |
---|
33 | $optionParser invokehidden proc unknown {args} {} |
---|
34 | } |
---|
35 | |
---|
36 | # ---------------------------------------------------------------------- |
---|
37 | # USAGE: Rappture::resources::register ?<name> <proc> ...? |
---|
38 | # |
---|
39 | # Used by other files throughout the Rappture GUI to register their |
---|
40 | # own commands for the resources parser. Creates a command <name> |
---|
41 | # in the resources interpreter, and registers the <proc> to handle |
---|
42 | # that command. |
---|
43 | # ---------------------------------------------------------------------- |
---|
44 | proc Rappture::resources::register {args} { |
---|
45 | variable optionParser |
---|
46 | foreach {name proc} $args { |
---|
47 | $optionParser alias $name $proc |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | # ---------------------------------------------------------------------- |
---|
52 | # USAGE: Rappture::resources::load ?<errorCallback>? |
---|
53 | # |
---|
54 | # Called in the main program to load resources from the file |
---|
55 | # $SESSIONDIR/resources. As a side effect of loading resources, |
---|
56 | # the data is stored in various variables throughout the package. |
---|
57 | # |
---|
58 | # Returns 1 if successful, and 0 otherwise. Any errors are passed |
---|
59 | # along to the <errorCallback>. If not specified, then the default |
---|
60 | # callback pops up a message box. |
---|
61 | # ---------------------------------------------------------------------- |
---|
62 | proc Rappture::resources::load {{callback tk_messageBox}} { |
---|
63 | global env |
---|
64 | variable optionParser |
---|
65 | |
---|
66 | # |
---|
67 | # First, make sure that we've loaded all bits of code that |
---|
68 | # might register commands with the resources interp. These |
---|
69 | # are all in procs named xxx_init_resources. |
---|
70 | # |
---|
71 | global auto_index |
---|
72 | foreach name [array names auto_index *_init_resources] { |
---|
73 | eval $name |
---|
74 | } |
---|
75 | |
---|
76 | # |
---|
77 | # Look for a $SESSIONDIR variable and a file called |
---|
78 | # $SESSIONDIR/resources. If found, then load the settings |
---|
79 | # from that file. |
---|
80 | # |
---|
81 | if {[info exists env(SESSIONDIR)]} { |
---|
82 | set file $env(SESSIONDIR)/resources |
---|
83 | if {![file exists $file]} { |
---|
84 | return 0 |
---|
85 | } |
---|
86 | |
---|
87 | if {[catch { |
---|
88 | set fid [open $file r] |
---|
89 | set info [read $fid] |
---|
90 | close $fid |
---|
91 | $optionParser eval $info |
---|
92 | } result]} { |
---|
93 | if {"" != $callback} { |
---|
94 | after 1 [list $callback -title Error -icon error -message "Error in resources file:\n$result"] |
---|
95 | } |
---|
96 | return 0 |
---|
97 | } |
---|
98 | } |
---|
99 | return 1 |
---|
100 | } |
---|