summaryrefslogtreecommitdiff
path: root/cad/src/utilities/Initialize.py
blob: 2860b5a9b64663f727ac5865118cb4a69a3b83e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

# Copyright 2007 Nanorex, Inc.  See LICENSE file for details.
"""
  Initialize.py

  Routines for supporting initialization functions.

  An initialization function should look like this::

    def initialize():
      if (Initialize.startInitialization(__name__)):
        return
      ... initialization code here ...
      Initialize.endInitialization(__name__)

  This will prevent the initialization code from being run more than
  once.  Circular dependencies among initialization functions are also
  detected.  To break such loops, you can try dividing a single
  initialization function into early and late parts.  If you do this,
  pass the same identifing string as the 'extra' argument to each of
  startInitialization and endInitialization. [QUESTION: do you mean
  the same string in a single initialize function, but different
  strings in each initialize function, or the same string in both
  initialize functions? Guess: the former. [bruce 070702]]

  If you wish your initialization code to be rerun, you can call
  forgetInitialization, which will cause startInitialization with the
  same arguments to return False on its next call.

  Note that nothing in this module calls initialization functions in
  the first place, or helps determine when to call them. You must add
  at least one call to each one to an appropriate place in the code,
  and you are on your own to make sure it is called before its side
  effects were first needed, but not too early to work or be legal.
  (One way is to call it before every attempt to rely on its side
  effects, but this might be inefficient.)

  @author: Eric Messick
  @version: $Id$
  @copyright: 2007 Nanorex, Inc.
  @license: GPL
"""

_RUNNING = "Running"
"Constant for storing in _ms_initializationStatus"

_ms_initializationStatus = {}
"""
Dictionary mapping name+extra to the state of the given function.

Takes on one of three values::

  True:     Function has already completed.
  _RUNNING: Function is currently running.
  False:    Function has not started.

Note that a missing key means the same as False.
"""

_VERBOSE = False

import exceptions

class _InitializationLoop(exceptions.Exception):
    def __init__(self, args):
        self.args = args

def startInitialization(name, extra=""):
    """
    Called at the beginning of each initialization function.

    @param name: which module is being initialized (pass __name__).
    @type name: string

    @param extra: optional.  which function in this module.
    @type extra: string

    @return: True if this function is either running or completed.

    """
    key = name + extra
    currentState = False
    if (_ms_initializationStatus.has_key(key)):
        currentState = _ms_initializationStatus[key]
    if (currentState):
        if (currentState is _RUNNING):
            raise _InitializationLoop, key
        if (_VERBOSE):
            print "initialize recalled: " + key
        return True
    _ms_initializationStatus[key] = _RUNNING
    if (_VERBOSE):
        print "initializing " + key
    return False

def endInitialization(name, extra=""):
    """
    Called at the end of each initialization function.

    @param name: which module is being initialized (pass __name__).
    @type name: string

    @param extra: optional.  which function in this module.
    @type extra: string

    """
    key = name + extra
    if (_VERBOSE):
        print "done initializing: " + key
    _ms_initializationStatus[key] = True

def forgetInitialization(name, extra=""):
    """
    Called to allow an initialization function to run again.

    @param name: which module is being initialized (pass __name__).
    @type name: string

    @param extra: optional.  which function in this module.
    @type extra: string

    """
    key = name + extra
    _ms_initializationStatus[key] = False