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

15 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-13 10:34 +0000

1#!/usr/bin/env python 

2 

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# 

24 

25# This class takes template files and substitutes the values for the given 

26# keys, writing a new file generated from the template. 

27# 

28 

29 

30class TemplateWriter: 

31 """Class to take a template file, substitute values through it, and 

32 write a new file with those values. 

33 """ 

34 

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") 

44 

45 while True: 

46 line = fpInput.readline() 

47 if len(line) == 0: 

48 break 

49 

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()