-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrstTrial.py
185 lines (133 loc) · 5.73 KB
/
rstTrial.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 8 23:09:48 2022
@author: Christian
"""
import os
import numpy as np
import openseespyhint.getsrc.parseRST as pr
import openseespyhint.getsrc.parseArgs as pa
headingIdentifier = ' ====='
srcDir = 'src-generated'
rstDir = 'rst'
tocOffset = 4
rstTabText = ' '
tabText2 = rstTabText*2
# Get all possible files
rstFiles, rstFilePaths = pr.getRstFilePaths(rstDir)
# Seperate command TOCs
cmdPaths = pr.getCmdPaths(rstFilePaths)
# manually remove the visfiles
cmdPaths.remove('rst\\ops_vis_fib_sec_list_to_cmds.rst')
cmdPaths.remove('rst\\plotcmds.rst')
# Iterate through all the possible command groups
for cmdPath in cmdPaths:
# Get the name of the command file
cmdName = cmdPath.strip('.rst')
cmdName = cmdName.strip('\\')
cmdName = cmdName[:-3]
# Get the name of the output folder and make it
oCmdFolder = os.path.join(srcDir, cmdName)
os.makedirs(oCmdFolder, exist_ok=True)
# Readad the command TOC file and get the text
cmdTocTxt = pr.readFile(cmdPath)
istoc, indexes = pr.checkIfTOC(cmdTocTxt)
# Read the TOC file
tocIndex = indexes[0] + 3
tocEntries = pr.getTocEntries(cmdTocTxt, tocIndex)
# For each command TOC, read the sub TOC files
for tocEntry in tocEntries:
# Get the file name
path = os.path.join(rstDir, tocEntry+ '.rst')
rstText = pr.readFile(path)
rstText.append('')
# Initialize varialbes that will be used to contain extracted text
docstrings = {}
arguments = {} # contains the function arguments
functions = [] # contains the function name
defLines = {} # contains the function defition
intLines = {} # intermediate text needed for special arguments
callLines = {} # Contains the final function call
functionFiles = [] # Contains all the function files.
# Generate the first line of the file
outText = 'import openseespy.opensees as ops' + '\n' + '\n'
# Check if the current file is also a TOC.
istoc, indexes = pr.checkIfTOC(rstText)
# Find all the entries in the current TOC
for ind in indexes:
currentInd = ind + tocOffset
functionFiles = pr.parseTocForFunctionFiles(rstText, functionFiles, currentInd)
# If the the entry isn't a TOC, read functions from the index file the file
if not istoc:
functionFiles = [tocEntry]
print(tocEntry)
print('not toc')
# If the file is a TOC
for file in functionFiles:
# Get the input and output directory paths
ipath = os.path.join(rstDir, file + '.rst')
opath = os.path.join(srcDir, file + '.rst')
# read the rst text
rstText = pr.readFile(ipath)
# Find where the function definiton is:
Inds = pr.getFunctionDefinitionInd(rstText)
funcDocstrings = pr.getDocstringText(rstText, Inds)
# parse the arguments and generate the text
Nind = len(Inds)
for ii in range(Nind):
Ind = Inds[ii]
args = pr.getArgsFromTxt(rstText[Ind])
funcText = rstText[Ind].split(".. function:: ")[1]
funcText = funcText.strip("\n")
# print(funcText)
defLine, interText, funcText = pa.getFunctionText(funcText)
funcName = args[0].strip('"')
funcName = funcName.strip("'")
functions.append(funcName)
defLines[funcName] = defLine
intLines[funcName] = interText
callLines[funcName] = funcText
arguments[funcName] = rstText[Ind]
docstrings[funcName] = pr.cleanDocstring(funcDocstrings[ii])
# The name of the output file in the TOC
tocFile = os.path.join(srcDir, cmdName, tocEntry + '.py')
print(functions)
for function in functions:
outTextTmp = defLines[function] + docstrings[function] + intLines[function] + callLines[function]
outTextTmp = '\n'.join(outTextTmp)
outTextTmp += '\n'
outTextTmp += '\n'
# print(outText)
outText += outTextTmp
# print()
# with tocFile as
with open(tocFile, 'w',encoding='utf-8') as f:
f.write(outText)
# inds = []
# ii = 0
# for line in rstText:
# if headingIdentifier in line:
# print(line)
# inds.append(ii)
# ii +=1
# if there is only one function, then the docstring is the start of that funciton to the end.
# If there is more than one function, then either:
# There are multiple docstrings
# There are
# print(funcName)
# for each entry in the command TOC, find the sub-page
# - there may be more than one page
# - there may be more than one page
# - there may be more than one page
# Create a folder for each top level command file
# for each sub page, check if the page is a TOC
# If it is a TOC
# - create a directory in the cmd folder for the TOC entry
# - Extract all entries in the TOC.
# - For each entry, create a function for all functions in the file. (see function)
# If it isn't a TOC, extract all functions text from the file
# - check how many functions are in each file, parse each function into a
# - for the raw text in each functon, extract the arguements
# - Find optional tags, i.e. 'sp'
# - Find optional arguements <- how will this work
# - check how many functions are in each file