-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathclone.py
More file actions
165 lines (126 loc) · 4.84 KB
/
Copy pathpathclone.py
File metadata and controls
165 lines (126 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import sys
import time
import logging
import os
import shutil
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from watchdog.events import FileSystemEventHandler
from watchdog.events import EVENT_TYPE_MODIFIED
from watchdog.events import EVENT_TYPE_MOVED
from watchdog.events import EVENT_TYPE_CREATED
from watchdog.events import EVENT_TYPE_DELETED
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, \
QFileDialog
from PyQt5.QtWidgets import QPlainTextEdit
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtCore import QObject
from PyQt5.QtCore import pyqtSignal
class ClonePathEventHandler(LoggingEventHandler):
"""Logs all the events captured."""
def __init__(self, src, dst, logger ):
LoggingEventHandler.__init__(self)
self.src = src
self.dst = dst
self.log = logger
self._method_map = {
EVENT_TYPE_MODIFIED: self.on_modified,
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
}
def src2dst(self, srcpath):
return os.path.join(self.dst, srcpath[(len(self.src)+1):])
def dispatch(self, event):
"""Dispatches events to the appropriate methods.
:param event:
The event object representing the file system event.
:type event:
:class:`FileSystemEvent`
"""
self._method_map[event.event_type](event)
def on_moved(self, event):
super().on_moved(event)
src = self.src2dst(event.src_path)
dst = self.src2dst(event.dest_path)
try:
self.log("Moving: %s -> %s" % (src, dst))
shutil.move(src, dst)
except Exception as e:
self.log("Exception (it's probably ok) %s" % e)
def on_created(self, event):
super().on_created(event)
what = 'directory' if event.is_directory else 'file'
src = event.src_path
dst = self.src2dst(src)
try:
if what == 'file':
self.log("Copying file: %s -> %s" % (src, dst))
shutil.copy2(src, dst)
elif what == 'directory':
self.log("Copying directory: %s -> %s" % (src, dst))
shutil.copytree(src, dst)
except Exception as e:
self.log("Exception (it's probably ok) %s" % e)
def on_deleted(self, event):
super().on_deleted(event)
what = 'directory' if event.is_directory else 'file'
dst = event.src_path
self.log("NO deletion of %s" % dst)
def on_modified(self, event):
super().on_modified(event)
what = 'directory' if event.is_directory else 'file'
src = event.src_path
dst = self.src2dst(src)
try:
if what == 'file':
self.log("Copying modified file: %s -> %s" % (src, dst))
shutil.copy2(src, self.src2dst(src))
elif what =='directory':
shutil.copytree(src, self.src2dst(src))
self.log("Copying modified directory: %s -> %s" % (src, dst))
except Exception as e:
self.log("Exception (it's probably ok) %s" % e)
class App(QWidget):
logSignal = pyqtSignal(str, name="Log")
def __init__(self):
super().__init__()
self.title = "Path cloning"
self.left = 100
self.top = 100
self.width = 640
self.height = 480
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.src = os.path.abspath(QFileDialog.getExistingDirectory(
self, "Select source directory"))
self.dst = os.path.abspath(QFileDialog.getExistingDirectory(
self, "Select destination directory"))
self.layout = QVBoxLayout(self)
self.output = QPlainTextEdit(self)
self.output.setReadOnly(True)
self.output.setLineWrapMode(QPlainTextEdit.NoWrap)
self.output.move(10, 10)
self.output.resize(400, 200)
self.output.appendPlainText("src: %s\ndst: %s" % (self.src, self.dst))
self.output.setStyleSheet('background-color: rgb(50, 50, 50); color: rgb(200, 200, 200)')
self.layout.addWidget(self.output, 0)
self.logSignal.connect(self.log)
self.show()
def log(self, event):
self.output.appendPlainText(str(event))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
event_handler = ClonePathEventHandler(ex.src, ex.dst, ex.logSignal.emit)
#event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, ex.src, recursive=True)
observer.start()
status = app.exec_()
observer.stop()
observer.join()
sys.exit(status)