def __init__(self): """Creates a new instance of the DatabaseWrapper. This function starts a new database engine and ensures that the database has been initialized. """ self.db_path = config.get("General", "database_path") self.init_database()
def __init__(self, module_name, session=None): """Creates a logging instance that will write messages to a log file with the specified module_name, as well as to the console. If the messages are of log level WARNING, ERROR, or CRITICAL, they will also be written to the log_entries database table. If the application is backed by SQLite, it's a good idea to specify a database session when the object is created by a thread that might log error messages while writing to the database, because SQLite locks the entire file on writes, which can block attempts to log to the database if the thread that is calling the log object is using a separate database transaction. Note that if a session is specified, it is the responsibility of the calling class to clean it up. """ # set up logging self.log = logging.getLogger(module_name) self.log.setLevel(logging.DEBUG) # dictate log message formatting file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') cls_formatter = logging.Formatter('%(levelname)s:%(name)s - %(message)s') # only log to the console if configured to do so if config.get("Logging", "log_to_console") == "true": # create a handler that dumps log messages to the console ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) ch.setFormatter(cls_formatter) self.log.addHandler(ch) # only log to file if configured to do so if config.get("Logging", "log_to_files") == "true": # confirm that logging directory exists log_dir = config.get("Logging", "log_directory_path") if not os.path.isdir(log_dir): print u"Creating log directory %s" % log_dir os.mkdir(log_dir) # create a handler that dumps log messages to an appropriately named file log_path = os.path.abspath(os.path.join(log_dir, module_name + '.log')) fs = logging.FileHandler(log_path) fs.setLevel(logging.DEBUG) fs.setFormatter(file_formatter) self.log.addHandler(fs) # create a handler that logs to the database. By default, it logs anything WARNING, ERROR, and CRITICAL sqa = SqlAlchemyLogger(module_name, session) self.log.addHandler(sqa)
def password_hash(self, username, password): """Hashes the specified username and password and returns the result. The resulting hash can be stored in or compared to the passhash column of the User table.""" hash = hashlib.sha512() hash.update(username) hash.update(password) hash.update(config.get("General", "salt")) return hash.hexdigest()