-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
115 lines (92 loc) · 3.75 KB
/
combine.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
# // ---------------------------------------------------------------------
# // ------- [cuhHub] Tools - Combiner
# // ---------------------------------------------------------------------
# -----------------------------------------
# // ---- Imports
# -----------------------------------------
import os
import argparse
# -----------------------------------------
# // ---- Variables
# -----------------------------------------
parser = argparse.ArgumentParser(
description = "Combines all files in a directory into one.",
)
# -----------------------------------------
# // ---- Functions
# -----------------------------------------
# // Read a file
def quickRead(path: str, mode: str = "r"):
with open(path, mode) as f:
return f.read()
# // Write to a file
def quickWrite(path: str, content: str, mode: str = "w"):
directory = os.path.dirname(path)
if not os.path.exists(directory):
os.makedirs(directory, exist_ok = True)
with open(path, mode) as f:
return f.write(content)
# // Check if path is in list
def pathInList(path: str, paths: list):
for currentPath in paths:
if os.path.exists(currentPath) and os.path.samefile(path, currentPath):
return True
return False
# // Get contents of all files in a path
def recursiveRead(targetDir: str, allowedFileExtensions: list[str], pathExceptions: list[str]) -> dict[str, str]:
# list files
files = os.listdir(targetDir)
contents = {}
# iterate through them
for file in files:
# get file-related variables
_, extension = os.path.splitext(file)
path = os.path.join(targetDir, file)
# file is folder, but is an exception
if pathInList(path, pathExceptions):
continue
# file extension check
if extension == "":
# file is folder, so read it too
contents = {**contents, **recursiveRead(path, allowedFileExtensions, pathExceptions)}
# file extension check
if extension not in allowedFileExtensions and len(allowedFileExtensions) > 0:
continue
# get file content
content = quickRead(path, "r")
# append file content to contents
contents[path] = content
return contents
# -----------------------------------------
# // ---- Main
# -----------------------------------------
# // Setup
# setup parser args
parser.add_argument("-d", "-p", "--directory", "--path", type = str, help = "The directory containing files to combine.", required = True)
parser.add_argument("-de", "--destination", type = str, help = "The file which should have the content of all files combined. Created automatically if it doesn't exist.", required = True)
parser.add_argument("-afe", "--allow_file_extension", type = str, nargs = "*", help = "The file extensions to allow.", default = [])
parser.add_argument("-ip", "--ignore_path", type = str, nargs = "*", help = "The paths to ignore when combining.", default = [])
args = parser.parse_args()
# // Main
# get content of all files
result = recursiveRead(
args.directory,
args.allow_file_extension,
[*args.ignore_path, *[args.destination, os.path.relpath(__file__)]]
)
# print message
print("Combined the following files:\n- " + "\n- ".join([path.replace("\\", "/") for path in result.keys()]))
# format result
for path, content in result.items():
newContent = [
"----------------------------------------------",
f"-- // [File] {path}",
"----------------------------------------------",
content
]
result[path] = "\n".join(newContent)
# dump it into output file
try:
quickWrite(args.destination, "\n\n".join(result.values()), "w")
except:
print("Failed to output.")