__main__.py 6.83 KB
Newer Older
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v0.0.0,
#
#   @file      __main__.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   main program of the module to use in command-line mode.
#   for printing usage:
#
#            > python myplotlib -h
#
#   @section Test
#
#   run a few Test to verify integrity of library.
#
#   @section Plot
#
#   interface to the plot scripts
#
#   @section History
#
#   v 0.0.0 - __main__.py file for the myplotlib module
#             no tests and no plots
#
# ======================================================================
#
#
# IMPORT ---------------------------------------------------------------
from __init__ import INFO, SPCE, DBUG
from __init__ import screen, print2file
from __init__ import MyFig, MyFig4Test
from __init__ import readStupidData, readStupidData2, somePlottingRoutine

import sys
import getopt

# LOCAL VARIABLE
run4Plot = False
arg4Plot = ''
run4Test = False

# GET PASSED OPTIONS --------------------------------------------------- 
usage = '''
{0}Usage:       python mfdPlot [-h]
{1}                            [-t/--test [all/<num of test>]
{1}                            [-p/--plot <Name of the Plot>]
{1}                            [-a/--args <String of the arguments>]
{1}                            [--debug]
'''.format(INFO, SPCE)

# GET OPT
try:
    opts, args = getopt.getopt(
        sys.argv[1:], "htp:", ["test=", "plot=", "args=", "debug"])
    
except getopt.GetoptError:
    print(usage)
    sys.exit(1)

# CASE OPT
for opt, arg in opts:
    if(opt == '-h'):
        print(usage)
        sys.exit(0)

    elif opt in ("-t", "--test"):
        run4Test = True

    elif opt in ("-p", "--plot"):
        run4Plot = True
        arg4Plot = str(arg)

    elif opt in ("--debug"):
        
        if(run4Test):
            print(DBUG +
                  '{title:{c}^{n}}'
                  .format(title=' Run for Test ', c='=', n=72))
            
            sys.exit(0)
     
        if(run4Plot):
            print(DBUG +
                  '{title:{c}^{n}}'
                  .format(title=' Run for Plot ', c='=', n=72))
            
            print(SPCE + '           Arguments: ' + str(arg4Plot))
            sys.exit(0)


# SECTION: TESTS ------------------------------------------------------
if run4Test:

    print(INFO + '{title:{c}^{n}}'.format(title=' Run for Test ', c='=', n=72))

    # Status of the tests
    status = True
    fail = []

    print
    # TEST 1.0: CREATE A MyFig OBJECT FROM readStupidData
    try:
        if screen(MyFig, readStupidData(ipath='data1'), fignum=0):
            print(INFO + "Test-1.0: pass")
        else:
            status = False
            fail.append('Test-1.0')
    except:
        status = False
        fail.append('Test-1.0')

    # TEST 1.1: CREATE A MyFig OBJECT FROM NO DATA should return False
    try:
        if not screen(MyFig, 'data2', fignum=1):
            print(INFO + "Test-1.1: pass")
        else:
            status = False
            fail.append('Test-1.1')
    except:
        status = False
        fail.append('Test-1.1')

    # TEST 1.2: UPDATE MyFig (1) WITH ALREADY LOADED DATA: 'data1'
    try:
        if screen(MyFig, 'data1', fignum=1):
            print(INFO + "Test-1.2: pass")
        else:
            status = False
            fail.append('Test-1.2')
    except:
        status = False
        fail.append('Test-1.2')

    # TEST 1.3: UPDATE MyFig (0) WITH NEW DATA from readStupidData2
    try:
        if screen(MyFig, readStupidData2(ipath='data2'), fignum=0):
            print(INFO + "Test-1.3: pass")
        else:
            status = False
            fail.append('Test-1.3')
    except:
        status = False
        fail.append('Test-1.3')

    # TEST 1.4: UPDATE MyFig (0) WITH A NEW CLASS. Should destroy
    # Former figure and create new one.
    try:
        if screen(MyFig4Test, 'data1', fignum=0):
            print(INFO + "Test-1.4: pass")
        else:
            status = False
            fail.append('Test-1.4')
    except:
        status = False
        fail.append('Test-1.4')

    # TEST 2.0: print MyFig4Test into test.png
    try:
        if print2file(MyFig4Test, 'data1', 'test2.0.png'):
            print(INFO + "Test-2.0: pass")
        else:
            status = False
            fail.append('Test-2.0')
    except:
        status = False
        fail.append('Test-2.0')

    # TEST 2.1: print MyFig4Test into test.png
    try:
        if print2file(MyFig4Test, 'data1', 'test2.1.eps'):
            print(INFO + "Test-2.1: pass")
        else:
            status = False
            fail.append('Test-2.1')
    except:
        status = False
        fail.append('Test-2.1')

    # TEST 2.2: print MyFig4Test into test.png
    try:
        if print2file(MyFig4Test, 'data1', 'test2.2'):
            print(INFO + "Test-2.2: pass")
        else:
            status = False
            fail.append('Test-2.2')
    except:
        status = False
        fail.append('Test-2.2')

    # TEST 2.3: print MyFig4Test into test.png
    try:
        if print2file(MyFig4Test, 'data1', 'test2.3', oformat='eps'):
            print(INFO + "Test-2.3: pass")
        else:
            status = False
            fail.append('Test-2.3')
    except:
        status = False
        fail.append('Test-2.3')
        
    # TEST 2.4: print MyFig4Test into test.png
    try:
        if print2file(MyFig4Test,
                      'data1',
                      'test2.4.png',
                      opath='myplotlib/img/'):
            print(INFO + "Test-2.4: pass")
        else:
            status = False
            fail.append('Test-2.4')
    except:
        status = False
        fail.append('Test-2.4')

    # TEST 2.5: print MyFig4Test into test.png
    try:
        if not print2file(MyFig4Test,
                          'data1',
                          'test2.5.png',
                          opath='myplotlib/not-existing-folder/'):
            print(INFO + "Test-2.5: pass")
        else:
            status = False
            fail.append('Test-2.5')
    except:
        status = False
        fail.append('Test-2.5')

    # Print final status
    print
    print
    print(INFO + 'Status of test series: ' + str(status))
    if not status:
        print(SPCE + 'Number of Failed Tests: ' + str(len(fail)))
        print(SPCE + 'List of Failed Tests: ')
        for test in fail:
            print(SPCE + '\t- ' + str(test))


# SECTION: PLOTS ------------------------------------------------------
elif run4Plot:
    
    print(INFO + '{title:{c}^{n}}'.format(title=' Run for Plot ', c='=', n=72))
  
#  try:
    data = arg4Plot
    somePlottingRoutine(data)
#  except:
#    print('\n'+SEVR+"There is something wrong with your arguments.")
#    print(usage)
#    print(INFO+"To start interactively:")
#    print(SPCE+">> from mfdPlot import * \n")

else:

    print(usage)