Created
May 8, 2024 14:57
-
-
Save ShenTengTu/73a6ad8640da53db69e6a9f274465377 to your computer and use it in GitHub Desktop.
Python class for monitoring a file in Notepad++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import atexit | |
import subprocess as subp | |
from pathlib import Path | |
from concurrent.futures import ThreadPoolExecutor | |
class NotepadMonitor: | |
""" | |
A class to monitor a file in Notepad++. | |
""" | |
__pool__ = ThreadPoolExecutor() | |
def __init__(self, file: Path): | |
self.__file = file | |
self.__fut = None | |
def __run(self): | |
file = self.__file | |
file.touch() | |
args = [ | |
"notepad++", | |
"-monitor", | |
"-noPlugin", | |
"-nosession", | |
"-notabbar", | |
"-ro", | |
"-alwaysOnTop", | |
file.as_posix(), | |
] | |
subp.run(args) | |
def start(self): | |
if self.__fut: | |
raise RuntimeError("Already started") | |
self.__fut = self.__pool__.submit(self.__run) | |
fp = self.__file.open("w") | |
atexit.register(fp.close) | |
return fp | |
def wait(self): | |
if self.__fut: | |
self.__fut.result() | |
self.__fut = None | |
def main(): | |
from time import sleep | |
path_here = Path(__file__).parent | |
o = NotepadMonitor(path_here / "log.txt") | |
fp = o.start() | |
for i in range(100): | |
fp.write(f"Hello World! {i}\n") | |
fp.flush() | |
sleep(0.1) | |
o.wait() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment