<s> import copy <EOL> import os <EOL> from rack . api import common <EOL> def get_view_builder ( req ) : <EOL> base_url = req . application_url <EOL> return ViewBuilder ( base_url ) <EOL> class ViewBuilder ( common . ViewBuilder ) : <EOL> def __init__ ( self , base_url ) : <EOL> """<STR_LIT>""" <EOL> self . base_url = base_url <EOL> def build_choices ( self , VERSIONS , req ) : <EOL> version_objs = [ ] <EOL> for version in VERSIONS : <EOL> version = VERSIONS [ version ] <EOL> version_objs . append ( { <EOL> "<STR_LIT:id>" : version [ '<STR_LIT:id>' ] , <EOL> "<STR_LIT:status>" : version [ '<STR_LIT:status>' ] , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : self . generate_href ( version [ '<STR_LIT:id>' ] , req . path ) , <EOL> } , <EOL> ] , <EOL> "<STR_LIT>" : version [ '<STR_LIT>' ] , <EOL> } ) <EOL> return dict ( choices = version_objs ) <EOL> def build_versions ( self , versions ) : <EOL> version_objs = [ ] <EOL> for version in sorted ( versions . keys ( ) ) : <EOL> version = versions [ version ] <EOL> version_objs . append ( { <EOL> "<STR_LIT:id>" : version [ '<STR_LIT:id>' ] , <EOL> "<STR_LIT:status>" : version [ '<STR_LIT:status>' ] , <EOL> "<STR_LIT>" : version [ '<STR_LIT>' ] , <EOL> "<STR_LIT>" : self . _build_links ( version ) , <EOL> } ) <EOL> return dict ( versions = version_objs ) <EOL> def build_version ( self , version ) : <EOL> reval = copy . deepcopy ( version ) <EOL> reval [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : self . base_url . rstrip ( '<STR_LIT:/>' ) + '<STR_LIT:/>' , <EOL> } ) <EOL> return dict ( version = reval ) <EOL> def _build_links ( self , version_data ) : <EOL> """<STR_LIT>""" <EOL> href = self . generate_href ( version_data [ '<STR_LIT:id>' ] ) <EOL> links = [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : href , <EOL> } , <EOL> ] <EOL> return links <EOL> def generate_href ( self , version , path = None ) : <EOL> """<STR_LIT>""" <EOL> prefix = self . _update_compute_link_prefix ( self . base_url ) <EOL> if version . find ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> version_number = '<STR_LIT>' <EOL> else : <EOL> version_number = '<STR_LIT>' <EOL> if path : <EOL> path = path . strip ( '<STR_LIT:/>' ) <EOL> return os . path . join ( prefix , version_number , path ) <EOL> else : <EOL> return os . path . join ( prefix , version_number ) + '<STR_LIT:/>' </s>
