Skip to content

Commit

Permalink
Support for negative indices; more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robertkrimen committed Aug 23, 2011
1 parent 3a3803b commit ea13709
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
15 changes: 2 additions & 13 deletions app.py
Expand Up @@ -31,18 +31,7 @@
jinja2 = jinja2_.Environment( loader=jinja2_.FileSystemLoader( 'jinja2-assets' ) )

import gist_it

'''
Take a (line) slice of content, based on a start/end index
'''
def take_slice(content, start_line = 0, end_line = 0):
if (start_line == 0 and end_line == 0):
return content

if (end_line == 0):
return '\n'.join(content.splitlines()[start_line:])

return '\n'.join(content.splitlines()[start_line:end_line])
from gist_it import take_slice

def render_gist_html( base, gist, document ):
result = jinja2.get_template( 'gist.jinja.html' ).render( cgi = cgi, base = base, gist = gist, document = document )
Expand Down Expand Up @@ -84,7 +73,7 @@ def get( self, location ):
return

else:
gist = gist_it.Gist.parse( location, slice = self.request.get( 'slice' ) )
gist = gist_it.Gist.parse( location, slice_ = self.request.get( 'slice' ) )
if not gist:
self.response.set_status( 500 )
self.response.out.write( "Unable to parse \"%s\": Not a valid repository path?" % ( location ) )
Expand Down
24 changes: 21 additions & 3 deletions gist_it.py
Expand Up @@ -9,10 +9,22 @@ def parse( location, **arguments ):
return Gist.parse( location, **arguments )

def parse_slice( slice_ ):
match = re.match( r'^(\d*):?(\d*)$', slice_ )
match = re.match( r'^(-?\d+)?:?(-?\d+)?$', slice_ )
if match is None:
return ( 0, 0 )
return map( lambda _: int(_) if len(_) else 0, match.groups(0) )
return map( lambda _: int(_) if _ is not None else 0, match.groups(0) )

'''
Take a (line) slice of content, based on a start/end index
'''
def take_slice( content, start_line = 0, end_line = 0 ):
if (start_line == 0 and end_line == 0):
return content

if (end_line == 0):
return '\n'.join(content.splitlines()[start_line:])

return '\n'.join(content.splitlines()[start_line:end_line])

class Gist:

Expand Down Expand Up @@ -56,12 +68,18 @@ def parse( self, location, slice_ = '' ):
user_repository_branch_path = parse[ 'user_repository_branch_path' ] = '/'.join([ user_repository, branch, path ]);
user_repository_url = parse[ 'user_repository_url' ] = urlparse.urljoin( 'https://github.com', user_repository )

slice_ = parse_slice( slice_ )
parse[ 'start_line' ] = slice_[0]
parse[ 'end_line' ] = slice_[1]

return Gist( **parse )

def __init__( self, **arguments ):
for key in [ 'user', 'repository', 'branch', 'path',
'blob_path', 'blob_url',
'raw_path', 'raw_url',
'user_repository', 'user_repository_branch_path', 'user_repository_url' ]:
'user_repository', 'user_repository_branch_path', 'user_repository_url',
'start_line', 'end_line',
]:
setattr( self, key, arguments[ key ] )

6 changes: 6 additions & 0 deletions test/test_gist_it.py
Expand Up @@ -30,7 +30,13 @@ def runTest( self ):
self.assertEqual( gist.user_repository_branch_path, 'robertkrimen/yzzy-projection/master/src/yzzy/projection/View.as' )
self.assertEqual( gist.user_repository_url, 'https://github.com/robertkrimen/yzzy-projection' )

self.assertEqual( gist.start_line, 0 )
self.assertEqual( gist.end_line, 0 )

gist = gist_it.parse( 'github/robertkrimen/yzzy-projection/raw/master/src/yzzy/projection/View.as', slice_ = '1:' )

self.assertEqual( gist.start_line, 1 )
self.assertEqual( gist.end_line, 0 )

if __name__ == '__main__':
unittest2.main()
33 changes: 33 additions & 0 deletions test/test_slice.py
Expand Up @@ -50,5 +50,38 @@ def runTest( self ):
self.assertEqual( slice_[0], 0 )
self.assertEqual( slice_[1], 0 )

slice_ = gist_it.parse_slice( '-0:-0' )
self.assertEqual( len( slice_ ), 2 )
self.assertEqual( slice_[0], 0 )
self.assertEqual( slice_[1], 0 )

slice_ = gist_it.parse_slice( '-1:' )
self.assertEqual( len( slice_ ), 2 )
self.assertEqual( slice_[0], -1 )
self.assertEqual( slice_[1], 0 )

content = """
Line 2
Line 3
Line 4
Line 5
Line 6
Line 8
"""
self.assertEqual( gist_it.take_slice( content, 0, 0 ), content )
self.assertEqual( gist_it.take_slice( content, 1, 2 ), "Line 2" )
self.assertEqual( gist_it.take_slice( content, 0, 2 ), "\nLine 2" )
self.assertEqual( gist_it.take_slice( content, 0, -1 ), """
Line 2
Line 3
Line 4
Line 5
Line 6
""" )
self.assertEqual( gist_it.take_slice( content, -1, 0 ), "Line 8" )



if __name__ == '__main__':
unittest2.main()

0 comments on commit ea13709

Please sign in to comment.