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

11 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-10-10 01:46 -0700

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 

25from lsst.resources import ResourcePath, ResourcePathExpression 

26 

27 

28class TemplateWriter: 

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

30 write a new file with those values. 

31 """ 

32 

33 def rewrite(self, input: ResourcePathExpression, output: ResourcePathExpression, pairs): 

34 """Given a input template, take the keys from key/values in the config 

35 object and substitute the values, and write those to the output file. 

36 @param input - the input template name 

37 @param output - the output file name 

38 @param pairs of values to substitute in the template 

39 """ 

40 with ResourcePath(output).open(mode="w") as f_out: 

41 with ResourcePath(input).open(mode="r") as f_in: 

42 for line in f_in.readlines(): 

43 # replace the user defined names 

44 for name in pairs: 

45 key = "$" + name 

46 val = str(pairs[name]) 

47 line = line.replace(key, val) 

48 f_out.write(line)