Permalink
Browse files

Added util.Gist with parsing

  • Loading branch information...
robertkrimen committed May 31, 2011
1 parent 8bd2ac0 commit 696c97c89fa156e1b37da1e2bfa7b6bfe84c88d2
Showing with 64 additions and 0 deletions.
  1. +26 −0 test/test0.py
  2. +38 −0 util.py
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# vim: ft=python:
+import random
+import unittest2
+import sys;
+import os;
+
+sys.path.append( os.path.join( sys.path[0], ".." ) )
+import util
+
+class t( unittest2.TestCase ):
+ def runTest( self ):
+ self.assertTrue( True, 'Xyzzy' )
+
+ parse = util.parse_github_path( 'robertkrimen/yzzy-projection/raw/master/src/yzzy/projection/View.as' )
+ self.assertEqual( parse['blob-path'], 'robertkrimen/yzzy-projection/blob/master/src/yzzy/projection/View.as' )
+ self.assertEqual( parse['raw-url'], 'https://github.com/robertkrimen/yzzy-projection/raw/master/src/yzzy/projection/View.as' )
+ self.assertEqual( parse['path'], 'src/yzzy/projection/View.as' )
+ self.assertEqual( parse['user-repository-branch-path'], 'robertkrimen/yzzy-projection/master/src/yzzy/projection/View.as' )
+ self.assertEqual( parse['user-repository'], 'robertkrimen/yzzy-projection' )
+
+ gist = util.Gist.parse( 'robertkrimen/yzzy-projection/raw/master/src/yzzy/projection/View.as' )
+ self.assertEqual( gist.user, 'robertkrimen' )
+
+if __name__ == '__main__':
+ unittest2.main()
View
38 util.py
@@ -2,6 +2,44 @@
import posixpath
import urlparse
+class Gist:
+ @classmethod
+ def parse( self, path ):
+ splitpath = posixpath.normpath( path ).split( '/' )
+ parse = {}
+
+ if len( splitpath ) < 5:
+ return None
+
+ # user / respository / format / branch / path
+ user = parse[ 'user' ] = splitpath[ 0 ]
+ repository = parse[ 'repository' ] = splitpath[ 1 ]
+ branch = parse[ 'branch' ] = splitpath[ 3 ]
+ path = parse[ 'path' ] = '/'.join( splitpath[ 4: ] )
+
+ # format = blob
+ splitpath[ 2 ] = 'blob'
+ blob_path = parse[ 'blob_path' ] = '/'.join( splitpath )
+ blob_url = parse[ 'blob_url' ] = urlparse.urljoin( 'https://github.com', blob_path )
+
+ # format = raw
+ splitpath[ 2 ] = 'raw'
+ raw_path = parse[ 'raw_path' ] = '/'.join( splitpath )
+ raw_url = parse[ 'raw_url' ] = urlparse.urljoin( 'https://github.com', raw_path )
+
+ user_repository = parse[ 'user_repository' ] = '/'.join([ user, repository ])
+ 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 )
+
+ 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' ]:
+ setattr( self, key, arguments[ key ] )
+
def parse_github_path( path ):
splitpath = posixpath.normpath( path ).split( '/' )
parse = {}

0 comments on commit 696c97c

Please sign in to comment.