3
from shoppingcart import ShoppingCart
24
def enter(self, name):
25
print "Customer %s enters the store." % name
26
print "Customer takes a shopping cart."
27
# create a cart and return it as a pyro object to the client
29
self.customersInStore[name]=cart
30
return self.__proxyfy(cart)
32
return self.customersInStore.keys()
35
def payByName(self, name):
36
print "Customer %s goes to the counter to pay." % name
37
cart=self.customersInStore[name]
38
return self.payCart(cart, name)
39
def payCart(self,cart,name=None):
42
receipt.append("Receipt for %s." % name)
43
receipt.append("Receipt Date: "+time.asctime())
45
for item in cart.getContents():
46
price=self.inventory[item]
48
receipt.append("%13s %.2f" % (item,price))
50
receipt.append("%13s %.2f" % ("total:",total))
52
return "\n".join(receipt)
53
def leave(self, name):
54
print "Customer %s leaves." % name
55
cart=self.customersInStore[name]
56
print " their shopping cart contains:",cart.getContents()
57
if cart.getContents():
58
print " it is not empty, they are trying to shoplift!"
59
raise Exception("attempt to steal a full cart prevented")
60
# delete the cart and unregister it with pyro
61
del self.customersInStore[name]
62
self.__unproxyfy(cart)
65
def __proxyfy(self, object):
66
"""register the object with the daemon and return a proxy"""
67
uri=self._pyroDaemon.register(object)
68
return Pyro.Proxy(uri)
69
def __unproxyfy(self, object):
70
"""unregister the object with the daemon"""
71
self._pyroDaemon.unregister(object)
78
uri=daemon.register(shop)
80
ns.remove("example.shop")
81
ns.register("example.shop", uri)
82
print "Shop Server is ready."