~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/logic/adapters/Base.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
4
# GNU Enterprise is free software; you can redistribute it
 
5
# and/or modify it under the terms of the GNU General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2, or (at your option) any later version.
 
8
#
 
9
# GNU Enterprise is distributed in the hope that it will be
 
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
 
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 
12
# PURPOSE. See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public
 
15
# License along with program; see the file COPYING. If not,
 
16
# write to the Free Software Foundation, Inc., 59 Temple Place
 
17
# - Suite 330, Boston, MA 02111-1307, USA.
 
18
#
 
19
# Copyright 2001-2005 Free Software Foundation
 
20
#
 
21
# $Id: Base.py 7057 2005-02-23 16:53:58Z johannes $
 
22
 
 
23
import sys
 
24
import traceback
 
25
import string
 
26
 
 
27
from gnue.common.logic.language import ImplementationError, AbortRequest
 
28
from gnue.common.logic.NamespaceCore import NamespaceElement, NamespaceFunction
 
29
 
 
30
 
 
31
# =============================================================================
 
32
# Base class for LanguageAdapters
 
33
# =============================================================================
 
34
 
 
35
class BaseLanguageAdapter:
 
36
  """
 
37
  This is the base class for language adapters. A language adapter has to
 
38
  provide a public function for creation of new execution contexts.
 
39
  """
 
40
 
 
41
  # ---------------------------------------------------------------------------
 
42
  # Create and return a new execution context
 
43
  # ---------------------------------------------------------------------------
 
44
 
 
45
  def createNewContext (self):
 
46
    """
 
47
    Abstract: Create a new execution context.
 
48
    """
 
49
    raise ImplementationError, (self.__class__, 'createNewContext ()')
 
50
 
 
51
 
 
52
 
 
53
# =============================================================================
 
54
# Base class for execution contexts
 
55
# =============================================================================
 
56
 
 
57
class BaseExecutionContext:
 
58
  """
 
59
  An execution context provides an environment where code can be transformed
 
60
  and executed.
 
61
  """
 
62
 
 
63
  # ---------------------------------------------------------------------------
 
64
  # Constructor
 
65
  # ---------------------------------------------------------------------------
 
66
 
 
67
  def __init__ (self, runtime = None):
 
68
    self._runtime    = runtime
 
69
    self.shortname   = "unknown_executioncontext"
 
70
    self.description = "There is no description provided"
 
71
 
 
72
 
 
73
  # ---------------------------------------------------------------------------
 
74
  # Merge a namespace into the execution contexts namespace
 
75
  # ---------------------------------------------------------------------------
 
76
 
 
77
  def defineNamespace (self, addNS, asGlobal = False):
 
78
    """
 
79
    Merge the given namespace @addNS into the execution context. This function
 
80
    is doing this using bindFunction () and bindObject () depeding on the
 
81
    namespace elements type.
 
82
    """
 
83
    for (name, value) in addNS.items ():
 
84
      if name is not None:
 
85
        if isinstance (value, NamespaceFunction):
 
86
          self.bindFunction (name, value, asGlobal)
 
87
 
 
88
        if isinstance (value, NamespaceElement):
 
89
          self.bindObject (name, value, asGlobal)
 
90
 
 
91
 
 
92
  # ---------------------------------------------------------------------------
 
93
  # Bind an object into the contexts namespace using the given name
 
94
  # ---------------------------------------------------------------------------
 
95
 
 
96
  def bindObject (self, name, aObject, asGlobal = False):
 
97
    """
 
98
    Abstract: A descendant overrides this function to bind a given object into
 
99
    the local or global namespace.
 
100
    """
 
101
    raise ImplementationError, (self.__class__, 'bindObject ()')
 
102
 
 
103
 
 
104
  # ---------------------------------------------------------------------------
 
105
  # Bind a function into the contexts namespace using the given name
 
106
  # ---------------------------------------------------------------------------
 
107
 
 
108
  def bindFunction (self, name, aFunction, asGlobal = False):
 
109
    """
 
110
    Abstract: A descendant overrides this function to bind a given function 
 
111
    with into the local or global namespace.
 
112
    """
 
113
    raise ImplementationError, (self.__class__, 'bindFunction ()')
 
114
 
 
115
 
 
116
  # ---------------------------------------------------------------------------
 
117
  # Create a new function instance 
 
118
  # ---------------------------------------------------------------------------
 
119
 
 
120
  def buildFunction (self, name, code, parameters = {}):
 
121
    """
 
122
    Abstract: Create a new instance of a virtual function and prepare it's 
 
123
    code.
 
124
    """
 
125
    raise ImplementationError, (self.__class__, 'buildFunction ()')
 
126
 
 
127
 
 
128
  # ---------------------------------------------------------------------------
 
129
  # Release an execution context
 
130
  # ---------------------------------------------------------------------------
 
131
 
 
132
  def release (self):
 
133
    """
 
134
    Release an execution context: remove references in the namespace and the
 
135
    like.
 
136
    """
 
137
 
 
138
    pass
 
139
 
 
140
 
 
141
 
 
142
# =============================================================================
 
143
# Base class of a virtual function
 
144
# =============================================================================
 
145
 
 
146
class VirtualFunction:
 
147
  """
 
148
  This is the base class of virtual functions. Such an instance must be able to
 
149
  prepare a sourcecode and to execute it.
 
150
  """
 
151
 
 
152
  # ---------------------------------------------------------------------------
 
153
  # Constructor
 
154
  # ---------------------------------------------------------------------------
 
155
 
 
156
  def __init__ (self, context, name, code, parameters):
 
157
    self._context    = context
 
158
    self._name       = name
 
159
    self._code       = code
 
160
    self._parameters = parameters
 
161
    self._prepare ()
 
162
 
 
163
 
 
164
  # ---------------------------------------------------------------------------
 
165
  # Execute the function using the given arguments
 
166
  # ---------------------------------------------------------------------------
 
167
 
 
168
  def execute (self, *args, **params):
 
169
    """
 
170
    Execute the function using the given arguments. A descendant must override
 
171
    this function.
 
172
    """
 
173
    raise ImplementationError, (self.__class__, 'execute ()')
 
174
 
 
175
 
 
176
 
 
177
  # ---------------------------------------------------------------------------
 
178
  # Generate a user requested abort 
 
179
  # ---------------------------------------------------------------------------
 
180
 
 
181
  def requestAbort (self, message):
 
182
    """
 
183
    Abstract: Generate a user abort request.  Linked into the execution
 
184
    namespace so that scripts can request an abort.
 
185
    """
 
186
    raise AbortRequest(message)
 
187
  
 
188
  # ---------------------------------------------------------------------------
 
189
  # redirect a call of an instance to the execute () function.
 
190
  # ---------------------------------------------------------------------------
 
191
 
 
192
  def __call__ (self, *args, **params):
 
193
    """
 
194
    If an instance gets called, redirect this call to the execute () function.
 
195
    """
 
196
    return apply (self.execute, args, params)
 
197
 
 
198
 
 
199
  # ---------------------------------------------------------------------------
 
200
  # Prepare the given source code
 
201
  # ---------------------------------------------------------------------------
 
202
 
 
203
  def _prepare (self):
 
204
    """
 
205
    This function gets called on constructing an instance at could be used by a
 
206
    descendant to prepare the sourcecode, i.e. compile it.
 
207
    """
 
208
    pass
 
209