Exemple #1
0
def reload(file, logger):
    path = os.path.dirname(os.path.abspath(file))
    handler = RegexMatchingEventHandler([r".*"], [], False, True)
    handler.on_modified = on_modified(logger)
    my_observer = Observer()
    my_observer.schedule(handler, path, recursive=True)
    my_observer.start()
Exemple #2
0
 def test_ignore_directories(self):
     handler1 = RegexMatchingEventHandler(g_allowed_regexes,
                                          g_ignore_regexes, True)
     handler2 = RegexMatchingEventHandler(g_allowed_regexes,
                                          g_ignore_regexes, False)
     self.assertTrue(handler1.ignore_directories)
     self.assertFalse(handler2.ignore_directories)
 def __init__(self, root, reg):
     RegexMatchingEventHandler.__init__(self, regexes=[reg])
     self.handlers = {}
     self.df_total = None  # 所有文件的最后记录的总数据
     self._root = root
     self._reg = reg
     self._init()
def test_ignore_directories():
    handler1 = RegexMatchingEventHandler(g_allowed_regexes,
                                         g_ignore_regexes, True)
    handler2 = RegexMatchingEventHandler(g_allowed_regexes,
                                         g_ignore_regexes, False)
    assert handler1.ignore_directories
    assert not handler2.ignore_directories
Exemple #5
0
 def test___init__(self):
     handler1 = RegexMatchingEventHandler(g_allowed_regexes, g_ignore_regexes, True)
     handler2 = RegexMatchingEventHandler(g_allowed_regexes, g_ignore_regexes, False)
     self.assertEqual([r.pattern for r in handler1.regexes], g_allowed_regexes)
     self.assertEqual([r.pattern for r in handler1.ignore_regexes], g_ignore_regexes)
     self.assertTrue(handler1.ignore_directories)
     self.assertFalse(handler2.ignore_directories)
Exemple #6
0
    def _watch_reducers(self):
        event_handler = RegexMatchingEventHandler(['.*'])
        event_handler.on_modified = self._on_reducer_modifed

        observer = Observer()
        observer.schedule(event_handler,
                          os.path.join(ROOT_DIR, 'reducers'),
                          recursive=False)
        observer.start()
Exemple #7
0
def main():
    # load config and values
    loadConfig()

    # clean up any unix accoutn that was not removed
    cleanupZombieUsers()

    # Start the file watch service
    fileEventHandler = RegexMatchingEventHandler(regexMatch, ignore_patterns,
                                                 ignoreDirectories,
                                                 caseSensitive)
    fileEventHandler.on_modified = watchFile.on_modified
    my_observer = Observer()
    my_observer.schedule(fileEventHandler, path, recursive=goRecursively)
    my_observer.start()
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher

    # on different commands - answer in Telegram
    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))
    dispatcher.add_handler(CommandHandler("restart", restart_command))
    dispatcher.add_handler(CommandHandler("backup", backup_command))
    dispatcher.add_handler(CommandHandler("perm", perm_command))
    dispatcher.add_handler(CommandHandler("genssh", genSSH_command))
    dispatcher.add_handler(CommandHandler("list", list_command))
    dispatcher.add_handler(CommandHandler("clone", clone_command))
    dispatcher.add_handler(CommandHandler("op", op_command))
    dispatcher.add_handler(CommandHandler("deop", deop_command))
    dispatcher.add_handler(CommandHandler("hwinfo", hwinfo_command))
    dispatcher.add_handler(CommandHandler("broadcast", broadcast_command))
    dispatcher.add_handler(CommandHandler("test", test_command))
    dispatcher.add_handler(CommandHandler("players", players_command))
    dispatcher.add_handler(CommandHandler("status", status_command))
    dispatcher.add_handler(CommandHandler("propGet", propGet_command))
    dispatcher.add_handler(CommandHandler("cmd", anyCommand_command))
    dispatcher.add_handler(CommandHandler("mapreg", mapReg_command))
    #    dispatcher.add_handler(CommandHandler("kb", kb))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))

    # on noncommand i.e message - echo the message on Telegram
    dispatcher.add_handler(
        MessageHandler(Filters.text & ~Filters.command, badCMD))

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
def test_handler():
    handler1 = RegexMatchingEventHandler(g_allowed_regexes,
                                         g_ignore_regexes, True)
    handler2 = RegexMatchingEventHandler(g_allowed_regexes,
                                         g_ignore_regexes, False)
    assert [r.pattern for r in handler1.regexes] == g_allowed_regexes
    assert [r.pattern for r in handler1.ignore_regexes] == g_ignore_regexes
    assert handler1.ignore_directories
    assert not handler2.ignore_directories
