Created
October 5, 2016 08:57
-
-
Save mherrmann/a977536068fbc914870e97b0ee1cbb08 to your computer and use it in GitHub Desktop.
Revisions
-
mherrmann created this gist
Oct 5, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ from ctypes import CFUNCTYPE, POINTER, c_uint8, c_void_p, c_long, addressof, \ byref, CDLL, cdll, c_char_p from ctypes.util import find_library def is_hidden(path): if not isinstance(path, bytes): path = path.encode('utf-8') with AutoRelease() as autorelease: url = autorelease(CFURLCreateFromFileSystemRepresentation( None, path, len(path), False )) val = c_void_p(0) err = CFError() ret = CFURLCopyResourcePropertyForKey( url, kCFURLIsHiddenKey, addressof(val), byref(err) ) if ret: autorelease(val) return bool(CFBooleanGetValue(val)) else: raise OSError( 'CFURLCopyResourcePropertyForKey failed with error %d' % CFErrorGetCode(err) ) class AutoRelease: def __init__(self): self.to_release = [] self.pool = None def __enter__(self): self.pool = \ objc.objc_msgSend(NSAutoreleasePool, objc.sel_registerName('alloc')) self.pool = objc.objc_msgSend(self.pool, objc.sel_registerName('init')) return self def __call__(self, obj_to_release): self.to_release.append(obj_to_release) return obj_to_release def __exit__(self, *_): for obj in self.to_release: CFRelease(obj) objc.objc_msgSend(self.pool, objc.sel_registerName('drain')) CoreFoundation = CDLL(find_library('CoreFoundation')) Foundation = CDLL(find_library('Foundation')) objc = cdll.LoadLibrary(find_library('objc')) Boolean = c_uint8 CFTypeRef = c_void_p CFIndex = c_long CFString = CFTypeRef CFURL = CFTypeRef CFError = CFTypeRef CFErrorRef = POINTER(CFError) CFRelease = CFUNCTYPE(None, CFTypeRef)(('CFRelease', Foundation)) NSAutoreleasePool = objc.objc_getClass('NSAutoreleasePool') CFURLCreateFromFileSystemRepresentation = \ CFUNCTYPE(CFURL, c_void_p, c_char_p, CFIndex, Boolean)\ (('CFURLCreateFromFileSystemRepresentation', CoreFoundation)) CFURLCopyResourcePropertyForKey = \ CFUNCTYPE(Boolean, CFURL, CFString, c_void_p, CFErrorRef)\ (('CFURLCopyResourcePropertyForKey', CoreFoundation)) CFBooleanGetValue = \ CFUNCTYPE(Boolean, c_void_p)\ (('CFBooleanGetValue', CoreFoundation)) CFErrorGetCode = \ CFUNCTYPE(CFIndex, CFError)\ (('CFErrorGetCode', CoreFoundation)) kCFURLIsHiddenKey = CFString.in_dll(CoreFoundation, 'kCFURLIsHiddenKey')