def report_html(self, request, result=None, query=None): builtins = Collection.get_builtins("fm.eventclassificationrules") r = ["["] r += [ ",\n".join([ indent(rr.to_json()) for rr in EventClassificationRule.objects.order_by("name") if rr.uuid and smart_text(rr.uuid) not in builtins ]) ] r += ["]", ""] return "<pre>" + escape("\n".join(r)) + "</pre>"
def convert(cls, o, i, order=None): if o is None: return indent("null", i) if isinstance(o, str): return indent('"%s"' % json_escape(o), i) elif isinstance(o, bool): return indent("true" if o else "false", i) elif isinstance(o, int): return indent("%d" % o, i) elif isinstance(o, float): return indent(str(o), i) elif isinstance(o, uuid.UUID): return indent('"%s"' % o, i) elif isinstance(o, list): if len(o) == 0: return indent("[]", i) t = [cls.convert(e, 0, order) for e in o] lt = reduce(lambda x, y: x + y, [len(x) for x in t]) lt += i + (len(o) - 1) * 2 if lt > 72: # Long line r = [indent("[", i)] r += [",\n".join(indent(x, i + 4) for x in t)] r += [indent("]", i)] return "\n".join(r) else: r = "[%s]" % ", ".join(t) return indent(r, i) elif isinstance(o, dict): if not o: return indent("{}", i) keys = sorted(o) if order: nk = [k for k in order if k in keys] nk += [k for k in keys if k not in order] keys = nk r = ",\n".join("%s: %s" % (cls.convert(k, 0), cls.convert(o[k], 0)) for k in keys) return indent("{\n%s\n}" % indent(r, 4), i) raise ValueError("Cannot encode %r" % o)
def test_indent(config, expected): assert indent(config, n=1) == expected