-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataBase.py
254 lines (199 loc) · 6.74 KB
/
dataBase.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
# coding: utf-8
# In[30]:
from uuid import uuid1 as uid
import time
import json
from PyQt5.QtWidgets import QMessageBox, QFileDialog
# In[2]:
class DATA:
"""数据的基本信息"""
def __init__(self):
self.time = str(time.strftime("%Y%m%d%H%M%S", time.localtime()))
self.name = 'uname'
self.type = ''
self.parent = ''
self.info = ''
# In[3]:
#####结构#####
# 文件夹
# |---文件夹
# |---项目
# |---文件1
# |---文件2
#
# 文件夹
#
class FILE(DATA):
"""文件信息"""
def __init__(self, name='uname file'):
super().__init__()
self.name = name
self.type = 'file'
self.status = ''
self.tag = ''
self.path = ''
class FOLDER(DATA):
"""文件夹基本信息"""
def __init__(self, name='uname folder'):
super().__init__()
self.name = name
self.type = 'folder'
class PROJECT(DATA):
"""项目信息"""
def __init__(self, name='uname project'):
super().__init__()
self.type = 'project'
self.name = name
self.status = ''
self.path = ''
self.files = []
# In[78]:
class DATABASE():
def __init__(self):
self.data = {}
def addItem(self, item):
if "id" in item.__dict__.keys():
id = item.__dict__["id"]
else:
id = str(uid())
self.data[id] = item.__dict__
def removeItem(self, id):
self.data.pop(id)
def refreshData(self):
for id in self.data:
# 用于兼容、更新旧数据
try:
parent = self.data[id]["parent"]
except:
self.data[id]["parent"] = ''
try:
type_ = self.data[id]["type"]
except:
self.data[id]["type"] = "file"
def getFolderStructure(self):
data = self.data
structure = {"root": []}
for id in data:
type_ = data[id]["type"]
if type_ == "folder":
structure[id] = []
for id in data:
type_ = data[id]["type"]
if type_ == "folder":
parent = data[id]["parent"]
if parent == '':
structure["root"].append(id)
else:
structure[parent].append(id)
return structure
def getDirTree(self, query_id):
'''生成目录结构'''
# 列出所有目录
folder_ids = []
for id in self.data:
type_ = self.data[id]['type']
if type_ == 'folder':
folder_ids.append(id)
# 根据一级节点信息生成目录树。
trees = []
for id in folder_ids:
parent = self.data[id]['parent']
if parent == '':
parent = 'root'
tree = parent + "/" + id
self.data[id]["tree"] = tree
trees.append(tree)
# 列出未归类的文件夹
unsorted_id = []
for id in folder_ids:
current_tree = self.data[id]["tree"]
if "root" in current_tree[:5]:
# 判断是否整理出完整路径
# print('sorted')
pass
else:
# 不是完整路径,往上补一级。
unsorted_id.append(id)
# 循环寻找上一级目录,直到全部补齐
while len(unsorted_id):
for id in unsorted_id:
current_tree = self.data[id]["tree"]
if "root" in current_tree[:5]:
unsorted_id.remove(id)
else:
# 不是完整路径,往上补一级。
parent = current_tree.split('/')[0]
for i in trees:
if parent == i.split('/')[-1]: # 查到到父节点
new_tree = i + current_tree.replace(parent, '')
trees.remove(current_tree)
trees.append(new_tree)
self.data[id]['tree'] = new_tree
#print("fffffffffffffftree is " + tree)
#print(trees)
for tree in trees:
a = tree.split("/")
#print("a= "+ str(a))
if query_id in a[-1]:
#print(trees)
return tree
def writeJson(self, path="data.json"):
path = path
content = json.dumps(self.data)
with open(path, "w") as json_file:
try:
json_file.write(content)
print("saved")
except Exception as e:
print(e)
def writeTable(self, path):
col = [ "name", "status", "info", "path", "time", "parent"]
import pandas as pd
sheet = pd.DataFrame(columns=col)
for id in list(self.data.keys())[::-1]:
type = self.data[id]["type"]
if type == "project":
pass
else:
continue
sheet.loc[id, "name"] = self.data[id]["name"]
sheet.loc[id, "status"] = self.data[id]["status"]
sheet.loc[id, "info"] = self.data[id]["info"]
sheet.loc[id, "path"] = self.data[id]["path"]
sheet.loc[id, "time"] = self.data[id]["time"]
sheet.loc[id, "parent"] = self.data[id]["parent"]
sheet.to_excel(path)
def toSheet(self):
col = ["abbr", "name", "status", "tag", "info", "path", "parent"]
sheet = pd.DataFrame(columns=col)
for id in self.data.keys():
sheet.loc[id, "abbr"] = self.data[id]["abbr"]
sheet.loc[id, "name"] = self.data[id]["name"]
status_code = self.data[id]["status"][0]
sheet.loc[id, "status"] = self.data[id]["status"]
sheet.loc[id, "tag"] = self.data[id]["tag"]
sheet.loc[id, "info"] = self.data[id]["info"]
sheet.loc[id, "path"] = self.data[id]["path"]
sheet.loc[id, "time"] = self.data[id]["time"]
sheet.loc[id, "partent"] = self.data[id]["parent"]
return sheet
def readJson(self, file):
with open(file, "r") as json_file:
try:
self.data = json.load(json_file)
except Exception as e:
print(e)
def changeData(self, id, data_type, new_data):
new_data = new_data
if data_type == "parent":
if new_data == "root":
new_data = ""
self.data[id][data_type] = new_data
self.writeJson()
def changeTmpData(self, id, data_type, new_data):
new_data = new_data
if data_type == "parent":
if new_data == "root":
new_data = ""
self.data[id][data_type] = new_data