Skip to content

Commit

Permalink
Revert "Revert "Initial MacOS Support""
Browse files Browse the repository at this point in the history
This reverts commit 65d77d3.
  • Loading branch information
rob-white committed Oct 22, 2018
1 parent 65d77d3 commit cee88b5
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 38 deletions.
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ name = "pypi"

[packages]
pyinotify = {version = "*", platform_system = "== 'Linux'"}
pywin32 = {version = "*", platform_system = "== 'Windows'"}
pypiwin32 = {version = "*", platform_system = "== 'Windows'"}
macfsevents = {version = "*", platform_system = "== 'Darwin'"}

[dev-packages]
twine = "*"
pytest = "*"
pylint = "*"
127 changes: 106 additions & 21 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
> Watch folders for file/directory events with a simple API.
## Supports
* Linux & Windows
* Linux, Windows, Mac
* Python 2.7 & 3.4-3.7

## Installation
Expand Down Expand Up @@ -115,8 +115,34 @@ FolderSpy.watch(SaveFolder())
"""An event occurred that was on a directory."""
```

### Mac
```python
def process_IN_ATTRIB(self, event):
"""Metadata changed for a file."""

def process_IN_CREATE(self, event):
"""A file/directory was created in watched directory."""

def process_IN_DELETE(self, event):
"""A file/directory was deleted in watched directory."""

def process_IN_MODIFY(self, event):
"""A file was modified."""

def process_IN_MOVED_FROM(self, event):
"""A file/directory was moved away from the current watched directory."""

def process_IN_MOVED_TO(self, event):
"""A file/directory was moved into the current watched directory."""
```

## Dependencies
* Linux: ```pyinotify```
* Windows: ```pypiwin32```
* Mac: ```macfsevents```

## To-Do
* MacOS Support
* Start/Exit Events
* Tests
* Clean-up

Expand Down
4 changes: 4 additions & 0 deletions folderspy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
__version__ = '0.0.4'

import platform

if platform.system() == 'Windows':
from .windows import WatchableFolder, FolderSpy
elif platform.system() == 'Linux':
from .linux import WatchableFolder, FolderSpy
elif platform.system() == 'Darwin':
from .mac import WatchableFolder, FolderSpy
else:
raise Exception('{0} is not a supported.'.format(platform.system()))
2 changes: 1 addition & 1 deletion folderspy/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.3'
__version__ = '0.0.4'
4 changes: 2 additions & 2 deletions folderspy/linux/spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def _setup_watchers(folders):
proc_fun=folder,
rec=folder.recursive
)

@staticmethod
def _event_loop(folders):
def _event_loop(folders, **kwargs):
"""Kicks off the event loop that begins watching for events."""

asyncore.loop()
2 changes: 2 additions & 0 deletions folderspy/mac/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .folder import WatchableFolder
from .spy import FolderSpy
57 changes: 57 additions & 0 deletions folderspy/mac/folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os.path

from fsevents import Stream
from fsevents import IN_MODIFY, IN_ATTRIB, IN_CREATE, IN_DELETE, IN_MOVED_FROM, IN_MOVED_TO


class FileEvent(object):

def __init__(self, full_path, maskname):
self.name = os.path.basename(full_path)
self.pathname = full_path
self.maskname = maskname


class WatchableFolder(object):

MASK_NAMES = {
IN_MODIFY: 'IN_MODIFY',
IN_ATTRIB: 'IN_ATTRIB',
IN_CREATE: 'IN_CREATE',
IN_DELETE: 'IN_DELETE',
IN_MOVED_FROM: 'IN_MOVED_FROM',
IN_MOVED_TO: 'IN_MOVED_TO'
}

def __init__(self, path='', recursive=False, listen_to=None):
self.path = path
self.recursive = recursive
self.listen_to = listen_to
self.stream = Stream(self._on_event, self.path, file_events=True)

def _on_event(self, event):
"""Called when any event is triggered."""

mask_name = self.MASK_NAMES[event.mask]

getattr(self, 'process_{0}'.format(mask_name))(
FileEvent(event.name, mask_name)
)

def process_IN_ATTRIB(self, event):
"""Metadata changed for a file."""

def process_IN_CREATE(self, event):
"""A file/directory was created in watched directory."""

def process_IN_DELETE(self, event):
"""A file/directory was deleted in watched directory."""

def process_IN_MODIFY(self, event):
"""A file was modified."""

def process_IN_MOVED_FROM(self, event):
"""A file/directory was moved away from the current watched directory."""

def process_IN_MOVED_TO(self, event):
"""A file/directory was moved into the current watched directory."""
Loading

0 comments on commit cee88b5

Please sign in to comment.