From 41a6c11c556bcc665e3266f22086cbd54bf59954 Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Tue, 4 May 2021 07:15:36 -0700 Subject: [PATCH] Handle TYPE_MESSAGE fields with durations or counts as a special case (#1375) * Handle TYPE_MESSAGE fields with durations or counts as a special case * Allow schema TYPE_ENUM field values with/without common prefix --- src/gam/gapi/chromepolicy.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/gam/gapi/chromepolicy.py b/src/gam/gapi/chromepolicy.py index 80772688f6..f0c0d5a249 100644 --- a/src/gam/gapi/chromepolicy.py +++ b/src/gam/gapi/chromepolicy.py @@ -98,10 +98,18 @@ def printshow_policies(): for policy in sorted(policies, key=lambda k: k.get('value', {}).get('policySchema', '')): print() name = policy.get('value', {}).get('policySchema', '') + schema = CHROME_SCHEMA_TYPE_MESSAGE.get(name) print(name) values = policy.get('value', {}).get('value', {}) for setting, value in values.items(): - if isinstance(value, str) and value.find('_ENUM_') != -1: + # Handle TYPE_MESSAGE fields with durations or counts as a special case + if schema and setting == schema['casedField']: + value = value.get(schema['type'], '') + if value: + if value.endswith('s'): + value = value[:-1] + value = int(value) // schema['scale'] + elif isinstance(value, str) and value.find('_ENUM_') != -1: value = value.split('_ENUM_')[-1] print(f' {setting}: {value}') @@ -323,14 +331,17 @@ def update_policy(): value = gam.getBoolean(value, field) elif vtype in ['TYPE_ENUM']: value = value.upper() + prefix = schemas[myarg]['settings'][field]['enum_prefix'] enum_values = schemas[myarg]['settings'][field]['enums'] - if value not in enum_values: + if value in enum_values: + value = f'{prefix}{value}' + elif value.replace(prefix, '') in enum_values: + pass + else: expected_enums = ', '.join(enum_values) msg = f'Expected {myarg} {field} value to be one of ' \ f'{expected_enums}, got {value}' controlflow.system_error_exit(8, msg) - prefix = schemas[myarg]['settings'][field]['enum_prefix'] - value = f'{prefix}{value}' elif vtype in ['TYPE_LIST']: value = value.split(',') if myarg == 'chrome.users.chromebrowserupdates' and \