-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (55 loc) · 2.04 KB
/
main.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
import os
import json
import fnmatch
def read_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
except UnicodeDecodeError:
return None # Skip binary files or files that cannot be decoded
def parse_gitignore(gitignore_path):
ignore_patterns = []
try:
with open(gitignore_path, 'r', encoding='utf-8') as file:
for line in file:
line = line.strip()
if line and not line.startswith('#'):
ignore_patterns.append(line)
except FileNotFoundError:
pass
return ignore_patterns
def is_ignored(file_path, ignore_patterns):
for pattern in ignore_patterns:
if fnmatch.fnmatch(file_path, pattern):
return True
return False
def dir_to_json(directory):
result = {}
ignore_patterns = []
for root, dirs, files in os.walk(directory):
gitignore_path = os.path.join(root, '.gitignore')
ignore_patterns.extend(parse_gitignore(gitignore_path))
relative_path = os.path.relpath(root, directory)
if relative_path == ".":
relative_path = ""
sub_result = result
if relative_path:
for part in relative_path.split(os.sep):
sub_result = sub_result.setdefault(part, {})
for file in files:
file_path = os.path.join(root, file)
relative_file_path = os.path.relpath(file_path, directory)
if not is_ignored(relative_file_path, ignore_patterns):
file_content = read_file(file_path)
if file_content is not None:
sub_result[file] = file_content
return result
def main():
directory = r'.'
json_data = dir_to_json(directory)
json_output = os.path.join(directory, 'output.json')
with open(json_output, 'w', encoding='utf-8') as json_file:
json.dump(json_data, json_file, ensure_ascii=False, indent=4)
print(f"JSON data has been written to {json_output}")
if __name__ == "__main__":
main()