Controller examples

This code let you create a basic template of a controller.

(Source code)

  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
#!/usr/bin/env python
import sys

"""
ControllerTemplate.py: Create a basic controller's template.
Its parameters are  the file name plus .py,
    the class inherited if it had (optional)
    and "yes" if you want to use the obsolete convention.
The necessary "defs" are marked as #TODO

python ControllerTemplate.py ExampleClass.py InheritedClass NoCT
"""
__author__ = "Carlos Falcon - cfalcon@cells.es"


class ControllerTemplate():

    def __init__(self, f, e=""):
        self.filename = f
        self.end = e
        self.ind = 'ind'
    #    pass

    def addHead(self):
        f = open(self.filename, "w")
        f.write('##############################################################################\n' +
                '##\n' +
                '## This file is part of Sardana\n' +
                '##\n' +
                '## http://www.sardana-controls.org/\n' +
                '##\n' +
                '## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain\n' +
                '##\n' +
                '## Sardana is free software: you can redistribute it and/or modify\n' +
                '## it under the terms of the GNU Lesser General Public License as published by\n' +
                '## the Free Software Foundation, either version 3 of the License, or\n' +
                '## (at your option) any later version.\n' +
                '##\n' +
                '## Sardana is distributed in the hope that it will be useful,\n' +
                '## but WITHOUT ANY WARRANTY; without even the implied warranty of\n' +
                '## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n' +
                '## GNU Lesser General Public License for more details.\n' +
                '##\n' +
                '## You should have received a copy of the GNU Lesser General Public License\n' +
                '## along with Sardana.  If not, see <http://www.gnu.org/licenses/>.\n' +
                '##\\n' +
                '##############################################################################\n\n')\


    def addIncludes(self, inherit, others=None):
        f = open(self.filename, "a")
        text = "from sardana import State\n"
        if inherit != "":
            text = text + "from sardana.pool.controller import " + inherit + "\n"
            if inherit.find("Motor") >= 0:
                self.ind = 'axis'
        if others is not None:
            text = text + others
        text = text + "#ADD others includes\n\n"
        f.write(text)
        # f.close()

    def createBasicClass(self):
        f = open(self.filename, "a")
        text = "#TODO - Delete it if you don't need\n"
        text = text + 'class BasicClass():\n' +\
            '\tpass\n\n'
        f.write(text)

    def createMainClass(self, inherit):
        f = open(self.filename, "a")
        text = "class " + self.filename[0:len(self.filename) - 3] + "(" + inherit + "):\n" +\
            '\t"""Description""" #TODO\n' +\
            '\tgender = "Simulation"\n' +\
            '\tmodel  = "Basic"\n' +\
            '\torganization = "CELLS - ALBA"\n' +\
            '\timage = "IMAGE.png"\n' +\
            '\tlogo = "ALBA_logo.png"\n\n' +\
            '\t#TODO - Delete it if you don\'t need\n' +\
            '\tctrl_properties= { \'AAA\' : { \'Type\' : \'DevString\', \'Description\' : \'AAA\' } }\n' +\
            '\taxis_attributes = { \'AAA\' : { \'type\' : str, \'Description\' : \'AAA\' }}\n\n' +\
            '\tMaxDevice = 1024 #TODO Standar value\n\n'

        fun = '# --------------------------------------------------------------------------\n' +\
            '# Init()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef __init__(self, inst, props, *args, **kwargs):\n'
        if inherit != "":
            fun = fun + '\t\t' + inherit + \
                '.__init__(self, inst, props, *args, **kwargs)\n'
        fun = fun + '\t\t#TODO\n'
        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# AddDevice/DelDevice()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef AddDevice(self,' + self.ind + '):\n'
        fun = fun + '\t\t#TODO\n'
        fun = fun + '\tdef DeleteDevice(self, ' + self.ind + '):\n'
        fun = fun + '\t\t#TODO\n'
        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# State()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef PreStateOne' + self.end + \
            '(self, ' + self.ind + '):\n' + '\t\tpass\n'
        fun = fun + '\tdef StateOne(self, ' + self.ind + '):\n'
        fun = fun + '\t\tstate = State.On\n'
        fun = fun + '\t\tstatus = "Undefined"\n'
        if inherit.find("Motor") >= 0:
            fun = fun + '\t\tswitchstate = 0"\n'
            fun = fun + '\t\t#TODO\n'
            fun = fun + '\t\treturn state, status, switchstate"\n'
        else:
            fun = fun + '\t\t#TODO\n'
            fun = fun + '\t\treturn state, status\n'

        fun = fun + '\tdef PreStateAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'

        fun = fun + '\tdef StateAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'
        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# Read()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef PreReadOne' + self.end + \
            '(self, ' + self.ind + '):\n' + '\t\tpass\n'
        fun = fun + '\tdef ReadOne(self, ' + self.ind + '):\n'
        fun = fun + '\t\t#TODO\n'
        fun = fun + '\tdef PreReadAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'
        fun = fun + '\tdef ReadAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'

        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# Start/Stop()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef PreStartOne' + self.end
        if inherit.find("Motor") >= 0:
            fun = fun + '(self, ' + self.ind + ', pos):\n'
        else:
            fun = fun + '(self, ' + self.ind + '):\n'
        fun = fun + '\t\tpass\n'
        fun = fun + '\tdef StartOne' + self.end + \
            '(self, ' + self.ind + ', pos):\n'
        fun = fun + '\t\t#TODO\n'

        fun = fun + '\tdef AbortOne(self, ' + self.ind + '):\n'
        fun = fun + '\t\t#TODO\n'

        fun = fun + '\tdef StopOne(self, ' + self.ind + '):\n'
        fun = fun + '\t\tself.AbortOne(' + self.ind + ')\n'

        fun = fun + '\tdef PreStartAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'

        fun = fun + '\tdef StartAll' + self.end + '(self):\n'
        fun = fun + '\t\tpass\n'

        fun = fun + '\tdef AbortAll(self):\n'
        fun = fun + '\t\tpass\n'
        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# SetAxisPar/GetAxisPar()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef SetAxisPar(self, ' + self.ind + ', name, value):\n'
        fun = fun + '\t\t#TODO - Delete it if you don\'t need\n'

        fun = fun + '\tdef GetAxisPar(self, ' + self.ind + ', name):\n'
        fun = fun + '\t\t#TODO - Delete it if you don\'t need\n'
        text = text + fun

        fun = '# --------------------------------------------------------------------------\n' +\
            '# SetAxisExtraPar/GetAxisExtraPar()\n' +\
            '# --------------------------------------------------------------------------\n' +\
            '\tdef SetAxisExtraPar(self, ' + self.ind + ', name, value):\n'
        fun = fun + '\t\t#TODO - Delete it if you don\'t need\n'

        fun = fun + '\tdef GetAxisExtraPar(self, ' + self.ind + ', name):\n'
        fun = fun + '\t\t#TODO - Delete it if you don\'t need - \n'
        text = text + fun
        f.write(text)


def main():
    # Add MACRO_PATH
    filename = ""
    end = ""
    inherit = ""
    if(len(sys.argv) > 1):
        print "Creating " + sys.argv[1]
        filename = sys.argv[1]
        if(len(sys.argv) > 2):
            inherit = sys.argv[2]
        if(len(sys.argv) > 3):
            end = "CT"
        s = ControllerTemplate(filename, end)
        s.addHead()
        s.addIncludes(inherit)
        s.createBasicClass()
        s.createMainClass(inherit)
    else:
        print "Please introduce filename"

if __name__ == "__main__":
    main()