Coverage for python/lsst/ctrl/execute/templateWriter.py : 11%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
3#
4# LSST Data Management System
5# Copyright 2008-2016 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#
25# This class takes template files and substitutes the values for the given
26# keys, writing a new file generated from the template.
27#
30class TemplateWriter:
31 """Class to take a template file, substitute values through it, and
32 write a new file with those values.
33 """
35 def rewrite(self, input, output, pairs):
36 """Given a input template, take the keys from key/values in the config
37 object and substitute the values, and write those to the output file.
38 @param input - the input template name
39 @param output - the output file name
40 @param pairs of values to substitute in the template
41 """
42 fpInput = open(input, 'r')
43 fpOutput = open(output, 'w')
45 while True:
46 line = fpInput.readline()
47 if len(line) == 0:
48 break
50 # replace the user defined names
51 for name in pairs:
52 key = "$"+name
53 val = str(pairs[name])
54 line = line.replace(key, val)
55 fpOutput.write(line)
56 fpInput.close()
57 fpOutput.close()