Skip to content

Commit

Permalink
Fix formatting (black)
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Apr 18, 2024
1 parent ecdab7b commit 3f599d4
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 135 deletions.
106 changes: 56 additions & 50 deletions fsevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,16 @@
import unicodedata

from _fsevents import (
CF_POLLIN,
CF_POLLOUT,
FS_CFLAGFILEEVENTS,
FS_CFLAGIGNORESELF,
FS_CFLAGNODEFER,
FS_CFLAGNONE,
FS_CFLAGUSECFTYPES,
FS_CFLAGWATCHROOT,
FS_EVENTIDSINCENOW,
FS_FILEEVENTS,
FS_FLAGEVENTIDSWRAPPED,
FS_FLAGHISTORYDONE,
FS_FLAGKERNELDROPPED,
FS_FLAGMOUNT,
FS_FLAGMUSTSCANSUBDIRS,
FS_FLAGNONE,
FS_FLAGROOTCHANGED,
FS_FLAGUNMOUNT,
FS_FLAGUSERDROPPED,
FS_IGNORESELF,
FS_ITEMCHANGEOWNER,
FS_ITEMCREATED,
FS_ITEMFINDERINFOMOD,
Expand All @@ -44,27 +34,26 @@

class Mask(int):
stringmap = {
FS_FLAGMUSTSCANSUBDIRS: 'MustScanSubDirs',
FS_FLAGUSERDROPPED: 'UserDropped',
FS_FLAGKERNELDROPPED: 'KernelDropped',
FS_FLAGEVENTIDSWRAPPED: 'EventIDsWrapped',
FS_FLAGHISTORYDONE: 'HistoryDone',
FS_FLAGROOTCHANGED: 'RootChanged',
FS_FLAGMOUNT: 'Mount',
FS_FLAGUNMOUNT: 'Unmount',

FS_FLAGMUSTSCANSUBDIRS: "MustScanSubDirs",
FS_FLAGUSERDROPPED: "UserDropped",
FS_FLAGKERNELDROPPED: "KernelDropped",
FS_FLAGEVENTIDSWRAPPED: "EventIDsWrapped",
FS_FLAGHISTORYDONE: "HistoryDone",
FS_FLAGROOTCHANGED: "RootChanged",
FS_FLAGMOUNT: "Mount",
FS_FLAGUNMOUNT: "Unmount",
# Flags when creating the stream.
FS_ITEMCREATED: 'ItemCreated',
FS_ITEMREMOVED: 'ItemRemoved',
FS_ITEMINODEMETAMOD: 'ItemInodeMetaMod',
FS_ITEMRENAMED: 'ItemRenamed',
FS_ITEMMODIFIED: 'ItemModified',
FS_ITEMFINDERINFOMOD: 'ItemFinderInfoMod',
FS_ITEMCHANGEOWNER: 'ItemChangedOwner',
FS_ITEMXATTRMOD: 'ItemXAttrMod',
FS_ITEMISFILE: 'ItemIsFile',
FS_ITEMISDIR: 'ItemIsDir',
FS_ITEMISSYMLINK: 'ItemIsSymlink'
FS_ITEMCREATED: "ItemCreated",
FS_ITEMREMOVED: "ItemRemoved",
FS_ITEMINODEMETAMOD: "ItemInodeMetaMod",
FS_ITEMRENAMED: "ItemRenamed",
FS_ITEMMODIFIED: "ItemModified",
FS_ITEMFINDERINFOMOD: "ItemFinderInfoMod",
FS_ITEMCHANGEOWNER: "ItemChangedOwner",
FS_ITEMXATTRMOD: "ItemXAttrMod",
FS_ITEMISFILE: "ItemIsFile",
FS_ITEMISDIR: "ItemIsDir",
FS_ITEMISSYMLINK: "ItemIsSymlink",
}

_svals = list(stringmap.items())
Expand Down Expand Up @@ -97,7 +86,8 @@ def check_path_string_type(*paths):
for path in paths:
if not isinstance(path, str):
raise TypeError(
"Path must be string, not '%s'." % type(path).__name__)
"Path must be string, not '%s'." % type(path).__name__
)


class Observer(threading.Thread):
Expand Down Expand Up @@ -139,16 +129,25 @@ def _schedule(self, stream):
if stream.file_events:
callback = FileEventCallback(stream.callback, stream.raw_paths)
else:

def callback(paths, masks, ids):
for path, mask, id in zip(paths, masks, ids):
if sys.version_info[0] >= 3:
path = path.decode('utf-8')
path = path.decode("utf-8")
if stream.ids is False:
stream.callback(path, mask)
elif stream.ids is True:
stream.callback(path, mask, id)

schedule(self, stream, callback, stream.paths, stream.since, stream.latency, stream.cflags)

schedule(
self,
stream,
callback,
stream.paths,
stream.since,
stream.latency,
stream.cflags,
)

def schedule(self, stream):
self.lock.acquire()
Expand Down Expand Up @@ -182,23 +181,26 @@ def stop(self):
self.event = None
event.set()


