-
Notifications
You must be signed in to change notification settings - Fork 55
Feature Request: pyscript.reload to support new files. #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Good points. Actually, how about reload (with no params) looks for differences (comparing file contents and maybe mtimes, and checking for new and deleted files) and just makes the minimum changes - unloads deleted files, reloads changed files, and loads new files? Files with no changes aren't touched. The existing It makes the default of plain Once #74 is implemented on linux, then |
That is EVEN BETTER! This way, if something changes, just call reload and it will do what is needed. And yes, on Linux, with #74, Some harder questions: YAML and modules. When a module (in pyscript/modules) changes, a reload to modules.filename reloads the module, but any existing scripts using that module hold on to the old copy until they are also reloaded. If modules.filename is NOT reloaded, then reloading the script file does NOT cause the new module file to be used. To sum up, both the module AND the script using the module need to be reloaded in order to really see the module changes. Since pyscript already dips into how "import" works, is there a way to track the imports each script uses and, if that import is a module and that module is reloaded, also reload the script? When YAML changes, reloading JUST the script that uses the YAML results in that script seeing the new YAML. This is great. Is there anyway to detect when YAML changes? If so, could the old YAML be compared to the new YAML and if changes are detected at pyscript/apps/appname then apps.appname would be reloaded as well? This doesn't affect me directly in most cases, because I use a different YAML format than the convention suggested by pyscript because I like to separate my YAML by functionality. So I use pyscript/apps_list/[{app: appname, a: 1}, {app: appname, a: 2}] format instead. I think the only way to get MY YAML usage to autoreload would be some kind of YAML registration function called in the app code to indicate which parts of YAML it uses. But I can write that in user code if there's a way to detect YAML changes. For YAML changes, what if there was a |
I just pushed a significant rewrite of While this is a breaking change, the old behavior can be forced with |
!!!! Thank you! I'll pull "master" this morning and test it. |
This worked perfectly with everything I threw at it.
In each case, above, after making changes, I only called "pyscript.reload" with no parameters. The only request I have, and it's not a big deal, is... when a script is loaded a message of "Loaded /config/pyscript/test.py" is generated. This is useful to know that something loaded (or reloaded). However, when a script is deleted (so, unloaded and then NOT reloaded), there is no confirmation in the log. Adding a "Unloaded /config/pyscript/test.py" log message would be useful. If this happens before the "Loaded" log line on a reload (because it is both unloaded and then loaded) that's okay. Optionally, the log could say "Loaded", "Unloaded", or "Reloaded", depending on which of the three things are happening. |
Another thing I just tested that worked perfectly... If a script has an exception during load (not in a trigger function, but in the code that defines trigger functions), then pyscript does NOT count it as loaded. So the next call to reload will try to load that file again, even if no changes have been made to the file. I tested it like this:
pyscript.manually_set did not exist in my system. Every time I issued pyscript.reload I'd correctly see:
Once I manually set that entity and value, I issued pyscript.reload again and the script started up as desired (though I didn't actually expect it to work, so that was a bonus). |
I was able to reduce the complexity of my own file change "watchdog" script thanks to these changes to reload. Together, they are working exactly as expected. https://gist.github.com/dlashua/f7d88f9a5afdcf7af17ce24266925a0b |
Reworked the watchdog to use aionotify instead. This should be easier to incorporate into pyscript core if desired. https://gist.github.com/dlashua/f7d88f9a5afdcf7af17ce24266925a0b The |
I pushed a commit that now logs Loaded/Unloaded/Reloaded. Note that a change to something that is imported (eg, a module or a sub-file in an app) will separately report |
Currently, calling
pyscript.reload
with aglobal_ctx=
set to a newly created file results in an error indicating the global context cannot be found, which makes sense because a full reload hasn't happened yet so that global context has never been loaded, and, therefore, can't be reloaded.For deleted files, pyscript.reload works just fine. The global_ctx is unloaded and, since the file no longer exists, is not reloaded.
It would be great if reload could look for the new file and cause it to load. Optionally, pyscript.load could be used instead to establish a difference between a new file and a reload. Though, if this happens, I suggest that reload does not work on deleted files and a pyscript.unload gets added.
This is a nice stepping stone to having full autoREload support in pyscript since this functionality will be needed for that.
The text was updated successfully, but these errors were encountered: