--- /dev/null
+#!/usr/bin/env python
+
+class VendingException( Exception ): pass
+
+FILENAME="snacks.conf"
+
+def parse_line( l ):
+ toks = l.strip().split()
+ if not len( toks ):
+ return
+
+ if toks[0][:1] == '#':
+ return
+
+ if len( toks ) < 4:
+ raise VendingException( "Bad line '%s' in snack config file, too few items" % l )
+
+ # price
+ try:
+ price = int( toks[0] )
+ except ValueError:
+ raise VendingException( "Bad line '%s' in snack config file, bad price" % l)
+
+ # slots
+ slots = toks[1].split(",")
+ for s in slots:
+ if len(s) != 2:
+ raise VendingException( "Bad line %s' in snack config file, bad slot '%s'" % (l, s) )
+
+ # shortname for dispense
+ shortname = toks[2]
+
+ # name
+ name = ' '.join( toks[3:] )
+
+ return ( price, slots, shortname, name )
+
+def get_snacks( filename = FILENAME ):
+
+ snacks = []
+ try:
+ f = file( filename )
+ snacks = filter( bool, map( parse_line, f ) )
+ except IOError, e:
+ raise VendingException( e )
+
+ ret = {}
+ for price, slots, shortname, name in snacks:
+ for s in slots:
+ ret[s] = (price, shortname, name)
+
+ return ret
+
+def get_snack( slot ):
+
+ snacks = get_snacks()
+ if slot not in key:
+ raise VendingException( "Slot '%s' isn't in config file" % slot )
+
+ return snacks[slot]
+
+def get_price( slot ):
+ p, sn, n = get_snacks( slot )
+ return p
+
+def get_name( slot ):
+ p, sn, n = get_snacks( slot )
+ return n
+
+def get_short_name( slot ):
+ p, sn, n = get_snacks( slot )
+ return sn
+
+if __name__ == '__main__':
+ snacks = get_snacks()
+
+ print snacks
(slot_num, price, slot_name) = c.split(' ', 2)
if slot_name == 'dead': continue
choices += '%s8-%s (%sc) '%(slot_num, slot_name, price)
+
+# we don't want to print snacks for now since it'll be too large
+# and there's physical bits of paper in the machine anyway - matt
+# try:
+# snacks = get_snacks()
+# except:
+# snacks = {}
+#
+# for slot, ( name, price ) in snacks.items():
+# choices += '%s8-%s (%sc) ' % ( slot, name, price )
+
choices += '55-DOOR '
- choices += 'OR A SNACK. '
+ choices += 'OR ANOTHER SNACK. '
choices += '99 TO READ AGAIN. '
choices += 'CHOICE? '
msg.append((choices, False, None))
else:
v.display('GOT COKE!')
else:
- v.display(vstatus.cur_selection+' - $1.00')
- if ((os.system('su - "%s" -c "dispense snack"'%(vstatus.username)) >> 8) == 0):
+ # first see if it's a named slot
+ try:
+ price, shortname, name = get_snack( vstatus.cur_selection )
+ except:
+ price, shortname, name = get_snack( '--' )
+ dollarprice = "$%.2f" % ( price / 100.0 )
+ v.display(vstatus.cur_selection+' - %s'%dollarprice)
+ if ((os.system('su - "%s" -c "dispense %s"'%(vstatus.username, shortname)) >> 8) == 0):
v.vend(vstatus.cur_selection)
v.display('THANK YOU')
else:
--- /dev/null
+# format:
+# all are whitespace seperated, name can have whitespace.
+# price - in cents
+# slots - comma seperated list of slots with this item.
+# - don't put any spaces in this
+# - the magically named '--' slot is the default for
+# unadded slots
+# name - the name, spaces are OK.
+
+# price slots dispensename name
+# eg
+# 550 88,00 5out $5 withdrawal
+ 550 88,00 5out $5 withdrawal
+ 110 -- snack tasty snack