Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- <Author>
- Armon Dadgar
- <Start Date>
- October 21st, 2009
- <Description>
- This module provides a wrapper around the namespace layer, which allows
- namespaces to be dynamically created and manipulated.
- """
- # Used for safety checking
- import safe
- # Used for thread-specific storage
- import threading
- # This is to work around safe...
- safe_compile = compile
- # This is the default context provided to a namespace
- # See _get_default_context_copy()
- DEFAULT_CONTEXT = {}
- # BAD: This should be done properly
- DEFAULT_CONTEXT["thread_local"] = threading.local
- # Initialize the default context with the user functions
- # Wrap this in an if to prevent initialization on every import
- _initialized = False
- # Returns a copy of the default context, properly initialized
- def get_default_context_copy():
- global _initialized
- if not _initialized:
- _initialized = True
- # Use the namespace module to setup the default context
- import namespace
- namespace.wrap_and_insert_api_functions(DEFAULT_CONTEXT)
- # Return a copy of the default context, this allows for
- # isolation of modules using the default context
- copy = DEFAULT_CONTEXT.copy()
- # Insert things that would require a deep copy (since dict.copy() returns a shallow copy)
- # Since mycontext is a dictionary, we need a _separate_ copy rather than a shallow
- # copy which would be a reference to the same dictionary
- copy["mycontext"] = {}
- # Return the copy
- return copy
- # This class is used to represent a namespace
- class VirtualNamespace(object):
- """
- The VirtualNamespace class is used as a wrapper around the
- context and instructions represented by a virtual namespace.
- """
- # Constructor
- def __init__(self, code, name="<string>"):
- """
- <Purpose>
- Initializes the VirtualNamespace class.
- <Arguments>
- code:
- (String) The code to run in the namespace
- name:
- (String, optional) The name to use for the code. When the module is
- being executed, if there is an exception, this name will appear in
- the trace back.
- <Exceptions>
- A safety check is performed on the code, and an exception will be raised
- if the code fails the safety check.
- """
- # Check for the code
- # Do a type check
- if type(code) != str:
- raise Exception, "Code must be a string!"
- if type(name) != str:
- raise Exception, "Name must be a string!"
- # Remove any windows carriage returns
- code = code.replace('\r\n','\n')
- # Do a safety check
- try:
- safe.safe_check(code)
- except Exception, e:
- raise Exception, "Code failed safety check! Error: "+str(e)
- # All good, store the compiled byte code
- self.code = safe_compile(code,name,"exec")
- # Evaluates the virtual namespace
- def evaluate(self,context):
- """
- <Purpose>
- Evaluates the wrapped code within a context.
- <Arguments>
- context: A global context to use when executing the code.
- <Exceptions>
- Any that may be raised by the code that is being evaluated.
- """
- # Call safe_run
- safe.safe_run(self.code, context)
- # Make this available
- DEFAULT_CONTEXT["VirtualNamespace"] = VirtualNamespace
Add Comment
Please, Sign In to add comment