Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #from pymongo import *
- from pymongo import MongoClient
- from pymongo.errors import ConnectionFailure
- from monitor import *
- from datetime import *
- ###
- ####
- # CLIENT COMMANDS
- ####
- class muhdb:
- ##anything defined here is GLOBAL TO THE CLASS
- #def __init__(self,host,port, database):
- def __init__(self, host, port, user, password):
- ##anything defined here is local to the object
- self.host = host
- self.port = port
- self.user = user
- self.password = password
- self.connection = MongoClient(self.host, self.port)
- # self.database = self.connection[database]
- # print(self.database)
- def connect(self):
- try:
- self.connection = MongoClient(self.host, self.port)
- print("Connected successfully")
- print(connection)
- return connection
- except ConnectionFailure as confail:
- sys.stderr.write("Could not connect to MongoDB: %s" % confail)
- sys.exit(1)
- def discon(self):
- self.connection.close()
- print("T E RM I N A T e d")
- ##PROOF OF CONCEPT
- def insertpost(self):
- name = input("name: ")
- age = input("age: ")
- db = input("db: ")
- col = input("col: ")
- post = {"name" : name, "age" : age}
- peeps = self.connection[db]
- peeps[col]
- print(self.connection[db])
- print(peeps[col])
- ins_id = peeps[col].insert_one(post).inserted_id
- # print(ins_id)
- def insirc(self, server, channel, speaker, message):
- db = self.connection[server]
- col = db[channel]
- ## XXX Gonna need tuning tbh
- doc = {"server" : server,
- "channel" : channel,
- "speaker" : speaker,
- "time" : datetime.now(),
- "message" : message}
- ins_id = col.insert_one(doc).inserted_id
- print(ins_id)
- def addusera(self, user, pswd):
- self['pippin'].command("createUser", user, pwd=pswd, roles=["readWrite"])
- def adduser(self):
- user = input("username: ")
- pswd = input("password: ")
- db = input("db: ")
- self.connection[db].command("createUser", user, pwd=pswd, roles=["readWrite"])
- def chusergrpa(db, user, role):
- db.command("updateUser", user, roles=[role])
- def chusergrp(db):
- user = input("username: ")
- role = input("roles: ")
- db.command("updateUser", user, roles=[role])
- def currdb(self):
- print(self.database)
- def listdb(self):
- print("DB's:")
- for i in self.connection.list_database_names():
- print(" " + i)
- def dumpcol(self):
- self.listdb()
- database = input("db: ")
- listcol(database)
- col = input("choose col: ")
- for i in self.connection[database][col]():
- print(i)
- def listcol(self):
- database = input("db: ")
- print("Collections: ")
- for i in self.connection[database].collection_names():
- print(" " + i)
- def dropdb(self): # must be string or db
- #XXX NEEDS ERROR HANDLING IN THE TRY
- db = input("Which un didja wanna kill? ")
- try:
- print(self.connection.drop_database(db))
- print("Dropped " + db)
- except ConnectionFailure as confail:
- sys.stderr.write("Could not drop: %s" % confail)
- def find_doc(self): # must be string or db
- print("something")
- def dropdbl(self): # must be string or db
- self.listdb()
- self.dropdb()
- self.listdb()
- def comlogger(self):
- con = MongoClient(event_listeners=[CommandLogger()])
- print(con)
- def servlogger(self):
- con = MongoClient(event_listeners=[ServerLogger()])
- print(con)
- def toplogger(self):
- con = MongoClient(event_listeners=[TopologyLogger()])
- print(con)
- def heartlogger(self):
- con = MongoClient(event_listeners=[HeartbeatLogger()])
- print(con)
- def add(con):
- return con
- #########################33
- ## collections
- #########################33
- # def listcol(db):
- # for i in db.list_collection_names():
- # print("Collections: ")
- # print(" " + i)
- def add(con):
- print(con.address())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement