diff --git a/mode_dir.py b/mode_dir.py index e078a22..a221dc5 100644 --- a/mode_dir.py +++ b/mode_dir.py @@ -1,4 +1,4 @@ -import grp, method, mode2, os.path, pwd, re +import commands, grp, method, mode2, os.path, pwd, re from lex3 import Grammar, PatternRule, RegionRule, PatternGroupRule from point2 import Point from method import Method, Argument @@ -64,13 +64,12 @@ class Dir(mode2.Fundamental): } def __init__(self, w): mode2.Fundamental.__init__(self, w) - self.add_action(Chmod()) - self.add_action(Chown()) - self.add_action(Chgrp()) - - self.add_action(TouchPath()) - self.add_action(RefreshView()) + self.add_action_and_bindings(RefreshView(), ('C-c r',)) self.add_action_and_bindings(OpenPath(), ('RETURN',)) + self.add_action_and_bindings(Chmod(), ('C-c m',)) + self.add_action_and_bindings(Chown(), ('C-c o',)) + self.add_action_and_bindings(Chgrp(), ('C-c g',)) + self.add_action_and_bindings(TouchPath(), ('C-c t',)) self.add_action_and_bindings(RemovePath(), ('DELETE', 'BACKSPACE', 'C-d')) def name(self): return "Dir" @@ -113,10 +112,25 @@ class OpenPath(Method): w.set_error("opening %r" % path) w.application.methods['open-file'].execute(w, filename=path) -class Chmod(Method): +class DirCmd(Method): + def _make_cmd(self, w, path, **vargs): + return '' + def _run(self, w, **vargs): + basename = _resolve_name(w) + path = os.path.join(w.buffer.path, basename) + cmd = self._make_cmd(w, path, **vargs) + (status, output) = commands.getstatusoutput(cmd) + if status != 0: + w.set_error("%s failed (exit %d)" % (self.name, status)) + w.application.methods['refresh-view'].execute(w, filename=path) + _find_name(w, basename) + +class Chmod(DirCmd): args = [Argument('mode', type=type(''), prompt="New Mode: ")] octal_re = re.compile('^[0-7]{1,4}$') symbolic_re = re.compile('(?:[ugoa]*(?:[-+=](?:[rwxXst]*|[ugo]))+ *,?)+') + def _make_cmd(self, w, path, **vargs): + return 'chmod %r %r' % (vargs['mode'], path) def _execute(self, w, **vargs): if self.octal_re.match(vargs['mode']): pass @@ -124,15 +138,33 @@ class Chmod(Method): pass else: w.set_error("Not a valid mode: %r" % vargs['mode']) - basename = _resolve_name(w) - path = os.path.join(w.buffer.path, basename) - retval = os.system('chmod %r %r' % (vargs['mode'], path)) - w.application.methods['refresh-view'].execute(w, filename=path) - _find_name(w, basename) - if retval != 0: - w.set_error("chmod %r failed (exit %d)" % (vargs['mode'], retval)) -class Chown(Method): + self._run(w, **vargs) + +def _valid_owner(owner): + if not owner: + return False + elif owner.isdigit(): + return True + try: + pwd.getpwnam(owner) + return True + except: + return False +def _valid_group(group): + if not group: + return False + elif group.isdigit(): + return True + try: + grp.getgrnam(group) + return True + except: + return False + +class Chown(DirCmd): args = [Argument('owner', type=type(''), prompt="New Owner: ")] + def _make_cmd(self, w, path, **vargs): + return 'chown %r %r' % (vargs['owner'], path) def _execute(self, w, **vargs): fields = vargs['owner'].split(':') if len(fields) == 1: @@ -142,31 +174,22 @@ class Chown(Method): else: w.set_error("Malformed 'owner' argument: %r" % vargs['owner']) return - - if not owner.isdigit(): - try: - pwd.getpwnam(owner) - except: - w.set_error("User %r does not exist" % owner) - return - if group is not None and not group.isdigit(): - try: - grp.getgrnam(group) - except: - w.set_error("Group %r does not exist" % group) - return - - basename = _resolve_name(w) - path = os.path.join(w.buffer.path, basename) - retval = os.system('chown %r %r' % (vargs['owner'], path)) - w.application.methods['refresh-view'].execute(w, filename=path) - _find_name(w, basename) - if retval != 0: - w.set_error("chown %r failed (exit %d)" % (vargs['mode'], retval)) -class Chgrp(Method): + if not _valid_owner(owner): + w.set_error('User %r does not exist' % owner) + return + if group is not None and not _valid_group(group): + w.set_error('Group %r does not exist' % group) + return + self._run(w, **vargs) +class Chgrp(DirCmd): args = [Argument('group', type=type(''), prompt="New Group: ")] + def _make_cmd(self, w, path, **vargs): + return 'chgrp %r %r' % (vargs['group'], path) def _execute(self, w, **vargs): - w.set_error('chgrp not implemented') + if not _valid_group(vargs['group']): + w.set_error('Group %r does not exist' % group) + return + self._run(w, **vargs) class TouchPath(Method): args = [Argument('filename', datatype="path", prompt="Touch File: ")]