if isinstance(d, (tuple, list, set)): if id(d) in dict_ids: raise ValueError("Cannot convert self-referential data to JSON") else:
dict_ids.add(id(d))
v = [key_to_str(x, dict_ids) for x in d]
dict_ids.remove(id(d)) return v else: return d
if id(d) in dict_ids: raise ValueError("Cannot convert self-referential data to JSON") else:
dict_ids.add(id(d))
for k, v in d.items(): if isinstance(k, bytes):
k = k.decode(encoding="utf-8", errors="backslashreplace") elif isinstance(k, CBORSimpleValue):
k = f"cbor_simple:{k.value:d}" elif isinstance(k, (FrozenDict, frozenset, tuple)):
k = str(k)
if isinstance(v, dict):
rval[k] = key_to_str(v, dict_ids) elif isinstance(v, (tuple, list, set)):
rval[k] = [key_to_str(x, dict_ids) for x in v] else:
rval[k] = v
return rval
def main() -> None:
prog = "python -m cbor2.tool"
description = ( "A simple command line interface for cbor2 module " "to validate and pretty-print CBOR objects."
)
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument("-o", "--outfile", type=str, help="output file", default="-")
parser.add_argument( "infiles",
nargs="*",
default=["-"],
help="Collection of CBOR files to process or - for stdin",
)
parser.add_argument( "-k", "--sort-keys",
action="store_true",
default=False,
help="sort the output of dictionaries alphabetically by key",
)
parser.add_argument( "-p", "--pretty",
action="store_true",
default=False,
help="indent the output to look good",
)
parser.add_argument( "-s", "--sequence",
action="store_true",
default=False,
help="Parse a sequence of concatenated CBOR items",
)
parser.add_argument( "-d", "--decode",
action="store_true",
default=False,
help="CBOR data is base64 encoded (handy for stdin)",
)
parser.add_argument( "-i", "--tag-ignore",
type=str,
default="",
help="Comma separated list of tags to ignore and only return the value",
)
options = parser.parse_args()
ignore_s = options.tag_ignore.split(",")
droptags = {int(n) for n in ignore_s if (len(n) and n[0].isdigit())}
my_hook = partial(tag_hook, ignore_tags=droptags)
with open(
outfile, mode="w", encoding="utf-8", errors="backslashreplace", closefd=closefd
) as outfp: for path in options.infiles: with ExitStack() as stack: if path == "-":
infile: BinaryIO = sys.stdin.buffer else:
infile = stack.enter_context(open(path, mode="rb"))
if options.decode:
infile = io.BytesIO(base64.b64decode(infile.read()))
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.