#!/usr/bin/python

"""
The contents of this file are subject to the Mozilla Public License  
Version 1.1 (the "License"); you may not use this file except in  
compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS"  
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  
License for the specific language governing rights and limitations under  
the License. 

The Original Code is available at http://downloads.xmlschemata.org/python/xvif/

The Initial Developer of the Original Code is Eric van der Vlist. Portions  
created by Eric van der Vlist are Copyright (C) 2002. All Rights Reserved. 

Relax NG is a specification edited by the OASIS RELAX NG Technical Committee:
http://www.oasis-open.org/committees/relax-ng/

This implementation uses the implementation notes written by James Clark:
http://www.thaiopensource.com/relaxng/implement.html

Contributor(s): 

"""

"""

A type library is a module with a set of classes with a "Type" suffix.

The association with the datatype URI is done through the "typeLibraries"
variable of the rng.RngParser class.

The library modules are loaded dynamically and the association between a
type and the class implementing it is done by introspection.


"""

import re, rexec, rngCoreTypeLib

class sandBox(rexec.RExec):
	ok_sys_names = ('copyright', 'version', 'platform', 'maxint', 'hexversion')
	ok_posix_names = ()

	def r_open(self, file, mode='r', buf=-1):
		raise IOError, "Illegal open() mode"


	def addImports(self, params):
		for p in params.keys():
			if p == "import":
				if re.match("from .* import", params[p]):
					self.r_exec(params[p])
				else:
					self.r_exec ("import %s" % params[p])

class _Python:

	def execFacet(self, expression, params={}):
		r_env = sandBox()
		r_env.addImports(params)
		module = r_env.add_module("__main__")
		mod_dict = module.__dict__
		mod_dict["self"] = self
		mod_dict["return_"] = None
		r_env.r_exec(expression)
		return mod_dict["return_"]

	def evalFacet(self, expression, params={}):
		r_env = sandBox()
		r_env.addImports(params)
		module = r_env.add_module("__main__")
		mod_dict = module.__dict__
		mod_dict["self"] = self
		return r_env.r_eval(rngCoreTypeLib.tokenType(expression))

class floatType(float, _Python):
	""" """

class intType(int, _Python):
	""" """
class longType(long, _Python):
	""" """

class strType(str, _Python):
	""" """

class tokenType(rngCoreTypeLib.tokenType, _Python):
	""" """

class unicodeType(unicode, _Python):
	""" """

class complexType(complex, _Python):
	""" """

