-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheckMentionsOfAssetsBeforeAssetsDirectoryMove.py
379 lines (274 loc) · 19.5 KB
/
checkMentionsOfAssetsBeforeAssetsDirectoryMove.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# coding=utf-8
from helper_routines import checkPath
from helper_routines import getNotesFileNames
from helper_routines import getFileNames
from helper_routines import getAttachmentsDirectoryNames
from helper_routines import bearEscapeDirectoryName
from helper_routines import quotePathForShell
import sys
import codecs
#import urllib
from subprocess import call
import re
import string
notesPath = sys.argv[1]
checkPath(notesPath)
NOTES_ABSOLUTE_PATH = "file:///Users/davidedellacasa/Public/10000notes/"
notesFileNames = getNotesFileNames(notesPath)
attachmentsDirectoryNames = getAttachmentsDirectoryNames(notesPath)
FIX_ASSETS_REFERENCES = False
"""
for eachDirectory in attachmentsDirectoryNames:
originalDirectoryPath = notesPath + eachDirectory
assetsFiles = getFileNames(originalDirectoryPath)
print(" checking we can access assets directory: " + eachDirectory)
if len(assetsFiles) == 0:
print(" WARNING: NO ASSETS (directory can be deleted?) " + originalDirectoryPath)
"""
for eachDirectory in attachmentsDirectoryNames:
#if eachDirectory.lower().find("illustrated group theory".lower()) == -1:
# #print("skipping " + eachDirectory)
# continue
originalDirectoryPath = notesPath + eachDirectory
eachDirectoryStem = eachDirectory.rstrip().rstrip(string.digits).rstrip()
# plainReferencesToDirectory = unicode("![](" + urllib.quote(eachDirectory.encode('utf8')))
eachDirectory_bearEscaped = bearEscapeDirectoryName(eachDirectory)
eachDirectory_bearEscaped_lower = eachDirectory_bearEscaped.lower()
plainReferencesToDirectory = unicode("![](" + eachDirectory_bearEscaped + "/")
plainReferencesToDirectory_lower = plainReferencesToDirectory.lower()
referencesToDirectoryCount = 0
notesPointingToDir = []
for noteFileName in notesFileNames:
if noteFileName[:-3].lower() != eachDirectory.lower():
continue
#if noteFileName.lower().find(eachDirectoryStem.lower()) == -1:
# continue
noteFilePath = notesPath + "/" + noteFileName
with codecs.open(noteFilePath, 'r', encoding='utf-8') as file:
data = file.read()
data_lower = data.lower()
file.close()
if data_lower.find(plainReferencesToDirectory_lower) != -1:
notesPointingToDir.append(noteFileName)
referencesToDirectoryCount = referencesToDirectoryCount + 1
complexMarkdownOccurrences_re = re.compile('\\[[^\\]]*\\]\\(' + re.escape(NOTES_ABSOLUTE_PATH.lower() + eachDirectory_bearEscaped_lower + "/"))
#print(complexMarkdownOccurrences_re.pattern)
#print(data_lower)
if re.search(complexMarkdownOccurrences_re, data_lower):
#print(" " + eachDirectory + " found (also) in form [something](directory) in note " + noteFileName)
if noteFileName not in notesPointingToDir:
notesPointingToDir.append(noteFileName)
referencesToDirectoryCount = referencesToDirectoryCount + 1
complexMarkdownOccurrences_re = re.compile('\\[[^\\]]*\\]\\(' + re.escape(eachDirectory_bearEscaped_lower + "/"))
#print(complexMarkdownOccurrences_re.pattern)
#print(data_lower)
if re.search(complexMarkdownOccurrences_re, data_lower):
#print(" " + eachDirectory + " found (also) in form [something](directory) in note " + noteFileName)
if noteFileName not in notesPointingToDir:
notesPointingToDir.append(noteFileName)
referencesToDirectoryCount = referencesToDirectoryCount + 1
if referencesToDirectoryCount == 0:
print("ERROR: " + str(referencesToDirectoryCount) + " notes referencing directory " + eachDirectory)
elif referencesToDirectoryCount > 1:
print("ERROR: " + str(referencesToDirectoryCount) + " notes referencing directory " + eachDirectory + " :")
for noteFileName in notesPointingToDir:
print(" " + noteFileName)
elif referencesToDirectoryCount == 1:
assetsFiles = getFileNames(originalDirectoryPath)
#print(" checking all assets:")
if len(assetsFiles) == 0:
print(" WARNING: NO ASSETS (directory can be deleted?) " + originalDirectoryPath)
for assetFile in assetsFiles:
#print(" " + assetFile)
if assetFile == ".DS_Store":
continue
# all file types that need a link with absolute path TO THE DIRECTORY
#if not (assetFile.endswith(".psd") or assetFile.endswith(".zip") or assetFile.endswith(".pde") or assetFile.endswith(".swf") or assetFile.endswith(".xlsb") or assetFile.endswith(".xlsx") or assetFile.endswith(".js") or assetFile.endswith(".love")):
# continue
assetFile_lower = assetFile.lower()
assetFile_bearEscaped = bearEscapeDirectoryName(assetFile)
assetFile_bearEscaped = assetFile_bearEscaped.replace(u"?","%3F")
assetFile_bearEscaped_lower = assetFile_bearEscaped.lower()
howManyFilesPointToAsset = 0
notesPointingToAsset = []
for noteFileName in notesFileNames:
noteFileNameStem = noteFileName[:-3].rstrip().rstrip(string.digits).rstrip()
#print("checking with note: >" + noteFileNameStem.lower() + "< >" + eachDirectoryStem.lower() + "<")
if noteFileName[:-3].lower() != eachDirectory.lower():
continue
#if noteFileNameStem.lower() != eachDirectoryStem.lower():
# continue
#print("MATCH!!!! checking with note: " + noteFileName)
noteFilePath = notesPath + "/" + noteFileName
with codecs.open(noteFilePath, 'r', encoding='utf-8') as file:
data = file.read()
data_lower = data.lower()
file.close()
# ---------------------------------------------------------
plainReferencesToAsset = re.compile(re.escape("![](" + eachDirectory_bearEscaped_lower + "/" + assetFile_bearEscaped_lower + ")"))
#print(plainReferencesToAsset.pattern)
#print(data_lower)
if re.search(plainReferencesToAsset, data_lower):
#print(" " + assetFile + " found (also) in form [something](directory) in note " + noteFileName)
if noteFileName not in notesPointingToAsset:
notesPointingToAsset.append(noteFileName)
howManyFilesPointToAsset = howManyFilesPointToAsset + 1
# ---------------------------------------------------------
complexMarkdownOccurrences_re = re.compile('\\[[^\\]]*\\]\\(' + re.escape(NOTES_ABSOLUTE_PATH.lower() + eachDirectory_bearEscaped_lower + "/" + assetFile_bearEscaped_lower + ")"))
#print(complexMarkdownOccurrences_re.pattern)
#print(data_lower)
if re.search(complexMarkdownOccurrences_re, data_lower):
#print(" " + assetFile + " found (also) in form [something](directory) in note " + noteFileName)
if noteFileName not in notesPointingToAsset:
notesPointingToAsset.append(noteFileName)
howManyFilesPointToAsset = howManyFilesPointToAsset + 1
# ---------------------------------------------------------
markdownLinkToDirectory_re = re.compile(re.escape("[" + assetFile_lower + "](" + NOTES_ABSOLUTE_PATH.lower() + eachDirectory_bearEscaped_lower + "/)" ))
#print(markdownLinkToDirectory_re.pattern)
#print(data_lower)
if re.search(markdownLinkToDirectory_re, data_lower):
if noteFileName not in notesPointingToAsset:
notesPointingToAsset.append(noteFileName)
howManyFilesPointToAsset = howManyFilesPointToAsset + 1
# ---------------------------------------------------------
# TODO THESE REFERENCES HERE WILL HAVE TO BE FIXED
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_bearEscaped_lower + "'>" + assetFile_lower + "</a>" ))
#print(htmlLinkToFileOccurrences_re.pattern)
#print(data_lower)
if re.search(htmlLinkToFileOccurrences_re, data_lower):
print("html ref " + eachDirectory + "/" + assetFile + " in " + noteFileName)
if noteFileName not in notesPointingToAsset:
notesPointingToAsset.append(noteFileName)
howManyFilesPointToAsset = howManyFilesPointToAsset + 1
if howManyFilesPointToAsset == 0:
print(" ERROR: counter: " + str(howManyFilesPointToAsset) + " for asset " + eachDirectory + "/" + assetFile)
if FIX_ASSETS_REFERENCES:
try:
#print(noteFilePath)
with codecs.open(noteFilePath, 'r', encoding='utf-8') as file:
data = file.read()
file.close()
"""
assetFile_butWebp = assetFile[:-4] + ".webp"
assetFile_butWebp_lower = assetFile_butWebp.lower()
assetFile_butWebp_bearEscaped = bearEscapeDirectoryName(assetFile_butWebp)
assetFile_butWebp_bearEscaped = assetFile_butWebp_bearEscaped.replace(u"?","%3F")
assetFile_butWebp_bearEscaped_lower = assetFile_butWebp_bearEscaped.lower()
# example:
# <a href='main-qimg-d704ccd59944418e7f3c5e5810caf505.webp'>main-qimg-d704ccd59944418e7f3c5e5810caf505.webp</a>
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_butWebp_bearEscaped_lower + "'>" + assetFile_butWebp_lower + "</a>" ), re.IGNORECASE)
assetLinkAsItShouldBe = "![]("+ eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
"""
"""
assetFile_butWebp = assetFile[:-4] + ".octet-stream"
assetFile_butWebp_lower = assetFile_butWebp.lower()
assetFile_butWebp_bearEscaped = bearEscapeDirectoryName(assetFile_butWebp)
assetFile_butWebp_bearEscaped = assetFile_butWebp_bearEscaped.replace(u"?","%3F")
assetFile_butWebp_bearEscaped_lower = assetFile_butWebp_bearEscaped.lower()
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_butWebp_bearEscaped_lower + "'>" + assetFile_butWebp_lower + "</a>" ), re.IGNORECASE)
assetLinkAsItShouldBe = "![]("+ eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
"""
"""
# example:
#<a href='image_23%2033.octet-stream'>image_23 33.octet-stream</a>
#[image_23 33.html](file:///Users/davidedellacasa/Public/10000notes/duct%20tape%20typography%20-%20Google%20Search/image_23%2033.html)
assetFile_butWebp = assetFile[:-5] + ".octet-stream"
assetFile_butWebp_lower = assetFile_butWebp.lower()
assetFile_butWebp_bearEscaped = bearEscapeDirectoryName(assetFile_butWebp)
assetFile_butWebp_bearEscaped = assetFile_butWebp_bearEscaped.replace(u"?","%3F")
assetFile_butWebp_bearEscaped_lower = assetFile_butWebp_bearEscaped.lower()
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_butWebp_bearEscaped_lower + "'>" + assetFile_butWebp_lower + "</a>" ), re.IGNORECASE)
assetLinkAsItShouldBe = "["+ assetFile +"]("+ NOTES_ABSOLUTE_PATH + eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
"""
"""
# for octet-stream -> jpeg
assetFile_butWebp = assetFile[:-5] + ".octet-stream"
assetFile_butWebp_lower = assetFile_butWebp.lower()
assetFile_butWebp_bearEscaped = bearEscapeDirectoryName(assetFile_butWebp)
assetFile_butWebp_bearEscaped = assetFile_butWebp_bearEscaped.replace(u"?","%3F")
assetFile_butWebp_bearEscaped_lower = assetFile_butWebp_bearEscaped.lower()
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_butWebp_bearEscaped_lower + "'>" + assetFile_butWebp_lower + "</a>" ), re.IGNORECASE)
assetLinkAsItShouldBe = "![]("+ eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
"""
"""
# for svg+xml -> svg
assetFile_butWebp = assetFile + "+xml"
assetFile_butWebp_lower = assetFile_butWebp.lower()
assetFile_butWebp_bearEscaped = bearEscapeDirectoryName(assetFile_butWebp)
assetFile_butWebp_bearEscaped = assetFile_butWebp_bearEscaped.replace(u"?","%3F")
assetFile_butWebp_bearEscaped_lower = assetFile_butWebp_bearEscaped.lower()
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_butWebp_bearEscaped_lower + "'>" + assetFile_butWebp_lower + "</a>" ), re.IGNORECASE)
print(htmlLinkToFileOccurrences_re.pattern)
assetLinkAsItShouldBe = "![]("+ eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
#print("it should be: " + assetLinkAsItShouldBe)
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
#else:
# print(" no substitution in " + noteFilePath)
"""
except Exception, e:
print("ERROR: " + str(e) )
elif howManyFilesPointToAsset == 1:
if FIX_ASSETS_REFERENCES:
try:
#print(noteFilePath)
with codecs.open(noteFilePath, 'r', encoding='utf-8') as file:
data = file.read()
file.close()
htmlLinkToFileOccurrences_re = re.compile(re.escape("<a href='" + assetFile_bearEscaped_lower + "'>" + assetFile_lower + "</a>" ), re.IGNORECASE)
# all file types that can be embedded with relative path
#assetLinkAsItShouldBe = "![]("+ eachDirectory_bearEscaped + "/" + assetFile_bearEscaped +")"
# all file types that need a link with absolute path TO THE DIRECTORY
assetLinkAsItShouldBe = "["+assetFile+"]("+ NOTES_ABSOLUTE_PATH + eachDirectory_bearEscaped + "/)"
#insensitive_re = re.compile(re.escape(plainReferencesToAsset), re.IGNORECASE)
data_new = re.sub(htmlLinkToFileOccurrences_re, assetLinkAsItShouldBe, data)
if data_new != data:
with codecs.open(noteFilePath, 'w', encoding='utf-8') as fileW:
print(" changing links in " + noteFilePath)
fileW.write(data_new)
fileW.close()
#raw_input("Press Enter to continue...")
except Exception, e:
print("ERROR: " + str(e) )
elif howManyFilesPointToAsset > 1:
print(" ERROR: counter: " + str(howManyFilesPointToAsset) + " for asset " + eachDirectory + "/" + assetFile)
for noteFilePointingToAssetName in notesPointingToAsset:
print(" " + noteFilePointingToAssetName)