Skip to content

Commit

Permalink
added support for move from/to cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
gregghz committed Oct 31, 2012
1 parent f455d34 commit 437eab6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ job1:
# When monitoring a directory, the events marked with an asterisk (*) above
# can occur for files in the directory, in which case the name field in the
# returned event data identifies the name of the file within the directory.
events: ['create', 'move_to']
events: ['create', 'move_from', 'move_to']

# TODO:
# this currently isn't implemented, but this is where support will be added for:
Expand All @@ -74,4 +74,5 @@ job1:
# handle newly created directories when watching recursively. It's fine
# to leave out when recursive is false or you won't be creating new
# directories.
command: cp -r $filename /home/user/Documents/$dest_file
# $src_path is only used in move_to and is the corresponding path from move_from
command: cp -r $filename /home/user/Documents/$dest_file # $src_path
31 changes: 21 additions & 10 deletions watcher.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def __init__(self, command, recursive, mask, parent, prefix, root):
self.mask = mask #the watch mask
self.parent = parent #should be calling instance of WatcherDaemon
self.prefix = prefix #prefix to handle recursively watching new dirs
self.root = root #root of watch (actually used to calculate subdirs)
def runCommand(self, event):
self.root = root #root of watch (actually used to calculate subdirs)
self.move_map = {}

def runCommand(self, event, ignore_cookie=True):
t = Template(self.command)

#build the dest_file
Expand All @@ -179,14 +181,21 @@ def runCommand(self, event):
elif self.root != "":
dfile = re.sub('^'+re.escape(self.root+os.sep),'',event.path) + os.sep + dfile

#rdfile = os.path.dirname(event.path)+event.name

#find the src_path if it exists
src_path = ''
if not ignore_cookie and hasattr(event, 'cookie') and event.cookie in self.move_map:
src_path = self.move_map[event.cookie]
del self.move_map[event.cookie]

#run substitutions on the command
command = t.substitute(watched=event.path,
filename=event.pathname,
dest_file=dfile,
tflags=event.maskname,
nflags=event.mask)
command = t.safe_substitute({
'watched': event.path,
'filename': event.pathname,
'dest_file': dfile,
'tflags': event.maskname,
'nflags': event.mask,
'src_path': src_path
})

#try the command
try:
Expand Down Expand Up @@ -239,11 +248,12 @@ def process_IN_MOVE_SELF(self, event):

def process_IN_MOVED_FROM(self, event):
print "Moved from: ", event.pathname
self.move_map[event.cookie] = event.pathname
self.runCommand(event)

def process_IN_MOVED_TO(self, event):
print "Moved to: ", event.pathname
self.runCommand(event)
self.runCommand(event, False)

def process_IN_OPEN(self, event):
print "Opened: ", event.pathname
Expand All @@ -259,6 +269,7 @@ def run(self):
self.notifiers = []

# parse jobs.yml and add_watch/notifier for each entry
print jobs_file
jobs = load(jobs_file, Loader=Loader)
if jobs is not None:
for job in jobs.iteritems():
Expand Down

0 comments on commit 437eab6

Please sign in to comment.