Skip to content

Runs command or callback function when path file modified time changes

License

Notifications You must be signed in to change notification settings

python-utilities/watch_path

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Watch Path

Runs command or callback function when path file modified time changes

Byte size of Watch Path Open Issues Open Pull Requests Latest commits



Requirements

Python version 2 or 3


Quick Start

Install this project via Pip version 2 or 3

pip3 install --user --upgrade watch-path

Your Requirements File

Suggested additions for your requirements.txt file so everyone has a good time with dependencies

watch-path

Commit and Push

git add requirements.txt


git commit -F- <<'EOF'
:heavy_plus_sign: Adds `python-utilities/watch_path#1` as dependency


## Anything else worth committing
EOF


git push

🎉 Excellent 🎉 your project is now ready to begin unitizing code from this repository!


Usage

  • Print available CLI (Command Line Interface) options
watch_path --help
  • Run command when test.txt file changes
watch_path --file test.txt
  --command 'cat test.txt'\
  --sleep 0.5\
  --decode utf-8

Example of inheriting and modifying a class from watch_path...

#!/usr/bin/env python


from watch_path import Watch_Path


class Customized_Watch_Path(Watch_Path):
    """
    Customizes `watch_path` class
    """

    def __init__(self, ignore_empty, **kwargs):
        """
        Adds `ignore_empty` to initialization parameters of class
        """
        super(watch_path, self).__init__(**kwargs)
        self.update(ignore_empty = ignore_empty)

    def next(self):
        """
        Adds logic to ignore empty/non-existent paths
        """
        try:
            new_time_stamp = self.file_modified_time(self['path'])
        except OSError as e:
            print(e)
            if self['ignore_empty'] is not True:
                self.throw(GeneratorExit)

        if new_time_stamp != self['time_stamp']:
            self['time_stamp'] = new_time_stamp
            return self['callback'](path = self['path'],
                                    time_stamp = new_time_stamp,
                                    **self['callback_kwargs'])


def custom_callback(**kwargs):
    print("Detected disturbances in {path}".format(path = kwargs['path']))


if __main__ == '__name__':
    """
    Code that is run if this file is executed as a script instead of imported
    """
    custom_file_watcher = Customized_Watch_Path(callback = custom_callback
                                                path = 'test.txt',
                                                ignore_empty = True)

    try:
        for callback_results in custom_file_watcher:
            if callback_results:
                print(callback_results['stdout'].decode('UTF-8'))

            sleep(1)

    except KeyboardInterrupt:
        print('Stopping watcher and exiting...')

Notes

This repository may not be feature complete and/or fully functional, Pull Requests that add features or fix bugs are certainly welcomed.


Attribution


License

Documentation for Watch Path
Copyright (C) 2023 S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

For further details review full length version of AGPL-3.0 License.