Mercurial > p > roundup > code
comparison roundup/backends/sessions_sqlite.py @ 6823:fe0091279f50
Refactor session db logging and key generation for sessions/otks
While I was working on the redis sessiondb stuff, I noticed that
log_wanrning, get_logger ... was duplicated. Also there was code to
generate a unique key for otks that was duplicated.
Changes:
creating new sessions_common.py and SessionsCommon class to provide
methods:
log_warning, log_info, log_debug, get_logger, getUniqueKey
getUniqueKey method is closer to the method used to make
session keys in client.py.
sessions_common.py now report when random_.py chooses a weak
random number generator. Removed same from rest.py.
get_logger reconciles all logging under
roundup.hyperdb.backends.<name of BasicDatabase class>
some backends used to log to root logger.
have BasicDatabase in other sessions_*.py modules inherit from
SessionCommon.
change logging to use log_* methods.
In addition:
remove unused imports reported by flake8 and other formatting
changes
modify actions.py, rest.py, templating.py to use getUniqueKey
method.
add tests for new methods
test_redis_session.py
swap out ModuleNotFoundError for ImportError to prevent crash in
python2 when redis is not present.
allow injection of username:password or just password into redis
connection URL. set pytest_redis_pw envirnment variable to password
or user:password when running test.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 07 Aug 2022 01:51:11 -0400 |
| parents | 375d40a9e730 |
| children | a96a239db0d9 |
comparison
equal
deleted
inserted
replaced
| 6822:5053ee6c846b | 6823:fe0091279f50 |
|---|---|
| 9 sqlite db, which wasn't supported. This module was created so sqlite | 9 sqlite db, which wasn't supported. This module was created so sqlite |
| 10 didn't have to use dbm for the session/otk data. It hopefully will | 10 didn't have to use dbm for the session/otk data. It hopefully will |
| 11 provide a performance speedup. | 11 provide a performance speedup. |
| 12 """ | 12 """ |
| 13 __docformat__ = 'restructuredtext' | 13 __docformat__ = 'restructuredtext' |
| 14 import os, time, logging | |
| 15 | 14 |
| 16 from roundup.anypy.html import html_escape as escape | 15 from roundup.backends import sessions_rdbms |
| 17 import roundup.backends.sessions_rdbms as rdbms_session | |
| 18 | 16 |
| 19 class BasicDatabase(rdbms_session.BasicDatabase): | 17 |
| 18 class BasicDatabase(sessions_rdbms.BasicDatabase): | |
| 20 ''' Provide a nice encapsulation of an RDBMS table. | 19 ''' Provide a nice encapsulation of an RDBMS table. |
| 21 | 20 |
| 22 Keys are id strings, values are automatically marshalled data. | 21 Keys are id strings, values are automatically marshalled data. |
| 23 ''' | 22 ''' |
| 24 name = None | 23 name = None |
| 24 | |
| 25 def __init__(self, db): | 25 def __init__(self, db): |
| 26 self.db = db | 26 self.db = db |
| 27 self.conn, self.cursor = self.db.sql_open_connection(dbname=self.name) | 27 self.conn, self.cursor = self.db.sql_open_connection(dbname=self.name) |
| 28 | 28 |
| 29 self.sql('''SELECT name FROM sqlite_master WHERE type='table' AND name='%ss';'''%self.name) | 29 self.sql('''SELECT name FROM sqlite_master WHERE type='table' AND ''' |
| 30 '''name='%ss';''' % self.name) | |
| 30 table_exists = self.cursor.fetchone() | 31 table_exists = self.cursor.fetchone() |
| 31 | 32 |
| 32 if not table_exists: | 33 if not table_exists: |
| 33 # create table/rows etc. | 34 # create table/rows etc. |
| 34 self.sql('''CREATE TABLE %(name)ss (%(name)s_key VARCHAR(255), | 35 self.sql('''CREATE TABLE %(name)ss (%(name)s_key VARCHAR(255), |
| 35 %(name)s_value TEXT, %(name)s_time REAL)'''%{"name":self.name}) | 36 %(name)s_value TEXT, %(name)s_time REAL)''' % {"name": self.name}) |
| 36 self.sql('CREATE INDEX %(name)s_key_idx ON %(name)ss(%(name)s_key)'%{"name":self.name}) | 37 self.sql('CREATE INDEX %(name)s_key_idx ON ' |
| 38 '%(name)ss(%(name)s_key)' % {"name": self.name}) | |
| 37 self.commit() | 39 self.commit() |
| 38 | |
| 39 def log_debug(self, msg, *args, **kwargs): | |
| 40 """Log a message with level DEBUG.""" | |
| 41 | |
| 42 logger = self.get_logger() | |
| 43 logger.debug(msg, *args, **kwargs) | |
| 44 | |
| 45 def log_info(self, msg, *args, **kwargs): | |
| 46 """Log a message with level INFO.""" | |
| 47 | |
| 48 logger = self.get_logger() | |
| 49 logger.info(msg, *args, **kwargs) | |
| 50 | |
| 51 def get_logger(self): | |
| 52 """Return the logger for this database.""" | |
| 53 | |
| 54 # Because getting a logger requires acquiring a lock, we want | |
| 55 # to do it only once. | |
| 56 if not hasattr(self, '__logger'): | |
| 57 self.__logger = logging.getLogger('roundup') | |
| 58 | |
| 59 return self.__logger | |
| 60 | 40 |
| 61 def sql(self, sql, args=None, cursor=None): | 41 def sql(self, sql, args=None, cursor=None): |
| 62 """ Execute the sql with the optional args. | 42 """ Execute the sql with the optional args. |
| 63 """ | 43 """ |
| 64 self.log_debug('SQL %r %r' % (sql, args)) | 44 self.log_debug('SQL %r %r' % (sql, args)) |
| 67 if args: | 47 if args: |
| 68 cursor.execute(sql, args) | 48 cursor.execute(sql, args) |
| 69 else: | 49 else: |
| 70 cursor.execute(sql) | 50 cursor.execute(sql) |
| 71 | 51 |
| 52 | |
| 72 class Sessions(BasicDatabase): | 53 class Sessions(BasicDatabase): |
| 73 name = 'session' | 54 name = 'session' |
| 55 | |
| 74 | 56 |
| 75 class OneTimeKeys(BasicDatabase): | 57 class OneTimeKeys(BasicDatabase): |
| 76 name = 'otk' | 58 name = 'otk' |
| 77 | 59 |
| 78 # vim: set et sts=4 sw=4 : | 60 # vim: set et sts=4 sw=4 : |
