def parse_datetime(s): if s is None or not s.strip(): return None ev = enrichment.EnrichedValue(s) res = ev.optimistic_as_datetime if res is None: raise responses.ExceptionalResponse.bad_request( 'Invalid datetime <%s>' % s) return res
def wants_numbers(self, data): for sd in data['series_data']: non_number_idxs = set() for i, data_point in enumerate(sd.get('data_points', [])): ev = enrichment.EnrichedValue(data_point.value) converted = ev.optimistic_as_number if converted is not None: sd['data_points'][i].replace_value(converted) else: non_number_idxs.add(i) # remove non-numbers if non_number_idxs: sd['data_points'] = [ x for i, x in enumerate(sd.get('data_points', [])) if i not in non_number_idxs ]
def to_displayable_html(val, style=None, _level=0): style = style or '' try: if _level > MAX_DISPLAYABLE_OBJECT_LEVELS: return '' ev = enrichment.EnrichedValue(val) if ev.as_bool is not None: if ev.as_bool: style = HTML_STYLE_TRUE else: style = HTML_STYLE_FALSE return _span(style, str(val)) if val is None: return '∅' if isinstance(val, datetime.date): if not isinstance(val, datetime.datetime): val = webutil.format_date(val) else: val = webutil.format_datetime(val) return _span(style, cgi.escape(val, quote=True)) if isinstance(val, basestring): val = cgi.escape(val, quote=True) val = val.replace('\n', '<br>') return _span(style, val) if isinstance(val, numbers.Number): return _span(style, str(val)) if isinstance(val, SingleContent): return """<pre style='%s' class=single-content>%s</pre>""" % ( style, cgi.escape(val.content, quote=True)) if isinstance(val, MarkdownContent): return _span(style, val.render()) level_style = """margin-left: %spx;""" % (MARGIN_PX_FOR_EACH_LEVEL * _level) if isinstance(val, list): lines = [] lines.append("""<div class=displayable-list style='%s %s'>""" % (style, level_style)) for x in val: below = to_displayable_html(x, style, _level + 1) if isinstance(x, (list, dict)): lines.append(below) else: lines.append('<div>— %s</div>' % below) lines.append('</div>') return '\n'.join(lines) if isinstance(val, dict): lines = [] lines.append("""<div class=displayable-object style='%s %s'>""" % (style, level_style)) for k, v in val.iteritems(): lines.append('<div>— %s: %s</div>' % (k, to_displayable_html(v, style, _level + 1))) lines.append('</div>') return '\n'.join(lines) # Fallback: json serialization try: return serialize.json_dumps_external(val) except: try: return unicode(val) except: return repr(val) except: log.exception('In to_displayable_html, returning safe value') return '<em>Invalid value</em>'
def is_value_charts_compatible(val): if enrichment.EnrichedValue(val).optimistic_as_number is None: return False return True