Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
Bastion Namespace Reference

Data Structures

class  BastionClass
 

Functions

def Bastion
 

Variables

list __all__ = ["BastionClass", "Bastion"]
 
 sum
 

Detailed Description

Bastionification utility.

A bastion (for another object -- the 'original') is an object that has
the same methods as the original but does not give access to its
instance variables.  Bastions have a number of uses, but the most
obvious one is to provide code executing in restricted mode with a
safe interface to an object implemented in unrestricted mode.

The bastionification routine has an optional second argument which is
a filter function.  Only those methods for which the filter method
(called with the method name as argument) returns true are accessible.
The default filter method returns true unless the method name begins
with an underscore.

There are a number of possible implementations of bastions.  We use a
'lazy' approach where the bastion's __getattr__() discipline does all
the work for a particular method the first time it is used.  This is
usually fastest, especially if the user doesn't call all available
methods.  The retrieved methods are stored as instance variables of
the bastion, so the overhead is only occurred on the first use of each
method.

Detail: the bastion class has a __repr__() discipline which includes
the repr() of the original object.  This is precomputed when the
bastion is created.

Function Documentation

def Bastion.Bastion (   object,
  filter = lambda name: name[:1] != '_',
  name = None,
  bastionclass = BastionClass 
)
Create a bastion for an object, using an optional filter.

See the Bastion module's documentation for background.

Arguments:

object - the original object
filter - a predicate that decides whether a function name is OK;
         by default all names are OK that don't start with '_'
name - the name of the object; default repr(object)
bastionclass - class used to create the bastion; default BastionClass

Definition at line 85 of file Bastion.py.

References audiodev.__init__(), and fnmatch.filter().

85 
86  name=None, bastionclass=BastionClass):
87  """Create a bastion for an object, using an optional filter.
88 
89  See the Bastion module's documentation for background.
90 
91  Arguments:
92 
93  object - the original object
94  filter - a predicate that decides whether a function name is OK;
95  by default all names are OK that don't start with '_'
96  name - the name of the object; default repr(object)
97  bastionclass - class used to create the bastion; default BastionClass
98 
99  """
100 
101  # Note: we define *two* ad-hoc functions here, get1 and get2.
102  # Both are intended to be called in the same way: get(name).
103  # It is clear that the real work (getting the attribute
104  # from the object and calling the filter) is done in get1.
105  # Why can't we pass get1 to the bastion? Because the user
106  # would be able to override the filter argument! With get2,
107  # overriding the default argument is no security loophole:
108  # all it does is call it.
109  # Also notice that we can't place the object and filter as
110  # instance variables on the bastion object itself, since
111  # the user has full access to all instance variables!
112 
113  def get1(name, object=object, filter=filter):
114  """Internal function for Bastion(). See source comments."""
115  if filter(name):
116  attribute = getattr(object, name)
117  if type(attribute) == MethodType:
118  return attribute
119  raise AttributeError, name
120 
121  def get2(name, get1=get1):
122  """Internal function for Bastion(). See source comments."""
123  return get1(name)
124 
125  if name is None:
126  name = `object`
127  return bastionclass(get2, name)
128 

Variable Documentation

list __all__ = ["BastionClass", "Bastion"]

Definition at line 29 of file Bastion.py.

sum

Definition at line 133 of file Bastion.py.