123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- # coding: utf8
- import MySQLdb
- class noaMysql(object):
- def __init__(self, credentials):
- credentials['use_unicode'] = True
- self.__conn = MySQLdb.connect(**credentials)
- self.__swearwords = []
- self.__quotes = []
- self.__users = {
- "admin" : [],
- "moderator" : [],
- "privileged" : [],
- "default" : []
- }
- self.__loadData()
- def __loadData(self):
- """
- Get data from MySQL tables
- """
- #Load users privileges
- cursor = self.__conn.cursor(MySQLdb.cursors.DictCursor)
- cursor.execute("SELECT * FROM users")
- for row in cursor.fetchall():
- self.__users[row['role']].append(row['nickname'])
-
- # Load swearwords
- cursor.execute("SELECT * FROM swearwords")
- for row in cursor.fetchall():
- self.__swearwords.append(row['swearword'])
- # Load quotes
- cursor = self.__conn.cursor(MySQLdb.cursors.DictCursor)
- cursor.execute("SELECT * FROM quotes")
- for row in cursor.fetchall():
- self.__quotes.append(row['quote'])
- #######################################
- def addSwearword(self, swearword):
- """
- Append a new swearword into database
- """
- cursor = self.__conn.cursor()
- cursor.execute('INSERT INTO swearwords (swearword) VALUES("%s")' % (swearword))
- self.__conn.commit()
- self.__swearwords.append(swearword)
- def getSwearwords(self):
- """
- Returns ban words
- """
- return self.__swearwords
- def delSwearword(self, swearword):
- """
- Remove ban word from database
- """
- try:
- index = self.__swearwords.index(swearword)
- except ValueError:
- return None
-
- cursor = self.__conn.cursor()
- cursor.execute('DELETE FROM swearwords WHERE swearword = "%s"' % (swearword))
- self.__conn.commit()
- del self.__swearwords[index]
- return index
- #######################################
- def addQuote(self, quote):
- """
- Append a new swearword into database
- """
- cursor = self.__conn.cursor()
- cursor.execute('INSERT INTO quotes (quote) VALUES("%s")' % (quote))
- self.__conn.commit()
- self.__quotes.append(quote)
- def getQuote(self, index):
- """
- Returns quotes
- """
- try:
- return self.__quotes[index]
- except IndexError:
- return None
- #######################################
- def getUsers(self):
- """
- Return users
- """
- return self.__users
|