myTool.py 4 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ================= FILE HEADER ========================================
#
#   myplotlib v0.0.0,
#
#   @file      myTool.py
#   @author    Yori 'AGy' Fournier
#   @licence   CC-BY-SA
#
#   MyTool is a tool box for interactive usage. It allows the user
#   for a simple usage of its own Figure-like class. The functions
#   here are meant for interactive data analyse and consistent plotting.
#
#   @functions
#
#           - setCurrentData: This tool is meant for interactive work,
#                             it set the given data identifier as being
#                             the current one such that it can be called
#                             with its identifier and the default
#                             one: G_RAWDATA['current'].
#
#           - print2file: This function is meant to be used in script
#                         mode of myplotlib, more than in interactive mode,
#                         eventhough it can be used in both. It will save
#                         the plot within a file.
#
#   @section History
#
#   v 0.0.0 - minimal toolBox for myplotlib, with myfig, setCurrentData,
#             screen, print2file functions
#
# ======================================================================
#
# IMPORT ---------------------------------------------------------------
37
38
39
from . import D_OPATH, D_OFORMAT
from . import G_RAWDATAS, _G_WINDOWS
from . import INFO, SEVR, WARN, SPCE
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
40

41
from . import ion, ioff, is_interactive
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
42

43
from . import MyWin
44

45
from . import np
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
46
47
48
49


# TOOL FUNCTION FOR READING EXISTING DATA ------------------------------
def setCurrentData(name):
50
    
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
51
52
53
54
55
56
57
58
59
60
    # RQ: grawdatas is a global variable
    try:
        G_RAWDATAS['current'] = G_RAWDATAS[name]
    except KeyError:
        print(SEVR + 'The data does not exist. --> EXIT')
        return(False)
      
    return(True)


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
# PRINT TO SCREEN ------------------------------------------------------
def print2screen(ClassName, inputArg, *args, **kwargs):
    
    try:
        fig = ClassName(inputarg=inputArg, *args, **kwargs)
    except:
        print(SEVR + 'The type of figure you gave does not exists.')
        return(False)
    
    try:
        MyWin(fig, *args, **kwargs)
    except:
        print(SEVR + "I couldn't draw the figure on a window... did you test the lib?")
        return(False)
        
    return(True)


def printListCurrentWindows():
    
    for window in _G_WINDOWS:
        if window == _G_WINDOWS[0]:
            print(INFO + "Figure " + window.num)
        else:
            print(SPCE + "Figure " + window.num)


def getWindow(ID):
    
    for window in _G_WINDOWS:
        if window.num == ID:
            return(window)
    
    print(WARN + "The window does not exist.")
    return(False)


def getFigOnWindow(ID):
    
    for window in _G_WINDOWS:
        if window.num == ID:
            return(window.canvas.figure)
    
    print(WARN + "The window does not exist.")
    return(False)


def drawFigOnWindow(fig, ID):
    
    return(MyWin(fig, fignum=ID))


113
114
115
116
117
118
119
120
121
122
def giveDataToWindow(data, ID):
    
    fig = getFigOnWindow(ID)
    fig.update(inputarg = data)
    win = getWindow(ID)
    win.refresh()
    
    return(True)


123
124
125
126
127
128
129
130
def closeWindow(ID):
    
    for window in _G_WINDOWS:
        if window.num == ID:
            return(window.close())


def closeAllWindows():
131
    
132
133
134
135
136
137
    # a for-loop won't work because close() acts on _G_WINDOWS
    #    for window in _G_WINDOWS:
    while(len(_G_WINDOWS) > 0):
        window = _G_WINDOWS[0]
        print(INFO + "window :" + str(window.num))
        window.close()
138
        
139
140
141
142
143
144
    if(len(_G_WINDOWS) == 0):
        return(True)
    else:
        return(False)


Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
145
# PRINT TO FILE --------------------------------------------------------
146
def print2file(ClassName, inputArg, filename, *args, **kwargs):
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
147
148
    
    try:
149
        fig = ClassName(inputarg=inputArg, *args, **kwargs)
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
150
    except:
151
        print(SEVR + 'The type of figure you gave does not exists.')
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
152
153
        return(False)
    
154
    fig.print2file(filename, *args, **kwargs)
Yori 'AGy' Fournier's avatar
Yori 'AGy' Fournier committed
155
156
157
    
    # end
    return(True)