Junk table dispatch for execute

This commit is contained in:
Reid 'arrdem' McKenzie 2022-07-28 20:46:56 -06:00
parent b9617079ab
commit 5a34f095d7

View file

@ -16,21 +16,15 @@ class Vfs(object):
def __init__(self, log=None):
self._log = log or []
def execute(self, /, callback=None):
for e in self._log:
_log.debug(e)
if callback:
callback(e)
if e[0] == "exec":
def _execute_exec(self, e):
_, dir, cmd = e
run(cmd, cwd=str(dir))
elif e[0] == "link":
def _execute_link(self, e):
_, src, dest = e
if dest.is_file() or dest.is_symlink():
if dest.is_symlink() and dest.readlink() == src:
continue
return
else:
_log.warn(f"Replacing {dest}")
dest.unlink()
@ -41,27 +35,24 @@ class Vfs(object):
assert not dest.exists(), f"{dest} should not exist"
dest.symlink_to(src)
elif e[0] == "copy":
raise NotImplementedError()
elif e[0] == "chmod":
def _execute_chmod(self, e):
_, dest, mode = e
dest.chmod(mode)
elif e[0] == "mkdir":
def _execute_mkdir(self, e):
_, dest = e
if dest.is_dir():
continue
return
elif dest.exists() or dest.is_symlink():
dest.unlink()
dest.mkdir(exist_ok=True)
elif e[0] == "unlink":
def _execute_unlink(self, e):
_, dest = e
# Note that a path which is a dangling symlink will NOT exist but WILL be a symlink
if not dest.exists() and not dest.is_symlink():
continue
return
# Files and dirs just unlink
if dest.is_symlink() or dest.is_file():
dest.unlink()
@ -72,8 +63,28 @@ class Vfs(object):
else:
raise Exception(f"Couldn't unlink {dest}")
def _append(self, msg):
self._log.append(msg)
def _execute_unimplemented(self, e):
raise NotImplementedError()
def _entry_to_command(self, e):
return e
def execute(self, /, callback=None):
for e in self._log:
cmd = self._entry_to_command(e)
_log.debug(f"Executing %r as %r", e, cmd)
if callback:
callback(cmd)
# Using self as a dispatch table lol
getattr(self, f"_execute_{cmd[0]}", self._execute_unimplemented)(cmd)
def _command_to_entry(self, cmd):
return cmd
def _append(self, cmd):
self._log.append(self._command_to_entry(cmd))
def link(self, src, dest):
self._append(("link", src, dest))