Exemple #9
0
    def __init__(self,
                 callback: callable,
                 regexes: list,
                 wait: bool = True,
                 **kwargs):
        RegexMatchingEventHandler.__init__(self, regexes=regexes)

        self.callback_arguments = kwargs
        self.callback = callback
        self.wait = wait
Exemple #10
0
    def __init__(self, callback, loop=None, **kwargs):
        """asyncio compatible minimal regex matching event
        handler for watchdog.

        :param callback: function to apply to filenames.
        :param loop: ayncio-like event loop.
        """

        RegexMatchingEventHandler.__init__(self, **kwargs)
        self._loop = loop or asyncio.get_event_loop()
        self.callback = callback
Exemple #11
0
    def __init__(self, callback, loop=None, **kwargs):
        """asyncio compatible minimal regex matching event
        handler for watchdog.

        :param callback: function to apply to filenames.
        :param loop: ayncio-like event loop.
        """

        RegexMatchingEventHandler.__init__(self, **kwargs)
        self._loop = loop or asyncio.get_event_loop()
        self.callback = callback
Exemple #12
0
def get_event_handler(converter):
    def on_created(event):
        converter(event.src_path)

    def on_modified(event):
        print(f'[MODIFIED] {event.__dict__}')

    handler = RegexMatchingEventHandler(regexes=[r".*[.]djvu"])
    handler.on_created = on_created
    handler.on_modified = on_modified

    return handler
Exemple #13
0
    def __init__(self, path, **kwargs):
        """Create observer for the directory at `path`."""
        Observer.__init__(self, **kwargs)
        # replace default event queue with ordered set queue to disallow
        # duplicate events even if added out of order
        self._event_queue = OrderedSetQueue()
        RegexMatchingEventHandler.__init__(self)

        self.path = os.path.abspath(path)
        self.root_observer = Observer(**kwargs)
        self.root_watch = None
        self._stopped_handlers = dict()
        self._dispatching_enabled = True
Exemple #14
0
 def _watcher(self):
     handler = RegexMatchingEventHandler(regexes=[self.regex],
                                         ignore_directories=True)
     handler.on_modified = lambda x: self.q.put(x.src_path)
     observer = Observer()
     observer.schedule(handler, self.path)
     observer.start()
     try:
         while True:
             time.sleep(1)
     except KeyboardInterrupt:
         observer.stop()
     observer.join()
    def __init__(self):
        RegexMatchingEventHandler.__init__(self, ignore_regexes=Daemon.IGNORED_REGEX, ignore_directories=True)

        # Just Initialize variable the Daemon.start() do the other things
        self.daemon_state = 'down'  # TODO implement the daemon state (disconnected, connected, syncronizing, ready...)
        self.running = 0
        self.client_snapshot = {} # EXAMPLE {'<filepath1>: ['<timestamp>', '<md5>', '<filepath2>: ...}
        self.local_dir_state = {} # EXAMPLE {'last_timestamp': '<timestamp>', 'global_md5': '<md5>'}
        self.listener_socket = None
        self.observer = None
        self.cfg = self.load_cfg(Daemon.CONFIG_FILEPATH)
        self.init_sharing_path()
        self.conn_mng = ConnectionManager(self.cfg)
Exemple #16
0
    def _set_root(self, root):
        """Set up watching `root` or closest existing parent."""
        if self.root_watch is not None and self.root_watch.path == root:
            # already watching the specified root, return early
            return

        # schedule new root watch
        while True:
            try:
                watch = self.root_observer.schedule(
                    event_handler=self, path=root, recursive=False
                )
            except OSError as sched_err:
                # root doesn't exist, move up one directory and try again
                try:
                    # clean up from failed scheduling
                    self.root_observer.unschedule(ObservedWatch(root, False))
                except KeyError:
                    pass
                newroot = os.path.dirname(root)
                if newroot == root:
                    # we've gotten to system root and still failed
                    raise sched_err
                else:
                    root = newroot
            else:
                # watch was set, break out of while loop
                break

        # add regex for root and next subdirectory
        regexes = []
        regexes.append("^" + re.escape(root) + "$")
        nextdir = self._get_next_dir_in_path(root)
        if nextdir != root:
            regexes.append("^" + re.escape(nextdir) + "$")

        # update handler (self) with regexes
        RegexMatchingEventHandler.__init__(
            self, regexes=regexes, ignore_directories=False
        )

        # unschedule old root watch
        if self.root_watch is not None:
            try:
                self.root_observer.unschedule(self.root_watch)
            except KeyError:
                # emitter already stopped
                pass

        self.root_watch = watch
