import MySQLdb import cPickle as pickle import os import MySQLdb.cursors class aasql: def __init__(self, data={}): self._dir = os.getcwd() self.settings = 'settings' try: self.data = self._loadData(self.settings) except: # couln't unpickle? file might not exist ... # what about wrong permissions ? self.data = {} self.data.update(data) if not "HOST" in self.data: self.data["HOST"] = 'localhost' if not "PASSWD" in self.data: self.data["PASSWD"] = '' # Ok, I could put a try here, but any sort of error could happen!? try: self.db = MySQLdb.connect(host = self.data["HOST"], db = self.data["DB"], user = self.data["USER"], passwd = self.data["PASSWD"], cursorclass = MySQLdb.cursors.DictCursor) # A connection was made? Everything went OK? Lets save these settings then self._saveData(self.settings, self.data) except: raise def _makeFilename(self, name): return os.path.join(self._dir, str(name)) def _loadData(self, oid): f = file(self._makeFilename(str(oid)), 'rb') return pickle.load(f) def _saveData(self, oid, data): f = file(self._makeFilename(str(oid)), 'wb') pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)