/usr/lib/python3/dist-packages/sqlalchemy_utils/types/timezone.py is in python3-sqlalchemy-utils 0.32.21-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import six
from sqlalchemy import types
from ..exceptions import ImproperlyConfigured
from .scalar_coercible import ScalarCoercible
class TimezoneType(types.TypeDecorator, ScalarCoercible):
"""
TimezoneType provides a way for saving timezones (from either the pytz or
the dateutil package) objects into database. TimezoneType saves timezone
objects as strings on the way in and converts them back to objects when
querying the database.
::
from sqlalchemy_utils import TimezoneType
class User(Base):
__tablename__ = 'user'
# Pass backend='pytz' to change it to use pytz (dateutil by
# default)
timezone = sa.Column(TimezoneType(backend='pytz'))
"""
impl = types.Unicode(50)
python_type = None
def __init__(self, backend='dateutil'):
"""
:param backend: Whether to use 'dateutil' or 'pytz' for timezones.
"""
self.backend = backend
if backend == 'dateutil':
try:
from dateutil.tz import tzfile
from dateutil.zoneinfo import gettz
self.python_type = tzfile
self._to = gettz
self._from = lambda x: six.text_type(x._filename)
except ImportError:
raise ImproperlyConfigured(
"'python-dateutil' is required to use the "
"'dateutil' backend for 'TimezoneType'"
)
elif backend == 'pytz':
try:
from pytz import timezone
from pytz.tzinfo import BaseTzInfo
self.python_type = BaseTzInfo
self._to = timezone
self._from = six.text_type
except ImportError:
raise ImproperlyConfigured(
"'pytz' is required to use the 'pytz' backend "
"for 'TimezoneType'"
)
else:
raise ImproperlyConfigured(
"'pytz' or 'dateutil' are the backends supported for "
"'TimezoneType'"
)
def _coerce(self, value):
if value is not None and not isinstance(value, self.python_type):
obj = self._to(value)
if obj is None:
raise ValueError("unknown time zone '%s'" % value)
return obj
return value
def process_bind_param(self, value, dialect):
return self._from(self._coerce(value)) if value else None
def process_result_value(self, value, dialect):
return self._to(value) if value else None
|