~ubuntu-branches/debian/sid/pyro/sid

« back to all changes in this revision

Viewing changes to examples/shoppingcart/shopserver.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet, Carl Chenet, Jakub Wilk
  • Date: 2010-09-14 01:04:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100914010428-02r7p1rzr7jvw94z
Tags: 1:3.9.1-2
[Carl Chenet]
* revert to 3.9.1-1 package because of the development status 
  of the 4.1 package is unsuitable for stable use
  DPMT svn #8557 revision (Closes: #589172) 
* added debian/source
* added debian/source/format
* package is now 3.0 (quilt) source format
* debian/control
  - Bump Standards-Version to 3.9.1

[Jakub Wilk]
* Add ‘XS-Python-Version: >= 2.5’ to prevent bytecompilation with python2.4
  (closes: #589053).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import time
2
 
import Pyro
3
 
from shoppingcart import ShoppingCart
4
 
 
5
 
class Shop(object):
6
 
    inventory= {
7
 
        "paper"     : 1.25,
8
 
        "bread"     : 1.50,
9
 
        "meat"      : 5.99,
10
 
        "milk"      : 0.80,
11
 
        "fruit"     : 2.65,
12
 
        "chocolate" : 3.99,
13
 
        "pasta"     : 0.50,
14
 
        "sauce"     : 1.20,
15
 
        "vegetables": 1.40,
16
 
        "cookies"   : 1.99,
17
 
        "pizza"     : 3.60,
18
 
        "shampoo"   : 2.22,
19
 
        "whiskey"   : 24.99
20
 
        }
21
 
 
22
 
    customersInStore={}
23
 
    
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
28
 
        cart=ShoppingCart()
29
 
        self.customersInStore[name]=cart
30
 
        return self.__proxyfy(cart)
31
 
    def customers(self):
32
 
        return self.customersInStore.keys()
33
 
    def goods(self):
34
 
        return self.inventory
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):
40
 
        receipt=[]
41
 
        if name:
42
 
            receipt.append("Receipt for %s." % name)
43
 
        receipt.append("Receipt Date: "+time.asctime())
44
 
        total=0.0
45
 
        for item in cart.getContents():
46
 
            price=self.inventory[item]
47
 
            total+=price
48
 
            receipt.append("%13s  %.2f" % (item,price))
49
 
        receipt.append("")
50
 
        receipt.append("%13s  %.2f" % ("total:",total))
51
 
        cart.empty()
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)
63
 
    
64
 
    # utility methods:
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)
72
 
 
73
 
 
74
 
######## main program
75
 
 
76
 
daemon=Pyro.Daemon()
77
 
shop=Shop()
78
 
uri=daemon.register(shop)
79
 
ns=Pyro.locateNS()
80
 
ns.remove("example.shop")
81
 
ns.register("example.shop", uri)
82
 
print "Shop Server is ready."
83
 
daemon.requestLoop()