formatted and removed 'optional code'
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-12-31 07:39:43 +01:00
parent ed60d06c76
commit 037d2d436c

View File

@@ -5,15 +5,15 @@ import time
config = { config = {
'dirs':{ "dirs": {
'from': os.getenv('IWATCH_FROM', '/in'), "from": os.getenv("IWATCH_FROM", "/in"),
'to': os.getenv('IWATCH_TO', '/out'), "to": os.getenv("IWATCH_TO", "/out"),
}, },
'action': 'move', "timeout": int(os.getenv("IWATCH_TIMEOUT", 30)),
'timeout': int(os.getenv('IWATCH_TIMEOUT', 30)),
} }
loglines = [''] loglines = [""]
def log(line): def log(line):
if line == loglines[-1]: if line == loglines[-1]:
@@ -21,66 +21,68 @@ def log(line):
print(line) print(line)
loglines[-1] = line loglines[-1] = line
def _main(): def _main():
i = inotify.adapters.InotifyTree(config['dirs']['from']) i = inotify.adapters.InotifyTree(config["dirs"]["from"])
files = {} files = {}
lastcount = 0 lastcount = 0
log('Looking for existing files in in') log(f'Looking for existing files in {config["dirs"]["from"]}')
for dirpath, _, filenames in os.walk(config['dirs']['from']): for dirpath, _, filenames in os.walk(config["dirs"]["from"]):
for f in filenames: for f in filenames:
log('found {}'.format(f)) log(f"found {f}")
files[dirpath + f] = { files[dirpath + f] = {"path": dirpath, "filename": f, "time": time.time()}
'path': dirpath,
'filename': f,
'time': time.time()
}
log('waiting for events') log("waiting for events")
for event in i.event_gen(yield_nones=True): for event in i.event_gen(yield_nones=True):
if event is None: if event is None:
completed = [] completed = []
for filename, file in files.items(): for filename, file in files.items():
if file['time'] < time.time()-config['timeout']: if file["time"] < time.time() - config["timeout"]:
completed.append(filename) completed.append(filename)
fpath = os.path.join(file['path'], file['filename']) fpath = os.path.join(file["path"], file["filename"])
if not os.path.isfile(fpath): if not os.path.isfile(fpath):
continue continue
if config['action'] == 'move': tpath = fpath.replace(config["dirs"]["from"], config["dirs"]["to"])
tpath = fpath.replace(config['dirs']['from'], config['dirs']['to'])
shutil.move(fpath, tpath) shutil.move(fpath, tpath)
log('moved {}'.format(fpath)) log(f"moved {fpath}")
else:
log('action unknown')
for filename in completed: for filename in completed:
del files[filename] del files[filename]
if len(files) != lastcount: if len(files) != lastcount:
log('waiting for {} files to be completed'.format(len(files))) log(f"waiting for {len(files)} files to be completed")
lastcount = len(files) lastcount = len(files)
continue continue
(_, type_names, path, filename) = event (_, type_names, path, filename) = event
if filename == '' or any(filter(lambda x: x in ['IN_MOVED_FROM'], type_names)): if filename == "" or any(filter(lambda x: x in ["IN_MOVED_FROM"], type_names)):
# Assume we moved the file ourself, so no need to take an action here
continue continue
fname = path + filename fname = path + filename
if 'IN_DELETE' in type_names: if "IN_DELETE" in type_names:
log('removed file {}/{}'.format(path, filename)) # Ok, the file is gone, just notify and remove from watched files
log(f"removed file {path}/{filename}")
if fname in files: if fname in files:
del files[fname] del files[fname]
continue continue
if not any(filter(lambda x: x in ['IN_MODIFY', 'IN_CREATE'], type_names)): if not any(filter(lambda x: x in ["IN_MODIFY", "IN_CREATE"], type_names)):
# skip all uninteresting events
continue continue
if fname in files: if fname not in files:
log("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format( path, filename, type_names)) log(f"PATH=[{path}] FILENAME=[{filename}] EVENT_TYPES={type_names}")
files[fname] = { files[fname] = {
'path': path, "path": path,
'filename': filename, "filename": filename,
} }
files[fname]['time'] = time.time() files[fname]["time"] = time.time()
if __name__ == '__main__': if __name__ == "__main__":
_main() _main()