source: branches/nanovis2/gui/scripts/resources.tcl @ 3334

Last change on this file since 3334 was 3334, checked in by ldelgass, 11 years ago

Sync with trunk (merge Rappture 1.2 changes from trunk)

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