Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #from pymongo import *
  2. from pymongo import MongoClient
  3. from pymongo.errors import ConnectionFailure
  4. from monitor import *
  5. from datetime import *
  6.  
  7. ###
  8. ####
  9. # CLIENT COMMANDS
  10. ####
  11.  
  12. class muhdb:
  13.  
  14. ##anything defined here is GLOBAL TO THE CLASS
  15. #def __init__(self,host,port, database):
  16.  
  17. def __init__(self, host, port, user, password):
  18. ##anything defined here is local to the object
  19. self.host = host
  20. self.port = port
  21. self.user = user
  22. self.password = password
  23. self.connection = MongoClient(self.host, self.port)
  24. # self.database = self.connection[database]
  25. # print(self.database)
  26.  
  27. def connect(self):
  28. try:
  29. self.connection = MongoClient(self.host, self.port)
  30. print("Connected successfully")
  31. print(connection)
  32. return connection
  33. except ConnectionFailure as confail:
  34. sys.stderr.write("Could not connect to MongoDB: %s" % confail)
  35. sys.exit(1)
  36.  
  37. def discon(self):
  38. self.connection.close()
  39. print("T E RM I N A T e d")
  40.  
  41. ##PROOF OF CONCEPT
  42. def insertpost(self):
  43. name = input("name: ")
  44. age = input("age: ")
  45. db = input("db: ")
  46. col = input("col: ")
  47.  
  48. post = {"name" : name, "age" : age}
  49.  
  50. peeps = self.connection[db]
  51. peeps[col]
  52. print(self.connection[db])
  53. print(peeps[col])
  54. ins_id = peeps[col].insert_one(post).inserted_id
  55. # print(ins_id)
  56.  
  57. def insirc(self, server, channel, speaker, message):
  58.  
  59. db = self.connection[server]
  60. col = db[channel]
  61. ## XXX Gonna need tuning tbh
  62. doc = {"server" : server,
  63. "channel" : channel,
  64. "speaker" : speaker,
  65. "time" : datetime.now(),
  66. "message" : message}
  67.  
  68. ins_id = col.insert_one(doc).inserted_id
  69. print(ins_id)
  70.  
  71.  
  72. def addusera(self, user, pswd):
  73. self['pippin'].command("createUser", user, pwd=pswd, roles=["readWrite"])
  74.  
  75. def adduser(self):
  76. user = input("username: ")
  77. pswd = input("password: ")
  78. db = input("db: ")
  79. self.connection[db].command("createUser", user, pwd=pswd, roles=["readWrite"])
  80.  
  81. def chusergrpa(db, user, role):
  82. db.command("updateUser", user, roles=[role])
  83.  
  84. def chusergrp(db):
  85. user = input("username: ")
  86. role = input("roles: ")
  87. db.command("updateUser", user, roles=[role])
  88.  
  89. def currdb(self):
  90. print(self.database)
  91.  
  92.  
  93. def listdb(self):
  94. print("DB's:")
  95. for i in self.connection.list_database_names():
  96. print(" " + i)
  97.  
  98. def dumpcol(self):
  99. self.listdb()
  100. database = input("db: ")
  101. listcol(database)
  102. col = input("choose col: ")
  103. for i in self.connection[database][col]():
  104. print(i)
  105.  
  106. def listcol(self):
  107. database = input("db: ")
  108. print("Collections: ")
  109. for i in self.connection[database].collection_names():
  110. print(" " + i)
  111.  
  112. def dropdb(self): # must be string or db
  113. #XXX NEEDS ERROR HANDLING IN THE TRY
  114. db = input("Which un didja wanna kill? ")
  115. try:
  116. print(self.connection.drop_database(db))
  117. print("Dropped " + db)
  118. except ConnectionFailure as confail:
  119. sys.stderr.write("Could not drop: %s" % confail)
  120.  
  121. def find_doc(self): # must be string or db
  122. print("something")
  123.  
  124. def dropdbl(self): # must be string or db
  125. self.listdb()
  126. self.dropdb()
  127. self.listdb()
  128.  
  129. def comlogger(self):
  130. con = MongoClient(event_listeners=[CommandLogger()])
  131. print(con)
  132.  
  133. def servlogger(self):
  134. con = MongoClient(event_listeners=[ServerLogger()])
  135. print(con)
  136.  
  137. def toplogger(self):
  138. con = MongoClient(event_listeners=[TopologyLogger()])
  139. print(con)
  140.  
  141. def heartlogger(self):
  142. con = MongoClient(event_listeners=[HeartbeatLogger()])
  143. print(con)
  144.  
  145.  
  146. def add(con):
  147. return con
  148.  
  149. #########################33
  150. ## collections
  151. #########################33
  152.  
  153. # def listcol(db):
  154. # for i in db.list_collection_names():
  155. # print("Collections: ")
  156. # print(" " + i)
  157.  
  158. def add(con):
  159. print(con.address())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement