Initial import
[uccvend-vendserver.git] / sql-edition / clients / dispense.py
diff --git a/sql-edition/clients/dispense.py b/sql-edition/clients/dispense.py
new file mode 100755 (executable)
index 0000000..00310b5
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/python
+
+import sys, os, re
+from pyPgSQL import PgSQL
+
+if __name__ == '__main__':
+       db = PgSQL.connect(user = 'anondispense', host = 'dispense', database = 'dispense')
+       cursor = db.cursor()
+       user = os.environ['USER']
+
+       if len(sys.argv) > 1:
+               # We have a request. Pass it to dispense by calling
+               # SELECT do_request(username, service, params as array)
+               service = sys.argv[1]
+               params = sys.argv[2:]
+               if params != []:
+                       paramstr = 'array['
+                       for i in params:
+                               paramstr += '%s,'
+                       paramstr = paramstr.rstrip(',') + ']'
+               else:
+                       paramstr = 'NULL'
+               cursor.execute('SELECT do_request(%%s, %%s, %s)'%paramstr, [user, service]+params)
+               result = cursor.fetchone()[0]
+               if result == True:
+                       print 'All good'
+               else:
+                       print 'Eeeep!'
+       else:
+               cursor.execute('SELECT * FROM get_services(%s)', user)
+               print '%-10s %-50s %-10s %s' % ('Provider', 'Item', 'Cost', 'Stock')
+               print '-------------------------------------------------------------------------------'
+               for item in cursor.fetchall():
+                       print '%10s '%item[0],
+                       if item[3] is None:
+                               item[3] = ''
+                       print '%-50s'%(item[1]+' '+item[3]),
+                       if item[2] is None:
+                               print '%-10s' % '',
+                       else:
+                               print '%-10d' % int(item[2]),
+                       if item[4] is None:
+                               item[4] = 'N/A'
+                       print '%s'%item[4]
+       cursor.execute('SELECT * FROM get_balance(%s)', user);
+       [_, cents,bytes] = cursor.fetchone()
+       print user, 'has', int(cents), 'cents and', int(bytes), 'bytes'
+       db.commit()

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