Skip to content

Commit

Permalink
Ignore directories while using hot reloading. (#2105)
Browse files Browse the repository at this point in the history
* Now directories can be ignored.

Now directories can be ignored.

* move to --ignore

* implement review suggestions 1,2,3

* --ignore-dirs + review suggestion 4

* remove is_in_dir

---------

Co-authored-by: ndonkoHenri <robotcoder4@protonmail.com>
  • Loading branch information
xzripper and ndonkoHenri authored Jan 15, 2024
1 parent 5a42c64 commit 2474ef4
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions sdk/python/packages/flet/src/flet/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
default="assets",
help="path to assets directory",
)
parser.add_argument(
"--ignore-dirs",
dest="ignore_dirs",
type=str,
default=None,
help="directories to ignore during watch. If more than one, separate with a semicolon.",
)

def handle(self, options: argparse.Namespace) -> None:
if options.module:
Expand Down Expand Up @@ -159,6 +166,13 @@ def handle(self, options: argparse.Namespace) -> None:
Path(os.path.dirname(script_path)).joinpath(assets_dir).resolve()
)

ignore_dirs = options.ignore_dirs
if ignore_dirs:
ignore_dirs = [
str(Path(os.path.dirname(script_path)).joinpath(directory).resolve())
for directory in ignore_dirs.split(";")
]

my_event_handler = Handler(
[sys.executable, "-u"]
+ ["-m"] * options.module
Expand All @@ -173,6 +187,7 @@ def handle(self, options: argparse.Namespace) -> None:
options.android,
options.hidden,
assets_dir,
ignore_dirs if options.directory or options.recursive else None,
)

my_observer = Observer()
Expand Down Expand Up @@ -205,6 +220,7 @@ def __init__(
android,
hidden,
assets_dir,
ignore_dirs,
) -> None:
super().__init__()
self.args = args
Expand All @@ -218,6 +234,7 @@ def __init__(
self.android = android
self.hidden = hidden
self.assets_dir = assets_dir
self.ignore_dirs = ignore_dirs or []
self.last_time = time.time()
self.is_running = False
self.fvp = None
Expand Down Expand Up @@ -258,9 +275,21 @@ def start_process(self):
th.start()

def on_any_event(self, event):
is_in_ignore_dirs = False
for directory in self.ignore_dirs:
child = os.path.abspath(event.src_path)
# check if the file which triggered the reload is in the (ignored) directory
is_in_ignore_dirs = os.path.commonpath([directory]) == os.path.commonpath(
[directory, child]
)
if is_in_ignore_dirs:
break

if (
self.script_path is None or event.src_path == self.script_path
) and not event.is_directory:
not is_in_ignore_dirs
and (self.script_path is None or event.src_path == self.script_path)
and not event.is_directory
):
current_time = time.time()
if (current_time - self.last_time) > 0.5 and self.is_running:
self.last_time = current_time
Expand Down

0 comments on commit 2474ef4

Please sign in to comment.