#!/usr/bin/env python
import bcrypt
import bson.binary
import pymongo

client = pymongo.MongoClient()
accounts = client.mr_plotter.accounts

def disp_help(tokens):
    print "Type one of the following commands and press <Enter> or <Return> to execute it:"
    print " ".join(funcs.keys())

def create_funcs():
    help = disp_help
    
    def adduser(tokens):
        if len(tokens) < 3:
            print "Usage: adduser username password [tag1] [tag2] ..."
            return
        username = tokens[1]
        password = tokens[2]
        phash = bcrypt.hashpw(password, bcrypt.gensalt())
        pbin = bson.binary.Binary(phash, bson.binary.USER_DEFINED_SUBTYPE)
        # ALL accounts have the public tag
        tags = set(tokens[3:])
        tags.add("public")
        accounts.update({"user": username}, {"$set": {"password": pbin, "tags": tuple(tags)}}, upsert = True)
        
    def setpassword(tokens):
        if len(tokens) != 3:
            print "Usage: setpassword username password"
            return
        username = tokens[1]
        password = tokens[2]
        phash = bcrypt.hashpw(password, bcrypt.gensalt())
        pbin = bson.binary.Binary(phash, bson.binary.USER_DEFINED_SUBTYPE)
        accounts.update({"user": username}, {"$set": {"password": pbin}})
            
    def rmuser(tokens):
        if len(tokens) != 2:
            print "Usage: rmuser username"
            return
        accounts.delete_one({"user": tokens[1]})
            
    def addtags(tokens):
        if len(tokens) < 3:
            print "Usage: addtags username tag1 [tag2] [tag3] ..."
            return
        accounts.update({"user": tokens[1]}, {"$addToSet": {"tags": {"$each": tokens[2:]}}})
            
    def rmtags(tokens):
        if len(tokens) < 3:
            print "Usage: rmtags username tag1 [tag2] [tag3] ..."
            return
        # ALL accounts have the public tag
        tags = set(tokens[2:])
        if "public" in tags:
            tags.remove("public")
        accounts.update({"user": tokens[1]}, {"$pullAll": {"tags": tuple(tags)}})
            
    def lstags(tokens):
        if len(tokens) != 2:
            print "Usage: lstags username"
            return
        account = accounts.find_one({"user": tokens[1]})
        print " ".join(account["tags"])
            
    def lsusers(tokens):
        if len(tokens) != 1:
            print "Usage: lsusers"
            return
        for account in accounts.find():
            print account["user"]
            
    def ls(tokens):
        if len(tokens) != 1:
            print "Usage: ls"
            return
        for account in accounts.find():
            print "{0}: {1}".format(account["user"], " ".join(account["tags"]))
        
    def exit(tokens):
        if len(tokens) != 1:
            print "Usage: exit"
            return
        raise SystemExit
        
    def close(tokens):
        if len(tokens) != 1:
            print "Usage: close"
            return
        raise SystemExit
        
    return locals()
    
funcs = create_funcs()

def exec_statement(string):
    tokens = string.split()
    if len(tokens) == 0:
        return
    
    command = tokens[0]
    if command not in funcs:
        exec_func = disp_help
        print "'{0}' is not a valid command".format(command)
    else:
        exec_func = funcs[command]
    
    exec_func(tokens)
    
if __name__ == "__main__":
    while True:
        try:
            statement = raw_input("Mr. Plotter> ")
        except EOFError:
            print
            raise SystemExit
        exec_statement(statement)

@@ -62,16 +62,368 @@
  pymongo
+%0Aimport sys%0A%0Amongohost = %22localhost%22%0Amongoport = 27017%0A%0Aerror = False%0Aif len(sys.argv) == 2:%0A    mongohost = sys.argv%5B1%5D%0Aif len(sys.argv) == 3:%0A    try:%0A        mongoport = int(sys.argv%5B2%5D)%0A    except ValueError:%0A        error = True%0A%0Aif error or len(sys.argv) %3E 3:%0A    print %22Usage: %7B0%7D %5Bmongo_hostname%5D %5Bmongoport%5D%22.format(sys.argv%5B0%5D)%0A    sys.exit()
 %0A%0Aclient
@@ -445,16 +445,36 @@
 oClient(
+mongohost, mongoport
 )%0Aaccoun
@@ -696,20 +696,16 @@
 sp_help%0A
-    
 %0A    def
@@ -1242,24 +1242,16 @@
 = True)%0A
-        
 %0A    def
@@ -1638,36 +1638,24 @@
 d%22: pbin%7D%7D)%0A
-            
 %0A    def rmu
@@ -1807,28 +1807,16 @@
 ns%5B1%5D%7D)%0A
-            
 %0A    def
@@ -2036,36 +2036,24 @@
 ens%5B2:%5D%7D%7D%7D)%0A
-            
 %0A    def rmt
@@ -2397,28 +2397,16 @@
 ags)%7D%7D)%0A
-            
 %0A    def
@@ -2610,28 +2610,16 @@
 tags%22%5D)%0A
-            
 %0A    def
@@ -2793,28 +2793,16 @@
 %22user%22%5D%0A
-            
 %0A    def
@@ -3012,24 +3012,16 @@
 ags%22%5D))%0A
-        
 %0A    def
@@ -3136,32 +3136,24 @@
  SystemExit%0A
-        
 %0A    def clo
@@ -3270,24 +3270,16 @@
 temExit%0A
-        
 %0A    ret
@@ -3291,20 +3291,16 @@
 ocals()%0A
-    
 %0Afuncs =
@@ -3412,20 +3412,16 @@
  return%0A
-    
 %0A    com
@@ -3602,20 +3602,16 @@
 ommand%5D%0A
-    
 %0A    exe
@@ -3625,20 +3625,16 @@
 tokens)%0A
-    
 %0Aif __na

Make accounts.py take MongoDB hostname and port as command line arguments