-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSL_Common.py
129 lines (108 loc) · 3.85 KB
/
SL_Common.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
# SL_Common.py
# SetLineid common class
# """Functionality:
# - Read from file
# - Backup file
# - Write to file
# - Find files by extension recursively"""
# - WIP - Restore files from backup
# - Rules for lines to skip
# - Rules for line blocks to skip
import os
class Common:
def __init__(self):
pass
# Read text from file, each line as a list element
@staticmethod
def readFile(path):
sData = []
with open (path, encoding="utf8") as fRead:
lines = fRead.readlines()
for line in lines:
sData.append(line.strip("\n"))
return sData
# Backup files without overwriting existing ones (hording), if present
@staticmethod
def backupFile(path, backupPath):
tryN = 1
fileName = os.path.basename(path)
if backupPath == None or backupPath == "":
backupPath = f"{path}"
else:
backupPath = os.path.normpath(f"{backupPath}/{fileName}.fc")
backupPathCopy = backupPath
while os.path.exists(backupPath):
if os.path.isdir(backupPath):
backupPath = os.path.normpath(f"{backupPathCopy}/{fileName}_{tryN}.fc")
elif os.path.isfile(backupPath):
backupPath = os.path.normpath(f"{backupPathCopy}_{tryN}.fc")
tryN += 1
fRead = Common.readFile(path)
Common.writeFile(fRead, backupPath)
# Write data (text) to file line-by-line
@staticmethod
def writeFile(sData, path):
with open (path, "w", encoding="utf8") as fWrite:
for line in sData:
fWrite.write(f"{line}\n")
# Creates list of file pathes
@staticmethod
def filePathCollector(dirPath, fileExts):
pathList = []
for root, dirs, files in os.walk(dirPath):
for f in files:
# check if file with any needed extension found
for ext in fileExts:
if f.endswith(ext):
# add file to list
filePath = os.path.join(root, f)
pathList.append(filePath)
break
return pathList
# Default/Simple/Basic line skip selector
@staticmethod
def skipLinesDefaultSelector(line: str) -> bool:
skipLine = False
sLine = line.strip()
# As Comment/Lore should be the last element, \
# any LineTag there should be neglected.
if sLine.startswith("//"):
skipLine = True
elif sLine.find("//") != -1:
sLine = sLine.split("//", 1)[0].strip()
if skipLine or not sLine or sLine.find("#line:skip") != -1 or sLine == "---" or sLine == "===":
skipLine = True
return skipLine
# Text block skip selector
@staticmethod
def skipBlockSelector(line: str, skipTrigger: bool, boxTrigger: bool) -> bool:
sLine = line.strip()
skip = Common.skipLinesDefaultSelector(line)
vIdNeeded = False
# <<box>> block
if sLine.startswith("<<endbox>>"):
boxTrigger = False
skipTrigger = False
elif sLine.startswith("<<box>>"):
if sLine.startswith("<<box>>") and boxTrigger:
pass
else:
boxTrigger = True
skipTrigger = False
# YarnSpinner node body block
if not boxTrigger:
if sLine.startswith("---"):
vIdNeeded = True
skipTrigger = False
elif sLine.startswith("==="):
skipTrigger = True
vIdNeeded = False
else:
if not skipTrigger:
vIdNeeded = True
else:
if skip:
skipTrigger = True
else:
vIdNeeded = True
return (vIdNeeded, skipTrigger, boxTrigger)