Show More
@@ -7,6 +7,7 b'' | |||
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | import abc | |
|
10 | 11 | import types |
|
11 | 12 | |
|
12 | 13 | from .i18n import _ |
@@ -26,12 +27,27 b' class ResourceUnavailable(error.Abort):' | |||
|
26 | 27 | class TemplateNotFound(error.Abort): |
|
27 | 28 | pass |
|
28 | 29 | |
|
30 | class wrapped(object): | |
|
31 | """Object requiring extra conversion prior to displaying or processing | |
|
32 | as value""" | |
|
33 | ||
|
34 | __metaclass__ = abc.ABCMeta | |
|
35 | ||
|
36 | @abc.abstractmethod | |
|
37 | def show(self, context, mapping): | |
|
38 | """Return a bytes or (possibly nested) generator of bytes representing | |
|
39 | the underlying object | |
|
40 | ||
|
41 | A pre-configured template may be rendered if the underlying object is | |
|
42 | not printable. | |
|
43 | """ | |
|
44 | ||
|
29 | 45 | # stub for representing a date type; may be a real date type that can |
|
30 | 46 | # provide a readable string value |
|
31 | 47 | class date(object): |
|
32 | 48 | pass |
|
33 | 49 | |
|
34 |
class hybrid( |
|
|
50 | class hybrid(wrapped): | |
|
35 | 51 | """Wrapper for list or dict to support legacy template |
|
36 | 52 | |
|
37 | 53 | This class allows us to handle both: |
@@ -60,6 +76,14 b' class hybrid(object):' | |||
|
60 | 76 | makemap = self._makemap |
|
61 | 77 | for x in self._values: |
|
62 | 78 | yield makemap(x) |
|
79 | ||
|
80 | def show(self, context, mapping): | |
|
81 | # TODO: switch gen to (context, mapping) API? | |
|
82 | gen = self.gen | |
|
83 | if callable(gen): | |
|
84 | return gen() | |
|
85 | return gen | |
|
86 | ||
|
63 | 87 | def __contains__(self, x): |
|
64 | 88 | return x in self._values |
|
65 | 89 | def __getitem__(self, key): |
@@ -74,7 +98,7 b' class hybrid(object):' | |||
|
74 | 98 | raise AttributeError(name) |
|
75 | 99 | return getattr(self._values, name) |
|
76 | 100 | |
|
77 |
class mappable( |
|
|
101 | class mappable(wrapped): | |
|
78 | 102 | """Wrapper for non-list/dict object to support map operation |
|
79 | 103 | |
|
80 | 104 | This class allows us to handle both: |
@@ -103,6 +127,13 b' class mappable(object):' | |||
|
103 | 127 | def itermaps(self): |
|
104 | 128 | yield self.tomap() |
|
105 | 129 | |
|
130 | def show(self, context, mapping): | |
|
131 | # TODO: switch gen to (context, mapping) API? | |
|
132 | gen = self.gen | |
|
133 | if callable(gen): | |
|
134 | return gen() | |
|
135 | return gen | |
|
136 | ||
|
106 | 137 | def hybriddict(data, key='key', value='value', fmt=None, gen=None): |
|
107 | 138 | """Wrap data to support both dict-like and string-like operations""" |
|
108 | 139 | prefmt = pycompat.identity |
@@ -123,12 +154,9 b' def hybridlist(data, name, fmt=None, gen' | |||
|
123 | 154 | def unwraphybrid(context, mapping, thing): |
|
124 | 155 | """Return an object which can be stringified possibly by using a legacy |
|
125 | 156 | template""" |
|
126 | gen = getattr(thing, 'gen', None) | |
|
127 | if gen is None: | |
|
157 | if not isinstance(thing, wrapped): | |
|
128 | 158 | return thing |
|
129 | if callable(gen): | |
|
130 | return gen() | |
|
131 | return gen | |
|
159 | return thing.show(context, mapping) | |
|
132 | 160 | |
|
133 | 161 | def unwrapvalue(thing): |
|
134 | 162 | """Move the inner value object out of the wrapper""" |
General Comments 0
You need to be logged in to leave comments.
Login now