[Zope3-checkins] CVS: Zope3/src/zope/security/examples - sandbox.py:1.1.2.2 sandbox_security.py:1.1.2.3
Tim Peters
tim.one@comcast.net
Tue, 24 Dec 2002 21:21:48 -0500
Update of /cvs-repository/Zope3/src/zope/security/examples
In directory cvs.zope.org:/tmp/cvs-serv19240/src/zope/security/examples
Modified Files:
Tag: NameGeddon-branch
sandbox.py sandbox_security.py
Log Message:
Whitespace normalization, via Python's Tools/scripts/reindent.py. The
files are fixed-points of that script now. Fixed a few cases where
code relied on significant trailing whitespace (ouch).
=== Zope3/src/zope/security/examples/sandbox.py 1.1.2.1 => 1.1.2.2 ===
--- Zope3/src/zope/security/examples/sandbox.py:1.1.2.1 Mon Dec 23 14:33:16 2002
+++ Zope3/src/zope/security/examples/sandbox.py Tue Dec 24 21:21:17 2002
@@ -11,7 +11,7 @@
after a few turns of the time generator
(think turn based games).
"""
-
+
def action():
" agent performs their action "
def setHome(self, home):
@@ -20,14 +20,14 @@
" where does this agent live "
def getAuthenticationToken(self):
" by what authority should the agent perform actions "
-
+
class IService(Interface):
"""
marker interface. services are available from sandboxes,
examples include time service, agent discovery, and sandbox
discovery.
"""
-
+
class ISandbox(Interface):
"""
a container for agents and services.
@@ -58,33 +58,33 @@
see IAgent doc
"""
__implements__ = IAgent
-
+
def __init__(self, id, home, auth_token, action):
self.id = id
self.auth_token = auth_token
self.home = home
self._action = action
-
+
def action(self):
self._action(self, self.getHome())
-
+
def setHome(self, home):
self.home = home
-
+
def getHome(self):
return self.home
-
+
def getAuthenticationToken(self):
return self.auth_token
class SandboxError(Exception): pass
-
+
class Sandbox(Identity):
"""
see ISandbox doc
"""
__implements__ = ISandbox
-
+
def __init__(self, id, service_factories):
self.id = id
self._services = {}
@@ -112,20 +112,20 @@
agent.setHome(self)
else:
raise SandboxError("couldn't add agent %s"%agent)
-
+
def addService(self, service):
-
+
if not self._services.has_key(service.getId()) \
and IService.isImplementedBy(service):
self._services[service.getId()]=service
service.setHome(self)
else:
raise SandboxError("couldn't add service %s"%service)
-
+
def transportAgent(self, agent, destination):
if self._agents.has_key(agent.getId()) \
and destination is not self \
- and ISandbox.isImplementedBy(destination):
+ and ISandbox.isImplementedBy(destination):
destination.addAgent(agent)
del self._agents[agent.getId()]
else:
@@ -141,7 +141,7 @@
self._home = home
def getHome(self):
return getattr(self, '_home')
-
+
class HomeDiscoveryService(Service):
"""
returns the ids of available agent homes
@@ -168,7 +168,7 @@
AgentDiscoveryService,
TimeService
)
-
+
def action_find_homes(agent, home):
home_service = home.getService('HomeDiscoveryService')
return home_service.getAvailableHomes()
@@ -197,7 +197,7 @@
def turn(self):
global _homes
-
+
for h in _homes.values():
agents = h.getAgents()
for a in agents:
@@ -212,12 +212,12 @@
for a in agents:
try:
self.setupAgent(a)
- home = a.getHome()
+ home = a.getHome()
new_home = GreenerPastures(a)
home.transportAgent(a, new_home)
except Exception, e:
- print 'moving', a, h, new_home, e
-
+ print 'moving', a, h, new_home, e
+
def WanderLust(agent):
""" is agent ready to move """
if int(whrandom.random()*100) <= 30:
@@ -254,14 +254,14 @@
Agent('thor', None, 'norse legend', action_find_homes),
Agent('thucydides', None, 'greek men', action_find_time),
Agent('archimedes', None, 'greek men', action_find_neighbors),
- Agent('prometheus', None, 'greek men', action_find_homes),
+ Agent('prometheus', None, 'greek men', action_find_homes),
]
for a in agents:
origin.addAgent(a)
-def main():
+def main():
world = TimeGenerator()
for x in range(5):
=== Zope3/src/zope/security/examples/sandbox_security.py 1.1.2.2 => 1.1.2.3 ===
--- Zope3/src/zope/security/examples/sandbox_security.py:1.1.2.2 Tue Dec 24 07:51:29 2002
+++ Zope3/src/zope/security/examples/sandbox_security.py Tue Dec 24 21:21:17 2002
@@ -28,14 +28,14 @@
NoSetAttr = lambda name: NotAllowed
#################################
-# location -> auth token -> permission mapping
+# location -> auth token -> permission mapping
class SimulationSecurityDatabase:
-
+
origin = {
'any':[ALL]
}
-
+
jail = {
'norse legend':[TransportAgent,
AccessServices,
@@ -43,9 +43,9 @@
AccessHomeService,
TransportAgent,
AccessAgents,],
-
+
'any':[AccessTimeService, AddAgent],
-
+
}
valhalla = {
@@ -58,16 +58,16 @@
AccessAgents,]
}
-
+
class SimulationSecurityPolicy:
__implements__ = ISecurityPolicy
-
+
def checkPermission(self, permission, object, context):
token = context.user.getAuthenticationToken()
home = object.getHome()
db = getattr(SimulationSecurityDatabase, home.getId(), None)
-
+
if db is None:
return False
@@ -78,9 +78,9 @@
allowed = db.get(token, ())
if permission in allowed:
return True
-
+
return False
-
+
def PermissionMapChecker(permissions_map={}, setattr_permission_func=NoSetAttr):
@@ -116,19 +116,19 @@
aservice_security = { AccessAgentService:['getLocalAgents'] }
agent_service_checker = PermissionMapChecker(aservice_security)
-
+
def wire_security():
from zope.security import securitymanagement
securitymanagement.setSecurityPolicy(SimulationSecurityPolicy())
-
+
import zope.security.examples.sandbox
- Checker.defineChecker(sandbox.Sandbox, sandbox_checker)
+ Checker.defineChecker(sandbox.Sandbox, sandbox_checker)
Checker.defineChecker(sandbox.TimeService, time_service_checker)
Checker.defineChecker(sandbox.AgentDiscoveryService, agent_service_checker)
Checker.defineChecker(sandbox.HomeDiscoveryService, home_service_checker)
-
+
def addAgent(self, agent):
if not self._agents.has_key(agent.getId()) \
and sandbox.IAgent.isImplementedBy(agent):
@@ -137,15 +137,15 @@
wrapped_home = checker.proxy(self)
agent.setHome(wrapped_home)
else:
- raise SandboxError("couldn't add agent %s"%agent)
+ raise SandboxError("couldn't add agent %s"%agent)
sandbox.Sandbox.addAgent = addAgent
-
+
def setupAgent(self, agent):
SecurityManagement.newSecurityManager(agent)
sandbox.TimeGenerator.setupAgent = setupAgent
-
+
def GreenerPastures(agent):
""" where do they want to go today """
import whrandom
@@ -159,5 +159,5 @@
if __name__ == '__main__':
- wire_security()
+ wire_security()
sandbox.main()