2007-07-19 14:28:21 -04:00
|
|
|
import datetime, md5, os, pwd, re, sets, shutil, stat
|
2007-06-05 00:49:24 -04:00
|
|
|
import aes, regex, highlight2
|
2007-06-04 22:30:58 -04:00
|
|
|
from point2 import Point
|
|
|
|
|
|
|
|
# undo/redo stack constants
|
|
|
|
ACT_NORM = 0
|
|
|
|
ACT_UNDO = 1
|
|
|
|
ACT_REDO = 2
|
|
|
|
STACK_LIMIT = 1024
|
|
|
|
|
|
|
|
# used for undo/redo stacks when text will need to be added back
|
|
|
|
class AddMove:
|
|
|
|
def __init__(self, buffer, p, lines):
|
|
|
|
self.buffer = buffer
|
|
|
|
self.p = p
|
|
|
|
self.lines = lines
|
|
|
|
def restore(self, act=ACT_UNDO):
|
|
|
|
assert act == ACT_UNDO or act == ACT_REDO
|
|
|
|
self.buffer.insert_lines(self.p, self.lines, act)
|
2007-07-19 12:38:17 -04:00
|
|
|
def getpos(self):
|
|
|
|
return self.p
|
2007-06-04 22:30:58 -04:00
|
|
|
|
|
|
|
# used for undo/redo stacks when text will need to be removed
|
|
|
|
class DelMove:
|
|
|
|
def __init__(self, buffer, p1, p2):
|
|
|
|
self.buffer = buffer
|
|
|
|
self.p1 = p1
|
|
|
|
self.p2 = p2
|
|
|
|
def restore(self, act):
|
|
|
|
assert act == ACT_UNDO or act == ACT_REDO
|
|
|
|
self.buffer.delete(self.p1, self.p2, act)
|
2007-07-19 12:38:17 -04:00
|
|
|
def getpos(self):
|
|
|
|
return self.p1
|
2007-06-04 22:30:58 -04:00
|
|
|
|
|
|
|
# abstract class
|
|
|
|
class Buffer(object):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'generic'
|
2007-06-04 22:30:58 -04:00
|
|
|
def __init__(self, nl='\n', stack_limit=STACK_LIMIT):
|
|
|
|
assert nl in ('\n', '\r', '\r\n'), "Invalid line ending"
|
|
|
|
self.lines = [""]
|
|
|
|
self.windows = []
|
|
|
|
self.undo_stack = []
|
|
|
|
self.redo_stack = []
|
|
|
|
self.stack_limit = stack_limit
|
|
|
|
self.nl = nl
|
|
|
|
self.modified = False
|
2007-06-05 00:49:24 -04:00
|
|
|
self.highlights = {}
|
2007-06-04 22:30:58 -04:00
|
|
|
|
|
|
|
# basic file operation stuff
|
|
|
|
def _open_file_r(self, path):
|
|
|
|
path = os.path.realpath(path)
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
raise Exception, "Path '%s' does not exist" % (path)
|
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
raise Exception, "Path '%s' cannot be read" % (path)
|
|
|
|
f = open(path, 'r')
|
|
|
|
return f
|
|
|
|
def _open_file_w(self, path):
|
|
|
|
if os.path.isfile(path):
|
|
|
|
raise Exception, "Path '%s' already exists" % (path)
|
|
|
|
d = os.path.dirname(path)
|
|
|
|
if not os.access(d, os.R_OK):
|
|
|
|
raise Exception, "Dir '%s' cannot be read" % (path)
|
|
|
|
if not os.access(d, os.W_OK):
|
|
|
|
raise Exception, "Dir '%s' cannot be written" % (path)
|
|
|
|
f = open(path, 'w')
|
|
|
|
return f
|
|
|
|
def _temp_path(self, path):
|
|
|
|
(dirname, basename) = os.path.split(path)
|
|
|
|
return os.path.join(dirname, ".__%s__pmacs" % (basename))
|
|
|
|
|
|
|
|
# undo/redo stack
|
|
|
|
def _stack_trim(self, stack):
|
|
|
|
if self.stack_limit:
|
|
|
|
while len(stack) > self.stack_limit:
|
|
|
|
stack.pop(0)
|
|
|
|
def add_to_stack(self, move, act):
|
|
|
|
if act == ACT_NORM:
|
|
|
|
self.redo_stack = []
|
|
|
|
self.undo_stack.append(move)
|
|
|
|
self._stack_trim(self.undo_stack)
|
|
|
|
elif act == ACT_UNDO:
|
|
|
|
self.redo_stack.append(move)
|
|
|
|
self._stack_trim(self.redo_stack)
|
|
|
|
elif act == ACT_REDO:
|
|
|
|
self.undo_stack.append(move)
|
|
|
|
self._stack_trim(self.undo_stack)
|
|
|
|
else:
|
|
|
|
raise Exception, "Invalid act: %d" % (act)
|
|
|
|
def undo(self):
|
|
|
|
if len(self.undo_stack):
|
|
|
|
move = self.undo_stack.pop(-1)
|
2007-06-26 17:28:38 -04:00
|
|
|
move.restore(ACT_UNDO)
|
2007-07-19 12:38:17 -04:00
|
|
|
return move.getpos()
|
2007-06-04 22:30:58 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "Nothing to Undo!"
|
|
|
|
def redo(self):
|
|
|
|
if len(self.redo_stack):
|
|
|
|
move = self.redo_stack.pop(-1)
|
2007-06-26 17:28:38 -04:00
|
|
|
move.restore(ACT_REDO)
|
2007-07-19 12:38:17 -04:00
|
|
|
return move.getpos()
|
2007-06-04 22:30:58 -04:00
|
|
|
else:
|
|
|
|
raise Exception, "Nothing to Redo!"
|
|
|
|
|
|
|
|
# window-buffer communication
|
|
|
|
def add_window(self, w):
|
|
|
|
if w not in self.windows:
|
|
|
|
self.windows.append(w)
|
2007-06-05 00:49:24 -04:00
|
|
|
modename = w.mode.name()
|
|
|
|
if modename not in self.highlights and w.mode.lexer is not None:
|
|
|
|
self.highlights[modename] = highlight2.Highlighter(w.mode.lexer)
|
|
|
|
self.highlights[modename].highlight(self.lines)
|
2007-06-17 22:14:07 -04:00
|
|
|
#if modename not in self.tabbing and w.mode.tabber is not None:
|
|
|
|
# self.tabbing[modename] =
|
2007-06-04 22:30:58 -04:00
|
|
|
def remove_window(self, w):
|
|
|
|
if w in self.windows:
|
|
|
|
self.windows.remove(w)
|
2007-06-05 00:49:24 -04:00
|
|
|
modename = w.mode.name()
|
|
|
|
found = False
|
|
|
|
for w2 in self.windows:
|
|
|
|
if w2.mode.name() == modename:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if not found:
|
|
|
|
del self.highlights[modename]
|
2007-06-04 22:30:58 -04:00
|
|
|
def _region_add(self, p1, p2, lines, act):
|
|
|
|
move = DelMove(self, p1, p2)
|
|
|
|
self.add_to_stack(move, act)
|
|
|
|
for w in self.windows:
|
|
|
|
w.region_added(p1, lines)
|
2007-06-05 00:49:24 -04:00
|
|
|
for name in self.highlights:
|
|
|
|
self.highlights[name].relex_add(self.lines, p1.y, p1.x, lines)
|
2007-06-04 22:30:58 -04:00
|
|
|
def _region_del(self, p1, p2, lines, act):
|
|
|
|
move = AddMove(self, p1, lines)
|
|
|
|
self.add_to_stack(move, act)
|
|
|
|
for w in self.windows:
|
|
|
|
w.region_removed(p1, p2)
|
2007-06-05 00:49:24 -04:00
|
|
|
for name in self.highlights:
|
|
|
|
self.highlights[name].relex_del(self.lines, p1.y, p1.x, p2.y, p2.x)
|
2007-06-04 22:30:58 -04:00
|
|
|
|
|
|
|
# internal validation
|
|
|
|
def _validate_point(self, p):
|
|
|
|
self._validate_xy(p.x, p.y)
|
|
|
|
def _validate_xy(self, x, y):
|
|
|
|
assert y >= 0 and y < len(self.lines), \
|
|
|
|
"xy1: %d >= 0 and %d < %d" % (y, y, len(self.lines))
|
|
|
|
assert x >= 0 and x <= len(self.lines[y]), \
|
|
|
|
"xy2: %d >= 0 and %d <= %d" % (x, x, len(self.lines[y]))
|
|
|
|
def _validate_y(self, y):
|
|
|
|
assert y >= 0 and y < len(self.lines), \
|
|
|
|
"y: %d >= 0 and %d < %d" % (y, y, len(self.lines))
|
|
|
|
|
|
|
|
# deal with the actual logical document string
|
|
|
|
def num_chars(self):
|
|
|
|
n = 0
|
|
|
|
for line in self.lines[:-1]:
|
|
|
|
n += len(line) + 1
|
|
|
|
n += len(self.lines[-1])
|
|
|
|
return n
|
|
|
|
def num_lines(self):
|
|
|
|
return len(self.lines)
|
|
|
|
def make_string(self, nl='\n'):
|
|
|
|
return nl.join(self.lines)
|
|
|
|
|
|
|
|
# methods to be overridden by subclasses
|
|
|
|
def name(self):
|
|
|
|
return "Generic"
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
def open(self):
|
|
|
|
pass
|
|
|
|
def changed(self):
|
|
|
|
return self.modified
|
|
|
|
def reload(self):
|
|
|
|
raise Exception, "%s reload: Unimplemented" % (self.name())
|
|
|
|
def save_as(self, path, force=False):
|
|
|
|
# check to see if the path exists, and if we're prepared to overwrite it
|
|
|
|
# if yes to both, get its mode so we can preserve the path's permissions
|
|
|
|
mode = None
|
|
|
|
if os.path.exists(path):
|
|
|
|
if force:
|
|
|
|
mode = os.stat(self.path)[0]
|
|
|
|
else:
|
|
|
|
raise Exception, "oh no! %r already exists" % path
|
|
|
|
|
|
|
|
# create the string that we're going to write into the file
|
|
|
|
data = self.write_filter(self.make_string(nl=self.nl))
|
|
|
|
|
|
|
|
# create a safe temporary path to write to, and write out data to it
|
|
|
|
temp_path = self._temp_path()
|
|
|
|
f2 = self._open_file_w(temp_path)
|
|
|
|
f2.write(data)
|
|
|
|
f2.close()
|
|
|
|
|
|
|
|
# move the temporary file to the actual path; maybe change permissions
|
|
|
|
shutil.move(temp_path, path)
|
|
|
|
if mode:
|
|
|
|
os.chmod(path, mode)
|
|
|
|
|
|
|
|
# the file has not been modified now
|
|
|
|
self.modified = False
|
|
|
|
def readonly(self):
|
|
|
|
return False
|
|
|
|
def read_filter(self, data):
|
|
|
|
return data
|
|
|
|
def write_filter(self, data):
|
|
|
|
return data
|
|
|
|
|
|
|
|
# point retrieval
|
|
|
|
def get_buffer_start(self):
|
|
|
|
return Point(0, 0)
|
|
|
|
def get_buffer_end(self):
|
|
|
|
return Point(len(self.lines[-1]), len(self.lines) - 1)
|
|
|
|
|
|
|
|
# data retrieval
|
|
|
|
def get_sublines(self, p1, p2):
|
|
|
|
self._validate_point(p1)
|
|
|
|
self._validate_point(p2)
|
|
|
|
assert p1 <= p2, "p1.x (%d) > p2.x (%d)" % (p1.x, p2.x)
|
|
|
|
lines = []
|
|
|
|
x = p1.x
|
|
|
|
for i in range(p1.y, p2.y):
|
|
|
|
lines.append(self.lines[i][x:])
|
|
|
|
x = 0
|
|
|
|
lines.append(self.lines[p2.y][x:p2.x])
|
|
|
|
return lines
|
|
|
|
def get_substring(self, p1, p2):
|
|
|
|
lines = self.get_sublines(p1, p2)
|
|
|
|
return '\n'.join(lines)
|
|
|
|
|
|
|
|
# buffer set
|
|
|
|
def set_lines(self, lines, force=False):
|
|
|
|
if not force and self.readonly():
|
|
|
|
raise Exception, "set_data: buffer is readonly"
|
|
|
|
start = self.get_buffer_start()
|
|
|
|
end = self.get_buffer_end()
|
|
|
|
self.delete(start, end, force=force)
|
|
|
|
self.insert_lines(start, lines, force=force)
|
|
|
|
self.modified = True
|
|
|
|
def set_data(self, data, force=False):
|
|
|
|
lines = data.split('\n')
|
|
|
|
self.set_lines(lines, force)
|
|
|
|
|
|
|
|
# insertion into buffer
|
|
|
|
def insert_lines(self, p, lines, act=ACT_NORM, force=False):
|
2007-06-13 22:18:41 -04:00
|
|
|
#if lines == ['(']:
|
|
|
|
# raise Exception, "damn"
|
2007-06-04 22:30:58 -04:00
|
|
|
llen = len(lines)
|
|
|
|
assert llen > 0
|
|
|
|
if not force:
|
|
|
|
assert not self.readonly(), "insert_string: buffer is read-only"
|
|
|
|
p2 = p.vadd(len(lines[-1]), llen - 1)
|
|
|
|
if llen > 1:
|
|
|
|
self.lines.insert(p.y + 1, [])
|
|
|
|
self.lines[p.y + 1] = lines[-1] + self.lines[p.y][p.x:]
|
|
|
|
self.lines[p.y] = self.lines[p.y][:p.x] + lines[0]
|
|
|
|
for i in range(1, llen - 1):
|
|
|
|
self.lines.insert(p.y + i, lines[i])
|
|
|
|
else:
|
|
|
|
self.lines[p.y] = self.lines[p.y][:p.x] + lines[-1] + self.lines[p.y][p.x:]
|
|
|
|
self._region_add(p, p2, lines, act)
|
|
|
|
self.modified = True
|
|
|
|
def insert_string(self, p, s, act=ACT_NORM, force=False):
|
|
|
|
lines = s.split("\n")
|
|
|
|
self.insert_lines(p, lines, act, force)
|
|
|
|
|
|
|
|
# deletion from buffer
|
|
|
|
def delete(self, p1, p2, act=ACT_NORM, force=False):
|
|
|
|
"""delete characters from p1 up to p2 from the buffer"""
|
|
|
|
if not force:
|
|
|
|
assert not self.readonly(), "delete_string: buffer is read-only"
|
|
|
|
self._validate_point(p1)
|
|
|
|
self._validate_point(p2)
|
|
|
|
if p1 == p2:
|
|
|
|
return
|
|
|
|
assert p1 < p2, "p1 %r > p2 %r" % (p1, p2)
|
|
|
|
lines = self.get_sublines(p1, p2)
|
|
|
|
line1 = self.lines[p1.y]
|
|
|
|
line2 = self.lines[p2.y]
|
|
|
|
self.lines[p1.y:p2.y+1] = ["%s%s" % (line1[:p1.x], line2[p2.x:])]
|
|
|
|
self._region_del(p1, p2, lines, act)
|
|
|
|
self.modified = True
|
|
|
|
def delete_char(self, p, act=ACT_NORM, force=False):
|
|
|
|
if p.x == len(self.lines[p.y]):
|
|
|
|
p2 = Point(0, p.y + 1)
|
|
|
|
else:
|
|
|
|
p2 = Point(p.x + 1, p.y)
|
|
|
|
self.delete(p, p2, act, force)
|
|
|
|
|
|
|
|
# random
|
|
|
|
def count_leading_whitespace(self, y):
|
|
|
|
line = self.lines[y]
|
|
|
|
m = regex.leading_whitespace.match(line)
|
|
|
|
if m:
|
|
|
|
return m.end()
|
|
|
|
else:
|
|
|
|
# should not happen
|
|
|
|
raise Exception, "iiiijjjj"
|
|
|
|
|
|
|
|
# scratch is a singleton
|
|
|
|
scratch = None
|
|
|
|
class ScratchBuffer(Buffer):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'scratch'
|
2007-06-04 22:30:58 -04:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
global scratch
|
|
|
|
if scratch is None:
|
|
|
|
scratch = object.__new__(ScratchBuffer, *args, **kwargs)
|
|
|
|
return scratch
|
|
|
|
def name(self):
|
|
|
|
return "*Scratch*"
|
|
|
|
def close(self):
|
|
|
|
global scratch
|
|
|
|
scratch = None
|
|
|
|
|
|
|
|
class DataBuffer(Buffer):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'data'
|
2007-06-04 22:30:58 -04:00
|
|
|
def __init__(self, name, data, nl='\n'):
|
|
|
|
Buffer.__init__(self, nl)
|
|
|
|
self._name = name
|
|
|
|
self.lines = data.split("\n")
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
def readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# console is another singleton
|
|
|
|
console = None
|
|
|
|
class ConsoleBuffer(Buffer):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'console'
|
2007-06-04 22:30:58 -04:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
global console
|
|
|
|
if console is None:
|
|
|
|
b = object.__new__(ConsoleBuffer, *args, **kwargs)
|
|
|
|
console = b
|
|
|
|
return console
|
|
|
|
def __init__(self, nl='\n'):
|
|
|
|
Buffer.__init__(self, nl)
|
2007-06-24 09:54:05 -04:00
|
|
|
self.clear()
|
|
|
|
def clear(self):
|
2007-06-04 22:30:58 -04:00
|
|
|
lines = ['Python Console\n',
|
|
|
|
"Evaluate python expressions in the editor's context (self)\n",
|
|
|
|
'Press Control-] to exit\n',
|
|
|
|
'\n']
|
|
|
|
console.set_data(''.join(lines), force=True)
|
|
|
|
def name(self):
|
|
|
|
return '*Console*'
|
|
|
|
def changed(self):
|
|
|
|
return False
|
|
|
|
def close(self):
|
|
|
|
global console
|
|
|
|
console = None
|
|
|
|
def readonly(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
class FileBuffer(Buffer):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'file'
|
2007-06-04 22:30:58 -04:00
|
|
|
def __init__(self, path, nl='\n', name=None):
|
|
|
|
'''fb = FileBuffer(path)'''
|
|
|
|
Buffer.__init__(self, nl)
|
|
|
|
self.path = os.path.realpath(path)
|
|
|
|
self.checksum = None
|
|
|
|
if name is None:
|
|
|
|
self._name = os.path.basename(self.path)
|
|
|
|
else:
|
|
|
|
self._name = name
|
|
|
|
if os.path.exists(self.path) and not os.access(self.path, os.W_OK):
|
|
|
|
self._readonly = True
|
|
|
|
else:
|
|
|
|
self._readonly = False
|
|
|
|
def readonly(self):
|
|
|
|
return self._readonly
|
|
|
|
|
|
|
|
def _open_file_r(self, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.path
|
|
|
|
path = os.path.realpath(path)
|
|
|
|
self.path = path
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
raise Exception, "Path '%s' does not exist" % (path)
|
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
raise Exception, "Path '%s' cannot be read" % (path)
|
|
|
|
f = open(path, 'r')
|
|
|
|
return f
|
|
|
|
def _open_file_w(self, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.path
|
|
|
|
if os.path.isfile(path):
|
|
|
|
raise Exception, "Path '%s' already exists" % (path)
|
|
|
|
d = os.path.dirname(path)
|
|
|
|
if not os.access(d, os.R_OK):
|
|
|
|
raise Exception, "Dir '%s' cannot be read" % (path)
|
|
|
|
if not os.access(d, os.W_OK):
|
|
|
|
raise Exception, "Dir '%s' cannot be written" % (path)
|
|
|
|
f = open(path, 'w')
|
|
|
|
return f
|
|
|
|
def _temp_path(self, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.path
|
|
|
|
(dirname, basename) = os.path.split(path)
|
|
|
|
return os.path.join(dirname, ".__%s__pmacs" % (basename))
|
|
|
|
|
|
|
|
# methods for dealing with the underlying resource, etc.
|
|
|
|
def name(self):
|
|
|
|
#return self.path
|
|
|
|
return self._name
|
|
|
|
def path_exists(self):
|
|
|
|
return os.path.exists(self.path)
|
|
|
|
def store_checksum(self, data):
|
|
|
|
self.checksum = md5.new(data)
|
|
|
|
def read(self):
|
|
|
|
if self.path_exists():
|
|
|
|
f = self._open_file_r()
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
self.store_checksum(data)
|
|
|
|
else:
|
|
|
|
data = ''
|
2007-07-19 20:22:45 -04:00
|
|
|
for i in range(0, min(len(data), 8)):
|
2007-07-19 14:37:39 -04:00
|
|
|
if ord(data[i]) > 127:
|
|
|
|
raise Exception, "editing binary files is not supported"
|
2007-06-04 22:30:58 -04:00
|
|
|
data = self.read_filter(data)
|
|
|
|
#FIXME: this is horrible...but maybe not as horrible as using tabs??
|
|
|
|
data = data.replace("\t", " ")
|
|
|
|
return data
|
|
|
|
def open(self):
|
|
|
|
data = self.read()
|
|
|
|
self.lines = data.split(self.nl)
|
|
|
|
def reload(self):
|
|
|
|
self.open()
|
|
|
|
def changed_on_disk(self):
|
|
|
|
assert self.checksum is not None
|
|
|
|
f = open(self.path)
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
m = md5.new(data)
|
|
|
|
return self.checksum.digest() != m.digest()
|
|
|
|
def save(self, force=False):
|
|
|
|
if self.readonly():
|
|
|
|
raise Exception, "can't save a read-only file"
|
|
|
|
|
|
|
|
if self.checksum is not None and force is False:
|
|
|
|
# the file already existed and we took a checksum so make sure it's
|
|
|
|
# still the same right now
|
|
|
|
if not self.path_exists():
|
|
|
|
raise Exception, "oh no! %r disappeared!" % self.path
|
|
|
|
if self.changed_on_disk():
|
|
|
|
raise Exception, "oh no! %r has changed on-disk!" % self.path
|
|
|
|
|
|
|
|
temp_path = self._temp_path()
|
|
|
|
data = self.make_string(nl=self.nl)
|
|
|
|
data = self.write_filter(data)
|
|
|
|
|
|
|
|
f2 = self._open_file_w(temp_path)
|
|
|
|
f2.write(data)
|
|
|
|
f2.close()
|
|
|
|
|
|
|
|
if self.path_exists():
|
|
|
|
mode = os.stat(self.path)[0]
|
|
|
|
os.chmod(temp_path, mode)
|
|
|
|
|
|
|
|
shutil.move(temp_path, self.path)
|
|
|
|
self.store_checksum(data)
|
|
|
|
self.modified = False
|
|
|
|
def save_as(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
class AesBuffer(FileBuffer):
|
2007-07-19 12:38:17 -04:00
|
|
|
btype = 'aesfile'
|
2007-07-08 16:00:28 -04:00
|
|
|
def __init__(self, path, password, nl='\n', name=None):
|
2007-06-04 22:30:58 -04:00
|
|
|
'''fb = FileBuffer(path)'''
|
2007-07-08 16:00:28 -04:00
|
|
|
FileBuffer.__init__(self, path, nl, name)
|
2007-06-04 22:30:58 -04:00
|
|
|
self.password = password
|
|
|
|
def read_filter(self, data):
|
2007-07-08 16:00:28 -04:00
|
|
|
return aes.decrypt_data(data, self.password)
|
2007-06-04 22:30:58 -04:00
|
|
|
def write_filter(self, data):
|
2007-07-08 16:00:28 -04:00
|
|
|
return aes.encrypt_data(data, self.password)
|
2007-07-19 12:38:17 -04:00
|
|
|
|
2007-07-19 14:28:21 -04:00
|
|
|
def _path_sort(a, b):
|
|
|
|
try:
|
|
|
|
x = cmp(a[0][0], b[0][0])
|
|
|
|
if x != 0:
|
|
|
|
return -x
|
|
|
|
return cmp(a[5], b[5])
|
|
|
|
except:
|
|
|
|
raise Exception, repr(a) + ' ' + repr(b)
|
2007-07-19 12:38:17 -04:00
|
|
|
class DirBuffer(Buffer):
|
|
|
|
btype = 'dir'
|
|
|
|
def __init__(self, path, nl='\n', name=None):
|
|
|
|
Buffer.__init__(self, nl)
|
|
|
|
self.path = os.path.realpath(path)
|
2007-07-19 22:36:36 -04:00
|
|
|
def changed(self):
|
|
|
|
return False
|
2007-07-19 12:38:17 -04:00
|
|
|
def readonly(self):
|
|
|
|
return True
|
|
|
|
def name(self):
|
2007-07-19 20:22:45 -04:00
|
|
|
return self.path
|
2007-07-19 12:38:17 -04:00
|
|
|
def path_exists(self):
|
|
|
|
return os.path.exists(self.path)
|
2007-07-19 17:51:20 -04:00
|
|
|
|
|
|
|
def _get_lines(self):
|
2007-07-19 12:38:17 -04:00
|
|
|
if not self.path_exists():
|
|
|
|
raise Exception, "directory %r does not exists" % self.path
|
2007-07-19 14:28:21 -04:00
|
|
|
|
|
|
|
names = os.listdir(self.path)
|
2007-07-19 12:38:17 -04:00
|
|
|
if self.path != '/':
|
2007-07-19 14:28:21 -04:00
|
|
|
names.insert(0, '..')
|
|
|
|
names.insert(0, '.')
|
|
|
|
|
2007-07-19 17:51:20 -04:00
|
|
|
fieldlines = []
|
2007-07-19 14:28:21 -04:00
|
|
|
maxlens = [0] * 5
|
|
|
|
for name in names:
|
|
|
|
# let's escape some troublesome characters
|
|
|
|
name = re.sub(r'([\a\b\n\r\t\v])', r'\\\1', name)
|
|
|
|
|
|
|
|
path = os.path.join(self.path, name)
|
|
|
|
info = os.stat(path)
|
|
|
|
|
|
|
|
# - regular, b block, c character, d directory, l symlink, p fifo
|
|
|
|
# s socket, ? unknown
|
|
|
|
perm = [' '] * 10
|
|
|
|
if stat.S_ISREG(info.st_mode):
|
|
|
|
perm[0] = '-'
|
|
|
|
elif stat.S_ISBLK(info.st_mode):
|
|
|
|
perm[0] = 'b'
|
|
|
|
elif stat.S_ISCHR(info.st_mode):
|
|
|
|
perm[0] = 'c'
|
|
|
|
elif stat.S_ISDIR(info.st_mode):
|
|
|
|
perm[0] = 'd'
|
|
|
|
elif stat.S_ISLNK(info.st_mode):
|
|
|
|
perm[0] = 'l'
|
|
|
|
elif stat.S_ISFIFO(info.st_mode):
|
|
|
|
perm[0] = 'p'
|
|
|
|
elif stat.S_ISSOCK(info.st_mode):
|
|
|
|
perm[0] = 's'
|
|
|
|
else:
|
|
|
|
perm[0] = '?'
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
symbols = ('r', 'w', 'x')
|
|
|
|
for bundle in ((stat.S_IRUSR, stat.S_IWUSR, stat.S_IXUSR),
|
|
|
|
(stat.S_IRGRP, stat.S_IWGRP, stat.S_IXGRP),
|
|
|
|
(stat.S_IROTH, stat.S_IWOTH, stat.S_IXOTH)):
|
|
|
|
for j in range(0, 3):
|
|
|
|
if info.st_mode & bundle[j]:
|
|
|
|
perm[i] = symbols[j]
|
|
|
|
else:
|
|
|
|
perm[i] = '-'
|
|
|
|
i += 1
|
2007-07-19 17:51:20 -04:00
|
|
|
|
|
|
|
if info.st_mode & stat.S_ISUID:
|
|
|
|
if perm[3] == 'x':
|
|
|
|
perm[3] = 's'
|
|
|
|
else:
|
|
|
|
perm[3] = 'S'
|
|
|
|
if info.st_mode & stat.S_ISGID:
|
|
|
|
if perm[6] == 'x':
|
|
|
|
perm[6] = 's'
|
|
|
|
else:
|
|
|
|
perm[6] = 'S'
|
|
|
|
if info.st_mode & stat.S_ISVTX:
|
|
|
|
if perm[9] == 'x':
|
|
|
|
perm[9] = 't'
|
|
|
|
else:
|
|
|
|
perm[9] = 'T'
|
|
|
|
|
2007-07-19 14:28:21 -04:00
|
|
|
perms = ''.join(perm)
|
|
|
|
|
|
|
|
try:
|
|
|
|
usr = pwd.getpwuid(info.st_uid)[0]
|
|
|
|
except:
|
|
|
|
usr = str(info.st_uid)
|
|
|
|
try:
|
|
|
|
grp = pwd.getpwuid(info.st_gid)[0]
|
|
|
|
except:
|
|
|
|
grp = str(info.st_gid)
|
|
|
|
|
|
|
|
size = info.st_size
|
|
|
|
unit = 0
|
|
|
|
units = ('B', 'K', 'M', 'G',)
|
|
|
|
while unit < 3 and size > 1024:
|
|
|
|
size = size / 1024
|
|
|
|
unit += 1
|
|
|
|
size = '%d%s' % (size, units[unit])
|
|
|
|
|
|
|
|
mtime = datetime.datetime.fromtimestamp(info.st_mtime).strftime('%b %d %H:%M')
|
|
|
|
|
|
|
|
fields = (perms, usr, grp, size, mtime, name)
|
|
|
|
for i in range(0, 5):
|
|
|
|
try:
|
|
|
|
maxlens[i] = max(maxlens[i], len(fields[i]))
|
|
|
|
except:
|
|
|
|
raise Exception, '%d %r' % (i, fields[i])
|
2007-07-19 17:51:20 -04:00
|
|
|
fieldlines.append(fields)
|
2007-07-19 14:28:21 -04:00
|
|
|
|
2007-07-19 17:51:20 -04:00
|
|
|
fieldlines.sort(cmp=_path_sort)
|
2007-07-19 14:28:21 -04:00
|
|
|
fmt = '%%%ds %%-%ds %%-%ds %%%ds %%%ds %%s' % tuple(maxlens)
|
|
|
|
|
2007-07-19 17:51:20 -04:00
|
|
|
lines = []
|
|
|
|
for fields in fieldlines:
|
2007-07-19 14:28:21 -04:00
|
|
|
s = fmt % fields
|
2007-07-19 17:51:20 -04:00
|
|
|
lines.append(s)
|
|
|
|
return lines
|
|
|
|
def open(self):
|
|
|
|
self.lines = self._get_lines()
|
2007-07-19 12:38:17 -04:00
|
|
|
def reload(self):
|
2007-07-19 17:51:20 -04:00
|
|
|
lines = self._get_lines()
|
|
|
|
self.set_lines(lines, force=True)
|
2007-07-19 12:38:17 -04:00
|
|
|
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"
|