##// END OF EJS Templates
formatter: add function to convert dict to appropriate format...
Yuya Nishihara -
r29794:4891f3b9 default
parent child Browse files
Show More
@@ -51,6 +51,12 b' class baseformatter(object):'
51 51 '''convert date tuple to appropriate format'''
52 52 return date
53 53 @staticmethod
54 def formatdict(data, key='key', value='value', fmt='%s=%s', sep=' '):
55 '''convert dict or key-value pairs to appropriate dict format'''
56 # use plain dict instead of util.sortdict so that data can be
57 # serialized as a builtin dict in pickle output
58 return dict(data)
59 @staticmethod
54 60 def formatlist(data, name, fmt='%s', sep=' '):
55 61 '''convert iterable to appropriate list format'''
56 62 return list(data)
@@ -75,6 +81,12 b' class baseformatter(object):'
75 81 if self._item is not None:
76 82 self._showitem()
77 83
84 def _iteritems(data):
85 '''iterate key-value pairs in stable order'''
86 if isinstance(data, dict):
87 return sorted(data.iteritems())
88 return data
89
78 90 class plainformatter(baseformatter):
79 91 '''the default text output scheme'''
80 92 def __init__(self, ui, topic, opts):
@@ -92,6 +104,10 b' class plainformatter(baseformatter):'
92 104 '''stringify date tuple in the given format'''
93 105 return util.datestr(date, fmt)
94 106 @staticmethod
107 def formatdict(data, key='key', value='value', fmt='%s=%s', sep=' '):
108 '''stringify key-value pairs separated by sep'''
109 return sep.join(fmt % (k, v) for k, v in _iteritems(data))
110 @staticmethod
95 111 def formatlist(data, name, fmt='%s', sep=' '):
96 112 '''stringify iterable separated by sep'''
97 113 return sep.join(fmt % e for e in data)
@@ -129,7 +145,11 b' class pickleformatter(baseformatter):'
129 145 self._ui.write(pickle.dumps(self._data))
130 146
131 147 def _jsonifyobj(v):
132 if isinstance(v, (list, tuple)):
148 if isinstance(v, dict):
149 xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u))
150 for k, u in sorted(v.iteritems())]
151 return '{' + ', '.join(xs) + '}'
152 elif isinstance(v, (list, tuple)):
133 153 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
134 154 elif v is None:
135 155 return 'null'
@@ -175,6 +195,14 b' class templateformatter(baseformatter):'
175 195 g = self._t(self._topic, ui=self._ui, **self._item)
176 196 self._ui.write(templater.stringify(g))
177 197 @staticmethod
198 def formatdict(data, key='key', value='value', fmt='%s=%s', sep=' '):
199 '''build object that can be evaluated as either plain string or dict'''
200 data = util.sortdict(_iteritems(data))
201 def f():
202 yield plainformatter.formatdict(data, key, value, fmt, sep)
203 return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]},
204 lambda d: fmt % (d[key], d[value]))
205 @staticmethod
178 206 def formatlist(data, name, fmt='%s', sep=' '):
179 207 '''build object that can be evaluated as either plain string or list'''
180 208 # name is mandatory argument for now, but it could be optional if
General Comments 0
You need to be logged in to leave comments. Login now