Skip to content

Commit 4e5db84

Browse files
committed
added ability to pass command line argument of regular expression pattern matching. renamed script to be FileUpdater
1 parent e7b14c4 commit 4e5db84

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

README.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
# FileUpdaterPDF
1+
# FileUpdater
2+
23
A Python File System Listener
34

45
## Problem:
6+
57
I had two different directories. One was specific to all things published by my portfolio site and the other was specific to all things related to my resume. I wanted to keep these two directories distinct on my local drive; however, everytime I updated my resume, I also had to make sure I remembered to copy the latest one exported in PDF from a Word document to the public portfolio directory and properly rename it. This is a simply but unnecessarily tedious task. I figured all I needed to do is the have some event listener focus on a particular directory and copy the file over when a modification or file creation was detected.
68

79
## Requirements:
8-
* Python 3.5+
9-
* Watchdog
10-
* GitPython
11-
You can install `Watchdog` and `GitPython` by running the following command:
12-
```
10+
11+
- Python 3.5+
12+
- Watchdog
13+
- GitPython You can install `Watchdog` and `GitPython` by running the following command:
14+
15+
```
1316
$ pip install -r requirements.txt
14-
```
17+
```
1518

1619
## How To Use:
17-
This script was intended to be setup in the Windows Task Scheduler and executed at startup.
18-
To execute the script, navigate to the `src` directory and run the following command:
20+
21+
This script was intended to be setup in the Windows Task Scheduler and executed at startup. To execute the script, navigate to the `src` directory and run the following command:
22+
23+
```
24+
$ python FileUpdater.py -n <author_name> -e <author_email> -m <commit_message> -g <git_directory> -s <directory_to_watch> -d <destination_directories_01 destination_directories_02 ...> -p <pattern_matcher>
25+
```
26+
27+
Example:
28+
1929
```
20-
$ python FileUpdaterPDF.py author_name author_email commit_message git_directory directory_to_watch destination_directories
30+
$ python FileUpdater.py -n "David Lam" -e "some_email@xyz.com" -m "updating cool file" -g \some\git\directory -s \some\directory\to\watch -d \directory\01 \directory\02 -p *.pdf *.xml *.json
2131
```
2232

2333
### Arguments:
24-
* author_name: A String of the author and committer of the update.
25-
* author_email: A String of the author and committer's email.
26-
* commit_message: A String of the message associated with the Git commit.
27-
* git_directory: A String of the path to the git directory the files will be moved to.
28-
* directory_to_watch: A String of the path of the directory the script will listen to.
29-
* destination_directories: A list of Strings (must include at least 1) of the path to which to move the updated file(s) to. The path must be space separated. This can include a new name of the file if needed. For example: if a file was originally called `apple.pdf`, you could specify the path `/some_path/orange.pdf` to be the new name of the copied file.
30-
31-
## Extending:
32-
Change the `patterns` variable if you wish to listen to changes to other files beyond a simple PDF.
34+
35+
- author_name: A String of the author and committer of the update.
36+
- author_email: A String of the author and committer's email.
37+
- commit_message: A String of the message associated with the Git commit.
38+
- git_directory: A String of the path to the git directory the files will be moved to.
39+
- directory_to_watch: A String of the path of the directory the script will listen to.
40+
- destination_directories: A list of space separated Strings (must include at least 1) of the path to which to move the updated file(s) to. This can include a new name of the file if needed. For example: if a file was originally called `apple.pdf`, you could specify the path `/some_path/orange.pdf` to be the new name of the copied file.
41+
- pattern_matcher: A list of space separated regular expression Strings (must include at least 1).
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
import time
22
import shutil
3-
import subprocess
4-
import sys
5-
import os.path
63
import argparse
74
from git import Repo, Actor
85
from watchdog.observers import Observer
96
from watchdog.events import PatternMatchingEventHandler
107

118
"""
12-
FileUpdaterPDF watches for creations and modifications of pdf files at the scheduled source and moves them to the destination.
9+
FileUpdater watches for creations and modifications of pdf files at the scheduled source and moves them to the destination.
1310
1411
Note: The destination should contain the new name if needed. Example: /path/some-new-name.pdf
1512
"""
1613

1714

18-
class FileUpdaterPDF(PatternMatchingEventHandler):
19-
# Change this to include other specific extensions.
20-
patterns = ["*.pdf"]
15+
class FileUpdater(PatternMatchingEventHandler):
16+
patterns = []
2117

22-
def __init__(self, author_name, author_email, commit_message, git_directory, destinations):
18+
def __init__(self, author_name, author_email, commit_message, git_directory, destinations, pattern_matcher):
2319
"""
24-
Construct a FileUpdaterPDF.
20+
Construct a FileUpdater.
2521
"""
26-
super(FileUpdaterPDF, self).__init__()
22+
super(FileUpdater, self).__init__()
2723
self.git_directory = git_directory
2824
self.destinations = destinations
2925
self.author_name = author_name
3026
self.author_email = author_email
3127
self.commit_message = commit_message
28+
self.patterns += pattern_matcher
3229

3330
def process(self, event):
3431
"""
@@ -111,15 +108,17 @@ def main():
111108
parser.add_argument("-s", "--source_path",
112109
help="the directory to watch", required=True)
113110
parser.add_argument("-d", "--destinations",
114-
help="the destination(s) of the file", nargs="+")
111+
help="the destination(s) of the file", nargs="+", required=True)
112+
parser.add_argument("-p", "--pattern_matcher",
113+
help="file pattern to match in source directory", nargs="+", required=True)
115114

116115
# Parse arguments.
117116
arguments = parser.parse_args()
118117

119-
# Run WatchDog with FileUpdaterPDF.
118+
# Run WatchDog with FileUpdater.
120119
observer = Observer()
121-
observer.schedule(FileUpdaterPDF(
122-
arguments.author_name, arguments.author_email, arguments.commit_message, arguments.git_directory, arguments.destinations), arguments.source_path)
120+
observer.schedule(FileUpdater(
121+
arguments.author_name, arguments.author_email, arguments.commit_message, arguments.git_directory, arguments.destinations, arguments.pattern_matcher), arguments.source_path)
123122
observer.start()
124123

125124
# Print some initial information.

0 commit comments

Comments
 (0)