Skip to content

Commit

Permalink
Handle TYPE_MESSAGE fields with durations or counts as a special case (
Browse files Browse the repository at this point in the history
…#1375)

* Handle TYPE_MESSAGE fields with durations or counts as a special case

* Allow schema TYPE_ENUM field values with/without common prefix
  • Loading branch information
taers232c authored May 4, 2021
1 parent 57d908e commit 41a6c11
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/gam/gapi/chromepolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Expand Down Expand Up @@ -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 \
Expand Down

0 comments on commit 41a6c11

Please sign in to comment.