def start(self): self.log.info("Refreshing existing files.") for root, dirs, files in os.walk(self.data_dir): for filename in files: self._maybe_queue_file(os.path.join(root, filename), enqueue=not (self.ignore_existing)) # watch if configured if self.ignore_changes: return observer = observers.Observer() observer.schedule(self, path=self.data_dir, recursive=True) self.log.info("Watching for new file under %s", self.data_dir) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join(timeout=30) if observer.isAlive(): self.log.error("Watchdog Observer failed to stop. Aborting.") os.kill(os.getpid(), signal.SIGKILL) return
def main(): pygame.init() display = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) surface = pygame.display.get_surface() screen_size = surface.get_width(), surface.get_height() path = os.path.join(HERE) controller = init.Controller() state = init.State(screen_size) def start_game(): controller.reset() for package in [init, loop, constants, sprites]: importlib.reload(package) loop.game_loop(display, controller, state) controller.shutdown_callback = start_game def reboot_game(_): controller.running = False event_handler = events.FileSystemEventHandler() event_handler.on_modified = reboot_game observer = observers.Observer() observer.schedule(event_handler, path) observer.start() start_game()
def watchdogMonitorDirectory(): path = sys.argv[1] if len(sys.argv) > 1 else '.' event_handler = events.LoggingEventHandler() observer = observers.Observer() observer.schedule(event_handler, "C:/", recursive=True) observer.start()
def monitor_filesystem(): class MyHandler(events.PatternMatchingEventHandler): patterns = ["*.txt"] def on_any_event(self, event): if event.event_type == 'moved' or event.event_type == 'deleted': search_inst.delete_path(event.src_path) if event.event_type == 'moved': search_inst.load_path(event.dest_path) if event.event_type == 'created' or event.event_type == 'modified': print('created:', event.src_path) search_inst.load_path(event.src_path) event_handler = MyHandler() observer = observers.Observer() observer.schedule(event_handler, path=DIR_PATH_NOTES, recursive=False) observer.start() print('monitoring:', DIR_PATH_NOTES) try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
def watch(directory, callback, cmdQueue): """Watch the given directory and call `callback` on new files. Monitoring is done using the watchdog module, which will try to use the best asynchronous API available (kqueue on OS X/BSD, inotify on Linux, who-knows-what on Windows). Args: - directory: A string representing the path to the directory to watch. - callback: A function which will be called on each new file appearing in `directory`. - cmdQueue: a Queue.Queue for sending/receiving commands to/from the containing process. """ if not os.path.isdir(directory): raise InvalidDirectory(directory) observer = observers.Observer() observer.schedule(CreatedFileHandler(callback), directory) observer.start() while True: cmd = cmdQueue.get() if cmd == config._STOP: observer.stop() observer.unschedule_all() observer.join() cmdQueue.task_done() break else: cmdQueue.put(cmd) time.sleep(1)
def LogFileWatcher(filepath, callback, message_prefix=""): """Return a watchdog thread that reacts to file modification. """ logfiledir = os.path.dirname(filepath) handler = _LogFileChangedHandler(filepath, callback, message_prefix) observer = observers.Observer() observer.schedule(handler, logfiledir) return observer
def __init__(self, path=''): super().__init__() self.__target_path = path self.__observer = observers.Observer() self.__event_handler = ChanegeHandla() self.__event_handler.directory_changed.connect(self.directory_changed.emit)
def start(self): if not self._observer: self._observer = observers.Observer() self._observer.schedule(self, self._path, False) self._url = "http://%s:%d/" % (edutils.get_ipaddr(), 8097) self._thread = threading.Thread(target=self.__run_httpd) self._thread.daemon = True self._thread.start()
def run(self): event_handler = PipWatch("*.py") observer = observers.Observer() observer.schedule(event_handler, self.distribution.get_name(), recursive=True) observer.start() try: while True: time.sleep(5) except KeyboardInterrupt: observer.stop()
def watch_filesystem(root_dir: str, events_queue: multiprocessing.Queue): event_handler = FileSystemEventHandler(events_queue) observer = observers.Observer() observer.schedule(event_handler, root_dir, recursive=True) observer.start() events_queue.put(READY) try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
def create_watcher(paths, cache): watchers = [] obj = MyFileSystemWatcher(cache) for path in paths: watchers.append((observers.Observer(), path)) for x in range(0, len(watchers)): try: watchers[x][0].schedule(obj, watchers[x][1], recursive=True) watchers[x][0].start() except Exception as e: print(e.args) return watchers
def begin_auto_update(reloader): """ Spawns a thread that will automatically perform the update on save. Returns: The handler object in use e.g. to pass into the GUI """ handler = Handler(reloader) observer = observers.Observer() observer.name = "File watching thread" observer.schedule(handler, path='.', recursive=True) observer.start() return handler
def watch_and_spawn(self, conf): import watchdog.events as events import watchdog.observers as observers print('Monitoring for changes...', file=sys.stderr) self.create_subprocess() parent = self class AggressiveEventHandler(events.FileSystemEventHandler): def __init__(self): self.wait = False def should_reload(self, event): for t in (events.FileSystemMovedEvent, events.FileModifiedEvent, events.DirModifiedEvent): if isinstance(event, t): return True return False def ignore_events_one_sec(self): if not self.wait: self.wait = True t = threading.Thread(target=self.wait_one_sec) t.start() def wait_one_sec(self): time.sleep(1) self.wait = False def on_modified(self, event): if self.should_reload(event) and not self.wait: print("Some source files have been modified", file=sys.stderr) print("Restarting server...", file=sys.stderr) parent.server_process.kill() self.ignore_events_one_sec() parent.create_subprocess() paths = self.paths_to_monitor(conf) event_handler = AggressiveEventHandler() for path, recurse in paths: observer = observers.Observer() observer.schedule(event_handler, path=path, recursive=recurse) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: pass
async def asyncwatch(*textobjectstores: TextObjectStorage): obs = observers.Observer() for st, path in [(st, p) for st in textobjectstores for p in st.files]: obs.schedule(st, str(path.parent), recursive=False) async def _watch(): obs.start() while obs.is_alive: await asyncio.sleep(0.5) asyncio.create_task(_watch()) return lambda: obs.stop()
def watch(container_id=None, rebuild_agent=False, interval=2): container_id = container_id or work.last_container_id services_to_restart = set() services_to_restart_lock = threading.Lock() class Handler(events.FileSystemEventHandler): def __init__(self, services): self.services = set(services) def on_modified(self, event): if event.is_directory: with services_to_restart_lock: services_to_restart.update(self.services) observer = observers.Observer() for package, services in configuration.package_services.items(): src = '{}/{}/{}'.format(configuration.source_root, configuration.package_dir[package], package) observer.schedule(Handler(services), path=src, recursive=True) observer.start() scheduler = sched.scheduler(time.time, time.sleep) def restart_changed_services(): with services_to_restart_lock: current_services_to_restart = services_to_restart.copy() services_to_restart.clear() for service in current_services_to_restart: if service == constants.AGENT_STUB_SERVICE and rebuild_agent: build_agent(container_id) else: _restart_service(container_id, service) scheduler.enter(interval, 1, restart_changed_services, ()) restart_changed_services() message = 'Filesystem watch started.' if rebuild_agent: message = ( '{} Relevant services will be restarted and CentOS agent ' 'package (dockercompute) rebuilt on code changes.'.format(message)) else: message = ('{} Relevant services will be restarted on code changes. ' 'Use --rebuild-agent to also rebuild Centos agent package ' '(dockercompute) on code changes.'.format(message)) logger.info(message) try: scheduler.run() except KeyboardInterrupt: pass
def _serve(self, app, directory, create, recursive, services_service, **config): """Start the publisher Args: app (nagare.server.base_application): the application to receive the files modifications events directory (str): directory to watch create (boolean): create ```directory`` if it doesn't exist recursive (boolean): watch the whole directory tree starting at ``directory`` Returns: str: the banner """ super(Publisher, self)._serve(app) if not os.path.exists(directory) and create: os.mkdir(directory) # Keep only the configuration options specifics to this publisher config = {k: v for k, v in config.items() if k not in publisher.Publisher.CONFIG_SPEC} event_handler = events.PatternMatchingEventHandler(**config) # Keywords passed to the application: # - ``event_type``: type of the event (``moved``, ``deleted``, ``created``, ``modified`` or ``closed``) # - ``src_path``: path of the modified file/directory # - ``is_directory``: ``True`` if the event was emitted for a directory # - ``dest_path``: in case of ``moved`` event, the new file/directory path event_handler.on_any_event = lambda event: self.start_handle_request( app, services_service, event_type=event.event_type, src_path=event.src_path, is_directory=event.is_directory, dest_path=getattr(event, 'dest_path', None) ) observer = observers.Observer() observer.schedule(event_handler, directory, recursive=recursive) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
def setUp(self): super(TestWatsonServer, self).setUp() self.mox.StubOutClassWithMocks(SimpleXMLRPCServer, "SimpleXMLRPCServer") hostport = ("localhost", 0x221B) self.server_mock = SimpleXMLRPCServer.SimpleXMLRPCServer( hostport, allow_none=True) self.server_mock.register_instance(mox.IsA(core.WatsonServer)) self.mox.StubOutClassWithMocks(observers, "Observer") self.observer_mock = observers.Observer() self.mox.StubOutClassWithMocks(core, "EventScheduler") self.scheduler_mock = core.EventScheduler()
def __init__(self): logging.info('Starting watson server %s', __version__) self._config = Config(load_config_safe(DEFAULT_GLOBAL_CONFIG_FILE)) self._projects = {} self._builder = ProjectBuilder() self._observer = observers.Observer() self._scheduler = EventScheduler() self._init_pynotify() # TODO(dejw): read (host, port) from config in user's directory self.endpoint = ('localhost', 0x221B) self._api = SimpleXMLRPCServer.SimpleXMLRPCServer(self.endpoint, allow_none=True) self._api.register_instance(self)
def serve(config, options=None): """ Start the devserver, and rebuild the docs whenever any changes take effect. """ # Create a temporary build directory, and set some options to serve it tempdir = tempfile.mkdtemp() options['site_dir'] = tempdir # Only use user-friendly URLs when running the live server options['use_directory_urls'] = True # Perform the initial build config = load_config(options=options) build(config, live_server=True) # Note: We pass any command-line options through so that we # can re-apply them if the config file is reloaded. event_handler = BuildEventHandler(options) config_event_handler = ConfigEventHandler(options) observer = observers.Observer() observer.schedule(event_handler, config['docs_dir'], recursive=True) for theme_dir in config['theme_dir']: observer.schedule(event_handler, theme_dir, recursive=True) observer.schedule(config_event_handler, '.') observer.start() class TCPServer(socketserver.TCPServer): allow_reuse_address = True class DocsDirectoryHandler(FixedDirectoryHandler): base_dir = config['site_dir'] host, port = config['dev_addr'].split(':', 1) server = TCPServer((host, int(port)), DocsDirectoryHandler) print('Running at: http://%s:%s/' % (host, port)) print('Live reload enabled.') print('Hold ctrl+c to quit.') server.serve_forever() # Clean up observer.stop() observer.join() shutil.rmtree(tempdir)
def monitor(path): ''' Monitorea los eventos del sistema de archivos de una 'cuenta'(carpeta) de un usuario y luego envia los cambios a los clientes conectados ''' #session['id_tasks'] = self.request.id ''' Error al modificar archivos desde el servidor. por revisar ''' socketio = SocketIO(message_queue=app.config['CELERY_BROKER_URL'] ) #Conectando a la cola de mensajes de la App monitorsystem = event_handler.EventHandler() observer = observers.Observer() observer.schedule(monitorsystem, path=path, recursive=True) observer.start() print("Ruta:", path) try: while True: if monitorsystem.message != {}: _message = monitorsystem.message # await websocket.send(json.dumps(_message)) #print(f'Sending... \n {_message} \n ') monitorsystem.message = {} print(_message) socketio.emit('files', json.dumps(_message)) _message = {} else: pass except KeyboardInterrupt: observer.stop() observer.join()
def watch_dirs(scanned_dirs, verbosity): print("Watching directories:") for scanned_dir in scanned_dirs: print(scanned_dir) print("\nPress Control+C to exit.\n") compilers = utils.get_compilers().values() observer = observers.Observer() for scanned_dir in scanned_dirs: handler = EventHandler(scanned_dir, verbosity, compilers) observer.schedule(handler, path=scanned_dir, recursive=True) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
def start(self, journal_path): with self._lock: if not self._running: if not journal_path or len(journal_path) == 0: raise ValueError("Invalid journal path specified.") self._running = True loglist = _get_log_files(journal_path, self._prefix) if loglist: file_date, self._logfilename = loglist[-1] self._date_created = file_date.date() self._logfile = open(self._logfilename, "r") self._logfile.seek(0, os.SEEK_END) self._where = self._logfile.tell() self._logfile.close() else: self._logfilename = None self._logfile = None self._date_created = None self._where = 0 self._previous_time = None self._log.debug("Parsing [%s] [%s]" % (self._logfilename, str(self._date_created))) self._observer = observers.Observer() self._observer.schedule(self, journal_path, False) self._observer.start() self._thread = threading.Thread( target=self.__file_modified_thread) self._thread.daemon = True self._thread.start()
def watch(): observer = observers.Observer() observer.schedule(Watcher(), str(Constants.MASTER_PATH), recursive=True) observer.start() observer.join()
def start(self): self._observer = observers.Observer() for dir_, handler in self._watches.items(): self._observer.schedule(handler, dir_, recursive=True) self._observer.start()
client_address = None class FileHandlerCustom(ev.LoggingEventHandler): def on_any_event(self, event): print("I am a nerd who generated these events " + str(event)) if (connection): connection.sendall(bytearray(str(event), 'utf8')) if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S') path = sys.argv[1] if len(sys.argv) > 1 else '.' path = str(os.path.abspath(r"/Users/ISHI-VS/PycharmProjects/ProjectA")) event_handler = FileHandlerCustom() observer = ob.Observer() observer.schedule(event_handler, path, recursive=True) observer.start() try: while True: #time.sleep(1) print(sys.stderr, 'waiting for a connection') connection, client_address = sock.accept() print(sys.stderr, 'client connected:', client_address) except KeyboardInterrupt: observer.stop() finally: connection.close() observer.join()
def main(stdscr, argv): global trace, the_game, kr def chess_drawboard(s): # win.clear() for s in s.split(' ')[0].split('/'): win.addstr("+-+-+-+-+-+-+-+-+\n") while len(s) > 0: if s[0] in '12345678': win.addstr("| " * int(s[0])) else: win.addstr(f"|{s[0]}") s = s[1:] win.addstr("|\n") win.addstr("+-+-+-+-+-+-+-+-+\n") # win.refresh() def chess_observer(game, action): global the_game # win.clear() try: if action[0] == 'accepted': the_game = game elif action[0] == 'move': win.addstr("\n" + f" move {action[2]}:" + '\n') # win.refresh() chess_drawboard(str(game.logic)) me = 'your move next' if game.players[game.cnt % 2] == kr.owner \ else 'other side to move' win.addstr(me + '\n') win.refresh() except: pass if len(argv) < 2: trace = f"usage: {sys.argv[0]} OWNER PEERS" + '\n' + \ f" ex: {sys.argv[0]} A B,C" return curses.echo() curses.curs_set(0) stdscr.scrollok(False) stdscr.addstr(0, 0, ('-' * (curses.COLS - len(prog) - 1)) + prog) stdscr.hline(curses.LINES - 2, 0, '-', curses.COLS - 1) txt = " exit with CTRL-C" stdscr.addstr(curses.LINES - 2, curses.COLS - len(txt) - 1, txt) stdscr.refresh() win = curses.newwin(curses.LINES - 3, curses.COLS - 2, 1, 0) win.scrollok(True) win.idlok(True) win.clear() win.move(0, 0) win.refresh() owner = argv[1] peers = [] if len(argv) == 2 else argv[2].split(',') observer = wdo.Observer() observer.start() config = core.CONFIG('config.pcap') config.load() secrets = config[f"/secret/{owner}"] kr = core.KEY_RING(owner) kr.owner, kr.pk, kr.sk, kr.skc = secrets nd = core.STORAGE() uv = core.USER_VIEW(nd, kr) uv.follow([owner] + peers) priv = core.PRIVATE_CHANNEL(uv) chess_app = core.CHESS_APP(priv) moves = [] def newg(ng): try: win.addstr(f"chessapp update: {(ng[0],ng[1].ref)}" + '\n') # ng[1].observe(lambda updt,ng=ng: win.addstr(f"chess game {ng[1].ref} : {updt}" + '\n'), 0) ng[1].observe(lambda u, ng=ng: chess_observer(ng[1], u), 0) win.refresh() except: trace = traceback.format_exc() pass chess_app.observe(newg, 0) for n in [owner] + peers: nd.attach_feed(n, f"log-{n}.pcap", obs=observer) nd.restore() color = 'white' if the_game.players[0] != kr.owner else 'black' while True: win.refresh() try: stdscr.addstr(curses.LINES - 1, 0, f"{color}> _") stdscr.clrtoeol() stdscr.move(curses.LINES - 1, len(color) + 2) stdscr.refresh() move = stdscr.getstr(curses.LINES - 1, len(color) + 2).decode('utf8') except KeyboardInterrupt: print('\nterminating ...') break except Exception as e: trace = traceback.format_exc() break if the_game: if move == '?': win.addstr('\n' + f"List of moves for game {the_game.ref}" + '\n') for m in the_game.moves: if 'mv' in m.content: win.addstr(f" {m.content['n']} {m.content['mv']}" + '\n') continue if move == '!': chess_drawboard(str(the_game.logic)) continue if not the_game.make_move(move): win.addstr(f"'{move}' - not your turn, or invalid move" + '\n') observer.stop() observer.join()
def __init__(self, path: str = '.') -> None: self._observer = observers.Observer() self._observer.schedule(EventHandler(), path, True)
def __init__(self, q): self.queue = q self.node = [] self.observer = watch_observer.Observer()
from pl.zuiol.dm.watcher.monitoring import MyHandler from pl.zuiol.dm.gui import MainWindow from configparser import SafeConfigParser if __name__ == '__main__': #Read properties config = SafeConfigParser() config.read("..\\..\\..\\..\\properties\\settings.ini") sourceDirectory = config['defaults']['source'] print(sourceDirectory) #Start window for application app = QApplication(sys.argv) mw = MainWindow() mw.show() observer = observers.Observer() observer.schedule(MyHandler(mw), sourceDirectory, False) observer.start() """ try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() """ sys.exit(app.exec_())
""" import os from typing import List, Dict, Tuple, Optional from watchdog import events as watchdog_events, observers as watchdog_observers from watchdog.observers.api import ObservedWatch import datetime import threading from OpenDrive.client_side.od_logging import logger_sync from OpenDrive.client_side import paths, file_changes_json from OpenDrive.general import file_changes_json as gen_json from OpenDrive.general.paths import normalize_path, NormalizedPath from OpenDrive.client_side import program_state observer = watchdog_observers.Observer() watchers: Dict[NormalizedPath, Tuple['FileSystemEventHandler', ObservedWatch]] = {} def start_observing() -> None: """Start watching at all folders in a new thread. Calling this is enough and no further function calls inside this module are needed.""" file_changes_json.init_file() all_folders = file_changes_json.get_all_data() logger_sync.info(f"Watching at folders for changes: {all_folders}") for folder_path, folder in all_folders.items(): _add_watcher(folder_path, folder["include_regexes"], folder["exclude_regexes"]) observer.start() program_state.watching.add_on_stop(sync_waiter.waiter.set)