2022-10-08 10:48:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2023-03-09 19:52:06 +00:00
|
|
|
#
|
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
|
|
|
|
|
#
|
|
|
|
|
# This file is a part of the Armbian Build Framework
|
|
|
|
|
# https://github.com/armbian/build/
|
|
|
|
|
#
|
|
|
|
|
#!/usr/bin/env python3
|
2022-10-08 10:48:21 +00:00
|
|
|
import collections.abc
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def eprint(*args, **kwargs):
|
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flatten(d, parent_key='', sep='_'):
|
|
|
|
|
items = []
|
|
|
|
|
for k, v in d.items():
|
|
|
|
|
new_key = parent_key + sep + k if parent_key else k
|
|
|
|
|
if isinstance(v, collections.abc.MutableMapping):
|
|
|
|
|
items.extend(flatten(v, new_key, sep=sep).items())
|
|
|
|
|
else:
|
|
|
|
|
items.append((new_key, v))
|
|
|
|
|
return dict(items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
json_object = json.load(sys.stdin)
|
|
|
|
|
eprint("Loaded {} objects from stdin...".format(len(json_object)))
|
|
|
|
|
|
|
|
|
|
flat = []
|
|
|
|
|
for obj in json_object:
|
|
|
|
|
flat.append(flatten(obj, '', '.'))
|
|
|
|
|
|
|
|
|
|
columns_map = {}
|
|
|
|
|
for obj in flat:
|
|
|
|
|
# get the string keys
|
|
|
|
|
for key in obj.keys():
|
|
|
|
|
value = obj[key]
|
|
|
|
|
if type(value) == str:
|
|
|
|
|
columns_map[key] = True
|
|
|
|
|
if type(value) == bool:
|
|
|
|
|
columns_map[key] = True
|
|
|
|
|
|
|
|
|
|
columns = columns_map.keys()
|
|
|
|
|
|
2022-11-03 18:24:00 +00:00
|
|
|
eprint("columns: {}".format(len(columns)))
|
2022-10-08 10:48:21 +00:00
|
|
|
|
2022-11-03 18:24:00 +00:00
|
|
|
# Now, find the columns of which all values are the same
|
|
|
|
|
# and remove them
|
|
|
|
|
columns_to_remove = []
|
|
|
|
|
for column in columns:
|
|
|
|
|
values = []
|
|
|
|
|
for obj in flat:
|
|
|
|
|
value = obj.get(column)
|
|
|
|
|
values.append(value)
|
|
|
|
|
if len(set(values)) == 1:
|
|
|
|
|
columns_to_remove.append(column)
|
|
|
|
|
|
2023-01-30 15:51:54 +00:00
|
|
|
# eprint("columns with all-identical values: {}: '{}'".format(len(columns_to_remove), columns_to_remove))
|
2022-11-03 18:24:00 +00:00
|
|
|
|
|
|
|
|
# Now actually filter columns, removing columns_to_remove
|
|
|
|
|
columns = [column for column in columns if column not in columns_to_remove]
|
2022-10-08 10:48:21 +00:00
|
|
|
|
|
|
|
|
import csv
|
|
|
|
|
|
2022-11-03 18:24:00 +00:00
|
|
|
writer = csv.DictWriter(sys.stdout, fieldnames=columns, extrasaction='ignore')
|
2022-10-08 10:48:21 +00:00
|
|
|
|
2022-11-03 18:24:00 +00:00
|
|
|
writer.writeheader()
|
|
|
|
|
for obj in flat:
|
|
|
|
|
writer.writerow(obj)
|
|
|
|
|
|
2023-01-30 15:51:54 +00:00
|
|
|
eprint("Done writing CSV to stdout.")
|