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
🎉 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.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.
Metadata
Release files for watch-path 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| watch_path-0.1.1.tar.gz | 19.8 kB | Without provenance | Details for watch_path-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| watch_path-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for watch_path-0.1.1-py3-none-any.whl |
Total release size: 41.2 kB
Release files / watch_path-0.1.1.tar.gz
| Download URL | watch_path-0.1.1.tar.gz |
|---|---|
| Size | 19.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
237833e5d3ac17916dce1fdecafed07c18ced79d729cf3168c5bee11cb07ba07
|
|
BLAKE2b-256 checksum How to use checksums |
b2c8bc43a5a73e54cb991eee1f19d887f6a8075ce57eb5e3dec085bf492cd378
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.8
|
Release files / watch_path-0.1.1-py3-none-any.whl
| Download URL | watch_path-0.1.1-py3-none-any.whl |
|---|---|
| Size | 21.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4f561113a5c3f866dabe48de7b6266fd5481eba6a272f2cc6742b66875b3092
|
|
BLAKE2b-256 checksum How to use checksums |
894e4d5f7b4d6b4091d25cf259074a5cc2c98e92d43568881f285f7efd166b24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.8
|
Security for watch-path 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.