myDesignTool.py 5.93 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
1
2
3
4
5
from . import _D_PATH2_MYDESIGNS
from . import _D_PATH2_MYFIGURES
from . import _D_PATH2_MYAXES

from .. import WARN, INFO
6
7
8
9


def getSectionFromDesign(designFile):
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
10
    f = open(_D_PATH2_MYDESIGNS + str(designFile))
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
    
    designSections = {}
    
    startProcedure = False
    className = ''
    functionName = ''
    procedure = []
    
    for line in f.readlines():
        
        # If recording a procedure
        if startProcedure:
            
            # and found the end of it
            if line.startswith('/>'):
                
                print(INFO + str(className) + ' ' + str(functionName))
                
                # Save the procedure
                if str(className) in designSections.keys():
                    designSections[str(className)].update({str(functionName): procedure})
                else:
                    designSections.update({str(className): {}})
                    designSections[str(className)].update({str(functionName): procedure})
                
                # reset
                className = ''
                functionName = ''
                procedure = []
                
                startProcedure = False
                
            # and found bgining of a new one print WARNING
            elif line.startswith('<'):
                print(WARN + "I found a line starting with '<' before the end of the section, you may have forgot to close it.")
            
            # record the procedure
            else:
                procedure.append(line)
        
        # if not recording and found begining of a section
        if (line.startswith('<')) and (startProcedure is False):
            
            # check the format
            if line.startswith('< '):
                
                # get the ClassName and FunctionName
                label = line.split(' ')
                if len(label) < 2:
                    print(WARN + "apparently the format you use to declare this section is incrorrect. use: '< ClassName::FunctionName'")
                
                className, functionName = label[1].split('::')
                
                if('\n' in str(className)):
                    className = className[:-1]
                if('\n' in str(functionName)):
                    functionName = functionName[:-1]
                
                print(INFO + str(className) + ' ' + str(functionName))
                
                procedure = []
                
                # start recording
                startProcedure = True
            
            # if format wrong print WARNING
            else:
                print(WARN + "apparently you forgot a space after opening the section. please strictly use: '< ClassName::FunctionName'")
    
    for className in designSections.keys():
        print(INFO + str(className) + ": " + str(designSections[className].keys()))
    
    f.close()
    
    return(designSections)


def savePlot(filename):
    
    designSections = getSectionFromDesign(filename)
    figureClassName = None
    
    # IDENTIFY THE FIGURE
    for className in designSections.keys():
        if 'addAxes' in designSections[str(className)].keys():
            figureClassName = className
    
    # OBTAIN THE FUNCTIONS
    if 'declareKeywords' in designSections[str(figureClassName)].keys():
        declareKeywordsString = '\t'.join(designSections[str(figureClassName)]['declareKeywords'])
    else:
        declareKeywordsString = ''
    
    if 'addAxes' in designSections[str(figureClassName)].keys():
        addAxesString = '\t'.join(designSections[str(figureClassName)]['addAxes'])
    else:
        addAxesString = ''
    
    if figureClassName is None or addAxesString == '' or declareKeywordsString == '':
        print("ERROROROROR!!!!!")
        print(figureClassName)
        print(addAxesString)
        print(declareKeywordsString)
        
    # PUT THEM INTO THE STRUCTURE
    template = _CLASS_TEMPLATE.format(className=figureClassName,
                                      declareKeywords=declareKeywordsString,
                                      addAxes=addAxesString)
            
    # CREATE THE FIGURE FILE
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
121
    f = open(_D_PATH2_MYFIGURES + str(figureClassName)[0].lower() + str(figureClassName)[1:] + '.py', 'w')
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
    
    # WRITE THE FIGURE
    f.writelines(template)
    
    f.close()
    
    del designSections[str(figureClassName)]
    
    # IDENTIFY THE AXES
    for axes in designSections.keys():
        
        axesClassName = axes
        
        if 'formatRawData' in designSections[axes].keys():
            
            formatRawDataString = '\t'.join(designSections[axes]['formatRawData'])
        
        if 'plotting' in designSections[axes].keys():
            plottingString = '\t'.join(designSections[axes]['plotting'])
        
        # OPEN THE FILE
        f = open(_D_PATH2_MYAXES + str(axesClassName)[0].lower() + str(axesClassName)[1:] + '.py', 'w')
        
        template = _AXES_TEMPLATE.format(axesName=axesClassName,
                                         formatRawData=formatRawDataString,
                                         plotting=plottingString)
        
        f.writelines(template)
        
        f.close()

_AXES_TEMPLATE = '''# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!


# CLASS {axesName:=<63}
class {axesName}(MyAxes):

    # FORMAT RAWDATA --------------------------------------------------
    def formatRawData(self):
{formatRawData}
    
    # PLOTTING --------------------------------------------------------
    def plotting(self):
{plotting}

'''

_CLASS_TEMPLATE = '''# IMPORT --------------------------------------------------------------
YOU NEED TO DO THAT YOURSELF!


# CLASS {className:=<63}
class {className}(MyFig):
    
    # DECLARE KEYWORDS ------------------------------------------------
    def declareKeywords(self):
{declareKeywords}
    
    # ADD AXES --------------------------------------------------------
    def addAxes(self):
{addAxes}

'''