From 8b2115a801a428217fb55d4f53c327d57fdbc211 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 9 Oct 2018 23:25:39 +1300 Subject: [PATCH] eos-parental-controls-client: Add OARS filter support This basic support will return the value of a given OARS section to the caller. Signed-off-by: Philip Withnall https://phabricator.endlessm.com/T23999 --- .../eos-parental-controls-client.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/eos-parental-controls-client/eos-parental-controls-client.py b/eos-parental-controls-client/eos-parental-controls-client.py index 4436513..ae4f5ba 100644 --- a/eos-parental-controls-client/eos-parental-controls-client.py +++ b/eos-parental-controls-client/eos-parental-controls-client.py @@ -95,6 +95,23 @@ def __lookup_user_id_or_error(user): raise SystemExit(EXIT_INVALID_OPTION) +def __oars_value_to_string(value): + """Convert an EosParentalControls.AppFilterOarsValue to a human-readable + string.""" + mapping = { + EosParentalControls.AppFilterOarsValue.UNKNOWN: "unknown", + EosParentalControls.AppFilterOarsValue.NONE: "none", + EosParentalControls.AppFilterOarsValue.MILD: "mild", + EosParentalControls.AppFilterOarsValue.MODERATE: "moderate", + EosParentalControls.AppFilterOarsValue.INTENSE: "intense", + } + + try: + return mapping[value] + except KeyError: + return "invalid (OARS value {})".format(value) + + def command_get(user, quiet=False, interactive=True): """Get the app filter for the given user.""" user_id = __lookup_user_id_or_error(user) @@ -121,6 +138,17 @@ def command_check(user, path, quiet=False, interactive=True): raise SystemExit(EXIT_PATH_NOT_ALLOWED) +def command_oars_section(user, section, quiet=False, interactive=True): + """Get the value of the given OARS section for the given user, according + to their OARS filter.""" + user_id = __lookup_user_id_or_error(user) + app_filter = __get_app_filter_or_error(user_id, interactive) + + value = app_filter.get_oars_value(section) + print('OARS section ‘{}’ for user {} has value ‘{}’'.format( + section, user_id, __oars_value_to_string(value))) + + def main(): # Parse command line arguments parser = argparse.ArgumentParser( @@ -164,6 +192,18 @@ def main(): parser_check.add_argument('path', help='path to a program to check') + # ‘oars-section’ command + parser_oars_section = subparsers.add_parser('oars-section', + parents=[common_parser], + help='get the value of a ' + 'given OARS section') + parser_oars_section.set_defaults(function=command_oars_section) + parser_oars_section.add_argument('user', default='', nargs='?', + help='user ID or username to get the ' + 'OARS filter for (default: current ' + 'user)') + parser_oars_section.add_argument('section', help='OARS section to get') + # Parse the command line arguments and run the subcommand. args = parser.parse_args() args_dict = dict((k, v) for k, v in vars(args).items() if k != 'function')