<s> import json <EOL> import ufora . util . Unicode as Unicode <EOL> class RequestError ( Exception ) : <EOL> pass <EOL> valid_core_counts = [ <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> ] <EOL> valid_machine_states = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def formatClusterStatus ( status ) : <EOL> result = { } <EOL> for key in status . iterkeys ( ) : <EOL> if key == '<STR_LIT:type>' : <EOL> continue <EOL> elif key == '<STR_LIT>' and status [ key ] is not None : <EOL> result [ '<STR_LIT>' ] = round ( status [ key ] / <NUM_LIT> ) <EOL> elif key in valid_machine_states : <EOL> sizeToCount = status [ key ] <EOL> for size in sizeToCount . iterkeys ( ) : <EOL> if size != '<STR_LIT>' : <EOL> assert ( sizeToCount [ size ] == <NUM_LIT:0> ) <EOL> else : <EOL> result [ key ] = <NUM_LIT:8> * sizeToCount [ size ] <EOL> return result <EOL> class CoresController ( object ) : <EOL> def __init__ ( self , transport , username ) : <EOL> self . transport = transport <EOL> self . username = username <EOL> def set ( self , numOfCores , quiet = False ) : <EOL> if numOfCores not in valid_core_counts : <EOL> raise RequestError ( <EOL> "<STR_LIT>" % valid_core_counts <EOL> ) <EOL> if not quiet : <EOL> print "<STR_LIT>" , numOfCores <EOL> self . transport . desireMachines ( numOfCores / <NUM_LIT:8> , machineType = '<STR_LIT>' ) <EOL> def get ( self , quiet = False ) : <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> requestUri = "<STR_LIT>" % self . username <EOL> status , result = self . transport . submitQuery ( requestUri ) <EOL> if status != <NUM_LIT:200> : <EOL> raise RequestError ( status , result . content ) <EOL> return formatClusterStatus ( <EOL> json . loads ( result . content , object_hook = Unicode . convertToStringRecursively ) <EOL> ) <EOL> def renew ( self , quiet = False ) : <EOL> if not quiet : <EOL> print '<STR_LIT>' <EOL> currentStatus = self . get ( quiet = True ) <EOL> self . set ( currentStatus [ '<STR_LIT>' ] , quiet = True ) <EOL> return self . get ( quiet = True ) </s>
