2016-03-08 9 views
0

Мой код для класса, который определяет, когда конкретный файл был изменен:watchdog (библиотека Python) - Как отправить сигнал при изменении файла?

class MyEventHandler(FileSystemEventHandler, QtCore.QThread): 
    def __init__(self, filename): 
     super(MyEventHandler, self).__init__() 
     self.filename = filename 

    def on_modified(self, event): 
     if not event.is_directory and event.src_path.endswith(self.filename): 
      print "modified" 
      self.emit(QtCore.SIGNAL("fileModified")) 


class WatchOutForFileModifications(QtCore.QThread): 
    def __init__(self, path, filename): 
     super(WatchOutForFileModifications, self).__init__() 
     self.path = path 
     self.filename = filename 
     self.observer = Observer() 
     self.event_handler = MyEventHandler(self.filename) 
     self.observer.schedule(self.event_handler, self.path, recursive=False) 
     self.observer.start() 

    def run(self): 
     while 1: 
      self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified) 


    def modified(self): 
     self.emit(QtCore.SIGNAL("fileModified1")) 

И фрагмент кода для самого приложения:

class MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     uic.loadUi('test.ui', self) 

     path = "somePath" 
     filename = "someName" 

     self.fileWatcher = WatchOutForFileModifications(path, filename) 
     self.fileWatcher.start() 
     self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified) 
     self.show() 

    def fileModified(self): 
     print 1 

Проблема заключается в том, что, когда файл был изменен, я получить непрерывный поток из 1, который печатается. Я понимаю, что в классе WatchOutForFileModifications не должно быть сигнала, который был избит/подключен, но я не понимаю, как API: http://pythonhosted.org/watchdog/api.html#watchdog.observers.api.EventEmitter - должен работать. По крайней мере, я предполагаю, что это API, который я должен использовать для прослушивания изменений файлов.

EDIT

Работа кода после некоторых модификаций:

импорт SYS из PyQt4 импорта QtGui, QtCore, МСЖД

from watchdog.events import FileSystemEventHandler 
from watchdog.observers import Observer 

class MyEventHandler(FileSystemEventHandler, QtCore.QThread): 
    def __init__(self, filename): 
     super(MyEventHandler, self).__init__() 
     self.filename = filename 
     self.signalName = str(filename) + "_modified" 

    def on_modified(self, event): 
     if not event.is_directory and event.src_path.endswith(self.filename): 
      self.emit(QtCore.SIGNAL(self.signalName)) 


class FileModificationWatcher(QtCore.QThread): 
    def __init__(self, path, filename): 
     super(FileModificationWatcher, self).__init__() 
     self.path = path 
     self.filename = filename 
     self.observer = Observer() 
     self.event_handler = MyEventHandler(self.filename) 
     self.observer.schedule(self.event_handler, self.path, recursive=False) 
     self.observer.start() 

    def run(self): 
     pass 

    def getEmitter(self): 
     return self.event_handler 

    def getSignalName(self): 
     return self.event_handler.signalName 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     uic.loadUi('test.ui', self) 

     path = "somePath" 
     filename = "someName" 

     self.fileWatcher = FileModificationWatcher(path, filename) 
     self.fileWatcher.start() 
     self.connect(self.fileWatcher.getEmitter(), QtCore.SIGNAL(self.fileWatcher.getSignalName()), self.fileModified) 
     self.show() 

    def fileModified(self): 
     print 1 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 

    window = MainWindow() 
    sys.exit(app.exec_()) 

ответ

-1

в вашем WatchOutForFileModifications класс, попробуйте подключить сигнал в аннотация способ.

1

Проблема в том, что в WatchOutForFileModifications вы повторно подключаете сигнал к слоту в функции запуска. Чтобы решить эту проблему вы имеете, принять self.connect вызов и переместить его в __init__ класса, например, так:

from PyQt4 import QtCore, QtGui 
from watchdog.events import FileSystemEventHandler 
from watchdog.observers import Observer 


class MyEventHandler(FileSystemEventHandler, QtCore.QThread): 
    def __init__(self, filename): 
     super(MyEventHandler, self).__init__() 
     self.filename = filename 

    def on_modified(self, event): 
     if not event.is_directory and event.src_path.endswith(self.filename): 
      print("modified") 
      self.emit(QtCore.SIGNAL("fileModified")) 


class WatchOutForFileModifications(QtCore.QThread): 
    def __init__(self, path, filename): 
     super(WatchOutForFileModifications, self).__init__() 
     self.path = path 
     self.filename = filename 
     self.observer = Observer() 
     self.event_handler = MyEventHandler(self.filename) 
     self.observer.schedule(self.event_handler, self.path, recursive=False) 
     self.observer.start() 
     self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified) 

    def run(self): 
     pass 

    def modified(self): 
     self.emit(QtCore.SIGNAL("fileModified1")) 


class MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     super(MainWindow, self).__init__() 

     path = r'D:\Code\\' 
     filename = "Hexagon_Grid_Creation.py" 

     self.fileWatcher = WatchOutForFileModifications(path, filename) 
     #self.fileWatcher.start() 
     self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified) 
     self.show() 

    def fileModified(self): 
     print(1) 

if __name__ == '__main__': 
    app = QtGui.QApplication([]) 
    window = MainWindow() 
    window.show() 
    app.exec_() 

В этом случае, вы, вероятно, не нужен QThread; файл отслеживается обработчиком событий, поэтому у вас на самом деле нет ничего другого, что вам нужно выполнить в фоновом режиме. Я думаю, вы могли бы полностью отказаться от этого класса и просто создать экземпляр обработчика событий в классе MainWindow.

+1

Фактически я понял, что могу установить соединение прямо из «MyEventHandler» в «MainWindow», пропуская обработку сигнала в «WatchOutForFileModifications» в целом. – emihir0