-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
176 lines (141 loc) · 5.19 KB
/
main.py
File metadata and controls
176 lines (141 loc) · 5.19 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
165
166
167
168
169
170
171
172
173
174
175
176
from PyQt4 import QtGui
import os
import json
import sys
import designer
import utils
KEY_DOCUMENTS = 'documents'
KEY_CATEGORIES = 'categories'
KEY_ERROR = 'error'
config = None
class LoadDialog(designer.loadDialog):
def browseDocuments(self):
global documents_directory
directory = QtGui.QFileDialog.getExistingDirectory(
self,
"Pick a filder with documents",
config[KEY_DOCUMENTS]
)
if directory:
print("New documents directory %s" % directory)
self.documents_directory = directory
self.documentsPath.setText(directory)
config[KEY_DOCUMENTS] = directory
def browseCategories(self):
global categories_directory
directory = QtGui.QFileDialog.getExistingDirectory(
self,
"Pick a folder with categories",
config[KEY_CATEGORIES]
)
if directory:
print("New categories directory %s" % directory)
self.categories_directory = directory
self.categoriesPath.setText(directory)
config[KEY_CATEGORIES] = directory
def loadMainDialog(self):
save_configuration()
mainDialog = MainDialog()
mainDialog.show()
mainDialog.setupMainWindow()
mainDialog.populateDocument()
mainDialog.populateList()
self.close()
class MainDialog(designer.mainDialog):
def setupMainWindow(self):
self.setWindowTitle("Classifier")
self.documents_directory = utils.directory(config[KEY_DOCUMENTS])
self.documents = self.documents_directory.get_all_files()
self.categories_directory = utils.directory(config[KEY_CATEGORIES])
self.categories = self.categories_directory.get_all_directories()
QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Z"), self, self.undoAction)
def undoAction(self):
self.undoStack.undo(self.documents)
self.showRemainingItemsMessage()
def showRemainingItemsMessage(self):
self.statusBar.showMessage(str(len(self.documents)) + " items remaining")
def populateDocument(self):
path = self.documents_directory.path
name = self.documents[0]
file = utils.file(path, name)
print("Opening file %s" % file.full_name)
content = file.read()
self.documentView.setHtml(content)
self.showRemainingItemsMessage()
def populateList(self):
# self.icon = QtGui.QIcon('icon.png')
model = QtGui.QStandardItemModel(self.categoriesListView)
print("Loaded %d categories" % len(self.categories))
for category in self.categories:
item = QtGui.QStandardItem(category)
model.appendRow(item)
self.categoriesListView.setModel(model)
def listItemSelected(self, index):
self.showRemainingItemsMessage()
old_path = config[KEY_DOCUMENTS]
file_name = self.documents.pop(0)
new_category = config[KEY_CATEGORIES] + "\\" + self.categories[index.row()]
file = utils.file(old_path, file_name)
undo_command = utils.undo(
self.documentView,
file,
file.get_parent_directory().path
)
self.undoStack.push(undo_command)
message = "Moving file %s to %s" % (file.name, new_category.split('\\')[-1])
print(message)
file.move(new_category)
if len(self.documents) == 0:
self.close()
else:
self.populateDocument()
def save_configuration():
global config
config_file = open('config.json', 'w')
json.dump(config, config_file)
config_file.close()
def load_config():
global config
configuration = {}
try:
config_file = open('config.json', 'r')
configuration = json.load(config_file)
except:
pass
finally:
if (os.path.exists('config.json')):
config_file.close()
path = os.getcwd()
configuration[KEY_ERROR] = False
if KEY_CATEGORIES not in configuration or not os.path.exists(configuration[KEY_CATEGORIES]):
print("key %s not found" % KEY_CATEGORIES)
configuration[KEY_CATEGORIES] = path
configuration[KEY_ERROR] = True
if KEY_DOCUMENTS not in configuration or not os.path.exists(configuration[KEY_DOCUMENTS]):
print("key %s not found" % KEY_DOCUMENTS)
configuration[KEY_DOCUMENTS] = path
configuration[KEY_ERROR] = True
config = configuration
app = QtGui.QApplication(sys.argv)
mainDialog = None
loadDialog = None
def main():
global config
load_config()
if config[KEY_ERROR]:
print(config)
print("Opening load dialog")
loadDialog = LoadDialog(
documentsDirectory=config[KEY_DOCUMENTS],
categoriesDirectory=config[KEY_CATEGORIES]
)
loadDialog.show()
else:
mainDialog = MainDialog()
mainDialog.show()
mainDialog.setupMainWindow()
mainDialog.populateDocument()
mainDialog.populateList()
app.exec_()
if __name__ == '__main__':
main()