| 1 |
from zope.interface import Interface, Attribute, implements |
|---|
| 2 |
|
|---|
| 3 |
def getAbsoluteSegments(path, cwd='/'): |
|---|
| 4 |
""" |
|---|
| 5 |
@param path: either a string or a list of string segments |
|---|
| 6 |
which specifys the desired path. may be relative to the cwd |
|---|
| 7 |
|
|---|
| 8 |
@param cwd: optional string specifying the current working directory |
|---|
| 9 |
|
|---|
| 10 |
returns a list of string segments which most succinctly |
|---|
| 11 |
describe how to get to path from root |
|---|
| 12 |
""" |
|---|
| 13 |
if not isinstance(path, list): paths = path.split("/") |
|---|
| 14 |
else: paths = path |
|---|
| 15 |
|
|---|
| 16 |
if len(paths) and paths[0] == "": |
|---|
| 17 |
paths = paths[1:] |
|---|
| 18 |
else: |
|---|
| 19 |
paths = cwd.split("/") + paths |
|---|
| 20 |
|
|---|
| 21 |
result = [] |
|---|
| 22 |
|
|---|
| 23 |
for path in paths: |
|---|
| 24 |
if path == "..": |
|---|
| 25 |
if len(result) > 1: |
|---|
| 26 |
result = result[:-1] |
|---|
| 27 |
else: |
|---|
| 28 |
result = [] |
|---|
| 29 |
|
|---|
| 30 |
elif path not in ("", "."): |
|---|
| 31 |
result.append(path) |
|---|
| 32 |
|
|---|
| 33 |
return result |
|---|
| 34 |
|
|---|
| 35 |
def fetch(root, path, cwd='/'): |
|---|
| 36 |
""" |
|---|
| 37 |
@param root: IFileSystemContainer which represents the root node |
|---|
| 38 |
of the filesystem |
|---|
| 39 |
|
|---|
| 40 |
@param path: either a string or a list of string segments |
|---|
| 41 |
which specifys the desired path. may be relative to the cwd |
|---|
| 42 |
|
|---|
| 43 |
@param cwd: optional string specifying the current working directory |
|---|
| 44 |
|
|---|
| 45 |
returns node described by path relative to the cwd |
|---|
| 46 |
""" |
|---|
| 47 |
paths = getAbsoluteSegments(path, cwd) |
|---|
| 48 |
currNode = root |
|---|
| 49 |
for path in paths: |
|---|
| 50 |
currNode = currNode.child(path) |
|---|
| 51 |
return currNode |
|---|
| 52 |
|
|---|
| 53 |
def basename(path, cwd='/'): |
|---|
| 54 |
s = getAbsoluteSegments(path, cwd) |
|---|
| 55 |
if s: |
|---|
| 56 |
return s[-1] |
|---|
| 57 |
return '' |
|---|
| 58 |
|
|---|
| 59 |
def dirname(path, cwd='/'): |
|---|
| 60 |
return "/" + "/".join(getAbsoluteSegments(path, cwd)[:-1]) |
|---|
| 61 |
|
|---|
| 62 |
def getRoot(node): |
|---|
| 63 |
while node.parent is not node: |
|---|
| 64 |
node = node.parent |
|---|
| 65 |
return node |
|---|
| 66 |
|
|---|
| 67 |
def getSegments(node): |
|---|
| 68 |
ret = [] |
|---|
| 69 |
while node.parent is not node: |
|---|
| 70 |
ret.append(node.name) |
|---|
| 71 |
node = node.parent |
|---|
| 72 |
ret.reverse() |
|---|
| 73 |
return ret |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
class IFileSystem(Interface): |
|---|
| 80 |
|
|---|
| 81 |
root = Attribute("root IFileSystemNode of the IFileSystem") |
|---|
| 82 |
pathToCWD = Attribute("path to current working directory") |
|---|
| 83 |
|
|---|
| 84 |
def absPath(path): |
|---|
| 85 |
""" |
|---|
| 86 |
returns a normalized absolutized version of the pathname path |
|---|
| 87 |
""" |
|---|
| 88 |
|
|---|
| 89 |
def splitPath(path): |
|---|
| 90 |
""" |
|---|
| 91 |
returns a normalized absolutized version of the pathname path |
|---|
| 92 |
split on the filesystem's directory seperator |
|---|
| 93 |
""" |
|---|
| 94 |
|
|---|
| 95 |
def joinPath(tail, head): |
|---|
| 96 |
""" |
|---|
| 97 |
joins the two paths, tail and head |
|---|
| 98 |
""" |
|---|
| 99 |
|
|---|
| 100 |
def dirname(path): |
|---|
| 101 |
""" |
|---|
| 102 |
returns the directory name of the container for path |
|---|
| 103 |
""" |
|---|
| 104 |
|
|---|
| 105 |
def basename(path): |
|---|
| 106 |
""" |
|---|
| 107 |
returns the base name of pathname path |
|---|
| 108 |
""" |
|---|
| 109 |
|
|---|
| 110 |
def fetch(path): |
|---|
| 111 |
""" |
|---|
| 112 |
returns a node object representing the file with pathname path |
|---|
| 113 |
""" |
|---|
| 114 |
|
|---|
| 115 |
def _getImplicitChildren(dir): |
|---|
| 116 |
""" |
|---|
| 117 |
returns implicit children for a given dir |
|---|
| 118 |
this is placed in the filesystem so that the same |
|---|
| 119 |
directory can have different implicit children depending |
|---|
| 120 |
on what sort of filesystem it has been placed in |
|---|
| 121 |
|
|---|
| 122 |
(This may not be the best idea) |
|---|
| 123 |
|
|---|
| 124 |
returns a list of 2 element tuples, C{[ ( path, nodeObject ) ]}, e.g.:: |
|---|
| 125 |
|
|---|
| 126 |
[ ( ".", dir ), ( "..", dir.parent ) ] |
|---|
| 127 |
""" |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
class FileSystem: |
|---|
| 131 |
""" |
|---|
| 132 |
Wraps unix-like VFS backends, in which directory separator is '/', |
|---|
| 133 |
root's path is '/', and all directories have '.' and '..'. |
|---|
| 134 |
|
|---|
| 135 |
Effectively, this is just a convenience wrapper around the other |
|---|
| 136 |
functions in this module which remembers the root node and the |
|---|
| 137 |
current working directory. |
|---|
| 138 |
""" |
|---|
| 139 |
|
|---|
| 140 |
implements(IFileSystem) |
|---|
| 141 |
|
|---|
| 142 |
def __init__(self, root, pathToCWD="/"): |
|---|
| 143 |
self.root = root |
|---|
| 144 |
self.root.filesystem = self |
|---|
| 145 |
self.pathToCWD = pathToCWD |
|---|
| 146 |
|
|---|
| 147 |
def absPath(self, path): |
|---|
| 148 |
return "/" + "/".join(self.splitPath(path)) |
|---|
| 149 |
|
|---|
| 150 |
def splitPath(self, path): |
|---|
| 151 |
return getAbsoluteSegments(path, self.pathToCWD) |
|---|
| 152 |
|
|---|
| 153 |
def joinPath(self, tail, head): |
|---|
| 154 |
if tail == "/": |
|---|
| 155 |
return tail + head |
|---|
| 156 |
else: |
|---|
| 157 |
return tail + "/" + head |
|---|
| 158 |
|
|---|
| 159 |
def dirname(self, path): |
|---|
| 160 |
return dirname(path, self.pathToCWD) |
|---|
| 161 |
|
|---|
| 162 |
def basename(self, path): |
|---|
| 163 |
return basename(path, self.pathToCWD) |
|---|
| 164 |
|
|---|
| 165 |
def fetch(self, pathToFile="."): |
|---|
| 166 |
return fetch(self.root, pathToFile, self.pathToCWD) |
|---|
| 167 |
|
|---|
| 168 |
def _getImplicitChildren(self, dir): |
|---|
| 169 |
return [(".", dir), ("..", dir.parent)] |
|---|
| 170 |
|
|---|
| 171 |
|
|---|