From 492d173e40a2331baedb7d4bafa043b16ddc6c1c Mon Sep 17 00:00:00 2001 From: alecflett Date: Tue, 22 Dec 2009 01:31:27 +0000 Subject: fix dirsplit to deal with root-level paths, cleaning it up and eliminating regexp-overkill git-svn-id: http://freebase-python.googlecode.com/svn/trunk@238 5914aa95-5b3a-0410-a3b5-7b719e7fe9b2 --- freebase/fcl/fbutil.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'freebase/fcl/fbutil.py') diff --git a/freebase/fcl/fbutil.py b/freebase/fcl/fbutil.py index 1aaea5f..7ba051f 100755 --- a/freebase/fcl/fbutil.py +++ b/freebase/fcl/fbutil.py @@ -64,16 +64,27 @@ for k,vs in media_types.items(): media_type_to_extension[v] = k - - -DIRSPLIT = re.compile(r'^(.+)/([^/]+)$') - def dirsplit_unsafe(id): - m = DIRSPLIT.match(id) - if m is None: - return (None, id) - dir,file = m.groups() - return (dir,file) + """ + Split a freebase id into a prefix and a tail, dealing with + trailing slashes, etc. + + '/abc' -> '/', 'abc' + '/foo/bar' -> '/foo', 'bar' + 'foo/bar' -> 'foo', 'bar' + 'foo/bar/' -> 'foo', 'bar' + """ + id = id.rstrip('/') + parts = id.rsplit("/", 1) + + # if no "/" like "foo", then this comes back as ["foo"] + if len(parts) == 1: + return None, parts[0] + + if not parts[0]: + return '/', parts[1] + + return parts def dirsplit(id): dir,file = dirsplit_unsafe(id) -- cgit v1.3.1