Skip to main content

A Python package for monitoring file system changes

Project description

Watcher

A Python package for monitoring file system changes.

Installation

pip install watcher-fs

Command line usage

See: https://pavelkrupala.github.io/watcher-fs/command_line.html

Synchronous Usage

from watcher_fs.watcher import Watcher, TriggerType
from pathlib import Path 

test_dir = Path("test_dir")

def on_change_simple():
    print(f"Something changed.")

def on_change(change):
    print(f"File {change}")

watcher = Watcher()

# register as glob pattern
watcher.register("test_dir/**/*.txt", on_change_simple, TriggerType.PER_FILE)
watcher.register("test_dir/**/*.py", on_change, TriggerType.ANY_FILE, callback_extra=True)

# register as list of specified files
watcher.register([
    test_dir / "skin.styl",
    test_dir / "styl/default.styl",
    test_dir / "styl/utils.styl"
], on_change, TriggerType.ANY_FILE, callback_extra=True)

# Simulate a check
watcher.check()

# do something
with open(test_dir / "aaa.txt", "w") as f:
    f.write("Modified content")
with open(test_dir / "bbb.txt", "w") as f:
    f.write("Modified content")

with open(test_dir / "skin.styl", "w") as f:
    f.write("a = #0af")
with open(test_dir / "styl/default.styl", "w") as f:
    f.write("a = #f00")

# check again
watcher.check()

Asynchronous Usage

import asyncio
from pathlib import Path 
from watcher_fs.async_watcher import AsyncWatcher, TriggerType


# Example usage:
async def main():
    test_dir = Path("test_dir")

    async def on_change_simple():  # CHANGED: Async callback
        print(f"Something changed.")

    async def on_change(change):  # CHANGED: Async callback
        print(f"File {change}")

    def create_test_files(file_names):
        """Helper to create test files (synchronous for simplicity)."""
        for file_name in file_names:
            file_path = test_dir / file_name
            file_path.parent.mkdir(parents=True, exist_ok=True)
            with open(file_path, "w") as f:
                if file_name.endswith(".txt"):
                    f.write("Initial content")
                else:  # .styl
                    f.write("a = #fa0")

    create_test_files(["aaa.txt", "bbb.txt", "ccc.txt"])
    create_test_files(["skin.styl", "styl/default.styl", "styl/utils.styl"])

    watcher = AsyncWatcher()
    await watcher.register("test_dir/**/*.txt", on_change_simple, TriggerType.PER_FILE)
    await watcher.register(
        [test_dir / "skin.styl", test_dir / "styl/default.styl", test_dir / "styl/utils.styl"],
        on_change, TriggerType.ANY_FILE, callback_extra=True
    )

    # Simulate a check
    await watcher.check()

    # Modify files (synchronous for simplicity)
    with open(test_dir / "aaa.txt", "w") as f:
        f.write("Modified content")
    with open(test_dir / "bbb.txt", "w") as f:
        f.write("Modified content")
    with open(test_dir / "skin.styl", "w") as f:
        f.write("a = #0af")
    with open(test_dir / "styl/default.styl", "w") as f:
        f.write("a = #f00")

    # Check again
    await watcher.check()


if __name__ == "__main__":
    asyncio.run(main())

Documentation

See https://pavelkrupala.github.io/watcher-fs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

watcher_fs-1.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file watcher_fs-1.4-py3-none-any.whl.

File metadata

  • Download URL: watcher_fs-1.4-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for watcher_fs-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6d3b24db76d1fd8d8c3e58eef46c35d903383a05c1660bad6f99b345333607fb
MD5 fd613a09c2d5ee01c631297cf2dbb322
BLAKE2b-256 23d8308dcf2b19a46d7cb46eb0db8da61834b5e287dba1e9e80b5d8e3914b5b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page