Add in TZAware and TZNaive values for TreeGrids.

This commit is contained in:
Mike Auty
2018-03-08 01:20:34 +00:00
parent de9ad62412
commit c605cbd182
2 changed files with 30 additions and 3 deletions
+15 -1
View File
@@ -3,6 +3,7 @@ or in some other form. This module defines both the output format (:class:`Tree
which can interact with a TreeGrid to produce suitable output."""
import collections
import datetime
import typing
from abc import abstractmethod, ABCMeta
@@ -77,12 +78,25 @@ class BaseAbsentValue(object):
"""Class that represents values which are not present for some reason"""
# We don't class these off a shared base, because the SimpleTypes must only
# contain the types that the validator will accept (which would not include the base)
class TZAwareValue(datetime.datetime):
"""Class for TZ-aware datetimes"""
class TZNaiveValue(datetime.datetime):
"""Class for TZ-aware datetimes"""
_Type = typing.TypeVar("_Type")
ColumnsType = typing.List[typing.Tuple[str, typing.Type]]
SimpleTypes = typing.Union[typing.Type[int],
typing.Type[str],
typing.Type[float],
typing.Type[bytes],
typing.Type[TZAwareValue],
typing.Type[TZNaiveValue],
typing.Type[BaseAbsentValue]]
VisitorSignature = typing.Callable[[TreeNode, _Type], _Type]
@@ -100,7 +114,7 @@ class TreeGrid(object, metaclass = ABCMeta):
and to create cycles.
"""
simple_types = (int, str, float, bytes) # type: typing.ClassVar[typing.Tuple]
simple_types = (int, str, float, bytes, datetime) # type: typing.ClassVar[typing.Tuple]
def __init__(self, columns: ColumnsType, generator: typing.Generator) -> None:
"""Constructs a TreeGrid object using a specific set of columns
+15 -2
View File
@@ -52,13 +52,26 @@ class TreeNode(interfaces.renderers.TreeNode):
"Values must be a list of objects made up of simple types and number the same as the columns")
for index in range(len(self._treegrid.columns)):
column = self._treegrid.columns[index]
if not isinstance(values[index], (column.type, interfaces.renderers.BaseAbsentValue)):
val = values[index]
if not isinstance(val, (column.type, interfaces.renderers.BaseAbsentValue)):
raise TypeError(
"Values item with index {} is the wrong type for column {} (got {} but expected {})".format(
index,
column.name,
type(values[index]),
type(val),
column.type))
if isinstance(val, (interfaces.renderers.TZAwareValue, interfaces.renderers.TZNaiveValue)):
tznaive = val.tzinfo is None or val.tzinfo.utcoffset(val) is None
if isinstance(val, interfaces.renderers.TZAwareValue) and tznaive:
raise TypeError(
"Values item with index {} is not a timezone aware datetime object as required by column {}".format(
index,
column.name))
elif isinstance(val, interfaces.renderers.TZNaiveValue) and not tznaive:
raise TypeError(
"Values item with index {} is not a timezone naive datetime object as required by column {}".format(
index,
column.name))
@property
def values(self) -> typing.Iterable[interfaces.renderers.SimpleTypes]: