<s> import sys <EOL> import optparse <EOL> import json <EOL> import requests <EOL> from acmd import tool , html , parse_properties <EOL> from acmd import USER_ERROR , SERVER_ERROR , OK , error <EOL> from acmd . tools import get_argument , get_command , filter_system <EOL> parser = optparse . OptionParser ( "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT>" , const = True , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> dest = "<STR_LIT:password>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , <EOL> action = "<STR_LIT>" , const = True , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> @ tool ( '<STR_LIT>' , [ '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class UserTool ( object ) : <EOL> def execute ( self , server , argv ) : <EOL> options , args = parser . parse_args ( argv ) <EOL> action = get_command ( args , '<STR_LIT:list>' ) <EOL> actionarg = get_argument ( args ) <EOL> if action == '<STR_LIT:list>' or action == '<STR_LIT>' : <EOL> return list_users ( server , options ) <EOL> elif action == '<STR_LIT>' : <EOL> return create_user ( server , options , actionarg ) <EOL> elif action == '<STR_LIT>' : <EOL> username = actionarg <EOL> propstring = get_argument ( argv , <NUM_LIT:3> ) <EOL> props = parse_properties ( propstring ) <EOL> return set_profile_properties ( server , options , username , props ) <EOL> else : <EOL> parser . print_help ( ) <EOL> return USER_ERROR <EOL> def list_users ( server , options ) : <EOL> url = server . url ( '<STR_LIT>' ) <EOL> resp = requests . get ( url , auth = server . auth ) <EOL> if resp . status_code != <NUM_LIT:200> : <EOL> error ( "<STR_LIT>" . format ( resp . content ) ) <EOL> return SERVER_ERROR <EOL> data = resp . json ( ) <EOL> if options . raw : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( json . dumps ( data , indent = <NUM_LIT:4> ) ) ) <EOL> if options . compact : <EOL> for initial , group in filter_system ( data . items ( ) ) : <EOL> for username , userdata in filter_system ( group . items ( ) ) : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( username ) ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> for initial , group in filter_system ( data . items ( ) ) : <EOL> for username , userdata in filter_system ( group . items ( ) ) : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( username ) ) <EOL> return OK <EOL> def create_user ( server , options , username ) : <EOL> """<STR_LIT>""" <EOL> assert len ( username ) > <NUM_LIT:0> <EOL> form_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : username , <EOL> '<STR_LIT>' : options . password <EOL> } <EOL> url = server . url ( '<STR_LIT>' ) <EOL> resp = requests . post ( url , auth = server . auth , data = form_data ) <EOL> if resp . status_code != <NUM_LIT> : <EOL> error ( "<STR_LIT>" . format ( resp . content ) ) <EOL> return SERVER_ERROR <EOL> if options . raw : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( resp . content ) ) <EOL> else : <EOL> path = html . parse_value ( resp . text , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . stdout . write ( "<STR_LIT>" . format ( path ) ) <EOL> return OK <EOL> def get_user_path ( username ) : <EOL> return "<STR_LIT>" . format ( c = username [ <NUM_LIT:0> ] , name = username ) <EOL> def set_profile_properties ( server , options , username , props ) : <EOL> """<STR_LIT>""" <EOL> user_path = get_user_path ( username ) <EOL> path = "<STR_LIT>" . format ( user_path ) <EOL> url = server . url ( path ) <EOL> props = { '<STR_LIT>' + k : v for k , v in props . items ( ) } <EOL> resp = requests . post ( url , auth = server . auth , data = props ) <EOL> if resp . status_code != <NUM_LIT:200> : <EOL> sys . stderr . write ( "<STR_LIT>" . format ( path , resp . status_code ) ) <EOL> return SERVER_ERROR <EOL> if options . raw : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( resp . content ) ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( user_path ) ) <EOL> return OK </s>