class Stream(object):
def __init__(self, callback, *paths, **options):
file_events = options.pop('file_events', False)
since = options.pop('since',FS_EVENTIDSINCENOW)
cflags = options.pop('flags', FS_CFLAGNONE)
latency = options.pop('latency', 0.01)
ids = options.pop('ids', False)
assert len(options) == 0, "Invalid option(s): %s" % repr(options.keys())
file_events = options.pop("file_events", False)
since = options.pop("since", FS_EVENTIDSINCENOW)
cflags = options.pop("flags", FS_CFLAGNONE)
latency = options.pop("latency", 0.01)
ids = options.pop("ids", False)
assert len(options) == 0, "Invalid option(s): %s" % repr(
options.keys()
)
check_path_string_type(*paths)

self.callback = callback
self.raw_paths = paths

# The C-extension needs the path in 8-bit form.
self.paths = [
path if isinstance(path, bytes)
else path.encode('utf-8') for path in paths
path if isinstance(path, bytes) else path.encode("utf-8")
for path in paths
]

self.file_events = file_events
Expand All @@ -207,8 +209,9 @@ def __init__(self, callback, *paths, **options):
self.latency = latency
self.ids = ids


class FileEvent(object):
__slots__ = 'mask', 'cookie', 'name'
__slots__ = "mask", "cookie", "name"

def __init__(self, mask, cookie, name):
self.mask = mask
Expand All @@ -218,6 +221,7 @@ def __init__(self, mask, cookie, name):
def __repr__(self):
return repr((self.mask, self.cookie, self.name))


class FileEventCallback(object):
def __init__(self, callback, paths):
self.snapshots = {}
Expand All @@ -235,13 +239,13 @@ def __call__(self, paths, masks, ids):
for path in sorted(paths):
# supports UTF-8-MAC(NFD)
if not isinstance(path, unicode):
path = path.decode('utf-8')
path = unicodedata.normalize('NFD', path).encode('utf-8')
path = path.decode("utf-8")
path = unicodedata.normalize("NFD", path).encode("utf-8")

if sys.version_info[0] >= 3:
path = path.decode('utf-8')
path = path.rstrip('/')
path = path.decode("utf-8")

path = path.rstrip("/")
snapshot = self.snapshots[path]
current = {}
try:
Expand All @@ -266,13 +270,15 @@ def __call__(self, paths, masks, ids):
observed.discard(name)
else:
event = created.get(snap_stat.st_ino)
if (event is not None):
if event is not None:
self.cookie += 1
event.mask = IN_MOVED_FROM
event.cookie = self.cookie
tmp_filename = event.name
event.name = filename
events.append(FileEvent(IN_MOVED_TO, self.cookie, tmp_filename))
events.append(
FileEvent(IN_MOVED_TO, self.cookie, tmp_filename)
)
else:
event = FileEvent(IN_DELETE, None, filename)
deleted[snap_stat.st_ino] = event
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[tool.black]
line-length = 79

[tool.isort]
multi_line_output=3
91 changes: 48 additions & 43 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,53 @@ def read(fname):
with open(os.path.join(os.path.dirname(__file__), fname)) as f:
return f.read()


ext_modules = [
Extension(name = '_fsevents',
sources = ['_fsevents.c', 'compat.c'],
extra_link_args = ["-framework","CoreFoundation",
"-framework","CoreServices"],
),
]
Extension(
name="_fsevents",
sources=["_fsevents.c", "compat.c"],
extra_link_args=[
"-framework",
"CoreFoundation",
"-framework",
"CoreServices",
],
),
]

setup(name = "MacFSEvents",
version = "0.8.4",
description = "Thread-based interface to file system observation primitives.",
long_description = "\n\n".join((read('README.rst'), read('CHANGES.rst'))),
license = "BSD",
data_files = [("", [
"compat.h",
"LICENSE.txt",
"CHANGES.rst"
])],
author = "Malthe Borch",
author_email = "mborch@gmail.com",
url = 'https://github.com/malthe/macfsevents',
cmdclass = dict(build_ext=build_ext),
ext_modules = ext_modules,
platforms = ["Mac OS X"],
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: MacOS :: MacOS X',
'Programming Language :: C',
'Programming Language :: Python :: 2.4',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: System :: Filesystems',
],
zip_safe=False,
test_suite="tests",
py_modules=['fsevents'],
)
setup(
name="MacFSEvents",
version="0.8.4",
description=(
"Thread-based interface to file system observation " "primitives."
),
long_description="\n\n".join((read("README.rst"), read("CHANGES.rst"))),
license="BSD",
data_files=[("", ["compat.h", "LICENSE.txt", "CHANGES.rst"])],
author="Malthe Borch",
author_email="mborch@gmail.com",
url="https://github.com/malthe/macfsevents",
cmdclass=dict(build_ext=build_ext),
ext_modules=ext_modules,
platforms=["Mac OS X"],
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: MacOS :: MacOS X",
"Programming Language :: C",
"Programming Language :: Python :: 2.4",
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: System :: Filesystems",
],
zip_safe=False,
test_suite="tests",
py_modules=["fsevents"],
)
Loading

0 comments on commit 3f599d4

Please sign in to comment.