<s> import zlib <EOL> from . urlhelper import UrlHelper <EOL> from . constants import SHARD_STRATEGY_CYCLE <EOL> from . constants import SHARD_STRATEGY_CRC <EOL> from . constants import SIGNATURE_MODE_QUERY <EOL> class UrlBuilder ( object ) : <EOL> def __init__ ( <EOL> self , <EOL> domains , <EOL> use_https = True , <EOL> sign_key = None , <EOL> sign_mode = SIGNATURE_MODE_QUERY , <EOL> shard_strategy = SHARD_STRATEGY_CRC , <EOL> sign_with_library_version = True ) : <EOL> if not isinstance ( domains , list ) : <EOL> domains = [ domains ] <EOL> self . _domains = domains <EOL> self . _sign_key = sign_key <EOL> self . _sign_mode = sign_mode <EOL> self . _use_https = use_https <EOL> self . _shard_strategy = shard_strategy <EOL> self . _shard_next_index = <NUM_LIT:0> <EOL> self . _sign_with_library_version = sign_with_library_version <EOL> def create_url ( self , path , opts = { } ) : <EOL> if self . _shard_strategy == SHARD_STRATEGY_CRC : <EOL> crc = zlib . crc32 ( path . encode ( '<STR_LIT:utf-8>' ) ) & <NUM_LIT> <EOL> index = crc % len ( self . _domains ) <EOL> domain = self . _domains [ index ] <EOL> elif self . _shard_strategy == SHARD_STRATEGY_CYCLE : <EOL> domain = self . _domains [ self . _shard_next_index ] <EOL> self . _shard_next_index = ( <EOL> self . _shard_next_index + <NUM_LIT:1> ) % len ( self . _domains ) <EOL> else : <EOL> domain = self . _domains [ <NUM_LIT:0> ] <EOL> scheme = "<STR_LIT>" if self . _use_https else "<STR_LIT:http>" <EOL> url_obj = UrlHelper ( <EOL> domain , <EOL> path , <EOL> scheme , <EOL> sign_key = self . _sign_key , <EOL> sign_mode = self . _sign_mode , <EOL> sign_with_library_version = self . _sign_with_library_version , <EOL> opts = opts ) <EOL> return str ( url_obj ) </s>
