summaryrefslogtreecommitdiff
path: root/Mailman/database/types.py
diff options
context:
space:
mode:
authorBarry Warsaw2007-11-04 18:10:21 -0500
committerBarry Warsaw2007-11-04 18:10:21 -0500
commit46f480dfaa6286ff8950af817de1c35910b37e16 (patch)
tree194e8a3fb2150b7fa9ce5063a4b5b4acdf821365 /Mailman/database/types.py
parent85473d738ce8ec53ac518819832e0babc3558cf2 (diff)
downloadmailman-46f480dfaa6286ff8950af817de1c35910b37e16.tar.gz
mailman-46f480dfaa6286ff8950af817de1c35910b37e16.tar.zst
mailman-46f480dfaa6286ff8950af817de1c35910b37e16.zip
Diffstat (limited to 'Mailman/database/types.py')
-rw-r--r--Mailman/database/types.py60
1 files changed, 26 insertions, 34 deletions
diff --git a/Mailman/database/types.py b/Mailman/database/types.py
index 0f0e46fa3..ae71129cc 100644
--- a/Mailman/database/types.py
+++ b/Mailman/database/types.py
@@ -15,51 +15,43 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
+__all__ = [
+ 'Enum',
+ ]
+
+
import sys
-from datetime import timedelta
-from sqlalchemy import types
+from storm.properties import SimpleProperty
+from storm.variables import UnicodeVariable, Variable
-# SQLAlchemy custom type for storing munepy Enums in the database.
-class EnumType(types.TypeDecorator):
- # Enums can be stored as strings of the form:
- # full.path.to.Enum:intval
- impl = types.String
+class _EnumVariable(Variable):
+ """Storm variable."""
- def convert_bind_param(self, value, engine):
+ def parse_set(self, value, from_db):
if value is None:
return None
- return '%s.%s:%d' % (value.enumclass.__module__,
- value.enumclass.__name__,
- int(value))
+ if from_db:
+ path, intvalue = value.rsplit(':', 1)
+ modulename, classname = path.rsplit('.', 1)
+ __import__(modulename)
+ cls = getattr(sys.modules[modulename], classname)
+ return cls[int(intvalue)]
+ return value
- def convert_result_value(self, value, engine):
+ def parse_get(self, value, to_db):
if value is None:
return None
- path, intvalue = value.rsplit(':', 1)
- modulename, classname = path.rsplit('.', 1)
- __import__(modulename)
- cls = getattr(sys.modules[modulename], classname)
- return cls[int(intvalue)]
+ if to_db:
+ return '%s.%s:%d' % (value.enumclass.__module__,
+ value.enumclass.__name__,
+ int(value))
+ return value
-
-class TimeDeltaType(types.TypeDecorator):
- # timedeltas are stored as the string representation of three integers,
- # separated by colons. The values represent the three timedelta
- # attributes days, seconds, microseconds.
- impl = types.String
-
- def convert_bind_param(self, value, engine):
- if value is None:
- return None
- return '%s:%s:%s' % (value.days, value.seconds, value.microseconds)
+class Enum(SimpleProperty):
+ """Custom munepy.Enum type for Storm."""
- def convert_result_value(self, value, engine):
- if value is None:
- return None
- parts = value.split(':')
- assert len(parts) == 3, 'Bad timedelta representation: %s' % value
- return timedelta(*(int(value) for value in parts))
+ variable_class = UnicodeVariable