<s> import json <EOL> import sys <EOL> import optparse <EOL> import requests <EOL> from acmd import tool , html <EOL> from acmd import USER_ERROR , SERVER_ERROR , OK , error <EOL> from acmd . tools import get_command , get_argument , 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:-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 GroupsTool ( object ) : <EOL> def execute ( self , server , argv ) : <EOL> options , args = parser . parse_args ( argv ) <EOL> action = get_command ( args , '<STR_LIT:list>' ) <EOL> groupname = get_argument ( args ) <EOL> if action == '<STR_LIT:list>' : <EOL> return list_groups ( server , options ) <EOL> elif action == '<STR_LIT>' : <EOL> return create_group ( server , options , groupname ) <EOL> if action == '<STR_LIT>' : <EOL> username = get_argument ( args , <NUM_LIT:3> ) <EOL> return add_user ( server , options , groupname , username ) <EOL> else : <EOL> parser . print_help ( ) <EOL> return USER_ERROR <EOL> def create_group ( server , options , name ) : <EOL> """<STR_LIT>""" <EOL> assert len ( name ) > <NUM_LIT:0> <EOL> form_data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : name <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_group_path ( groupname ) : <EOL> return "<STR_LIT>" . format ( c = groupname [ <NUM_LIT:0> ] , name = groupname ) <EOL> def add_user ( server , options , groupname , username ) : <EOL> """<STR_LIT>""" <EOL> group_path = get_group_path ( groupname ) <EOL> path = "<STR_LIT>" . format ( group_path ) <EOL> url = server . url ( path ) <EOL> props = { '<STR_LIT>' : username } <EOL> resp = requests . post ( url , auth = server . auth , data = props ) <EOL> if resp . status_code != <NUM_LIT:200> : <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 list_groups ( 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> elif options . compact : <EOL> for initial , group in filter_system ( data . items ( ) ) : <EOL> for groupname , userdata in filter_system ( group . items ( ) ) : <EOL> sys . stdout . write ( "<STR_LIT>" . format ( groupname ) ) <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 </s>
