This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsucli.py
69 lines (60 loc) · 2.07 KB
/
fsucli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from sys import argv
from urllib2 import urlopen
from urllib import urlencode
from json import loads
from pprint import pprint
# Fsuvius CLI
# Made by Benjamin Lannon
# Minor tweaks from Alan Beadle
# Commands:
# python fsucli.py -> lists all accounts.
# python fsucli.py dock <id> <num> -> docks <num> fsu from <id>.
# python fsucli.py dock <id> -> docks 1 fsu from <id>.
# python fsucli.py add <id> <num> -> adds <num> fsu to <id>.
# python fsucli.py add <id> -> adds 1 fsu to <id>.
URL="http://fsuvius.cslabs.clarkson.edu"
class FsuviusError(Exception):
@classmethod
def check(cls, result):
if 'error' in result:
raise cls(result['error']['code'], result['error']['reason'])
def list_accounts():
result = loads(urlopen(URL + "/get").read())
FsuviusError.check(result)
acct_list = result['accounts']
hdr='{aid:<8}{name:30}{balance:10}'.format(aid='ID', name='NAME', balance='BALANCE')
print hdr
print '='*len(hdr)
for acct in acct_list:
print '{u[aid]:<8}{u[name]:30}{u[balance]:10}'.format(u=acct)
def dock_fsu(aid, num):
result = loads(urlopen(URL + '/mod', urlencode({'aid': aid, 'amt': num, 'dock': True})).read())
FsuviusError.check(result)
print("docked {} fsu from ID {}").format(num, aid)
list_accounts()
def add_fsu(aid, num):
result = loads(urlopen(URL + '/mod', urlencode({'aid': aid, 'amt': num})).read())
FsuviusError.check(result)
print("added {} fsu to ID {}").format(num, aid)
list_accounts()
def rename(aid, name):
result = loads(urlopen(URL + '/mv', urlencode({'aid': aid, 'name': name})).read())
FsuviusError.check(result)
print("renamed ID {} to {}").format(aid, name)
list_accounts()
def do_thing():
if len(argv) == 1:
list_accounts()
else:
if len(argv) == 3:
fsu=1
else:
fsu=argv[3]
if argv[1] == "dock":
dock_fsu(argv[2], fsu)
elif argv[1] == "add":
add_fsu(argv[2], fsu)
elif argv[1] == "rename":
rename(argv[2], fsu)
if __name__ == '__main__':
do_thing()