Add option --match-filename

This commit is contained in:
Peter J. Holzer 2022-01-13 13:57:10 +01:00
parent 8954588ab4
commit a4d54b8ca1
1 changed files with 12 additions and 6 deletions

10
kitsune
View File

@ -1,6 +1,8 @@
#!/usr/bin/python3
import argparse
import datetime
import fnmatch
import os
import sys
import stat
@ -23,7 +25,7 @@ def watch(args):
watched_dirs = []
watched_files = {}
filename_length = 0
for a in args:
for a in args.files:
st = os.stat(a)
if stat.S_ISDIR(st.st_mode):
watched_dirs.append(a)
@ -43,6 +45,7 @@ def watch(args):
last_ts = st.st_mtime_ns
for de in os.scandir(d):
if de.is_file():
if args.match_filename is None or fnmatch.fnmatch(de.name, args.match_filename):
if de.path not in watched_files:
watched_files[de.path] = WatchedFile(de.path)
if len(de.path) > filename_length:
@ -72,6 +75,9 @@ def watch(args):
time.sleep(0.1)
if __name__ == "__main__":
args = sys.argv[1:] if len(sys.argv) > 1 else ["."]
ap = argparse.ArgumentParser()
ap.add_argument("--match-filename")
ap.add_argument("files", nargs="*", default=["."])
args = ap.parse_args()
watch(args)