Permalink
Browse files

PEP 8

  • Loading branch information...
1 parent d46a079 commit c8bce65e24320ce182f87e085a2a8c3cd27ceb95 @streeter committed Apr 28, 2011
Showing with 25 additions and 22 deletions.
  1. +18 −16 readonly/__init__.py
  2. +1 −1 readonly/exceptions.py
  3. +4 −3 runtests.py
  4. +2 −2 setup.py
View
@@ -19,59 +19,61 @@
logger = getLogger('django.db.backends')
+
def _readonly():
return getattr(settings, 'SITE_READ_ONLY', False)
class ReadOnlyCursorWrapper(object):
"""
This is a wrapper for a database cursor.
-
+
This sits between django's own wrapper at
`django.db.backends.util.CursorWrapper` and the database specific cursor at
`django.db.backends.*.base.*CursorWrapper`. It overrides two specific
- methods: `execute` and `executemany`. If the site is in read-only mode, then
- the SQL is examined to see if it contains any write actions. If a write
- is detected, an exception is raised.
-
+ methods: `execute` and `executemany`. If the site is in read-only mode,
+ then the SQL is examined to see if it contains any write actions. If a
+ write is detected, an exception is raised.
+
A site is in read only mode by setting the SITE_READ_ONLY setting. For
obvious reasons, this is False by default.
-
+
Raises a DatabaseWriteDenied exception if writes are disabled.
"""
-
+
SQL_WRITE_BLACKLIST = (
# Data Definition
'CREATE', 'ALTER', 'RENAME', 'DROP', 'TRUNCATE',
# Data Manipulation
- 'INSERT INTO', 'UPDATE', 'REPLACE', 'DELETE FROM',
+ 'INSERT INTO', 'UPDATE', 'REPLACE', 'DELETE FROM',
)
-
+
def __init__(self, cursor):
self.cursor = cursor
self.readonly = _readonly()
-
+
def execute(self, sql, params=()):
# Check the SQL
if self.readonly and self._write_sql(sql):
raise DatabaseWriteDenied
return self.cursor.execute(sql, params)
-
+
def executemany(self, sql, param_list):
# Check the SQL
if self.readonly and self._write_sql(sql):
raise DatabaseWriteDenied
return self.cursor.executemany(sql, param_list)
-
+
def __getattr__(self, attr):
return getattr(self.cursor, attr)
-
+
def __iter__(self):
return iter(self.cursor)
-
+
def _write_sql(self, sql):
return sql.startswith(self.SQL_WRITE_BLACKLIST)
+
class CursorWrapper(util.CursorWrapper):
def __init__(self, cursor, db):
self.cursor = ReadOnlyCursorWrapper(cursor)
@@ -95,7 +97,7 @@ def execute(self, sql, params=()):
'time': "%.3f" % duration,
})
logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),
- extra={'duration':duration, 'sql':sql, 'params':params}
+ extra={'duration': duration, 'sql': sql, 'params': params}
)
def executemany(self, sql, param_list):
@@ -110,7 +112,7 @@ def executemany(self, sql, param_list):
'time': "%.3f" % duration,
})
logger.debug('(%.3f) %s; args=%s' % (duration, sql, param_list),
- extra={'duration':duration, 'sql':sql, 'params':param_list}
+ extra={'duration': duration, 'sql': sql, 'params': param_list}
)
if _readonly():
View
@@ -1,5 +1,5 @@
from django.db.utils import DatabaseError
+
class DatabaseWriteDenied(DatabaseError):
pass
-
View
@@ -7,7 +7,7 @@
if not settings.configured:
settings.configure(
DATABASE_ENGINE='sqlite3',
-
+
# Uncomment below to run tests with mysql
#DATABASE_ENGINE='django.db.backends.mysql',
#DATABASE_NAME='readonly_test',
@@ -23,11 +23,12 @@
from django.test.simple import run_tests
+
def runtests(*test_args):
if 'south' in settings.INSTALLED_APPS:
from south.management.commands import patch_for_test_db_setup
patch_for_test_db_setup()
-
+
if not test_args:
test_args = ['readonly']
parent = dirname(abspath(__file__))
@@ -37,4 +38,4 @@ def runtests(*test_args):
if __name__ == '__main__':
- runtests(*sys.argv[1:])
+ runtests(*sys.argv[1:])
View
@@ -23,12 +23,12 @@ def run(self, *args, **kwargs):
author='Chris Streeter',
author_email='pypi@chrisstreeter.com',
url='http://github.com/streeter/django-db-readonly',
- description = 'Add a global database read-only setting.',
+ description='Add a global database read-only setting.',
packages=find_packages(),
zip_safe=False,
install_requires=[
],
- test_suite = 'readonly.tests',
+ test_suite='readonly.tests',
include_package_data=True,
cmdclass={"test": mytest},
classifiers=[

0 comments on commit c8bce65

Please sign in to comment.