Upload 5 files
Browse files- utils/fileSelect.py +11 -0
- utils/files.py +61 -0
- utils/input.py +40 -0
- utils/options.py +44 -0
- utils/steps.py +39 -0
utils/fileSelect.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tkinter import Tk
|
2 |
+
from tkinter.filedialog import askdirectory
|
3 |
+
|
4 |
+
|
5 |
+
def getDirectory(**args):
|
6 |
+
title = args.get("title", None)
|
7 |
+
Tk().withdraw()
|
8 |
+
return askdirectory(title=title)
|
9 |
+
|
10 |
+
|
11 |
+
# getDirectory()
|
utils/files.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from os import listdir, makedirs
|
2 |
+
from os.path import isdir, isfile, join
|
3 |
+
from re import search, IGNORECASE
|
4 |
+
from shutil import copy
|
5 |
+
|
6 |
+
|
7 |
+
def getFilesWith(path: str, reg: str):
|
8 |
+
if not isdir(path):
|
9 |
+
print(path, "is not a valid path")
|
10 |
+
return None
|
11 |
+
content = listdir(path)
|
12 |
+
if len(content) == 0:
|
13 |
+
print(path, "has no content")
|
14 |
+
return None
|
15 |
+
files = [f for f in content if isfile(join(path, f)) and search(reg, f, IGNORECASE)]
|
16 |
+
if len(files) == 0:
|
17 |
+
print(path, "contains no", reg)
|
18 |
+
return None
|
19 |
+
return files
|
20 |
+
|
21 |
+
|
22 |
+
def createNewFolders(dirs: list):
|
23 |
+
for d in dirs:
|
24 |
+
if not isdir(d):
|
25 |
+
makedirs(d)
|
26 |
+
else:
|
27 |
+
print("directory already exists", d)
|
28 |
+
|
29 |
+
|
30 |
+
def createNewTemplates(objs, templatesDir, regTemplate, root):
|
31 |
+
templatefiles = getFilesWith(templatesDir, regTemplate)
|
32 |
+
for k in objs:
|
33 |
+
regPhase = r""
|
34 |
+
match k:
|
35 |
+
case "(i)SAT":
|
36 |
+
regPhase = r"sat"
|
37 |
+
case "iFAT":
|
38 |
+
regPhase = r"fat"
|
39 |
+
|
40 |
+
files = [f for f in templatefiles if search(regPhase, f, IGNORECASE)]
|
41 |
+
if len(files) == 0:
|
42 |
+
print("phase %s has no templates" % k)
|
43 |
+
continue
|
44 |
+
|
45 |
+
for o in objs[k]:
|
46 |
+
targetLocation = join(root, o)
|
47 |
+
tlFiles = getFilesWith(targetLocation, regPhase)
|
48 |
+
|
49 |
+
if tlFiles:
|
50 |
+
print(k, "files already exist in:", targetLocation)
|
51 |
+
print("--------------------")
|
52 |
+
[print("|-",f) for f in tlFiles]
|
53 |
+
print("--------------------")
|
54 |
+
continue
|
55 |
+
|
56 |
+
for f in files:
|
57 |
+
templatepath = join(templatesDir, f)
|
58 |
+
targetpath = targetLocation
|
59 |
+
if search(r"hut_\d{4}[a-zA-Z]{2}", f, IGNORECASE):
|
60 |
+
targetpath = join(targetLocation, f[:4] + o + f[10:])
|
61 |
+
copy(templatepath, targetpath)
|
utils/input.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.options import *
|
2 |
+
|
3 |
+
keys = {
|
4 |
+
"ifat": 0,
|
5 |
+
"if": 0,
|
6 |
+
"f": 0,
|
7 |
+
"isat": 1,
|
8 |
+
"(i)sat": 1,
|
9 |
+
"is": 1,
|
10 |
+
"s": 1,
|
11 |
+
"all": 2,
|
12 |
+
"a": 2,
|
13 |
+
}
|
14 |
+
|
15 |
+
|
16 |
+
def validatedPhaseInput():
|
17 |
+
inputPhase = None
|
18 |
+
while inputPhase is None:
|
19 |
+
printOptions()
|
20 |
+
inputPhase = input()
|
21 |
+
|
22 |
+
if inputPhase.isnumeric():
|
23 |
+
inputPhase = int(inputPhase)
|
24 |
+
if inputPhase not in range(len(inputPhases)):
|
25 |
+
print("\n", inputPhase, "is not a valid option")
|
26 |
+
inputPhase = None
|
27 |
+
else:
|
28 |
+
return inputPhases[inputPhase]
|
29 |
+
else:
|
30 |
+
inputPhase = inputPhase.lower()
|
31 |
+
if inputPhase not in keys:
|
32 |
+
print("\n", inputPhase, "is not a valid option")
|
33 |
+
inputPhase = None
|
34 |
+
else:
|
35 |
+
return inputPhases[keys[inputPhase]]
|
36 |
+
|
37 |
+
print(
|
38 |
+
"Something went seriously wrong, please consult the maintainer of the codebase."
|
39 |
+
)
|
40 |
+
return inputPhase
|
utils/options.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from re import search
|
2 |
+
|
3 |
+
phases = ["iFAT", "(i)SAT"]
|
4 |
+
|
5 |
+
inputPhases = {**{i: k for i, k in enumerate(phases)}, **{len(phases): "All"}}
|
6 |
+
|
7 |
+
exitinput = {"no", "n", "0"}
|
8 |
+
|
9 |
+
|
10 |
+
def printOptions():
|
11 |
+
print("\nchoose one of the following options;\n")
|
12 |
+
for key in inputPhases:
|
13 |
+
print("[%d] %s" % (key, inputPhases[key]))
|
14 |
+
print()
|
15 |
+
|
16 |
+
|
17 |
+
def getObjectsPerPhase(phase: str = "All"):
|
18 |
+
f = open("./objecten.txt", "r")
|
19 |
+
t = f.read().split("\n\n")
|
20 |
+
f.close()
|
21 |
+
|
22 |
+
objs = {p: [] for p in phases}
|
23 |
+
if phase in phases:
|
24 |
+
objs = {phase:[]}
|
25 |
+
|
26 |
+
regObject = r"\d{4}[a-zA-Z]{2}"
|
27 |
+
for g in t:
|
28 |
+
ls = g.split("\n")
|
29 |
+
k = ls[0]
|
30 |
+
if k in objs:
|
31 |
+
objs[k] = ls[1::]
|
32 |
+
else:
|
33 |
+
print("key [%s] is not recognized" % k)
|
34 |
+
|
35 |
+
objs = {k: objs[k] for k in objs if objs[k]}
|
36 |
+
|
37 |
+
for k in objs:
|
38 |
+
for i, o in enumerate(objs[k]):
|
39 |
+
m = search(regObject, o)
|
40 |
+
if not m:
|
41 |
+
continue
|
42 |
+
objs[k][i] = m.group(0)
|
43 |
+
|
44 |
+
return objs
|
utils/steps.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.options import *
|
2 |
+
from utils.fileSelect import *
|
3 |
+
from utils.files import *
|
4 |
+
from utils.input import *
|
5 |
+
|
6 |
+
from os.path import join, realpath
|
7 |
+
from os import startfile
|
8 |
+
|
9 |
+
regTemplate = r'template'
|
10 |
+
|
11 |
+
def choosePhase(): return validatedPhaseInput()
|
12 |
+
|
13 |
+
def getTemplatesDir():
|
14 |
+
p = getDirectory(title='Select the template directory')
|
15 |
+
while not p:
|
16 |
+
print('input a valid directory')
|
17 |
+
if input('continue?').lower() in exitinput: quit()
|
18 |
+
p = getDirectory(title='Select the template directory')
|
19 |
+
|
20 |
+
f = getFilesWith(p, regTemplate)
|
21 |
+
if not f:
|
22 |
+
print('no templates found')
|
23 |
+
if input('continue?').lower() in exitinput: quit()
|
24 |
+
return p
|
25 |
+
|
26 |
+
def getRoot(): return getDirectory(title='Select the root directory')
|
27 |
+
|
28 |
+
def copyPasteTemplates(root:str, phase:str, templatesDir:str):
|
29 |
+
objs = getObjectsPerPhase(phase)
|
30 |
+
|
31 |
+
objectslist = list(set([o for p in [objs[k] for k in objs] for o in p]))
|
32 |
+
|
33 |
+
createNewFolders([join(root,o) for o in objectslist])
|
34 |
+
print("directories created")
|
35 |
+
|
36 |
+
createNewTemplates(objs, templatesDir, regTemplate, root)
|
37 |
+
print("templates ready")
|
38 |
+
|
39 |
+
startfile(realpath(root))
|