Add [listlimit <Number>] to gam print cros

This limits the number of entries shown for activeTimeRanges and
recentUsers
This commit is contained in:
Ross Scroggs 2016-03-09 15:14:26 -08:00
parent 82d43d0b62
commit 3ef433687a

View File

@ -7390,21 +7390,23 @@ def output_csv(csv_list, titles, list_type, todrive):
import webbrowser
webbrowser.open(file_url)
def flatten_json(structure, key="", path="", flattened=None):
def flatten_json(structure, key="", path="", flattened=None, listLimit=None):
if flattened == None:
flattened = {}
if type(structure) not in(dict, list):
if not isinstance(structure, (dict, list)):
flattened[((path + ".") if path else "") + key] = structure
elif isinstance(structure, list):
for i, item in enumerate(structure):
flatten_json(item, "%d" % i, ".".join(filter(None, [path, key])), flattened)
if listLimit and (i >= listLimit):
break
flatten_json(item, "%d" % i, ".".join(filter(None, [path, key])), flattened=flattened, listLimit=listLimit)
else:
for new_key, value in structure.items():
if new_key in [u'kind', u'etag']:
continue
if value == u'1970-01-01T00:00:00.000Z':
value = u'Never'
flatten_json(value, new_key, ".".join(filter(None, [path, key])), flattened)
flatten_json(value, new_key, ".".join(filter(None, [path, key])), flattened=flattened, listLimit=listLimit)
return flattened
def doPrintUsers():
@ -8010,7 +8012,7 @@ def doPrintCrosDevices():
todrive = False
query = projection = orderBy = sortOrder = None
noLists = False
selectAttrib = None
listLimit = selectAttrib = None
i = 3
while i < len(sys.argv):
my_arg = sys.argv[i].lower().replace(u'_', u'')
@ -8032,6 +8034,9 @@ def doPrintCrosDevices():
selectAttrib = u'activeTimeRanges'
noLists = False
i += 1
elif my_arg == u'listlimit':
listLimit = int(sys.argv[i+1])
i += 2
elif my_arg == u'orderby':
orderBy = sys.argv[i+1].lower().replace(u'_', u'')
allowed_values = [u'location', u'user', u'lastsync', u'notes', u'serialnumber', u'status', u'supportenddate']
@ -8068,7 +8073,7 @@ def doPrintCrosDevices():
if all_cros:
if (not noLists) and (not selectAttrib):
for cros in all_cros:
cros_attributes.append(flatten_json(cros))
cros_attributes.append(flatten_json(cros, listLimit=listLimit))
for item in cros_attributes[-1]:
if item not in cros_attributes[0]:
cros_attributes[0][item] = item
@ -8094,7 +8099,9 @@ def doPrintCrosDevices():
cros_attributes[0][xattrib] = xattrib
titles.append(xattrib)
attribMap[attrib] = xattrib
for item in cros[selectAttrib]:
for i, item in enumerate(cros[selectAttrib]):
if listLimit and(i >= listLimit):
break
new_row = row.copy()
for attrib in item:
if isinstance(item[attrib], (bool, int)):