| 1 |
|
| 2 |
#combine phactori.py and all operations (and later other pieces) into |
| 3 |
#phactori_onefile.py, and then combine phactori_onefile.py and |
| 4 |
#PhactoriDriver_modular.py into PhactoriDriver_onefile.py |
| 5 |
|
| 6 |
def ReplaceQuotesWithEscapes(inStr): |
| 7 |
|
| 8 |
outStr = "" |
| 9 |
for ndx in range(0,len(inStr)): |
| 10 |
ch = inStr[ndx] |
| 11 |
if ch == "\n": |
| 12 |
outStr = outStr + "\\n" |
| 13 |
elif ch == '"': |
| 14 |
outStr = outStr + "'" |
| 15 |
else: |
| 16 |
outStr = outStr + ch |
| 17 |
|
| 18 |
return outStr |
| 19 |
|
| 20 |
def RemoveTripleQuotes(inlines): |
| 21 |
outlines = [] |
| 22 |
ndx = 0 |
| 23 |
isDone = False |
| 24 |
while not isDone: |
| 25 |
if ndx >= len(inlines): |
| 26 |
isDone = True |
| 27 |
continue |
| 28 |
|
| 29 |
#copy lines until you see """ |
| 30 |
if inlines[ndx].find('"""') == -1: |
| 31 |
outlines.append(inlines[ndx]) |
| 32 |
ndx += 1 |
| 33 |
continue |
| 34 |
|
| 35 |
oneLine = inlines[ndx] |
| 36 |
qndx1 = oneLine.find('"""') |
| 37 |
qndx2 = oneLine[qndx1+3:].find('"""') |
| 38 |
|
| 39 |
#if this line is a comment, just pass it through |
| 40 |
commentndx = inlines[ndx].find('#') |
| 41 |
if commentndx != -1: |
| 42 |
if commentndx < qndx1: |
| 43 |
outlines.append(inlines[ndx]) |
| 44 |
ndx += 1 |
| 45 |
continue |
| 46 |
|
| 47 |
print(("found tq:", ndx)) |
| 48 |
print(oneLine) |
| 49 |
|
| 50 |
print((qndx1, qndx2)) |
| 51 |
#handle case where line contains two """ |
| 52 |
if qndx2 > -1: |
| 53 |
insideQuotes = oneLine[qndx1+3:qndx1+3+qndx2] |
| 54 |
insideQuotesWithEscapes = ReplaceQuotesWithEscapes(insideQuotes) |
| 55 |
chgLine = oneLine[0:qndx1] + '"' + insideQuotesWithEscapes + '"' + oneLine[qndx1+3+qndx2+3:] |
| 56 |
outlines.append(chgLine) |
| 57 |
ndx += 1 |
| 58 |
print(("fixedone1", ndx)) |
| 59 |
print("complete replacement line 2:") |
| 60 |
print(chgLine) |
| 61 |
continue |
| 62 |
|
| 63 |
print("found2") |
| 64 |
print(oneLine) |
| 65 |
print((qndx1, qndx2)) |
| 66 |
|
| 67 |
prefix = oneLine[0:qndx1] |
| 68 |
|
| 69 |
chgLine = oneLine[qndx1+3:] |
| 70 |
|
| 71 |
#find next line that has """, closing quote |
| 72 |
qend_ndx = ndx + 1 |
| 73 |
while inlines[qend_ndx].find('"""') == -1: |
| 74 |
chgLine = chgLine + inlines[qend_ndx] |
| 75 |
qend_ndx += 1 |
| 76 |
|
| 77 |
oneLine = inlines[qend_ndx] |
| 78 |
|
| 79 |
print(("end of triple quote", qend_ndx)) |
| 80 |
print(oneLine) |
| 81 |
|
| 82 |
#freak out if this line has two """ |
| 83 |
qndx1 = oneLine.find('"""') |
| 84 |
qndx2 = oneLine[qndx1+3:].find('"""') |
| 85 |
if qndx2 != -1: |
| 86 |
print("can't handle close triple quote and opening triple quote on same line\n") |
| 87 |
exit(-100) |
| 88 |
|
| 89 |
chgLine = chgLine + oneLine[:qndx1] |
| 90 |
postfix = oneLine[qndx1+3:] |
| 91 |
|
| 92 |
chgLine = ReplaceQuotesWithEscapes(chgLine) |
| 93 |
chgLine = prefix + '"' + chgLine + '"' + postfix |
| 94 |
|
| 95 |
print("complete replacement line:") |
| 96 |
print(chgLine) |
| 97 |
outlines.append(chgLine) |
| 98 |
ndx = qend_ndx + 1 |
| 99 |
return outlines |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
def GetCombineSectionFromClass(inClassName): |
| 104 |
"""get lines from subclass (including subdirectory name)and return |
| 105 |
to be added to merged file""" |
| 106 |
fname = inClassName + ".py" |
| 107 |
pp = open(fname, "r") |
| 108 |
if pp == None: |
| 109 |
print(("could not open file:", fname)) |
| 110 |
exit(-5) |
| 111 |
pplines = pp.readlines() |
| 112 |
pp.close() |
| 113 |
#find beginning/end indexes |
| 114 |
ndx = 0 |
| 115 |
beginNdx = -1 |
| 116 |
endNdx = -1 |
| 117 |
while ndx < len(pplines): |
| 118 |
if pplines[ndx].startswith("#phactori_combine_to_single_python_file_subpiece_begin_1"): |
| 119 |
beginNdx = ndx |
| 120 |
if pplines[ndx].startswith("#phactori_combine_to_single_python_file_subpiece_end_1"): |
| 121 |
endNdx = ndx |
| 122 |
break |
| 123 |
ndx += 1 |
| 124 |
|
| 125 |
if (beginNdx < 0) or (endNdx < 0): |
| 126 |
print(("file:", fname)) |
| 127 |
if beginNdx < 0: |
| 128 |
print("could not find #phactori_combine_to_single_python_file_subpiece_begin_1") |
| 129 |
if endNdx < 0: |
| 130 |
print("could not find #phactori_combine_to_single_python_file_subpiece_end_1") |
| 131 |
exit(-6) |
| 132 |
|
| 133 |
retList = pplines[beginNdx:endNdx+1] |
| 134 |
return retList |
| 135 |
|
| 136 |
|
| 137 |
ff = open("phactori.py","r") |
| 138 |
phlines = ff.readlines() |
| 139 |
oo = open("phactori_onefile.py", "w") |
| 140 |
|
| 141 |
isDone = False |
| 142 |
phndx = 0 |
| 143 |
while(isDone == False): |
| 144 |
#copy until #phactori_combine_to_single_python_file_parent_ line |
| 145 |
while (phlines[phndx].startswith("#phactori_combine_to_single_python_file_parent_") == False): |
| 146 |
oo.write(phlines[phndx]) |
| 147 |
phndx += 1 |
| 148 |
if phndx >= len(phlines): |
| 149 |
isDone = True |
| 150 |
break |
| 151 |
|
| 152 |
if isDone == True: |
| 153 |
break |
| 154 |
|
| 155 |
#go ahead and copy combine comment |
| 156 |
oo.write(phlines[phndx]) |
| 157 |
phndx += 1 |
| 158 |
|
| 159 |
#make sure next line starts with from classname import * |
| 160 |
if (phlines[phndx].startswith("from ") == False): |
| 161 |
print(("expecting 'from X import *' line to replace, ndx", phndx)) |
| 162 |
exit(-1) |
| 163 |
|
| 164 |
#get class to combine in |
| 165 |
xx = phlines[phndx].split(" ") |
| 166 |
|
| 167 |
if xx[2] != "import": |
| 168 |
print(("expecting (2) 'from X import *' line to replace, ndx", phndx)) |
| 169 |
exit(-2) |
| 170 |
|
| 171 |
if xx[3][0] != "*": |
| 172 |
print(("expecting (3) 'from X import *' line to replace, ndx", phndx)) |
| 173 |
exit(-3) |
| 174 |
|
| 175 |
zz = xx[1].split(".") |
| 176 |
if len(zz) != 2: |
| 177 |
print(("expecting something like 'from import Operation.PhactoriGroupOperation import*', ndx", phndx)) |
| 178 |
exit(-4) |
| 179 |
|
| 180 |
subdir = zz[0] |
| 181 |
combineFileClass = zz[1] |
| 182 |
|
| 183 |
print(("combining dir, class, ndx:", subdir, combineFileClass, phndx)) |
| 184 |
|
| 185 |
#add in import line with comment |
| 186 |
cmtimprtln = "#" + phlines[phndx] |
| 187 |
oo.write(cmtimprtln) |
| 188 |
phndx += 1 |
| 189 |
|
| 190 |
#add in correct segment of external class (get correct lines from |
| 191 |
#sub piece and merge them in |
| 192 |
targetFilename = subdir + "/" + combineFileClass |
| 193 |
classToCombineAsStringList = GetCombineSectionFromClass(targetFilename) |
| 194 |
for ssl in classToCombineAsStringList: |
| 195 |
oo.write(ssl) |
| 196 |
|
| 197 |
oo.close() |
| 198 |
|
| 199 |
#now combine into one PhactoriDriver file |
| 200 |
|
| 201 |
pof = open("phactori_onefile.py", "r") |
| 202 |
poflineswtq = pof.readlines() |
| 203 |
pof.close() |
| 204 |
poflines = RemoveTripleQuotes(poflineswtq) |
| 205 |
|
| 206 |
phdrv = open("PhactoriDriver_modular.py", "r") |
| 207 |
if phdrv == None: |
| 208 |
print(("could not open file:", "PhactoriDriver_modular.py")) |
| 209 |
exit(-8) |
| 210 |
phdrvlines = phdrv.readlines() |
| 211 |
phdrv.close() |
| 212 |
|
| 213 |
|
| 214 |
phdrvof = open("PhactoriDriver_onefile.py", "w") |
| 215 |
if phdrvof == None: |
| 216 |
print(("could not open file:", "PhactoriDriver_onefile.py")) |
| 217 |
exit(-9) |
| 218 |
|
| 219 |
isDone = False |
| 220 |
lnndx = 0 |
| 221 |
frmphimprtlnfound = False |
| 222 |
while(isDone == False): |
| 223 |
#copy until "from phactori import *" line |
| 224 |
while (phdrvlines[lnndx].startswith("from phactori import *") == False): |
| 225 |
phdrvof.write(phdrvlines[lnndx]) |
| 226 |
lnndx += 1 |
| 227 |
if lnndx >= len(phdrvlines): |
| 228 |
isDone = True |
| 229 |
break |
| 230 |
|
| 231 |
if isDone == True: |
| 232 |
break |
| 233 |
|
| 234 |
if phdrvlines[lnndx].startswith("from phactori import *"): |
| 235 |
if frmphimprtlnfound: |
| 236 |
print("error: from phactori import * in in twice!\n") |
| 237 |
exit(-11) |
| 238 |
frmphimprtlnfound = True |
| 239 |
|
| 240 |
phdrvof.write("#" + phdrvlines[lnndx]) |
| 241 |
phdrvof.write("######phactori.py inserted here by PhactoriCombineToOneFile.py\n\n") |
| 242 |
lnndx += 1 |
| 243 |
for oneLine in poflines: |
| 244 |
phdrvof.write(oneLine) |
| 245 |
|
| 246 |
phdrvof.close() |
| 247 |
|
| 248 |
if frmphimprtlnfound == False: |
| 249 |
print("error, from phactori import * not found in PhactoriDriver_modular.py") |
| 250 |
exit(-12) |