summaryrefslogtreecommitdiff
path: root/freebase/fcl
diff options
context:
space:
mode:
authoralecflett <alecflett@5914aa95-5b3a-0410-a3b5-7b719e7fe9b2>2009-12-22 01:31:27 +0000
committeralecflett <alecflett@5914aa95-5b3a-0410-a3b5-7b719e7fe9b2>2009-12-22 01:31:27 +0000
commit492d173e40a2331baedb7d4bafa043b16ddc6c1c (patch)
tree94488469f48494ac9b98101de591030b0a1ec24c /freebase/fcl
parentc78750cc146b31bd71bcaacedbec4954252aadee (diff)
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
Diffstat (limited to 'freebase/fcl')
-rwxr-xr-xfreebase/fcl/fbutil.py25
1 files changed, 18 insertions, 7 deletions
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
+def dirsplit_unsafe(id):
+ """
+ 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]
-DIRSPLIT = re.compile(r'^(.+)/([^/]+)$')
+ if not parts[0]:
+ return '/', parts[1]
-def dirsplit_unsafe(id):
- m = DIRSPLIT.match(id)
- if m is None:
- return (None, id)
- dir,file = m.groups()
- return (dir,file)
+ return parts
def dirsplit(id):
dir,file = dirsplit_unsafe(id)