pmacs3/mode_dir.py

139 lines
4.8 KiB
Python
Raw Normal View History

import method, mode2, os.path
from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule
from point2 import Point
2007-07-19 22:36:36 -04:00
from method import Method, Argument
class PermGrammar(Grammar):
rules = [
2007-07-19 17:51:20 -04:00
PatternRule(r'sticky', r'[tT]'),
PatternRule(r'setid', r'[sS]'),
PatternRule(r'read', r'r'),
PatternRule(r'write', r'w'),
PatternRule(r'exec', r'x'),
]
class PathGrammar(Grammar):
rules = [
RegionRule(r'perm', r'(?<=^.)', PermGrammar, r' '),
PatternGroupRule(r'fields', r'owner', r'[^ ]+ +', r'group', r'[^ ]+ +',
r'size', r'[^ ]+ +',
r'mtime', r'[A-Za-z]{3} [ 0-9]{2} [0-9]{2}:[0-9]{2} +',
r'name', r'[^\n]*'),
]
class DirGrammar(Grammar):
rules = [
RegionRule(r'file', r'^-', PathGrammar, r'\n'),
RegionRule(r'blk', r'^b', PathGrammar, r'\n'),
RegionRule(r'chr', r'^c', PathGrammar, r'\n'),
RegionRule(r'dir', r'^d', PathGrammar, r'\n'),
RegionRule(r'lnk', r'^l', PathGrammar, r'\n'),
RegionRule(r'fifo', r'^p', PathGrammar, r'\n'),
RegionRule(r'sock', r'^s', PathGrammar, r'\n'),
RegionRule(r'unk', r'^\?', PathGrammar, r'\n'),
]
class Dir(mode2.Fundamental):
grammar = DirGrammar()
colors = {
'blk.start': ('cyan', 'default'),
'blk.name': ('cyan', 'default'),
'chr.start': ('yellow', 'default'),
'chr.name': ('yellow', 'default'),
'dir.start': ('blue', 'default'),
'dir.name': ('blue', 'default'),
'lnk.start': ('green', 'default'),
'lnk.name': ('green', 'default'),
'fifo.start': ('red', 'default'),
'fifo.name': ('red', 'default'),
'sock.start': ('red', 'default'),
'sock.name': ('red', 'default'),
'unk.start': ('magenta', 'default'),
'unk.name': ('magenta', 'default'),
2007-07-19 17:51:20 -04:00
#'perm.setid': ('red', 'default'),
#'perm.sticky': ('red', 'default'),
'perm.setid': ('yellow', 'default'),
'perm.sticky': ('yellow', 'default'),
'perm.read': ('magenta', 'default'),
'perm.write': ('magenta', 'default'),
'perm.exec': ('magenta', 'default'),
2007-07-19 17:51:20 -04:00
#'owner': ('blue', 'default'),
'owner': ('cyan', 'default'),
'group': ('cyan', 'default'),
'size': ('yellow', 'default'),
'mtime': ('green', 'default'),
}
def __init__(self, w):
mode2.Fundamental.__init__(self, w)
self.add_action(Chmod())
self.add_action(Chown())
self.add_action(Chgrp())
2007-07-19 22:36:36 -04:00
self.add_action(TouchPath())
self.add_action(RefreshView())
self.add_action_and_bindings(OpenPath(), ('RETURN',))
2007-07-19 17:51:20 -04:00
self.add_action_and_bindings(RemovePath(), ('DELETE', 'BACKSPACE', 'C-d'))
def name(self):
return "Dir"
2007-07-19 17:51:20 -04:00
def _resolve_token(w):
c = w.logical_cursor()
p = Point(0, c.y)
return w.get_next_token_by_type(p, 'name')
def _resolve_path(w):
t = _resolve_token(w)
path = os.path.join(w.buffer.path, t.string)
return path
2007-07-19 22:36:36 -04:00
class Chmod(Method):
def _execute(self, w, **vargs):
w.set_error('chmod not implemented')
2007-07-19 22:36:36 -04:00
class Chown(Method):
def _execute(self, w, **vargs):
w.set_error('chown not implemented')
2007-07-19 22:36:36 -04:00
class Chgrp(Method):
def _execute(self, w, **vargs):
w.set_error('chgrp not implemented')
2007-07-19 22:36:36 -04:00
class TouchPath(Method):
args = [Argument('filename', datatype="path", prompt="Touch File: ")]
def _execute(self, w, **vargs):
path = vargs['filename']
retval = os.system('touch %r' % path)
w.application.methods['refresh-view'].execute(w, filename=path)
if retval != 0:
w.set_error("touch failed with exit status %d" % retval)
class RemovePath(Method):
def _execute(self, w, **vargs):
2007-07-19 17:51:20 -04:00
path = _resolve_path(w)
2007-07-19 22:36:36 -04:00
w.application.methods['previous-line'].execute(w)
2007-07-19 17:51:20 -04:00
try:
os.remove(path)
w.set_error("deleted %r " % path)
2007-07-19 22:36:36 -04:00
w.application.methods['refresh-view'].execute(w, filename=path)
2007-07-19 17:51:20 -04:00
except:
w.set_error("failed to delete %r" % path)
2007-07-19 22:36:36 -04:00
class OpenPath(Method):
2007-07-19 17:51:20 -04:00
def _execute(self, w, **vargs):
path = _resolve_path(w)
w.set_error("opening %r" % path)
w.application.methods['open-file'].execute(w, filename=path)
2007-07-19 22:36:36 -04:00
class RefreshView(Method):
2007-07-19 17:51:20 -04:00
def _execute(self, w, **vargs):
c = w.logical_cursor()
t = _resolve_token(w)
s = t.string
w.buffer.reload()
2007-07-19 22:36:36 -04:00
w.goto(Point(c.x, 0))
2007-07-19 17:51:20 -04:00
found = False
while not found and w.cursor.y < len(w.buffer.lines):
t = _resolve_token(w)
if t.string == s:
found = True
else:
2007-07-19 22:36:36 -04:00
w.goto(Point(c.x, w.cursor.y + 1))
2007-07-19 17:51:20 -04:00
if not found:
2007-07-19 22:36:36 -04:00
w.goto(Point(0, 0))