File: PovRenderer.py

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (256 lines) | stat: -rw-r--r-- 7,996 bytes parent folder | download
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
#############################################################
##                                                         ##
## Copyright (c) 2003-2011 by The University of Queensland ##
## Earth Systems Science Computational Centre (ESSCC)      ##
## http://www.uq.edu.au/esscc                              ##
##                                                         ##
## Primary Business: Brisbane, Queensland, Australia       ##
## Licensed under the Open Software License version 3.0    ##
## http://www.opensource.org/licenses/osl-3.0.php          ##
##                                                         ##
#############################################################

"""
Defines various classes which control the parameters (arguments)
for running of a povray process.
"""
import string
import os
import tempfile

import esys.lsm.Logging
from   esys.lsm.vis.core.Exception import raiseNotImplemented
from   esys.lsm.vis                import core
from   esys.lsm.util               import Process, PathSearcher

logger = esys.lsm.Logging.getLogger("esys.lsm.vis.povray")

class Option:
    def __init__(self, name):
        self.name  = name

    def getName(self):
        return self.name

    def getArgList(self):
        raiseNotImplemented("Implement this method in derived class.")

class SingleOption(Option):
    def __init__(self, name, arg, val = None):
        Option.__init__(self, name)
        self.arg   = arg
        self.value = val

    def getArg(self):
        return self.arg

    def getValue(self):
        return self.value

    def setValue(self, val):
        self.value = val

    def getArgList(self):
        if (self.getValue() != None):
            return ["+" + self.getArg() + str(self.getValue())]
        return []

class BoolOption(SingleOption):
    def __init__(self, name, arg, val = None):
        SingleOption.__init__(self, name, arg, val = None)

    def getValue(self):
        return bool(SingleOption.getValue(self))
                                            
    def getArgList(self):
        if (self.getValue() != None):
            argChar = "-"
            if (self.getValue()):
                argChar = "+"
            return [argChar + self.getArg()]

        return []

class BoolValOption(SingleOption):
    def __init__(self, name, arg, val = None):
        SingleOption.__init__(self, name, arg, val)

    def getArgList(self):
        if (self.getValue() != None):
            if (len(str(self.getValue())) > 0):
                return ["+" + self.getArg() + str(self.getValue())]
            else:
                return ["-" + self.getArg()]

        return []

class ListOption(SingleOption):
    def __init__(self, name, arg, val = None):
        SingleOption.__init__(self, name, arg)
        self.setValue([])

    def appendValue(self, val):
        self.getValue().append(val)

    def getArgList(self):
        return \
            [
                "+" + self.getArg() + str(val) \
                for val in self.getValue()
            ]

class DisplayPauseOption(Option):
    def __init__(self, name):
        Option.__init__(self, name)
        self.display = BoolOption("Display", "D", None)
        self.pause   = BoolOption("Pause", "P", None)
        self.noPause = BoolOption("Pause", "P", False)

    def setOffScreen(self, offScreen):
        self.display.setValue(not offScreen)

    def getOffScreen(self):
        return (not self.display.getValue())

    def setPause(self, pause):
        self.pause.setValue(pause)

    def getPause(self):
        return self.pause.getValue()

    def getArgList(self):
        argList = self.display.getArgList()
        if (self.getOffScreen()):
            argList += self.noPause.getArgList()
        else:
            argList += self.pause.getArgList()
        return argList

_formatCharDict = dict()
_formatCharDict[str(core.PNG)] = "N"
_formatCharDict[str(core.PNM)] = "P"

from esys.lsm.util import InstallInfo

class PovProcess(Process):
    def __init__(self, argList, combineStdOutAndStdErr=False):
        Process.__init__(
            self,
            InstallInfo.getPovrayExePath(),
            argList,
            combineStdOutAndStdErr,
            printOutput=False
        )

class PovSdlInput:
    def __init__(self):
        self.tempFile = tempfile.mkstemp()
        self.file = os.fdopen(self.tempFile[0], 'wa')
        self.fileName = self.tempFile[1]

    def __del__(self):
        self.close()
        os.remove(self.getFileName())

    def getFile(self):
        return self.file

    def getFileName(self):
        return self.fileName

    def close(self):
        if (self.file != None):
          self.file.close()
          self.file = None

class PovRenderer:
    def __init__(self):
        self.nameOptDict = dict()
        self.initialiseOptionDict()

    def initialiseOptionDict(self):
        self.nameOptDict["Quality"]          = SingleOption("Quality", "Q")
        self.nameOptDict["Quality"].setValue(11)
        self.nameOptDict["Antialias"]        = SingleOption("Antialias", "A")
        self.nameOptDict["Antialias"].setValue(0.1)
        self.nameOptDict["DisplayPause"]     = DisplayPauseOption("DisplayPause")
        self.nameOptDict["Height"]           = SingleOption("Height", "H")
        self.nameOptDict["Width"]            = SingleOption("Width", "W")
        self.nameOptDict["Input_File_Name"]  = SingleOption("Input_File_Name", "I")
        self.nameOptDict["Output_To_File"]   = \
            BoolValOption("Output_To_File", "F")
        self.nameOptDict["Output_File_Name"] = SingleOption("Output_File_Name", "O")
        self.nameOptDict["Library_Path"]     = ListOption("Library_Path", "L")
        self.nameOptDict["All_Console"]      = \
            BoolValOption("All_Console", "G", "A")

    def getOption(self, optionName):
        return self.nameOptDict[optionName]

    def setOffScreen(self, doOffScreen):
        self.getOption("DisplayPause").setOffScreen(doOffScreen)

    def getOffScreen(self):
        return self.getOption("DisplayPause").getOffScreen()

    def setInteractive(self, interactive):
        self.getOption("DisplayPause").setPause(interactive)

    def getInteractive(self):
        return self.getOption("DisplayPause").getPause()

    def getImageFormat(self, fileName):
        return core.getFormatFromExtension(fileName)

    def getImageFormatChar(self, imageFormat):
        return _formatCharDict[str(imageFormat)]

    def setFileName(self, fileName, imageFormat=None):
        self.getOption("Output_File_Name").setValue(fileName)
        if (fileName != None):
            if (imageFormat == None):
                imageFormat = self.getImageFormat(fileName)
            self.getOption("Output_To_File").setValue(
                self.getImageFormatChar(imageFormat)
            )
        else:
            self.getOption("Output_To_File").setValue("")

    def getFileName(self):
        return self.getOption("Output_File_Name").getValue()

    def setSize(self, size):
        self.getOption("Width").setValue(size[0])
        self.getOption("Height").setValue(size[1])

    def getSize(self):
        return \
          [
              self.getOption("Width").getValue(),
              self.getOption("Height").getValue()
          ]

    def clear(self):
        pass

    def getArgList(self):
        argList = []
        for opt in self.nameOptDict.values():
            argList += opt.getArgList()
        return argList

    def openInput(self):
        self.sdlInput = PovSdlInput()
        return self.sdlInput.getFile()

    def getInput(self):
        return self.sdlInput.getFile()

    def closeInput(self):
        self.sdlInput.close()
        self.getOption("Input_File_Name").setValue(self.sdlInput.getFileName())
        process = PovProcess(self.getArgList())
        logger.info("Running process: " + process.getCommandLine())
        process.run()
        logger.info("povray output = " + str(process.getStdOutLineList()))