3 class VendingException( Exception ): pass
5 FILENAME="/etc/dispense/snacks.conf"
8 toks = l.strip().split()
12 if toks[0][:1] == '#':
16 raise VendingException( "Bad line '%s' in snack config file, too few items" % l )
20 price = int( toks[0] )
22 raise VendingException( "Bad line '%s' in snack config file, bad price" % l)
25 slots = toks[1].split(",")
28 raise VendingException( "Bad line %s' in snack config file, bad slot '%s'" % (l, s) )
30 # shortname for dispense
34 name = ' '.join( toks[3:] )
36 return ( price, slots, shortname, name )
38 def get_snacks( filename = FILENAME ):
43 snacks = filter( bool, map( parse_line, f ) )
45 raise VendingException( e )
48 for price, slots, shortname, name in snacks:
50 ret[s] = (price, shortname, name)
54 def get_snack( slot ):
57 if slot not in snacks:
58 raise VendingException( "Slot '%s' isn't in config file" % slot )
62 def get_price( slot ):
63 p, sn, n = get_snacks( slot )
67 p, sn, n = get_snacks( slot )
70 def get_short_name( slot ):
71 p, sn, n = get_snacks( slot )
74 if __name__ == '__main__':