12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
15 |
15 |
16 # import standard library |
16 # import standard library |
|
17 from datetime import date, datetime |
17 |
18 |
18 # import interfaces |
19 # import interfaces |
|
20 from pyams_catalog.interfaces import NO_RESOLUTION, DATE_RESOLUTION |
19 |
21 |
20 # import packages |
22 # import packages |
21 from hypatia.facet import FacetIndex |
23 from hypatia.facet import FacetIndex |
22 from hypatia.field import FieldIndex |
24 from hypatia.field import FieldIndex |
23 from hypatia.keyword import KeywordIndex |
25 from hypatia.keyword import KeywordIndex |
50 else: |
52 else: |
51 value = getattr(obj, self.discriminator, _marker) |
53 value = getattr(obj, self.discriminator, _marker) |
52 if callable(value): |
54 if callable(value): |
53 value = value(obj) |
55 value = value(obj) |
54 |
56 |
55 if value is _marker: |
57 if (value is None) or (value is _marker): |
56 return default |
58 return default |
57 |
59 |
58 if isinstance(value, Persistent): |
60 if isinstance(value, Persistent): |
59 raise ValueError('Catalog cannot index persistent object {0!r}'.format(value)) |
61 raise ValueError('Catalog cannot index persistent object {0!r}'.format(value)) |
60 |
62 |
68 """Field index with interface support""" |
70 """Field index with interface support""" |
69 |
71 |
70 def __init__(self, interface, discriminator, family=None): |
72 def __init__(self, interface, discriminator, family=None): |
71 InterfaceSupportIndexMixin.__init__(self, interface) |
73 InterfaceSupportIndexMixin.__init__(self, interface) |
72 FieldIndex.__init__(self, discriminator, family) |
74 FieldIndex.__init__(self, discriminator, family) |
|
75 |
|
76 |
|
77 def get_resolution(value, resolution): |
|
78 """Set resolution of given date or datetime""" |
|
79 if not value: |
|
80 return value |
|
81 if resolution < NO_RESOLUTION: |
|
82 args = [] |
|
83 if isinstance(value, date): |
|
84 resolution = max(resolution, DATE_RESOLUTION) |
|
85 args.extend(value.timetuple()[:resolution+1]) |
|
86 if isinstance(value, date): |
|
87 args.extend([0] * (2 - resolution)) |
|
88 value = date(*args) |
|
89 else: |
|
90 args.extend([0] * (5 - resolution)) |
|
91 args.append(value.tzinfo) |
|
92 value = datetime(*args) |
|
93 return value |
|
94 |
|
95 |
|
96 class DatetimeIndexWithInterface(FieldIndexWithInterface): |
|
97 """Normalized datetime index with interface support""" |
|
98 |
|
99 def __init__(self, interface, discriminator, resolution=DATE_RESOLUTION, family=None): |
|
100 FieldIndexWithInterface.__init__(self, interface, discriminator, family) |
|
101 self.resolution = resolution |
|
102 |
|
103 def discriminate(self, obj, default): |
|
104 value = super(DatetimeIndexWithInterface, self).discriminate(obj, default) |
|
105 return get_resolution(value, self.resolution) |
73 |
106 |
74 |
107 |
75 class KeywordIndexWithInterface(InterfaceSupportIndexMixin, KeywordIndex): |
108 class KeywordIndexWithInterface(InterfaceSupportIndexMixin, KeywordIndex): |
76 """Keyword index with interface support""" |
109 """Keyword index with interface support""" |
77 |
110 |