LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
TemplateWriter.py
Go to the documentation of this file.
1 from builtins import str
2 from builtins import object
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 import socket
26 
27 ##
28 # This class takes template files and substitutes the values for the given
29 # keys, writing a new file generated from the template.
30 #
31 
32 
33 class TemplateWriter(object):
34  """Takes templates and subtitutes the values for the given keys,
35  writing a new file generated from the template.
36  """
37 
38  def __init__(self):
39  # local values that are always set
40  self.orcaValues = dict()
41  self.orcaValues["ORCA_LOCAL_HOSTNAME"] = socket.gethostname()
42  return
43 
44  def rewrite(self, inputFile, outputFile, pairs):
45  """Given a input template, take the keys from the key/values in the config
46  object and substitute the values, and write those to the output file.
47  Parameters
48  ----------
49  inputFile : `str`
50  template input file
51  outputFile : `str`
52  resulting output file
53  pairs : `dict`
54  dictionary containing key/value pairs
55  """
56  fpInput = open(inputFile, 'r')
57  fpOutput = open(outputFile, 'w')
58 
59  while True:
60  line = fpInput.readline()
61  if len(line) == 0:
62  break
63 
64  # replace the "standard" orca names first
65  for name in self.orcaValues:
66  key = "$"+name
67  val = str(self.orcaValues[name])
68  line = line.replace(key, val)
69 
70  # replace the user defined names
71  for name in pairs:
72  key = "$"+name
73  val = str(pairs[name])
74  line = line.replace(key, val)
75  fpOutput.write(line)
76  fpInput.close()
77  fpOutput.close()
This class takes template files and substitutes the values for the given keys, writing a new file gen...