Updated config locations for new machine (and MIFARE serial port)
[zanchey/dispense2.git] / sql-edition / servers / SnackConfig.py
1 #!/usr/bin/env python
2
3 class VendingException( Exception ): pass
4
5 FILENAME="/etc/dispense2/snacks.conf"
6
7 def parse_line( l ):
8         toks = l.strip().split()
9         if not len( toks ):
10                 return
11
12         if toks[0][:1] == '#':
13                 return
14
15         if len( toks ) < 4:
16                 raise VendingException( "Bad line '%s' in snack config file, too few items" % l )
17
18         # price
19         try:
20                 price = int( toks[0] )
21         except ValueError:
22                 raise VendingException( "Bad line '%s' in snack config file, bad price" % l)
23
24         # slots
25         slots = toks[1].split(",")
26         for s in slots:
27                 if len(s) != 2:
28                         raise VendingException( "Bad line %s' in snack config file, bad slot '%s'" % (l, s) )
29         
30         # shortname for dispense
31         shortname = toks[2]
32
33         # name
34         name = ' '.join( toks[3:] )
35
36         return ( price, slots, shortname, name )
37
38 def get_snacks( filename = FILENAME ):
39
40         snacks = []
41         try:
42                 f = file( filename )
43                 snacks = filter( bool, map( parse_line, f ) )
44         except IOError, e:
45                 raise VendingException( e )
46
47         ret = {}
48         for price, slots, shortname, name in snacks:
49                         for s in slots:
50                                         ret[s] = (price, shortname, name)
51
52         return ret
53
54 def get_snack( slot ):
55         
56         snacks = get_snacks()
57         if slot not in snacks:
58                 raise VendingException( "Slot '%s' isn't in config file" % slot )
59         
60         return snacks[slot]
61
62 def get_price( slot ):
63                 p, sn, n = get_snacks( slot )
64                 return p
65
66 def get_name( slot ):
67                 p, sn, n = get_snacks( slot )
68                 return n
69
70 def get_short_name( slot ):
71                 p, sn, n = get_snacks( slot )
72                 return sn
73
74 if __name__ == '__main__':
75         snacks = get_snacks()
76
77         print snacks

UCC git Repository :: git.ucc.asn.au