Commit 17c597e3 by willmcgugan

Added a priority system for multifs

parent 8167f68a
...@@ -94,6 +94,7 @@ class MultiFS(FS): ...@@ -94,6 +94,7 @@ class MultiFS(FS):
self.auto_close = auto_close self.auto_close = auto_close
self.fs_sequence = [] self.fs_sequence = []
self.fs_lookup = {} self.fs_lookup = {}
self.fs_priorities = {}
self.writefs = None self.writefs = None
@synchronize @synchronize
...@@ -106,6 +107,9 @@ class MultiFS(FS): ...@@ -106,6 +107,9 @@ class MultiFS(FS):
def __unicode__(self): def __unicode__(self):
return u"<MultiFS: %s>" % ", ".join(unicode(fs) for fs in self.fs_sequence) return u"<MultiFS: %s>" % ", ".join(unicode(fs) for fs in self.fs_sequence)
def _get_priority(self, name):
return self.fs_priorities[name]
@synchronize @synchronize
def close(self): def close(self):
# Explicitly close if requested # Explicitly close if requested
...@@ -117,24 +121,39 @@ class MultiFS(FS): ...@@ -117,24 +121,39 @@ class MultiFS(FS):
# Discard any references # Discard any references
del self.fs_sequence[:] del self.fs_sequence[:]
self.fs_lookup.clear() self.fs_lookup.clear()
self.fs_priorities.clear()
self.writefs = None self.writefs = None
super(MultiFS, self).close() super(MultiFS, self).close()
def _priority_sort(self):
"""Sort filesystems by priority order"""
priority_order = sorted(self.fs_lookup.keys(), key=lambda n:self.fs_priorities[n], reverse=True)
self.fs_sequence = [self.fs_lookup[name] for name in priority_order]
@synchronize @synchronize
def addfs(self, name, fs, write=False): def addfs(self, name, fs, write=False, priority=0):
"""Adds a filesystem to the MultiFS. """Adds a filesystem to the MultiFS.
:param name: A unique name to refer to the filesystem being added. :param name: A unique name to refer to the filesystem being added.
The filesystem can later be retrieved by using this name as an index to the MultiFS, i.e. multifs['myfs'] The filesystem can later be retrieved by using this name as an index to the MultiFS, i.e. multifs['myfs']
:param fs: The filesystem to add :param fs: The filesystem to add
:param write: If this value is True, then the `fs` will be used as the writeable FS :param write: If this value is True, then the `fs` will be used as the writeable FS
:param priority: A number that gives the priorty of the filesystem being added.
Filesystems will be searched in descending priority order and then by the reverse order they were added.
So by default, the most recently added filesystem will be looked at first
""" """
if name in self.fs_lookup: if name in self.fs_lookup:
raise ValueError("Name already exists.") raise ValueError("Name already exists.")
priority = (priority, len(self.fs_sequence))
self.fs_priorities[name] = priority
self.fs_sequence.append(fs) self.fs_sequence.append(fs)
self.fs_lookup[name] = fs self.fs_lookup[name] = fs
self._priority_sort()
if write: if write:
self.setwritefs(fs) self.setwritefs(fs)
...@@ -166,6 +185,7 @@ class MultiFS(FS): ...@@ -166,6 +185,7 @@ class MultiFS(FS):
fs = self.fs_lookup[name] fs = self.fs_lookup[name]
self.fs_sequence.remove(fs) self.fs_sequence.remove(fs)
del self.fs_lookup[name] del self.fs_lookup[name]
self._priority_sort()
@synchronize @synchronize
def __getitem__(self, name): def __getitem__(self, name):
...@@ -173,7 +193,7 @@ class MultiFS(FS): ...@@ -173,7 +193,7 @@ class MultiFS(FS):
@synchronize @synchronize
def __iter__(self): def __iter__(self):
return reversed(self.fs_sequence[:]) return iter(self.fs_sequence[:])
def _delegate_search(self, path): def _delegate_search(self, path):
for fs in self: for fs in self:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment