#NEWLINE# @ requires ( str , 'uri' ) #NEWLINE# def fetch( self , environ , request , uri ) : #NEWLINE# #INDENT# args = { #NEWLINE# #INDENT# 'uri' : uri , #NEWLINE# 'after' : request . args . get ( 'after' , 0 ) , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# args [ 'limit' ] = int ( request . args . get ( 'limit' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# args [ 'limit' ] = None #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return BadRequest ( 'limit should be integer' ) #NEWLINE# #UNINDENT# if ( request . args . get ( 'parent' ) is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# args [ 'parent' ] = int ( request . args . get ( 'parent' ) ) #NEWLINE# root_id = args [ 'parent' ] #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return BadRequest ( 'parent should be integer' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# args [ 'parent' ] = None #NEWLINE# root_id = None #NEWLINE# #UNINDENT# plain = ( request . args . get ( 'plain' , '0' ) == '0' ) #NEWLINE# reply_counts = self . comments . reply_count ( uri , after = args [ 'after' ] ) #NEWLINE# if ( args [ 'limit' ] == 0 ) : #NEWLINE# #INDENT# root_list = [ ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# root_list = list ( self . comments . fetch ( ** args ) ) #NEWLINE# if ( not root_list ) : #NEWLINE# #INDENT# raise NotFound #NEWLINE# #UNINDENT# #UNINDENT# if ( root_id not in reply_counts ) : #NEWLINE# #INDENT# reply_counts [ root_id ] = 0 #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# nested_limit = int ( request . args . get ( 'nested_limit' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# nested_limit = None #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# return BadRequest ( 'nested_limit should be integer' ) #NEWLINE# #UNINDENT# rv = { #NEWLINE# #INDENT# 'id' : root_id , #NEWLINE# 'total_replies' : reply_counts [ root_id ] , #NEWLINE# 'hidden_replies' : ( reply_counts [ root_id ] - len ( root_list ) ) , #NEWLINE# 'replies' : self . _process_fetched_list ( root_list , plain ) , #NEWLINE# #UNINDENT# } #NEWLINE# if ( root_id is None ) : #NEWLINE# #INDENT# for comment in rv [ 'replies' ] : #NEWLINE# #INDENT# if ( comment [ 'id' ] in reply_counts ) : #NEWLINE# #INDENT# comment [ 'total_replies' ] = reply_counts [ comment [ 'id' ] ] #NEWLINE# if ( nested_limit is not None ) : #NEWLINE# #INDENT# if ( nested_limit > 0 ) : #NEWLINE# #INDENT# args [ 'parent' ] = comment [ 'id' ] #NEWLINE# args [ 'limit' ] = nested_limit #NEWLINE# replies = list ( self . comments . fetch ( ** args ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# replies = [ ] #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# args [ 'parent' ] = comment [ 'id' ] #NEWLINE# replies = list ( self . comments . fetch ( ** args ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# comment [ 'total_replies' ] = 0 #NEWLINE# replies = [ ] #NEWLINE# #UNINDENT# comment [ 'hidden_replies' ] = ( comment [ 'total_replies' ] - len ( replies ) ) #NEWLINE# comment [ 'replies' ] = self . _process_fetched_list ( replies , plain ) #NEWLINE# #UNINDENT# #UNINDENT# return JSON ( rv , 200 )