fix examples in README.md
This commit is contained in:
@@ -113,7 +113,7 @@ async def task(event, task_id):
|
|||||||
|
|
||||||
s = TaskScheduler(task=task, files=True, dirs=True)
|
s = TaskScheduler(task=task, files=True, dirs=True)
|
||||||
|
|
||||||
event_map = EventMap(default_task=task)
|
event_map = EventMap(default_task=s.schedule)
|
||||||
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
||||||
|
|
||||||
pyinotifyd_config = PyinotifydConfig(
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
@@ -125,8 +125,8 @@ pyinotifyd_config = PyinotifydConfig(
|
|||||||
s = ShellScheduler(
|
s = ShellScheduler(
|
||||||
cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False)
|
cmd="/usr/local/sbin/task.sh {pathname}", files=True, dirs=False)
|
||||||
|
|
||||||
event_map = EventMap({"IN_WRITE_CLOSE": s.schedule,
|
event_map = EventMap({"IN_WRITE_CLOSE": s.schedule})
|
||||||
"IN_DELETE": s.schedule})
|
|
||||||
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
watch = Watch(path="/tmp", event_map=event_map, rec=True, auto_add=True)
|
||||||
|
|
||||||
pyinotifyd_config = PyinotifydConfig(
|
pyinotifyd_config = PyinotifydConfig(
|
||||||
|
|||||||
@@ -319,13 +319,39 @@ class FileManager:
|
|||||||
def add_rule(self, *args, **kwargs):
|
def add_rule(self, *args, **kwargs):
|
||||||
self._rules.append(Rule(*args, **kwargs))
|
self._rules.append(Rule(*args, **kwargs))
|
||||||
|
|
||||||
async def set_mode_and_chown(self, path, mode, chown):
|
def _chmod_and_chown(self, path, mode, chown):
|
||||||
if mode is not None:
|
if mode is not None:
|
||||||
os.chmod(path, mode)
|
os.chmod(path, mode)
|
||||||
|
|
||||||
if chown is not None:
|
if chown is not None:
|
||||||
shutil.chown(path, *chown)
|
shutil.chown(path, *chown)
|
||||||
|
|
||||||
|
async def _set_mode_and_owner(self, path, rule):
|
||||||
|
if (rule.user is rule.group is None):
|
||||||
|
chown = None
|
||||||
|
else:
|
||||||
|
chown = (rule.user, rule.group)
|
||||||
|
|
||||||
|
work_on_dirs = not (rule.dirmode is chown is None)
|
||||||
|
work_on_files = not (rule.filemode is chown is None)
|
||||||
|
|
||||||
|
if work_on_dirs or work_on_files:
|
||||||
|
generator = [(os.path.dirname(path),
|
||||||
|
[],
|
||||||
|
[os.path.basename(path)])]
|
||||||
|
generator = os.walk(path)
|
||||||
|
|
||||||
|
for root, dirs, files in generator:
|
||||||
|
if work_on_dirs:
|
||||||
|
for p in [os.path.join(root, d) for d in dirs]:
|
||||||
|
self._chmod_and_chown(
|
||||||
|
p, rule.dirmode, chown)
|
||||||
|
|
||||||
|
if work_on_files:
|
||||||
|
for p in [os.path.join(root, f) for f in files]:
|
||||||
|
self._chmod_and_chown(
|
||||||
|
p, rule.filemode, chown)
|
||||||
|
|
||||||
async def task(self, event, task_id):
|
async def task(self, event, task_id):
|
||||||
path = event.pathname
|
path = event.pathname
|
||||||
match = None
|
match = None
|
||||||
@@ -347,15 +373,24 @@ class FileManager:
|
|||||||
f"unable to {rule.action} '{path}', "
|
f"unable to {rule.action} '{path}', "
|
||||||
f"resulting destination path is empty")
|
f"resulting destination path is empty")
|
||||||
|
|
||||||
|
if os.path.exists(dst):
|
||||||
|
raise RuntimeError(
|
||||||
|
f"unable to move file from '{path} to '{dst}', "
|
||||||
|
f"dstination path exists already")
|
||||||
|
|
||||||
dst_dir = os.path.dirname(dst)
|
dst_dir = os.path.dirname(dst)
|
||||||
if not os.path.isdir(dst_dir) and rule.auto_create:
|
if not os.path.isdir(dst_dir) and rule.auto_create:
|
||||||
self._log.info(
|
self._log.info(
|
||||||
f"{task_id}: create directory '{dst_dir}'")
|
f"{task_id}: create directory '{dst_dir}'")
|
||||||
|
first_subdir = dst_dir
|
||||||
|
while not os.path.isdir(first_subdir):
|
||||||
|
parent = os.path.dirname(first_subdir)
|
||||||
|
if not os.path.isdir(parent):
|
||||||
|
first_subdir = parent
|
||||||
|
else:
|
||||||
|
break
|
||||||
os.makedirs(dst_dir)
|
os.makedirs(dst_dir)
|
||||||
elif os.path.exists(dst):
|
await self._set_mode_and_owner(first_subdir, rule)
|
||||||
raise RuntimeError(
|
|
||||||
f"unable to move file from '{path} to '{dst}', "
|
|
||||||
f"dstination path exists already")
|
|
||||||
|
|
||||||
self._log.info(
|
self._log.info(
|
||||||
f"{task_id}: {rule.action} '{path}' to '{dst}'")
|
f"{task_id}: {rule.action} '{path}' to '{dst}'")
|
||||||
@@ -368,32 +403,7 @@ class FileManager:
|
|||||||
else:
|
else:
|
||||||
os.rename(path, dst)
|
os.rename(path, dst)
|
||||||
|
|
||||||
if (rule.user is rule.group is None):
|
await self._set_mode_and_owner(dst, rule)
|
||||||
chown = None
|
|
||||||
else:
|
|
||||||
chown = (rule.user, rule.group)
|
|
||||||
|
|
||||||
work_on_dirs = not (rule.dirmode is chown is None)
|
|
||||||
work_on_files = not (rule.filemode is chown is None)
|
|
||||||
|
|
||||||
if work_on_dirs or work_on_files:
|
|
||||||
if os.path.isfile(dst):
|
|
||||||
generator = [(os.path.dirname(dst),
|
|
||||||
[],
|
|
||||||
[os.path.basename(dst)])]
|
|
||||||
else:
|
|
||||||
generator = os.walk(path)
|
|
||||||
|
|
||||||
for root, dirs, files in generator:
|
|
||||||
if work_on_dirs:
|
|
||||||
for path in [os.path.join(root, d) for d in dirs]:
|
|
||||||
await self.set_mode_and_chown(
|
|
||||||
path, rule.dirmode, chown)
|
|
||||||
|
|
||||||
if work_on_files:
|
|
||||||
for path in [os.path.join(root, f) for f in files]:
|
|
||||||
await self.set_mode_and_chown(
|
|
||||||
path, rule.filemode, chown)
|
|
||||||
|
|
||||||
elif rule.action == "delete":
|
elif rule.action == "delete":
|
||||||
self._log.info(
|
self._log.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user