70 lines
1.1 KiB
Python
70 lines
1.1 KiB
Python
import config
|
|
|
|
import os, sqlite3
|
|
|
|
class dbObject:
|
|
|
|
def __init__(self, path=config.dbpath):
|
|
self.conn = sqlite3.connect(path)
|
|
self.cur = self.conn.cursor()
|
|
|
|
def closeConnection(self):
|
|
return self.conn.close()
|
|
|
|
def commit(self):
|
|
return self.conn.commit()
|
|
|
|
def executeQuery(self, query):
|
|
return self.cur.execute(query)
|
|
|
|
def GetConnection(self):
|
|
return self.conn
|
|
|
|
def GetCursor(self):
|
|
return self.cur
|
|
|
|
def isInitialized(self):
|
|
try:
|
|
self.executeQuery("SELECT * FROM tweets")
|
|
return True
|
|
except:
|
|
return False
|
|
|
|
|
|
def dbCheck(db):
|
|
if dbInitialized(db):
|
|
return True
|
|
raise ValueError("Provided database file " + path + " is not initialized. Create it using makedb.py or csvdb.py")
|
|
|
|
|
|
def dbHelper(path):
|
|
db = dbObject(path)
|
|
dbCheck(db)
|
|
return db
|
|
|
|
|
|
def dbInitialized(db):
|
|
return db.isInitialized()
|
|
|
|
|
|
def fileExists(path):
|
|
return os.path.isfile(path)
|
|
|
|
|
|
def parseArgs(argv):
|
|
args = []
|
|
path = None
|
|
nextpath = False
|
|
|
|
for a in argv[1:]:
|
|
if nextpath:
|
|
path = a
|
|
nextpath = False
|
|
elif a == "-f":
|
|
if path != None:
|
|
raise ValueError("You can only pass one database file.")
|
|
nextpath = True
|
|
else:
|
|
args += [a]
|
|
|
|
return args, path
|