From 08a392617c464e6336d47ba37168c3a3a2529123 Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@ucc.asn.au>
Date: Wed, 4 May 2011 14:35:06 +0000
Subject: [PATCH] Updated snack config to use `dispense iteminfo` for prices
 (goodbye config file)

---
 sql-edition/servers/SnackConfig.py | 81 +++++++++---------------------
 1 file changed, 25 insertions(+), 56 deletions(-)

diff --git a/sql-edition/servers/SnackConfig.py b/sql-edition/servers/SnackConfig.py
index 118cacd..2314074 100755
--- a/sql-edition/servers/SnackConfig.py
+++ b/sql-edition/servers/SnackConfig.py
@@ -2,62 +2,33 @@
 
 class VendingException( Exception ): pass
 
-FILENAME="/etc/dispense2/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
+import subprocess
+import os, re
 
 def get_snack( slot ):
 	
-	snacks = get_snacks()
-	if slot not in snacks:
-		raise VendingException( "Slot '%s' isn't in config file" % slot )
-	
-	return snacks[slot]
+	if slot == "--":
+		return (0, 'nothing', 'Nothing')
+	cmd = 'dispense iteminfo snack:%s' % slot
+#	print 'cmd = %s' % cmd
+	try:
+#		info = subprocess.check_output(["dispense","iteminfo",'snack:%s'%slot])
+		raw = os.popen(cmd)
+		info = raw.read()
+		raw.close()
+#		print 'cmd (2) = %s' % cmd
+#		print 'info = "%s"' % info
+		m = re.match("\s*[a-z]+:\d+\s+(\d+)\.(\d\d)\s+([^\n]+)", info)
+		val = ( int(m.group(1))*100 + int(m.group(2)), m.group(3), m.group(3) )
+#		print 'Price: %i, Name: %s' % (val[0], val[1])
+	except BaseException as e:
+		print "BaseException"
+		print e
+		val = (0, 'error', 'Error')
+	except:
+		print "Unknown exception"
+		val = (0, 'error', 'Error')
+	return val
 
 def get_price( slot ):
 		p, sn, n = get_snacks( slot )
@@ -72,6 +43,4 @@ def get_short_name( slot ):
 		return sn
 
 if __name__ == '__main__':
-	snacks = get_snacks()
-
-	print snacks
+	print "Don't run this"
-- 
GitLab