From ace6429ce630fb1583b88ccd9e4bc0a9f6b0692a Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Mon, 5 Sep 2022 13:05:52 -0400 Subject: [PATCH] Add `lib/pprint_ish.bzl` --- lib/pprint_ish.bzl | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/pprint_ish.bzl diff --git a/lib/pprint_ish.bzl b/lib/pprint_ish.bzl new file mode 100644 index 0000000..0019dae --- /dev/null +++ b/lib/pprint_ish.bzl @@ -0,0 +1,73 @@ +""" +A very limited version of Python's pprint module. + +Note: + This formats lists, dicts, etc. on multiple lines with simple indentation, + not visual indentation. +""" + +def pprint_ish(obj): + """A limited version of pprint for debugging Starlark.""" + + # Prefix newline to move content after the "DEBUG" line. + print("\n" + pformat_ish(obj)) + +def pformat_ish(obj, prefix = " "): + """A limited version of pformat.""" + + # Note: Can't recurse in Starlark at present? + obj = _pformat_preprocess(obj) + if type(obj) in ["list", "tuple"]: + return _pformat_list(obj, prefix) + elif type(obj) == "dict": + return _pformat_dict(obj, prefix) + elif type(obj) == "struct": + return _pformat_struct(obj, prefix) + else: + return repr(obj) + +def _pformat_preprocess(x): + if type(x) == "depset": + x = sorted(x.to_list()) + if type(x) == "Label": + x = str(x) + return x + +def _pformat_list(obj, prefix): + lines = ["["] + for x in obj: + x = _pformat_preprocess(x) + lines.append(prefix + repr(x) + ",") + lines += ["]"] + return "\n".join(lines) + +def _pformat_lvl_1(obj, prefix): + obj = _pformat_preprocess(obj) + + # Partial recursion :/ + if type(obj) in ["list", "tuple"]: + return _pformat_list(obj, prefix) + else: + return repr(obj) + +def _pformat_dict(obj, prefix): + lines = ["{"] + for k, v in obj.items(): + k = _pformat_lvl_1(k, prefix) + v = _pformat_lvl_1(v, prefix) + s = k + ": " + v + lines.append(_indent(s, prefix) + ",") + lines.append("}") + return "\n".join(lines) + +def _pformat_struct(obj, prefix): + lines = ["struct("] + for k in dir(obj): + v = getattr(obj, k) + s = _pformat_lvl_1(v, prefix) + lines.append(_indent(s, prefix) + ",") + lines += [")"] + return "\n".join(lines) + +def _indent(s, prefix): + return "\n".join([prefix + line for line in s.splitlines()])