2
# This file is part of GNU Enterprise.
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.
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.
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.
19
# Copyright 2001-2005 Free Software Foundation
21
# $Id: Base.py 7057 2005-02-23 16:53:58Z johannes $
27
from gnue.common.logic.language import ImplementationError, AbortRequest
28
from gnue.common.logic.NamespaceCore import NamespaceElement, NamespaceFunction
31
# =============================================================================
32
# Base class for LanguageAdapters
33
# =============================================================================
35
class BaseLanguageAdapter:
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.
41
# ---------------------------------------------------------------------------
42
# Create and return a new execution context
43
# ---------------------------------------------------------------------------
45
def createNewContext (self):
47
Abstract: Create a new execution context.
49
raise ImplementationError, (self.__class__, 'createNewContext ()')
53
# =============================================================================
54
# Base class for execution contexts
55
# =============================================================================
57
class BaseExecutionContext:
59
An execution context provides an environment where code can be transformed
63
# ---------------------------------------------------------------------------
65
# ---------------------------------------------------------------------------
67
def __init__ (self, runtime = None):
68
self._runtime = runtime
69
self.shortname = "unknown_executioncontext"
70
self.description = "There is no description provided"
73
# ---------------------------------------------------------------------------
74
# Merge a namespace into the execution contexts namespace
75
# ---------------------------------------------------------------------------
77
def defineNamespace (self, addNS, asGlobal = False):
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.
83
for (name, value) in addNS.items ():
85
if isinstance (value, NamespaceFunction):
86
self.bindFunction (name, value, asGlobal)
88
if isinstance (value, NamespaceElement):
89
self.bindObject (name, value, asGlobal)
92
# ---------------------------------------------------------------------------
93
# Bind an object into the contexts namespace using the given name
94
# ---------------------------------------------------------------------------
96
def bindObject (self, name, aObject, asGlobal = False):
98
Abstract: A descendant overrides this function to bind a given object into
99
the local or global namespace.
101
raise ImplementationError, (self.__class__, 'bindObject ()')
104
# ---------------------------------------------------------------------------
105
# Bind a function into the contexts namespace using the given name
106
# ---------------------------------------------------------------------------
108
def bindFunction (self, name, aFunction, asGlobal = False):
110
Abstract: A descendant overrides this function to bind a given function
111
with into the local or global namespace.
113
raise ImplementationError, (self.__class__, 'bindFunction ()')
116
# ---------------------------------------------------------------------------
117
# Create a new function instance
118
# ---------------------------------------------------------------------------
120
def buildFunction (self, name, code, parameters = {}):
122
Abstract: Create a new instance of a virtual function and prepare it's
125
raise ImplementationError, (self.__class__, 'buildFunction ()')
128
# ---------------------------------------------------------------------------
129
# Release an execution context
130
# ---------------------------------------------------------------------------
134
Release an execution context: remove references in the namespace and the
142
# =============================================================================
143
# Base class of a virtual function
144
# =============================================================================
146
class VirtualFunction:
148
This is the base class of virtual functions. Such an instance must be able to
149
prepare a sourcecode and to execute it.
152
# ---------------------------------------------------------------------------
154
# ---------------------------------------------------------------------------
156
def __init__ (self, context, name, code, parameters):
157
self._context = context
160
self._parameters = parameters
164
# ---------------------------------------------------------------------------
165
# Execute the function using the given arguments
166
# ---------------------------------------------------------------------------
168
def execute (self, *args, **params):
170
Execute the function using the given arguments. A descendant must override
173
raise ImplementationError, (self.__class__, 'execute ()')
177
# ---------------------------------------------------------------------------
178
# Generate a user requested abort
179
# ---------------------------------------------------------------------------
181
def requestAbort (self, message):
183
Abstract: Generate a user abort request. Linked into the execution
184
namespace so that scripts can request an abort.
186
raise AbortRequest(message)
188
# ---------------------------------------------------------------------------
189
# redirect a call of an instance to the execute () function.
190
# ---------------------------------------------------------------------------
192
def __call__ (self, *args, **params):
194
If an instance gets called, redirect this call to the execute () function.
196
return apply (self.execute, args, params)
199
# ---------------------------------------------------------------------------
200
# Prepare the given source code
201
# ---------------------------------------------------------------------------
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.