#
# Copyright 2008 Google Inc. All Rights Reserved.

"""
The user module contains the objects and methods used to
manage users in Autotest.

The valid action is:
list:    lists user(s)

The common options are:
--ulist / -U: file containing a list of USERs

See topic_common.py for a High Level Design and Algorithm.
"""

import os, sys
from autotest_lib.cli import topic_common, action_common


class user(topic_common.atest):
    """User class
    atest user list <options>"""
    usage_action = 'list'
    topic = msg_topic = 'user'
    msg_items = '<users>'

    def __init__(self):
        """Add to the parser the options common to all the
        user actions"""
        super(user, self).__init__()

        self.parser.add_option('-U', '--ulist',
                               help='File listing the users',
                               type='string',
                               default=None,
                               metavar='USER_FLIST')


    def parse(self, flists=None, req_items='users'):
        """Consume the common user options"""
        if flists:
            flists.append(('users', 'ulist', '', True))
        else:
            flists = [('users', 'ulist', '', True)]
        return self.parse_with_flist(flists, req_items)


    def get_items(self):
        return self.users


class user_help(user):
    """Just here to get the atest logic working.
    Usage is set by its parent"""
    pass


class user_list(action_common.atest_list, user):
    """atest user list <user>|--ulist <file>
    [--acl <ACL>|--access_level <n>]"""
    def __init__(self):
        super(user_list, self).__init__()

        self.parser.add_option('-a', '--acl',
                               help='Only list users within this ACL')

        self.parser.add_option('-l', '--access_level',
                               help='Only list users at this access level')


    def parse(self):
        (options, leftover) = super(user_list, self).parse(req_items=None)
        self.acl = options.acl
        self.access_level = options.access_level
        return (options, leftover)


    def execute(self):
        filters = {}
        check_results = {}
        if self.acl:
            filters['acl_group__name__in'] = [self.acl]
            check_results['acl_group__name__in'] = None

        if self.access_level:
            filters['access_level__in'] = [self.access_level]
            check_results['access_level__in'] = None

        if self.users:
            filters['login__in'] = self.users
            check_results['login__in'] = 'login'

        return super(user_list, self).execute(op='get_users',
                                              filters=filters,
                                              check_results=check_results)


    def output(self, results):
        if self.verbose:
            keys = ['id', 'login', 'access_level']
        else:
            keys = ['login']

        super(user_list, self).output(results, keys)
