This file is indexed.

/usr/lib/python2.7/dist-packages/code_saturne/autovnv/Parser.py is in code-saturne-data 4.3.3+repack-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2016 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Standard modules import
#-------------------------------------------------------------------------------

import os, sys, logging
from xml.dom import minidom
from xml.sax.handler import ContentHandler
from xml.sax import make_parser

#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------

logging.basicConfig()
log = logging.getLogger(__file__)
log.setLevel(logging.NOTSET)
#log.setLevel(logging.DEBUG)

#-------------------------------------------------------------------------------
# Checker of XML file syntax
#-------------------------------------------------------------------------------

def xmlChecker(filename):
    """Try to open the xml file, and return a message if an error occurs.

    @param filename name of the file of parameters ith its absolute path
    @return m error message
    """
    m = ""

    try:
        p = make_parser()
        p.setContentHandler(ContentHandler())
        p.parse(filename)
    except Exception as e:
        f = os.path.basename(filename)
        m = "%s file reading error. \n\n"\
            "This file is not in accordance with XML specifications.\n\n"\
            "The parsing syntax error is:\n\n%s" % (f, e)

    return m

#-------------------------------------------------------------------------------
# Reader of the XML file
#-------------------------------------------------------------------------------

class Parser(object):
    """ Parser -- class to parse XML file."""

    def __init__ (self, XMLFileName):
        """
        Constructor of the XML reader.
        @type XMLFileName: C{String}
        @param XMLFileName: name of the xml file
        """
        self.filename = XMLFileName

        try:
            self.doc =  minidom.parse(XMLFileName)
        except:
            print("No file of parameters or error in the name of the file or error in the syntax of the xml.\n")
            msg =  xmlChecker(self.filename)
            if msg:
                print(msg)
            sys.exit(1)

        self.root = self.doc.firstChild

        if self.root.nodeName != "autovnv":
            print(XMLFileName + ": Wrong XML file. The Root markup is not autovnv.\n")
            sys.exit(1)


    def write(self):
        return self.doc.toxml()


    def __checkDirectory(self, d):
        """
        Test if the given directory exists.
        @type d: C{String}
        @param d: name of the directory
        @rtype: C{String}
        @return: the directory
        """
        log.debug("__checkDirectory(): %s" % d)
        dir = os.path.abspath(d)
        if os.path.isdir(dir):
            log.debug("__checkDirectory(): %s" % dir)
            return dir
        else:
            print("Directory: %s does not exist." % dir)
            sys.exit(1)


    def getDataFromNode(self, father, childName):
        """
        @type father: C{DOM element}
        @param father: father node
        @type childName: C{String}
        @param childName: label of a markup, child of the father node
        @rtype: C{String}
        @return: data from the markup I{childName}
        """
        data = None
        l = father.getElementsByTagName(childName)

        if len(l) == 1:
            current = l.item(0)
            data = current.firstChild.data
        else:
            print("Error: in getDataFromNode several markup %s found." %  childName)
            sys.exit(1)

        return data


    def addChild(self, father, childname):
        """
        Add a new node as a child of the father node.
        @type father: C{DOM element}
        @param father: father node
        @type childName: C{String}
        @param childName: label of a markup, child of the father node
        @rtype: C{DOM element}
        @return: new node I{childName}
        """
        return father.appendChild(self.doc.createElement(childname))


    def getChild(self, father, childname):
        """
        Return a node as a child of the father node.
        @type father: C{DOM element}
        @param father: father node
        @type childName: C{String}
        @param childName: label of a markup, child of the father node
        @rtype: C{DOM element}
        @return: new node I{childName}
        """
        l = father.getElementsByTagName(childname)

        if len(l) == 0:
            return None
        elif len(l) == 1:
            return l.item(0)
        else:
            print("Error: in getChild several markup %s found." %  childname)
            sys.exit(1)


    def getChilds(self, father, childname):
        """
        Return a list of child of the father node.
        @type father: C{DOM element}
        @param father: father node
        @type childName: C{String}
        @param childName: label of a markup, childs of the father node
        @rtype: C{List} of C{DOM element}
        @return: list of nodes I{childName}
        """
        return father.getElementsByTagName(childname)


    def getRepository(self):
        """
        @rtype: C{String}
        @return: repository directory of all studies.
        """
        return self.__checkDirectory(self.getDataFromNode(self.root, "repository"))


    def getDestination(self):
        """
        @rtype: C{String}
        @return: destination directory of all studies.
        """
        return self.getDataFromNode(self.root, "destination")


    def getStudiesLabel(self):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE' status='on' compute="on" post="on"/>
            </study>

        @rtype: C{List} of C{String}
        @return: list of the label of all studies.
        """
        labels = []

        for node in self.root.getElementsByTagName("study"):
            label  = str(node.attributes["label"].value)
            status = str(node.attributes["status"].value)
            if status == 'on':
                if label not in labels:
                    labels.append(label)
                else:
                    print("Study: %s is repeted in the xml file of paramaters" % label)
                    sys.exit(1)
        return labels


    def getStudyNode(self, l):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE' status='on' compute="on" post="on"/>
            </study>

        @type l: C{String}
        @param l: label of a study
        @rtype: C{DOM element}
        @return: node of the xml document for study I{l}
        """
        node = None

        for n in self.root.getElementsByTagName("study"):
            label = str(n.attributes["label"].value)
            if label == l:
                if str(n.attributes["status"].value) != "on":
                    raise ValueError("Error: the getStudyNode method is used with the study %s turned off " % l)
                node = n
                break

            if not n:
                raise ValueError("Error: the getStudyNode does not found the node of the study %s" % l)

        return node


    def getCasesLabel(self, l, attr=None):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE' status='on' compute="on" post="on"/>
            </study>

        @type l: C{String}
        @param l: label of a study
        @type attr: C{String}
        @param attr: attribute I{compute} or I{post}
        @rtype: C{List} of C{String}
        @return: list of cases for study I{l}
        """
        labels = []

        for node in self.getStudyNode(l).getElementsByTagName("case"):
            status = str(node.attributes["status"].value)
            if status == 'on':
                labels.append(str(node.attributes["label"].value))

                if attr and str(node.attributes[attr].value) != 'on':
                    labels.pop()

        return labels


    def getCasesKeywords(self, l):
        """
        Read N_PROCS and USER_INPUT_FILES in:
            <study label='STUDY' status='on'>
                <case label='CASE1' run_id ="Grid 1" status='on' compute="on" post="on"/>
                <case label='CASE2' status='on' compute="on" post="on" compare="on"/>
            </study>
        @type l: C{String}
        @param l: label of a study
        @rtype: C{Dictionary}
        @return: keywords and value.
        """
        data = []

        for node in self.getStudyNode(l).getElementsByTagName("case"):
            if str(node.attributes["status"].value) == 'on':
                d = {}
                d['node']    = node
                d['label']   = str(node.attributes["label"].value)
                d['compute'] = str(node.attributes["compute"].value)
                d['post'] = str(node.attributes["post"].value)

                try:
                    d['compare'] = str(node.attributes["compare"].value)
                except:
                    d['compare'] = "on"

                try:
                    d['run_id'] = str(node.attributes["run_id"].value)
                except:
                    d['run_id'] = ""

                for n in node.childNodes:
                    if n.nodeType == minidom.Node.ELEMENT_NODE and n.childNodes:
                        if n.tagName not in ("compare", "prepro", "script", "data"):
                            d[n.tagName] = n.childNodes[0].data

                data.append(d)
        return data


    def setAttribute(self, node, attr, v):
        """
        Change:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="on" post="on"/>
            </study>
        To:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="off" post="on"/>
            </study>

        @type l: C{DOM Element}
        @param l: node of the I{attribute} to change
        @type attr: C{String}
        @param attr: attribute I{compute} or I{post}
        @type v: C{String}
        @param v: value of the attribute
        """
        node.attributes[attr].value = v

        f = os.path.join(self.getDestination(), self.filename)
        writer = open(f, mode="w" )
        self.doc.writexml(writer)
        writer.close()


    def getCompare(self, caseNode):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="on" post="on">
                    <compare repo="" dest="" args='--section Pressure --threshold 1e-2' status="on"/>
                </case>
            </study>
        @type caseNode: C{DOM Element}
        @param caseNode: node of the current case
        @rtype: C{True} or C{False}, C{String}, C{String}, C{Float}, C{String}
        @return: if the cs_io_dump/compare markup exists, and value of the threshold
        """
        compare, nodes, threshold, args, repo, dest = [], [], [], [], [], []

        for node in caseNode.getElementsByTagName("compare"):
            try:
                if str(node.attributes["status"].value) == 'on':
                    compare.append(True)
                else:
                    compare.append(False)
            except:
                compare.append(True)
            nodes.append(node)
            repo.append(str(node.attributes["repo"].value))
            dest.append(str(node.attributes["dest"].value))
            try:
                args.append(str(node.attributes["args"].value))
            except:
                args.append(None)
            try:
                threshold.append(str(node.attributes["threshold"].value))
            except:
                threshold.append(None)

        return compare, nodes, repo, dest, threshold, args


    def getPrepro(self, caseNode):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="on" post="on">
                    <prepro label="script_pre.py" args="" status="on"/>
                </case>
            </study>
        @type caseNode: C{DOM Element}
        @param caseNode: node of the current case
        """
        prepro, label, nodes, args = [], [], [], []

        for node in caseNode.getElementsByTagName("prepro"):
            if str(node.attributes["status"].value) == 'on':
                prepro.append(True)
            else:
                prepro.append(False)

            label.append(str(node.attributes["label"].value))
            nodes.append(node)
            try:
                args.append(str(node.attributes["args"].value))
            except:
                args.append("")

        return prepro, label, nodes, args


    def getScript(self, caseNode):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="on" post="on">
                    <script label="script_post.py" args="" dest="20110216-2147" status="on"/>
                </case>
            </study>
        @type caseNode: C{DOM Element}
        @param caseNode: node of the current case
        """
        script, label, nodes, args, repo, dest = [], [], [], [], [], []

        for node in caseNode.getElementsByTagName("script"):
            if str(node.attributes["status"].value) == 'on':
                script.append(True)
            else:
                script.append(False)

            label.append(str(node.attributes["label"].value))
            nodes.append(node)
            try:
                args.append(str(node.attributes["args"].value))
            except:
                args.append("")
            try:
                repo.append(str(node.attributes["repo"].value))
            except:
                repo.append(None)
            try:
                dest.append(str(node.attributes["dest"].value))
            except:
                dest.append(None)

        return script, label, nodes, args, repo, dest


    def getResult(self, node):
        """
        @type node: C{DOM Element}
        @param node: node of the current case
        @rtype: C{List}, C{List}
        @return: C{List} of nodes <resu>, and C{List} of file names
        """
        f  = str(node.attributes["file"].value)

        if node.tagName == "data":
            plots = node.getElementsByTagName("plot")
        elif node.tagName == "resu":
            plots = node.getElementsByTagName("scalar")
        else:
            plots = []

        try:
            dest  = str(node.attributes["dest"].value)
        except:
            dest = None
        try:
            repo = str(node.attributes["repo"].value)
        except:
            repo = None

        return plots, f, dest, repo


    def getInput(self, node):
        """
        @type node: C{DOM Element}
        @param node: node of the current case
        @rtype: C{List}, C{List}
        @return: C{List} of nodes <input>, and C{List} of file names
        """
        f  = str(node.attributes["file"].value)

        try:
            dest  = str(node.attributes["dest"].value)
        except:
            dest = None
        try:
            repo = str(node.attributes["repo"].value)
        except:
            repo = None
        try:
            tex = str(node.attributes["tex"].value)
        except:
            tex = None

        return  f, dest, repo, tex


    def getProbes(self, node):
        """
        Read:
            <probes file='monitoring_pressure.dat' dest="" fig="3"/>

        @type node: C{DOM Element}
        @param node: node of the current case
        """
        f  = str(node.attributes["file"].value)
        try:
            dest  = str(node.attributes["dest"].value)
        except:
            dest = None
        try:
            fig  = str(node.attributes["fig"].value)
        except:
            fig = None

        return f, dest, fig


    def getMeasurement(self, l):
        """
        Return the list of files of measurements.
        @type l: C{String}
        @param l: label of a study
        @rtype: C{List}
        @return: C{List} of list of nodes <plot>, and C{List} of files of measurements
        """
        nodes = []
        files = []

        for node in self.getStudyNode(l).getElementsByTagName("measurement"):
            nodes.append(node.getElementsByTagName("plot"))
            fileName = node.attributes["file"].value
            filePath = node.attributes["path"].value

            if filePath == "":
              for root, dirs, fs in os.walk(os.path.join(self.getRepository(), l)):
                  if fileName in fs:
                      filePath = root
                      break
            else: # for Code_Saturne exp data are supposed to be in POST
              for root, dirs, fs in os.walk(os.path.join(self.getRepository(), l, 'POST', filePath)):
                  if fileName in fs:
                      filePath = root
                      break

            files.append(os.path.join(filePath, fileName))

        return nodes, files


    def getPostPro(self, l):
        """
        Read:
            <study label='STUDY' status='on'>
                <case label='CASE1' status='on' compute="on" post="on"/>
                <postpro label="script_post.py" args="" status="on">
                    <data file="profile.dat">
                        <plot fig="1" xcol="1" ycol="2" legend="Grid 1"/>
                    </data>
                </postpro>
            </study>

        Return the list of files of postpro.
        @type l: C{String}
        @param l: label of a study
        @rtype: C{List}
        @return: C{List} of list of nodes <postpro>
        """
        scripts, labels, nodes, args = [], [], [], []

        for node in self.getStudyNode(l).getElementsByTagName("postpro"):
            if str(node.attributes["status"].value) == 'on':
                scripts.append(True)
            else:
                scripts.append(False)

            labels.append(str(node.attributes["label"].value))
            nodes.append(node)
            try:
                args.append(str(node.attributes["args"].value))
            except:
                args.append("")

        return scripts, labels, nodes, args


    def getSubplots(self, studyLabel):
        return self.getStudyNode(studyLabel).getElementsByTagName("subplot")


    def getFigures(self, studyLabel):
        return self.getStudyNode(studyLabel).getElementsByTagName("figure")


    def getPltCommands(self, node):
        """
        """
        cmd = []
        for n in node.getElementsByTagName("plt_command"):
            if n.nodeType == minidom.Node.ELEMENT_NODE and n.childNodes:
                if n.tagName == "plt_command":
                    cmd.append(n.childNodes[0].data)
        return cmd


    def getVTKCommands(self, node):
        """
        """
        cmd = []
        for n in node.getElementsByTagName("vtk_command"):
            if n.nodeType == minidom.Node.ELEMENT_NODE and n.childNodes:
                if n.tagName == "vtk_command":
                    cmd.append(n.childNodes[0].data)
        return cmd


    def getAttributes(self, node):
        """
        Return a dictionary with attributes and value of a node.
        """
        d = {}
        for k in node.attributes.keys():
            d[k] = node.attributes[k].value
        return d


    def getAttribute(self, node, k, default = None):
        """
        Return a value of an attribute.
        """
        if k in node.attributes.keys():
            return node.attributes[k].value
        else:
            if default == None:
                raise ValueError("Error: attribute %s is mandatory!" % k)
            else:
                return default


    def getAttributeTuple(self, node, k, default = None):
        """
        Return a value of an attribute.
        """
        if k in node.attributes.keys():
            n = node.attributes[k].value
            return tuple(float(s) for s in n[1:-1].split(','))
        else:
            if default == None:
                raise ValueError("Error: attribute %s is mandatory!" % k)
            else:
                return default


#-------------------------------------------------------------------------------