本文整理汇总了Python中fsevents.Observer类的典型用法代码示例。如果您正苦于以下问题:Python Observer类的具体用法?Python Observer怎么用?Python Observer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Observer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: callback
def callback(event):
global observer, git_sync
if event.mask == kFSEventStreamEventFlagItemCreated:
# Sublime Text Seems to trigger a lot of these and they don't seem to
# warrant a new sync, so lets skip these for now.
return
filename = event.name
git_dir = os.path.join(config['local_path'], '.git')
if git_dir in filename:
# Skip sync for file change that are in the .git directory.
return
if observer:
# Stop observing.
observer.unschedule(stream)
observer.stop()
git_sync.run_sync()
# Start observing again.
observer = Observer()
observer.schedule(stream)
observer.start()
示例2: main
def main():
observer = Observer()
observer.start()
path = '/Users/seungjin/Desktop'
def callback(event):
#print "mask: " + str(event.mask)
#print "cookie: " + str(event.cookie)
#print "name: " + str(event.name)
print event
if event.mask == 256: #looks like new file comming
newFile(str(event.name))
elif event.mask == 512: #looks line file deleted
rmFile(str(event.name))
elif event.mask == 2: #looks like overwriting?
print "hihihihi"
def newFile(filename):
print "new file is comming"
#pushing this file into cloud
gs = Googlestorage()
#print gs.list_objects()
gs.upload_objects(filename)
def rmFile(filename):
#print "%s is removed" % filename
gs = Googlestorage() # this is evil.. do i need to make global goolgestorage object??? idk
gs.delete_objects_and_buckets(filename)
stream = Stream(callback,path,file_events=True)
observer.schedule(stream)
示例3: main
def main():
def sigterm_handler(_signo, _stack_frame):
try:
Notifier.notify('Unregistering fs watch', title='LittleHelp', subtitle=project_path)
except:
pass
logging.info("Sigterm handler called")
observer.unschedule(stream)
observer.stop()
observer.join()
try:
Notifier.notify('Unregistered fs watch', title='LittleHelp', subtitle=project_path)
except:
pass
sys.exit(0)
try:
Notifier.notify('Registering watch', title='LittleHelp', subtitle=project_path)
observer = Observer()
stream = Stream(file_event_callback, project_path, file_events=True)
observer.schedule(stream)
observer.start()
signal.signal(signal.SIGTERM, sigterm_handler)
while True:
sleep(0.1)
except:
logging.exception("Unhandled exception")
示例4: test_single_file_added
def test_single_file_added(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Observer
observer = Observer(callback, file_events=True)
observer.start()
observer.schedule(path)
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f.close()
time.sleep(1.1)
# stop and join observer
observer.stop()
observer.join()
self.assertEquals(events[0][0], path)
示例5: __init__
class Dmon:
def __init__(self, dir, handlers):
self._handlers = handlers
self._dir = dir
def _call_handler(self, method, fname):
for handler in self._handlers:
getattr(handler, method)(fname)
def _run_pyinotify(self):
from lib import pyinotify
class PyInotifyEventProcessor(pyinotify.ProcessEvent):
def __init__(self, c_handler):
self._call = c_handler
def process_IN_CREATE(self, event):
self._call('on_create', event.pathname)
def process_IN_DELETE(self, event):
self._call('on_delete', event.pathname)
def process_IN_MODIFY(self, event):
self._call('on_update', event.pathname)
wm = pyinotify.WatchManager()
self._observer = pyinotify.Notifier(wm, PyInotifyEventProcessor(self._call_handler))
mask = pyinotify.ALL_EVENTS
wm.add_watch(self._dir, mask, rec=True)
while True:
self._observer.process_events()
if self._observer.check_events():
self._observer.read_events()
time.sleep(1)
def _run_macfse(self):
from lib import fsevents
from fsevents import Stream
from fsevents import Observer
def macfse_callback(event):
if event.mask in [256, 128]:
self._call_handler('on_create', event.name)
elif event.mask in [512, 64]:
self._call_handler('on_delete', event.name)
elif event.mask == 2:
self._call_handler('on_update', event.name)
self._observer = Observer()
self._stream = Stream(macfse_callback, self._dir, file_events=True)
self._observer.schedule(self._stream)
self._observer.start()
def start(self):
if 'darwin' in sys.platform.lower():
self._run_macfse()
elif 'linux' in sys.platform.lower():
self._run_pyinotify()
def stop(self):
if 'darwin' in sys.platform.lower():
self._observer.unschedule(self._stream)
self._observer.stop()
elif 'linux' in sys.platform.lower():
self._observer.stop()
示例6: run
def run(self):
observer = Observer()
observer.start()
#handler = self.process_event(self)
stream = Stream(self, self.path, file_events=True)
observer.schedule(stream)
示例7: main
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('path')
arg_parser.add_argument('-c', '--cmd', required=True, nargs='+', help='Console command for execute')
arg_parser.add_argument('-d', '--delay', type=int, default=5, nargs='?', help='Synchronization delay in seconds')
arg_parser.add_argument('-v', '--verbose', action='store_true', help='verbose flag')
try:
args = arg_parser.parse_args()
path = args.path
if not os.path.isdir(path):
raise IOError('Path `%s` is not a directory' % path)
event_handler = EventHandler()
timer = TimerInterval(callback_func, args.delay, event_handler=event_handler, cmd=args.cmd, verbose=args.verbose)
stream = Stream(event_handler.callback_handler, path, file_events=True)
observer = Observer()
observer.schedule(stream)
observer.start()
timer.start()
except IOError as e:
print e
except KeyboardInterrupt:
observer.unschedule(stream)
observer.stop()
timer.stop()
示例8: loop
def loop(self):
observer = Observer()
observer.schedule(self.stream)
observer.daemon = True
observer.start()
try:
while True:
# Cannot use observer.join() as it is not interrupted by
# Ctrl-C
time.sleep(10000)
finally:
observer.unschedule(self.stream)
observer.stop()
示例9: get_notified
def get_notified(path_regexps, project_path):
"""Get notified when files change, and minify them. """
observer = Observer()
observer.start()
def file_changed(file_change_event):
"""Callback for when a file has changed"""
file_path = file_change_event.name
if is_minifiable(file_path) and needs_minifying(file_path):
tell_user_and_minify(file_path)
stream = Stream(file_changed, project_path, file_events=True)
observer.schedule(stream)
示例10: start_watching
def start_watching(self):
""" Start watching our paths, and do the main work. """
# setup
observer = Observer()
stream = Stream(
self.capture_event,
self.settings['local']['watch_path'],
file_events=True
)
observer.schedule(stream)
# go go go
observer.run()
示例11: main
def main():
usage = 'usage: %prog [options] CALL_DIRECTORY'
parser = OptionParser(usage=usage)
parser.add_option('-p', '--port', dest='port',
help='port to listen on')
parser.add_option('-a', '--address', dest='address',
help='address to listen on')
(options, args) = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
call_definition_path = args[0]
if options.port:
port = int(options.port)
else:
port = PORT
if options.address:
ip_address = options.address
else:
ip_address = IP_ADDRESS
# Monitor the call definition path to restart the
# server if any of the files change, or new ones
# are added
observer = Observer()
observer.start()
stream = Stream(definition_change, call_definition_path)
observer.schedule(stream)
global call_handler
call_handler = CallHandler(call_definition_path)
server_class = BaseHTTPServer.HTTPServer
global httpd
httpd = server_class((ip_address, port), HttpRequestHandler)
print 'WebServiceSimulator started'
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
print 'Shutting down web service simulator'
httpd.server_close()
sys.exit(0)
示例12: __init__
def __init__(self, path, f, ignore = None):
logging.info("Using FSEvents")
self.path = path
self.f = f
self.ignore = ignore
self.observer = Observer()
self.observer.daemon = True
self._streams = []
self._streams.append(createStream(self.path, path, self._update))
self._streamed_folders = [self.path]
def add_stream(p):
if p in self._streamed_folders:
return
self._streams.append(
createStream(p, file_path, self._update))
self._streamed_folders.append(p)
# Start an extra listener for all symlinks
for file_path in fileList(self.path, folders=True):
if os.path.islink(file_path):
if os.path.isdir(file_path):
add_stream(os.path.realpath(file_path))
else:
add_stream(os.path.dirname(os.path.realpath(file_path)))
示例13: start
def start(self):
self.stream = Stream(self.directory_changed, *self.target_dirs)
self.observer = Observer()
self.observer.schedule(self.stream)
self.observer.daemon = True # Kill observer when main thread killed.
self.observer.start() # start observer in the other thread.
示例14: loop
def loop(self):
observer = Observer()
observer.schedule(self.stream)
observer.daemon = True
observer.start()
try:
while True:
try:
# Cannot use blocking get() as it is not interrupted by
# Ctrl-C
if self.wait_queue.get(10000) is True:
self.force_restart()
except Empty:
pass
finally:
observer.unschedule(self.stream)
observer.stop()
示例15: __init__
class FSEventsScanner:
"""
Runs Ok, as long as you have MacFSEvents installed - but does not catch changes to the permissions nor uid/gid
"""
def __init__(self):
self.paths = []
self.observer = Observer()
self.stream = None
def addPathToListenForChangesOn(self, thePath):
print "added path:" + thePath
self.paths.append(thePath)
def startListening(self):
self.observer.start()
self.stream = Stream(callback, *self.paths, file_events=True)
self.observer.schedule(self.stream)