Exemple #17
0
    def start(self):
        """
        Starts watchdog thread to monitor for incoming files. Calls the
        child class's `read` method when a new file appears.
        """

        print 'FileSource start method called...', self.directory
        event_handler = RegexMatchingEventHandler(regexes=['.*'],
                                                  ignore_regexes=[],
                                                  ignore_directories=False,
                                                  case_sensitive=False)
        event_handler.on_created = self.read  # Call child's `read` method
        event_handler.on_modified = self.read
        watch_path = self.directory
        observer = Observer()
        observer.schedule(event_handler, watch_path, recursive=False)
        observer.start()
        print 'observer started.'
Exemple #18
0
 def __init__(
     self,
     folder_config: FolderConfig = None,
     bucket_operator: BucketOperatorS3 = None,
 ):
     self.event_dict = dict()
     self.folder_config = folder_config or FolderConfig.default()
     self.bucket_operator = bucket_operator or BucketOperatorS3()
     BaseThread.__init__(self)
     FolderVisitor.__init__(
         self,
         self.folder_config,
     )
     RegexMatchingEventHandler.__init__(
         self,
         ignore_directories=True,
         regexes=self.folder_config.regex_include_list,
         ignore_regexes=self.folder_config.regex_exclude_list,
     )
Exemple #19
0
    def __init__(self, path, force_polling=False, **kwargs):
        """Create observer for the directory at `path`."""
        if force_polling:
            observer_class = PollingObserver
        else:
            observer_class = Observer

        self.root_observer = observer_class(**kwargs)

        # get proper emitter class from root_observer instance of observer_class
        BaseObserver.__init__(
            self, emitter_class=self.root_observer._emitter_class, **kwargs
        )
        # initialize as an event handler as well for handling the root observer events
        RegexMatchingEventHandler.__init__(self)

        self.path = os.path.abspath(path)
        self.root_watch = None
        self._stopped_handlers = dict()
Exemple #20
0
 def __init__(self, do_restart):
     RegexMatchingEventHandler.__init__(
         self,
         regexes=[r".+\.py"],
         ignore_regexes=[
             # VSCode's Black integration creates temp files like kaldi_main.py.80dd20e69f7d6eef4107c17b335180be.py
             # and if vscode runs Black on save, and we use those as a trigger to restart, we may restart before
             # the reformatted file is actually written, so we'll run old code in that case.
             # So as a workaround, ignore these temp files.
             # A more generic fix might be to wait until we see no more filesystem events for a time X before restarting,
             # but that would introduce a noticeable restart delay and would not be guaranteed to work either (say X is
             # less than the amount of time for a format operation to run - imagine big files).
             # A more reliable fix would be to MD5 everything right before restarting, save that to a file
             # somewhere, then check on startup restart if further changes have occurred.
             r".+\.py\.[a-f0-9]{32}\.py$"
         ],
         ignore_directories=True,
         case_sensitive=False,
     )
     self.last_modified = datetime.datetime.now()
     self.do_restart = do_restart
Exemple #21
0
    def __init__(self, path, force_polling=False, **kwargs):
        """Create observer for the directory at `path`."""
        if force_polling:
            observer_class = PollingObserver
        else:
            observer_class = Observer

        self.root_observer = observer_class(**kwargs)

        # get proper emitter class from root_observer instance of observer_class
        BaseObserver.__init__(self,
                              emitter_class=self.root_observer._emitter_class,
                              **kwargs)
        # replace default event queue with ordered set queue to disallow
        # duplicate events even if added out of order
        self._event_queue = OrderedSetQueue()
        RegexMatchingEventHandler.__init__(self)

        self.path = os.path.abspath(path)
        self.root_watch = None
        self._stopped_handlers = dict()
        self._dispatching_enabled = True
Exemple #22
0
def main():
    if len(sys.argv) < 2:
        print(f"Error: At least one arg is required. Usage: miro_uploader <destination_path> [monitored_path]")
        exit(1)

    destination_path = sys.argv[1]
    monitored_path = sys.argv[2] if len(sys.argv) > 2 else '.'

    def on_created(event):
        print(f"hey, {event.src_path} has been created!")

    def on_modified(event):
        try:
            print(f"hey buddy, {event.src_path} has been modified")
            return_path = copy2(event.src_path, destination_path)
            print(f"hey, that a copy path {return_path}")
            remove(event.src_path)
        except Exception:
            pass

    event_handler = RegexMatchingEventHandler(regexes=[r"^[\x00-\x7F]*jpg$"],
                                              ignore_directories=True, case_sensitive=False)
    event_handler.on_created = on_created
    event_handler.on_modified = on_modified
    observer = Observer()
    observer.schedule(event_handler, monitored_path, recursive=False)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        observer.join()
    finally:
        observer.stop()
        observer.join()
