-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredview.py
More file actions
592 lines (487 loc) · 24.8 KB
/
redview.py
File metadata and controls
592 lines (487 loc) · 24.8 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/python3
# Test
from os import mkdir, rmdir, path, listdir, walk, makedirs, symlink, remove, readlink
from os.path import exists, isdir, isfile, join, abspath
import traceback
import time
from shutil import rmtree
import shutil
import re
from os.path import isfile, join
import argparse
import textwrap
from src.utils import *
from src.dir_processor import Directory_Processor
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from datetime import datetime, timedelta
class Watcher:
def __init__(self, redviewGenerator):
self.redviewGenerator = redviewGenerator
self.DIRECTORY_TO_WATCH = self.redviewGenerator.src
self.observer = Observer()
def run(self):
event_handler = Handler(self.redviewGenerator)
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
class Handler(FileSystemEventHandler):
def __init__(self, redviewGenerator):
self.redviewGenerator = redviewGenerator
self.DIRECTORY_TO_WATCH = self.redviewGenerator.src
self.ignore_types = [".swp",".swx", "swpx"]
# Track recently processed directories to avoid redundant processing
self.last_summary_times = {}
self.summary_interval = timedelta(seconds=1)
def process(self, event):
print("Event occurred: ", event.event_type, event.src_path, path.dirname(event.src_path))
if self.ignore_file(event.src_path):
return 0
# Validate that the path is within our watched directory
if not self.is_path_valid(event.src_path):
print(f"Warning: Path {event.src_path} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Handle case where path has a trailing slash but is actually a file
src_path = event.src_path
if src_path.endswith('/') and exists(src_path.rstrip('/')) and not isdir(src_path.rstrip('/')):
src_path = src_path.rstrip('/')
print(f"Corrected path (removed trailing slash from file): {src_path}")
dest_file = src_path.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
dest_directory = path.dirname(dest_file)
dest_directory = clean_end_path(dest_directory)
targetsummary = clean_end_path(path.dirname(src_path))
if event.event_type == "deleted":
dest_file = dest_directory
if not path.exists(targetsummary):
print("parent directory also deleted")
return
# Ensure the target directory is within the watched directory
if not targetsummary.startswith(self.DIRECTORY_TO_WATCH):
print(f"Warning: Target summary path {targetsummary} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Check if target summary is a directory
if not exists(targetsummary) or not isdir(targetsummary):
# Try to get the parent directory if it's a file
parent_dir = path.dirname(targetsummary.rstrip('/'))
if exists(parent_dir) and isdir(parent_dir):
targetsummary = clean_end_path(parent_dir)
print(f"Using parent directory for summary: {targetsummary}")
else:
print(f"Warning: Cannot find a valid directory for summary: {targetsummary}")
return 0
# Generate directory summary
print("generate summary "+targetsummary+ " to "+dest_directory)
dir_processor = Directory_Processor(
self.redviewGenerator.FORMAT,
targetsummary,
dest_directory,
self.redviewGenerator.ROOT_DEST,
self.redviewGenerator.script_dir,
self.redviewGenerator.dir_to_exclude,
self.redviewGenerator.mainTags,
self.redviewGenerator.real_path,
self.redviewGenerator.exclude_hidden_dir
)
dir_processor.generate_dir_summary()
self.summary_event(event)
print("Event processed: ", event.event_type, event.src_path, path.dirname(event.src_path)+"\n")
def is_path_valid(self, src_path):
"""Check if the path is within our watched directory"""
# Handle None paths (shouldn't happen but just in case)
if src_path is None:
return False
try:
# Normalize paths to absolute paths
abs_src_path = path.abspath(src_path)
abs_watch_dir = path.abspath(self.DIRECTORY_TO_WATCH)
# Check if the path starts with our watched directory
return abs_src_path.startswith(abs_watch_dir)
except Exception as err:
print(f"Error validating path: {err=}, {type(err)=}")
return False
def on_modified(self, event):
self.process(event)
def on_moved(self, event):
"""Handle file/directory move events by updating both source and destination directories"""
if self.ignore_file(event.src_path) or self.ignore_file(event.dest_path):
return 0
# Validate that both paths are within our watched directory
if not self.is_path_valid(event.src_path) or not self.is_path_valid(event.dest_path):
print(f"Warning: Path {event.src_path} or {event.dest_path} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Handle the source directory (where the file was moved from)
src_directory = clean_end_path(path.dirname(event.src_path))
dest_src_directory = src_directory.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
dest_src_directory = clean_end_path(dest_src_directory)
# Handle the destination directory (where the file was moved to)
dest_directory = clean_end_path(path.dirname(event.dest_path))
dest_dest_directory = dest_directory.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
dest_dest_directory = clean_end_path(dest_dest_directory)
# Ensure both directories are within the watched directory
if not src_directory.startswith(self.DIRECTORY_TO_WATCH) or not dest_directory.startswith(self.DIRECTORY_TO_WATCH):
print(f"Warning: Source or destination directory is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Handle case where file is moved to a parent directory
if not path.exists(src_directory) and path.exists(dest_directory):
print(f"File moved to parent directory: {event.src_path} -> {event.dest_path}")
# Only process the destination directory in this case
src_directory = dest_directory
dest_src_directory = dest_dest_directory
# Create symlink for the moved file in the destination directory
if not event.is_directory:
dest_file = event.dest_path.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
try:
if path.exists(dest_file):
if path.islink(dest_file):
remove(dest_file)
elif path.isdir(dest_file):
rmtree(dest_file)
target = event.dest_path
if path.islink(event.dest_path):
target = readlink(event.dest_path)
target = target.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
print(f"Creating symlink from {target} to {dest_file}")
symlink(target, dest_file)
except Exception as err:
print(f"Error creating symlink: {err=}, {type(err)=}")
# Update source directory summary
print(f"Updating source directory summary: {src_directory} to {dest_src_directory}")
src_processor = Directory_Processor(
self.redviewGenerator.FORMAT,
src_directory,
dest_src_directory,
self.redviewGenerator.ROOT_DEST,
self.redviewGenerator.script_dir,
self.redviewGenerator.dir_to_exclude,
self.redviewGenerator.mainTags,
self.redviewGenerator.real_path,
self.redviewGenerator.exclude_hidden_dir
)
src_processor.generate_dir_summary()
# Update destination directory summary
print(f"Updating destination directory summary: {dest_directory} to {dest_dest_directory}")
dest_processor = Directory_Processor(
self.redviewGenerator.FORMAT,
dest_directory,
dest_dest_directory,
self.redviewGenerator.ROOT_DEST,
self.redviewGenerator.script_dir,
self.redviewGenerator.dir_to_exclude,
self.redviewGenerator.mainTags,
self.redviewGenerator.real_path,
self.redviewGenerator.exclude_hidden_dir
)
dest_processor.generate_dir_summary()
# Update timestamps for both directories
self.last_summary_times[src_directory] = datetime.now()
self.last_summary_times[dest_directory] = datetime.now()
def on_created(self, event):
if self.ignore_file(event.src_path):
return 0
# Validate that the path is within our watched directory
if not self.is_path_valid(event.src_path):
print(f"Warning: Path {event.src_path} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Handle case where path has a trailing slash but is actually a file
src_path = event.src_path
if src_path.endswith('/') and exists(src_path.rstrip('/')) and not isdir(src_path.rstrip('/')):
src_path = src_path.rstrip('/')
print(f"Corrected path (removed trailing slash from file): {src_path}")
# Get the parent directory of the created file/directory
parent_dir = clean_end_path(path.dirname(src_path))
# Ensure the parent directory is within the watched directory
if not parent_dir.startswith(self.DIRECTORY_TO_WATCH):
print(f"Warning: Parent directory {parent_dir} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Make sure parent_dir exists (could be created by another process)
if not path.exists(parent_dir):
print(f"Warning: Parent directory {parent_dir} does not exist")
try:
makedirs(parent_dir, exist_ok=True)
print(f"Created parent directory: {parent_dir}")
except Exception as err:
print(f"Error creating parent directory: {err=}, {type(err)=}")
return 0
dest_file = event.src_path.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
if event.is_directory:
print("create dir "+dest_file)
try:
makedirs(dest_file, exist_ok=True)
except FileExistsError:
return
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
return
else:
try:
# Handle case where directory is generated in place of symlink to a directory
if path.exists(dest_file) and path.isdir(dest_file) and path.islink(event.src_path):
rmtree(dest_file)
if not path.exists(dest_file):
target = event.src_path
location = dest_file
if path.islink(event.src_path):
target = readlink(event.src_path)
target = target.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
print("create symlink from "+target + " to " + location)
symlink(target, location)
except FileExistsError:
print(dest_file +" FileExistsError -> skipped")
return
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
return
# Only process if we haven't recently processed this directory
if datetime.now() - self.last_summary_times.get(parent_dir, datetime.min) >= self.summary_interval or event.is_directory:
self.last_summary_times[parent_dir] = datetime.now()
self.process(event)
def on_deleted(self, event):
if self.ignore_file(event.src_path):
return 0
# Validate that the path is within our watched directory
if not self.is_path_valid(event.src_path):
print(f"Warning: Path {event.src_path} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
# Handle case where path has a trailing slash but is actually a file
src_path = event.src_path
if src_path.endswith('/') and exists(src_path.rstrip('/')) and not isdir(src_path.rstrip('/')):
src_path = src_path.rstrip('/')
print(f"Corrected path (removed trailing slash from file): {src_path}")
# Get the parent directory of the deleted file/directory
parent_dir = clean_end_path(path.dirname(src_path))
# Ensure the parent directory is within the watched directory
if not parent_dir.startswith(self.DIRECTORY_TO_WATCH):
print(f"Warning: Parent directory {parent_dir} is outside the watched directory {self.DIRECTORY_TO_WATCH}")
return 0
dest_file = event.src_path.replace(self.redviewGenerator.src, self.redviewGenerator.ROOT_DEST)
# when directory is deleted
if event.is_directory:
print("delete dir "+dest_file)
try:
rmtree(dest_file)
except FileNotFoundError:
return
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
return
# when file is deleted
else:
print("delete file "+dest_file)
try:
remove(dest_file)
except FileNotFoundError:
# if a directory is deleted, the delete file event for a file from the deleted directory can be generated before the directory event itself
return
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
return
self.process(event)
if parent_dir in self.last_summary_times:
del self.last_summary_times[parent_dir]
def ignore_file(self, src_path):
# Handle None paths
if src_path is None:
return True
try:
for ignore_type in self.ignore_types:
if src_path.endswith(ignore_type):
return True
return False
except Exception:
# If we can't check the path, better to ignore it
return True
def summary_event(self, event):
parent_dir = clean_end_path(path.dirname(event.src_path))
self.last_summary_times[parent_dir] = datetime.now()
class RedviewGenerator:
def __init__(self, FORMAT : str, src : str, dest : str, script_dir : str, dir_to_exclude : list, exclude_hidden_dir : bool, mainTags : list):
self.FORMAT = FORMAT
self.src = src
self.dest = dest
self.ROOT_DEST = dest
self.script_dir = script_dir
self.dir_to_exclude = dir_to_exclude
self.exclude_hidden_dir = exclude_hidden_dir
self.mainTags = mainTags
self.real_path = path.dirname(path.realpath(__file__))
def export_web(self):
source_directory = self.script_dir + "/export/web/"
destination_directory = self.ROOT_DEST
# Copie les fichiers du répertoire source vers le répertoire de destination
#for root, dirs, files in walk(source_directory):
# Copie les répertoires du répertoire source vers le répertoire de destination
for root, dirs, files in walk(source_directory):
for file in files:
source_file = path.join(root, file)
destination_file = path.join(destination_directory, path.relpath(source_file, source_directory))
makedirs(path.dirname(destination_file), exist_ok=True)
shutil.copy2(source_file, destination_file)
for dir in dirs:
source_dir = path.join(root, dir)
destination_dir = path.join(destination_directory, path.relpath(source_dir, source_directory))
makedirs(destination_dir, exist_ok=True)
@staticmethod
def last_slash_index(path):
if path[-1] == "/":
path = path[:-1]
if path.rfind("/") != -1:
return path[:path.rfind("/")]
return path
def goto_parent_dir(self):
self.src = RedviewGenerator.last_slash_index(self.src)
self.dest = RedviewGenerator.last_slash_index(self.dest)
# self.markdown_getter.src = self.markdown_getter.src[:self.markdown_getter.src.rfind("/")]
def generate_doc(self):
"""
Generate documentation by recursively processing directories and files.
Handles special cases like broken symlinks and files with trailing slashes.
"""
# Check if source directory exists and is actually a directory
if not exists(self.src):
print(f"Warning: Source path does not exist: {self.src}")
return
# Handle case where src has a trailing slash but is actually a file or broken symlink
if self.src.endswith('/') and exists(self.src.rstrip('/')) and not isdir(self.src.rstrip('/')):
# Remove trailing slash for files
self.src = self.src.rstrip('/')
print(f"Corrected path (removed trailing slash from file): {self.src}")
# Now check if it's a directory after potential correction
if not isdir(self.src):
print(f"Warning: Source path is not a directory: {self.src}")
return
try:
# Process the current directory
dir_processor = Directory_Processor(
self.FORMAT,
self.src,
self.dest,
self.ROOT_DEST,
self.script_dir,
self.dir_to_exclude,
self.mainTags,
self.real_path,
self.exclude_hidden_dir
)
dir_processor.generate_dir_summary()
# Create symlinks for files in the current (root) directory
for file_name in listdir(self.src):
full_file_name = path.join(self.src, file_name)
if isfile(full_file_name):
link_name = path.join(self.dest, file_name)
if not exists(link_name):
try:
symlink(full_file_name, link_name)
except OSError as e:
print(f"Warning: Could not create symlink for {full_file_name}: {e}")
# Process subdirectories
for directory in dir_processor.child_dir:
src_subdir = clean_end_path(self.src + "/" + directory)
dest_subdir = clean_end_path(self.dest + "/" + directory.replace(" ", "_"))
# Skip if source subdirectory doesn't exist or isn't a directory
if not exists(src_subdir) or not isdir(src_subdir):
print(f"Warning: Skipping invalid directory: {src_subdir}")
continue
# Save current paths
original_src = self.src
original_dest = self.dest
# Set new paths for recursive call
self.src = src_subdir
self.dest = dest_subdir
# Recursively process subdirectory
self.generate_doc()
# Restore original paths
self.src = original_src
self.dest = original_dest
except FileNotFoundError as e:
print(f"Error: Directory not found: {self.src} - {e}")
except PermissionError as e:
print(f"Error: Permission denied when accessing: {self.src} - {e}")
except Exception as e:
print(f"Error generating documentation for {self.src}: {e}")
traceback.print_exc()
"""Used to copy all files in a dest directory, to limit web server configuration disclosure"""
def dest_to_data(self):
self.dest = self.dest + "/data/"
self.ROOT_DEST = self.ROOT_DEST + "/data/"
makedirs(path.dirname(self.dest), exist_ok=True)
#------------------Create document---------------
def redview_title():
text ="""
_ .-') ('-. _ .-') _ (`-. ('-. (`\ .-') /`
( \( -O ) _( OO)( ( OO) ) _(OO )_ _( OO) `.( OO ),'
,------. (,------.\ .'_ ,--(_/ ,. \ ,-.-') (,------.,--./ .--.
| /`. ' | .---',`'--..._)\ \ /(__/ | |OO) | .---'| | |
| / | | | | | | \ ' \ \ / / | | \ | | | | | |,
| |_.' |(| '--. | | ' | \ ' /, | |(_/(| '--. | |.'.| |_)
| . '.' | .--' | | / : \ /__),| |_.' | .--' | |
| |\ \ | `---.| '--' / \ / (_| | | `---.| ,'. |
`--' '--' `------'`-------' `-' `--' `------''--' '--'
"""
return text
# Define argparse
def main():
parser = argparse.ArgumentParser(add_help=False,prog='redview.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(redview_title()))
parser.add_argument("--source", "-s", help="Source directory which contains original md files", required=True)
parser.add_argument("--name", "-n", help="Nom du projet, par défaut redview", default="redview", required=False)
parser.add_argument("--path", "-p", help="Chemin destination de des notes formatées, par défaut /tmp/", default="/tmp/", required=False)
parser.add_argument("--format", "-f", help="Format de sortie optimisée pour : web, obsidian et markmap", choices=["web","md","obsidian","markmap"], default="web")
parser.add_argument("--watcher", "-w", help="Run redview in the background and update automatically note from source to path", action="store_true")
parser.add_argument("--verbose", "-v", help="Augmente le niveau de verbosité",action="store_true")
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit.')
args = parser.parse_args()
FORMAT = args.format
dest_path = args.path
dest_dir = args.name
dest = dest_path + dest_dir
dest = clean_end_path(dest)
script_dir = path.dirname(path.realpath(__file__))
dir_to_exclude = getDirToExclude(script_dir+"/conf.yaml")
exclude_hidden_dir = hiddenDirSettings(script_dir+"/conf.yaml")
mainTags = getMainTags(script_dir+"/conf.yaml")
# src = path.dirname(path.realpath(__file__))
src = args.source
src = clean_end_path(src)
# Handle case where src has a trailing slash but is actually a file
if src.endswith('/') and exists(src.rstrip('/')) and not isdir(src.rstrip('/')):
src = src.rstrip('/')
print(f"Corrected source path (removed trailing slash from file): {src}")
if not exists(src):
print("error: Source directory "+src+" not found")
exit()
if not isdir(src):
print("error: Source path "+src+" is not a directory")
exit()
if not path.exists(dest_path):
print("error: "+dest_path+" not found")
exit()
if path.exists(dest):
rmtree(dest)
# print("Old "+dest+" directorty found")
# ans = input("Do you want to replace it? (y/n)")
# if ans != "y":
# print("ok") #Shall be replaced by the launch of grip
# exit()
# else:
# rmtree(dest)
mkdir(dest)
redview = RedviewGenerator(FORMAT, src, dest, script_dir, dir_to_exclude, exclude_hidden_dir, mainTags)
# markdown_getter = MarkdownGetter(FORMAT, src, dest , dir_to_exclude , mainTags)
# markdown_writter = MarkdownWritter(FORMAT, dest, mainTags)
if FORMAT == "web" :
redview.export_web()
redview.dest_to_data()
redview.generate_doc()
if args.watcher:
w = Watcher(redview)
w.run()
if __name__ == '__main__':
main()