Python file watcher

Posted on
  • Hi.

    As I prefer the workflow where I save the file and see the changes immediately, I wanted to do the same with my bangle.js2 dev.

    But I couldn't get the espruino cli to work at all (tried noble, @abandonware-noble, multiple versions of node, etc) even though the connection was alright (on web ide, python or bluetoothctl).

    So my solution was to home brew a file watcher in python (using watchgod) that would send the monitored files to my clock on change (using bleak).

    It works quite well, so I'm sharing in case anyone want to take a look, but I don't know how well it would work on other espruino.

    The code is on github: https://github.com/diesphink/banglejs2-f­iles-watcher/, and down below is the usage:

    usage: watcher.py [-h] [--buffer_size BUFFER_SIZE] [--exec EXEC] [-v] address file [file ...]
    
    Bangle Watcher
    
    positional arguments:
      address               bluetooth address to connect
      file                  files to watch
    
    optional arguments:
      -h, --help            show this help message and exit
      --buffer_size BUFFER_SIZE
                            buffer size (default 20)
      --exec EXEC           script to run (load) after each upload
      -v, --verbose         verbosity: -v show response from bangle, -vv show all DEBUG logs
    
  • Hi,

    this looks pretty cool, thanks for sharing! I will try this with an external IDE later on.

    Did you by chance find any setup for an external IDE (besides the online or offline WebIDE) offering code completion for Espruino, or do you use the WebIDE with your "upload on save" feature?

    Thanks, Joost

  • I use vs code, but without any espruino code completion for the moment. I tried the new (beta?) WebIDE on chrome with the file api, but sadly it also does not open my local files, a lot of misfortunes that forced me to bake my own, :p

    One thing that I do like on this flow is that I can split the code in various modules, then the upload is super fast and I can see the changes instantly, and navigate between multiple files is super easy on an external IDE (I can't judge the webIDE with the project config, as I couldn't make it work).

  • Sounds really cool, unfortunately not at home right now so can not follow directly, but will do later on.
    Did you manage to open a console as well (in part like the left side of the WebIDE?)

    PS: I did see the project stuff going on in the WebIDE, but did not understand completely how to work with it until now. I'm fighting repeated human errors with saving different files (*.app.js, *.info etc.) via WebIDE -having repeatedly overwritten wrong files :-/

  • @HiFiJ, you can also run the App Loader locally (python3 -m http.server in the BangleApps dir, and hey presto http://localhost:8000) which will handle most of the heavy lifting of an app installation as long as you have apps.json set up.

    And after that you can just overwrite the one file you're interested in and leave the rest of the installation alone!

  • Thanks - looks interesting! The Espruino CLI/IDE/App Loader do perform some transformations to the code so it's possible your implementation may not work in some edge cases, but even so it's a very handy tool.

    Just FYI the Web IDE can do file watching too! Juct click the down-arrow below the 'open' icon and click 'Watch and Upload', then click the open icon and voila!

  • Yeah, I've experimented with making a staging step via espruino cli, but was too much trouble for a quick hack, my intention is to send it via upload on ide after I'm done with the code.

    On the web ide, the load just won't work, either on https://www.espruino.com/ide/ or https://espruino.github.io/EspruinoWebID­E/

    After clicking open with any of the four options, there's no file selection, and the console returns:

    Uncaught (in promise) DOMException: The user aborted a request.
    Promise.then (async)		
    loadFile	@	file.js:154
    click	@	file.js:61
    

    I'm using Google Chrome 96.0.4664.110 (Official Build) (64-bit) on Linux zem 5.13.0-19-generic #19-Ubuntu SMP Thu Oct 7 21:58:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux, and the "Upload a file" option on "Device storage" works with no issue.

    I think that the file watching on the IDE (with the project option) would have worked perfectly for my needs (and I would surely recommend that over this watcher), but I gave up on trying to figure out the error by looking at the code, maybe it's something on my chrome installation, I don't know.

    As I was doing the script to send data jsons to my watch, I just glued some stuff together and called it a day, sometimes you wanna hack, sometimes you just want things to work so you can get to the fun part, :P

  • I was just about to suggest/implement an unholy combination of the espruino-cli and this python implementation, and then I noticed there's an alternate BLE node.js module for linux that is a lot newer than noble and uses the DBus interface so it's also much better ergonomically.

    So I implemented a new serial plugin for the espruino tools that uses it. Seems to detect and connect to my Bangle without issue. A PR is up on the github now.

    Now I just need to figure out why the cli terminal isn't working properly...

  • Just merged - thanks! Although did you do the setcap thing for Noble? If there's one platform where I haven't had issues with Noble it's been Linux. Always seemed to work great for me.

  • Yes, I did, after every node change via nvm just to be sure. I also tried with sudo just in case.

    I also think that the example code from @abandonware/noble worked ok with node 8.6.0 to detect the device, but even in this I could not get it to work on espruino, and got into a rabbit hole with gyp, but I'm not 100% sure because there was a lot of tries in different scenarios.

    I always got the Error: No Ports Found at the end, after a few seconds.

    On a side note, I also thought it would be easier on linux, that's why I'm assuming it's some odd thing on my installation, I'll wait to see if the code from @pelrun works out, if not I'll explore more on this (like trying on a vanilla ubuntu vm).

  • Yeah, I spent way too long doing exactly the same things as @diego did, and I either got constant build errors due to not having exactly ABI version 88, or I'd get everything just right... and discovery wouldn't return a single result. Tossed the lot out and decided it was just rotten. (for reference, I'm running Ubuntu 21.10 here.)

    I've fixed the bugs in my node-ble branch (new PR is up) and it now nicely gives me a terminal and everything behaves as it should. Although not before spending three hours trying to determine why I got every rx packet twice... only for it to stop the instant I closed the (disconnected) WebIDE I had running in another window. So yeah, don't have that going when using this :)

  • Having a second process interfering with the one one works on is a classic :D

  • So THAT'S why sometimes I have double echo, thanks for that, it was a really weird behavior, :P

  • Hi Gordon, that's great (regarding the "watch and upload")! Could you give some more information where to find the arrow below the "open" icon you mentioned? (It is not the one beneath the upload button, correct?)

    Thanks a lot!


    1 Attachment

    • WebIDE.jpg
  • Ah, just for anybody blind as I am - the "watch and upload" function is to be found in the online version of the IDE at https://www.espruino.com/ide/ and seems unavailable in the Chrome webapp.

  • Ahh, thanks - yes, I think the Web App uses the Chrome Web App file API instead, which doesn't support watching :(

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Python file watcher

Posted by Avatar for diego @diego

Actions