Exemple #23
0
    def __init__(self, warehouse, file_path, **kwargs):
        super().__init__(**kwargs)
        self.warehouse = warehouse
        self.file_path = file_path
        self.observer = None

        patterns = str(self.file_path)
        ignore_patterns = ""
        ignore_directories = True
        case_sensitive = True
        self.my_event_handler = RegexMatchingEventHandler(
            patterns, ignore_patterns, ignore_directories, case_sensitive)

        self.my_event_handler.on_created = self.on_created
        self.my_event_handler.on_modified = self.on_modified
        setup_logging("debug", f"listener.log")
Exemple #24
0
    def __init__(self, path: str, watchdog_queue: Queue,
                 rabbitmq_queue: str) -> None:
        self.rabbitmq_queue = rabbitmq_queue
        self.path = path
        self.event_handler = RegexMatchingEventHandler(
            regexes=self.regexes,
            ignore_directories=self.ignore_directories,
            case_sensitive=self.case_sensitive,
        )
        self.event_handler.on_created = self.__on_created_event
        self.watchdog_queue = watchdog_queue
        for file in listdir(path):
            fpath = join(path, file)
            if isfile(fpath):
                event = FileCreatedEvent(fpath)
                self.watchdog_queue.put(item=event)

        while not watchdog_queue.empty():
            self.__on_created_event(watchdog_queue.get())
Exemple #25
0
def on_modified(event):
    print(f"{event.src_path} has been modified")
    os.system(f"git add .")
    os.system(f"git commit -m '{event.src_path} modified'")
    os.system("git push origin master")


# create the event handler
if __name__ == "__main__":
    # patterns = [".*"]
    ignore_patterns = ["^./.git"]
    ignore_directories = False
    case_sensitive = True
    # my_event_handler = RegexMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
    my_event_handler = RegexMatchingEventHandler(
        ignore_regexes=ignore_patterns)

    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified

    # create an observer
    path = "."
    go_recursively = True
    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    my_observer.start()
    # try:
    #     while True:
    #         time.sleep(5)
Exemple #26
0
 def __init__(self):
     RegexMatchingEventHandler.__init__(self, regexes=[r'.*\.ui$'])
     self.last_time = 0
Exemple #27
0
 def test_regexes(self):
     handler1 = RegexMatchingEventHandler(g_allowed_regexes, g_ignore_regexes, True)
     self.assertEqual([r.pattern for r in handler1.regexes], g_allowed_regexes)
Exemple #28
0
 def __init__(self, regs=[r".*.py$"]):
     RegexMatchingEventHandler.__init__(self, regexes=regs)
     self.reload_set = set()
Exemple #29
0
if __name__ == '__main__':

    FORMAT = '%(asctime)-15s %(message)s'
    logging.basicConfig(filename='/tmp/watchdog.log',
                        level=logging.INFO,
                        format=FORMAT)
    logging.info('Started')

    path = sys.argv[1] if len(sys.argv) > 1 else '/home/norbert/Desktop'

    patterns = ['.+']
    ignore_patterns = [f"^{path}\.+"]
    ignore_dirs = False
    case_sensitive = True

    my_event_handler = RegexMatchingEventHandler(patterns, ignore_patterns,
                                                 ignore_dirs, case_sensitive)
    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_delete
    my_event_handler.on_moved = on_moved
    my_event_handler.on_modified = on_modified

    go_recursively = True
    my_observer = Observer()

    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
Exemple #30
0
 def __init__(self):
     # Set the patterns for RegexMatchingEventHandler
     RegexMatchingEventHandler.__init__(self,
                                        regexes=['^\.(/|\\\\)test.*\.log$'],
                                        ignore_directories=True)
Exemple #31
0
 def __init__(self, regexes, event_dict):
     RegexMatchingEventHandler.__init__(self, regexes, ignore_directories=True)
     self._event_dict = event_dict
Exemple #32
0
def FileHandler():
    handler = RegexMatchingEventHandler(regexes=[r".*[.](epub|pdf|mobi)"])
    return handler
Exemple #33
0
def KindleConnectionHandler():
    handler = RegexMatchingEventHandler(regexes=[r".*/Kindle"])
    return handler
Exemple #34
0
 def __init__(self, *args, **kwargs):
     RegexMatchingEventHandler.__init__(self, *args, **kwargs)