X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=sql-edition%2Fservers%2FSnackConfig.py;fp=sql-edition%2Fservers%2FSnackConfig.py;h=e608f8143dda792f829d719804240f7f484d1449;hb=3b9f8220a4e4604a4d05fdbca2ff98719769f1bb;hp=0000000000000000000000000000000000000000;hpb=bf6527c2a53d2cdbc7f721006a62cbff377f9ff8;p=zanchey%2Fdispense2.git diff --git a/sql-edition/servers/SnackConfig.py b/sql-edition/servers/SnackConfig.py new file mode 100755 index 0000000..e608f81 --- /dev/null +++ b/sql-edition/servers/SnackConfig.py @@ -0,0 +1,77 @@ +#!/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