-
Notifications
You must be signed in to change notification settings - Fork 0
/
folderCls.py
executable file
·261 lines (233 loc) · 7.63 KB
/
folderCls.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
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os
import listFct
import textFct
from fileLocal import *
from fileCls import File, Article
from htmlCls import Html
import loggerFct
class Folder():
def __init__ (self, path='b/'):
path = shortcut (path)
self.path = path
self.list =[]
if self.path[-1] != os.sep: self.path = self.path + os.sep
# ________________________ agir sur les fichiers ________________________
def rename (self, wordOld, wordNew=""):
for file in self.list:
if wordOld not in file.title: continue
file.fromPath()
newPath = file.title.replace (wordOld, wordNew)
newPath = file.path.replace ('\t', newPath)
file.toPath()
os.rename (self.path + file.path, self.path + newPath)
def move (self, newPath):
# newPath est une string
if newPath[-1] != os.sep: newPath = newPath + os.sep
newPath = shortcut (newPath)
for file in self.list:
file.toPath()
os.rename (file.path, newPath + file.path)
self.path = newPath
def replace (self, wordOld, wordNew):
""" remplacer un motif dans le texte """
for file in self.list:
if wordOld in file.text:
file.replace (wordOld, wordNew)
file.write()
# ________________________ fonctions de base ________________________
def get (self, tagName=None, sens=True):
for dirpath, SousListDossiers, subList in os.walk (self.path):
if not subList: continue
if tagName and sens:
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if tagName not in subList[i]: trash = subList.pop(i)
elif tagName:
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if tagName in subList[i]: trash = subList.pop(i)
if subList:
for file in subList:
if '.' not in file: continue
elif 'index.' in file: continue
fileTmp = File (os.path.join (dirpath, file))
fileTmp.fromPath()
# fileTmp.path = fileTmp.path.replace (self.path, "")
self.list.append (fileTmp)
self.list.sort()
self.fromPath()
def filter (self, tagName, sens=True):
""" quand on a besoin de ré-exclure certains fichiers après le get.
quand je veux exclure sur plusieurs mots-clefs """
rangeFile = listFct.range (self.list)
rangeFile.reverse()
if sens:
for f in rangeFile:
if tagName not in self.list[f].path and tagName not in self.list[f].title: trash = self.list.pop(f)
else:
for f in rangeFile:
if tagName in self.list[f].path or tagName in self.list[f].title: trash = self.list.pop(f)
def createIndex (self):
index = Html (self.path + 'index.html')
index.text = '<h1>index du dossier ' + self.path +'</h1>\n'
self.get()
for file in self.list:
index.text = index.text + "<a href='" + file.path +"'>"+ file.title +'</a>\n'
"""
index.styles.append ('C:\\wamp64\\www\\site-dp\\library-css\\structure.css')
index.styles.append ('C:\\wamp64\\www\\site-dp\\library-css\\perso.css')
index.styles.append ('C:\\wamp64\\www\\site-dp\\library-css\\index-dossier.css')
"""
index.write()
def read (self):
rangeList = listFct.range (self.list)
self.toPath()
for i in rangeList: self[i].read()
self.fromPath()
def write (self):
self.toPath()
rangeList = listFct.range (self.list)
for i in rangeList: self[i].write()
def fromPath (self):
self.path = shortcut (self.path)
rangeList = listFct.range (self.list)
for i in rangeList:
self[i].path = shortcut (self[i].path)
self[i].fromPath()
self[i].path = self[i].path.replace (self.path, "")
def toPath (self):
self.path = shortcut (self.path)
newPath = shortcut (self[0].path)
if self.path in newPath: return
rangeList = listFct.range (self.list)
for i in rangeList:
self[i].toPath()
self[i].path = self.path + self[i].path
def iterate (self, function):
newList = Folder()
for file in self.list:
res = function (file)
if res: newList.append (res)
return newList
def append (self, file):
# file.path doit avoir été déracié
d= file.path.rfind ('.')
# file.path = self.path +'\t'+ file.path[d:]
file.shortcut()
file.toPath()
self.list.append (file)
def __str__ (self):
strList = 'Dossier: '+ self.path +'\nListe:'
for file in self:
file.toPath()
strList = strList +'\n'+ file.path.replace (self.path, "")
return strList
def __len__(self):
return len (self.list)
def __setitem__ (self, pos, item):
lenList = len (self.list)
if type (pos) == int:
while pos <0: pos += lenList
if pos < lenList: self.list[pos] = item
else: self.append (item)
elif type (pos) == slice:
posIndex = pos.indices (lenList)
rangeList = self.range (posIndex[0], posIndex[1], posIndex[2])
if type (item) in (tuple, list) and len (item) >= len (rangeList):
i=0
for l in rangeList:
self.list[l] = item[i]
i+=1
elif type (item) == Folder and len (item.list) >= len (rangeList):
i=0
for l in rangeList:
self.list[l] = item.list[i]
i+=1
def __getitem__ (self, pos):
lenList = len (self.list)
if type (pos) == int:
while pos <0: pos += lenList
while pos >= lenList: pos -= lenList
return self.list [pos]
elif type (pos) == slice:
posIndex = pos.indices (lenList)
rangeList = self.range (posIndex[0], posIndex[1], posIndex[2])
newList = Folder (self.path)
for l in rangeList: newList.append (self.list[l])
return newList
else: return None
def test (self):
self.path = shortcut ('a/')
self.get ('built on')
"""
print (self)
print (self[1])
self[0] = self[3]
self.rename ('built', 'dodo')
print (self)
"""
file = File ('fanfics/a doctor calls.txt')
self.append (file)
self.move ('b/temp/')
self.filter ('the')
print (self)
"""
self[0].path = self[0].path
self[0].read()
print (len (self[0].text), 'caractères')
"""
class FolderArticle (Folder):
def __init__ (self, path='a/', subject=""):
Folder.__init__(self, path)
self.subject = subject
if subject == 'fanfic' or subject == 'romance': self.path = 'a/fanfics/'
elif subject == 'cour': self.path = 'a/cours/'
elif subject == 'education': self.path = 'a/education/'
elif subject == 'roman': self.path = 'a/romans/'
self.path = shortcut (self.path)
def createIndex (self):
index = File (self.path + 'index.tsv')
self.get()
self.read()
self.list.sort()
for file in self.list:
file.toPath()
index.text = index.text + file.subject +'\t'+ file.author +'\t'+ file.title +'\t'+ file.path +'\n'
print (self.path)
index.text = index.text.replace (self.path, "")
index.write()
def createIndex_va (self):
index = Html (self.path + 'index.html')
index.text = '<h1>index du dossier ' + self.path +'</h1>\n<table>\n'
self.get()
self.read()
self.list.sort()
for file in self.list:
file.toPath()
index.text = index.text + "<tr><td>"+ file.subject + "</td><td>" + file.author + "</td><td><a href='" + file.path +"'>"+ file.title + "</a></td></tr>\n"
index.text = index.text + '</table>'
index.styles.append ('C:\\wamp64\\www\\site-dp\\library-css\\structure.css')
index.styles.append ('C:\\wamp64\\www\\site-dp\\library-css\\perso.css')
index.write()
def get (self, tagName=None, sens=True):
tmpList = Folder (self.path)
tmpList.get (tagName, sens)
for file in tmpList.list:
article = Article (file.path)
if article.type in ('html', 'txt'): self.append (article)
self.list.sort()
def __getitem__ (self, pos):
lenList = len (self.list)
if type (pos) == int:
if pos <0: pos += lenList
if pos > lenList or pos <0: return None
else: return self.list [pos]
elif type (pos) == slice:
posIndex = pos.indices (lenList)
rangeList = self.range (posIndex [0], posIndex [1], posIndex [2])
newList = FolderArticle (self.path)
for l in rangeList: newList.append (self.list[l])
return newList
else: return None