Simple wrapper for `os.stat`, calls callback function time-stamp changes
Project description
Watch Path
Runs command or callback function when path file modified time changes
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
:tada: Excellent :tada: 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.txtfile 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
-
StackOverflow -- How do I import
FileNotFounderrorfrom Python 3? -
StackOverflow -- What does the
bcharacter do in front of a string literal?
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file watch_path-0.1.1.tar.gz.
File metadata
- Download URL: watch_path-0.1.1.tar.gz
- Upload date:
- Size: 19.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
237833e5d3ac17916dce1fdecafed07c18ced79d729cf3168c5bee11cb07ba07
|
|
| MD5 |
6a9a6ab4d779aaf52fe123f76f3af291
|
|
| BLAKE2b-256 |
b2c8bc43a5a73e54cb991eee1f19d887f6a8075ce57eb5e3dec085bf492cd378
|
File details
Details for the file watch_path-0.1.1-py3-none-any.whl.
File metadata
- Download URL: watch_path-0.1.1-py3-none-any.whl
- Upload date:
- Size: 21.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4f561113a5c3f866dabe48de7b6266fd5481eba6a272f2cc6742b66875b3092
|
|
| MD5 |
8e26f082a9b5a678f1465ad60129e449
|
|
| BLAKE2b-256 |
894e4d5f7b4d6b4091d25cf259074a5cc2c98e92d43568881f285f7efd166b24
|