pmacs3/buffer/fs.py

80 lines
2.4 KiB
Python
Raw Normal View History

2008-10-29 10:58:06 -04:00
import dirutil, os
from buffer import Buffer
class DirBuffer(Buffer):
btype = 'dir'
def __init__(self, path, name=None):
Buffer.__init__(self)
self.path = os.path.realpath(path)
def changed(self):
return False
def readonly(self):
return True
def name(self):
return self.path
def path_exists(self):
return os.path.exists(self.path)
def _get_names(self):
if not self.path_exists():
raise Exception, "directory %r does not exists" % self.path
names = os.listdir(self.path)
if self.path != '/':
names.insert(0, '..')
names.insert(0, '.')
return names
def _make_path(self, name):
return os.path.join(self.path, name)
def _get_lines(self):
names = self._get_names()
fieldlines = []
maxlens = [0] * 5
for name in names:
path = self._make_path(name)
fields = dirutil.path_fields(path, name)
for i in range(0, 5):
try:
maxlens[i] = max(maxlens[i], len(fields[i]))
except:
raise Exception, '%d %r' % (i, fields[i])
fieldlines.append(fields)
fieldlines.sort(cmp=dirutil.path_sort)
fmt = '%%%ds %%-%ds %%-%ds %%%ds %%%ds %%s' % tuple(maxlens)
lines = []
for fields in fieldlines:
s = fmt % fields
lines.append(s)
return lines
def open(self):
self.lines = self._get_lines()
def reload(self):
lines = self._get_lines()
self.set_lines(lines, force=True)
def save(self, force=False):
raise Exception, "can't save a directory buffer"
def save_as(self, path):
raise Exception, "can't save a directory buffer"
class PathListBuffer(DirBuffer):
btype = 'pathlist'
def __init__(self, name, paths):
Buffer.__init__(self)
self.paths = list(paths)
self.path = os.getcwd()
self._name = name
def path_exists(self):
raise Exception
def _get_names(self):
cwd = os.getcwd()
return [x.replace(cwd, '.', 1) for x in self.paths]
def _make_path(self, name):
if name.startswith('.'):
return name.replace('.', os.getcwd(), 1)
else:
return name
def name(self):
return self._name