<s> """<STR_LIT>""" <EOL> import argparse <EOL> import logging <EOL> import os <EOL> import pprint <EOL> import sys <EOL> import textwrap <EOL> lib_dir = os . path . join ( os . path . dirname ( sys . modules [ __name__ ] . __file__ ) , <EOL> '<STR_LIT>' ) <EOL> if os . path . isdir ( lib_dir ) : <EOL> sys . path . insert ( <NUM_LIT:0> , lib_dir ) <EOL> from apiclient . discovery import build <EOL> import apiclient . errors <EOL> import auth_helper <EOL> import http_utils <EOL> def _PrintOneLine ( user_email , user_id , full_name ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' % ( user_email , user_id , full_name ) <EOL> def _PrintUserHeader ( ) : <EOL> """<STR_LIT>""" <EOL> _PrintOneLine ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _PrintOneUser ( user , basic = True ) : <EOL> """<STR_LIT>""" <EOL> _PrintOneLine ( user [ '<STR_LIT>' ] , user [ '<STR_LIT:id>' ] , user [ '<STR_LIT:name>' ] [ '<STR_LIT>' ] ) <EOL> if basic : <EOL> return <EOL> for field in sorted ( user . keys ( ) ) : <EOL> if field in ( '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ) : <EOL> continue <EOL> formatted_text = textwrap . fill ( <EOL> pprint . pformat ( user . get ( field ) ) , <EOL> subsequent_indent = ( <NUM_LIT:7> + len ( field ) ) * '<STR_LIT:U+0020>' , break_on_hyphens = False ) <EOL> print '<STR_LIT>' % ( field , formatted_text ) <EOL> class APIToolDirectoryUserError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UserDirectory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , http ) : <EOL> """<STR_LIT>""" <EOL> self . _service = build ( '<STR_LIT>' , '<STR_LIT>' , http = http ) <EOL> self . _users = self . _service . users ( ) <EOL> def _ProcessUserListPage ( self , apps_domain , max_page , next_page_token = None ) : <EOL> """<STR_LIT>""" <EOL> request = self . _users . list ( domain = apps_domain , maxResults = max_page , <EOL> pageToken = next_page_token ) <EOL> try : <EOL> users_list = request . execute ( ) <EOL> return users_list <EOL> except apiclient . errors . HttpError as e : <EOL> error_text = http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) <EOL> raise APIToolDirectoryUserError ( error_text ) <EOL> def _ProcessDomainUsers ( self , apps_domain , process_fn , max_results = None , <EOL> max_page = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> retrieved_count = <NUM_LIT:0> <EOL> next_page_token = None <EOL> if max_page < <NUM_LIT:1> or max_page > <NUM_LIT> : <EOL> max_page = <NUM_LIT:100> <EOL> if max_results is not None and max_results < max_page : <EOL> max_page = max_results <EOL> users_list = self . _ProcessUserListPage ( apps_domain = apps_domain , <EOL> max_page = max_page ) <EOL> while True : <EOL> for user in users_list . get ( '<STR_LIT>' , [ ] ) : <EOL> result = process_fn ( user ) <EOL> if result : <EOL> results . append ( result ) <EOL> retrieved_count += <NUM_LIT:1> <EOL> if max_results is not None and retrieved_count >= max_results : <EOL> return results , retrieved_count <EOL> next_page_token = users_list . get ( '<STR_LIT>' ) <EOL> if not next_page_token : <EOL> return results , retrieved_count <EOL> users_list = self . _ProcessUserListPage ( apps_domain = apps_domain , <EOL> max_page = max_page , <EOL> next_page_token = next_page_token ) <EOL> def AddDomainUser ( self , first_name , last_name , user_email , new_password ) : <EOL> """<STR_LIT>""" <EOL> body = { <EOL> '<STR_LIT>' : user_email , <EOL> '<STR_LIT:name>' : { '<STR_LIT>' : first_name , '<STR_LIT>' : last_name } , <EOL> '<STR_LIT:password>' : new_password <EOL> } <EOL> request = self . _users . insert ( body = body ) <EOL> try : <EOL> request . execute ( ) <EOL> return <EOL> except apiclient . errors . HttpError as e : <EOL> error_text = http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) <EOL> raise APIToolDirectoryUserError ( error_text ) <EOL> def DeleteDomainUser ( self , user_email ) : <EOL> """<STR_LIT>""" <EOL> request = self . _users . delete ( userKey = user_email ) <EOL> try : <EOL> request . execute ( ) <EOL> return <EOL> except apiclient . errors . HttpError as e : <EOL> if e . resp . status == <NUM_LIT> : <EOL> print '<STR_LIT>' % user_email <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> error_text = http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) <EOL> raise APIToolDirectoryUserError ( error_text ) <EOL> def GetDomainUser ( self , user_email ) : <EOL> """<STR_LIT>""" <EOL> request = self . _users . get ( userKey = user_email ) <EOL> try : <EOL> return request . execute ( ) <EOL> except apiclient . errors . HttpError as e : <EOL> if e . resp . status == <NUM_LIT> : <EOL> print '<STR_LIT>' % user_email <EOL> if '<STR_LIT:@>' not in user_email : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> error_text = http_utils . ParseHttpResult ( e . uri , e . resp , e . content ) <EOL> raise APIToolDirectoryUserError ( error_text ) <EOL> def PrintDomainUser ( self , user_email ) : <EOL> """<STR_LIT>""" <EOL> user = self . GetDomainUser ( user_email ) <EOL> if user : <EOL> _PrintUserHeader ( ) <EOL> _PrintOneUser ( user = user , basic = False ) <EOL> else : <EOL> print '<STR_LIT>' % user_email <EOL> def PrintDomainUsers ( self , apps_domain , max_results = None , max_page = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' % apps_domain <EOL> _PrintUserHeader ( ) <EOL> _ , count = self . _ProcessDomainUsers ( apps_domain = apps_domain , <EOL> process_fn = _PrintOneUser , <EOL> max_results = max_results , <EOL> max_page = max_page ) <EOL> print '<STR_LIT>' % count <EOL> def AddUserCommand ( flags , user_api ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_api . AddDomainUser ( flags . first_name , flags . last_name , <EOL> flags . user_email , flags . password ) <EOL> except APIToolDirectoryUserError as e : <EOL> print '<STR_LIT>' % ( flags . user_email , e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print '<STR_LIT>' % flags . user_email <EOL> def SetupAddUserParser ( cmd_subparsers ) : <EOL> """<STR_LIT>""" <EOL> parser_add_user = cmd_subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_add_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_add_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_add_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_add_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_add_user . set_defaults ( subcommand = AddUserCommand ) <EOL> def LsUserCommand ( flags , user_api ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_api . PrintDomainUser ( flags . user_email ) <EOL> except APIToolDirectoryUserError as e : <EOL> print '<STR_LIT>' % ( flags . user_email , e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def SetupLsUserParser ( cmd_subparsers ) : <EOL> """<STR_LIT>""" <EOL> parser_ls_user = cmd_subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_ls_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_ls_user . set_defaults ( subcommand = LsUserCommand ) <EOL> def LsUsersCommand ( flags , user_api ) : <EOL> """<STR_LIT>""" <EOL> max_results = flags . first_n if flags . first_n > <NUM_LIT:0> else None <EOL> try : <EOL> user_api . PrintDomainUsers ( flags . apps_domain , max_results = max_results ) <EOL> except APIToolDirectoryUserError as e : <EOL> print '<STR_LIT>' % ( flags . apps_domain , e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def SetupLsUsersParser ( cmd_subparsers ) : <EOL> """<STR_LIT>""" <EOL> parser_ls_users = cmd_subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_ls_users . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_ls_users . add_argument ( <EOL> '<STR_LIT>' , type = int , <EOL> help = '<STR_LIT>' ) <EOL> parser_ls_users . set_defaults ( subcommand = LsUsersCommand ) <EOL> def RmUserCommand ( flags , user_api ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_api . DeleteDomainUser ( flags . user_email ) <EOL> except APIToolDirectoryUserError as e : <EOL> print '<STR_LIT>' % ( flags . user_email , e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print '<STR_LIT>' % flags . user_email <EOL> def SetupRmUserParser ( cmd_subparsers ) : <EOL> """<STR_LIT>""" <EOL> parser_rm_user = cmd_subparsers . add_parser ( <EOL> '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser_rm_user . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser_rm_user . set_defaults ( subcommand = RmUserCommand ) <EOL> def _ParseFlags ( argv ) : <EOL> """<STR_LIT>""" <EOL> argparser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' , <EOL> parents = [ auth_helper . ARG_PARSER ] ) <EOL> cmd_subparsers = argparser . add_subparsers ( ) <EOL> SetupAddUserParser ( cmd_subparsers ) <EOL> SetupLsUserParser ( cmd_subparsers ) <EOL> SetupLsUsersParser ( cmd_subparsers ) <EOL> SetupRmUserParser ( cmd_subparsers ) <EOL> flags = argparser . parse_args ( argv ) <EOL> logging . basicConfig ( level = flags . logging_level , <EOL> format = '<STR_LIT>' , <EOL> datefmt = '<STR_LIT>' ) <EOL> return flags <EOL> def main ( argv ) : <EOL> """<STR_LIT>""" <EOL> flags = _ParseFlags ( argv ) <EOL> user_api = UserDirectory ( auth_helper . GetAuthorizedHttp ( flags ) ) <EOL> flags . subcommand ( flags , user_api ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
