The Gaudi Framework  master (37c0b60a)
update_version.ReplacementRule Class Reference

Public Member Functions

def __init__ (self, Union[str, re.Pattern] pattern, Union[str, Callable[[str, Fields], str]] replace)
 
str __call__ (self, str line, Fields fields)
 

Public Attributes

 pattern
 
 replace
 

Detailed Description

Helper to replace lines with patterns or applying functions.

>>> r = ReplacementRule(r"^version: ", "version: {cmake_version}")
>>> f = Fields("v1r1", datetime.date(2023, 9, 25))
>>> r("nothing to change\\n", f)
'nothing to change\\n'
>>> r("version: 1.0\\n", f)
'version: 1.1\\n'

Definition at line 72 of file update_version.py.

Constructor & Destructor Documentation

◆ __init__()

def update_version.ReplacementRule.__init__ (   self,
Union[str, re.Pattern]  pattern,
Union[str, Callable[[str, Fields], str]]  replace 
)

Definition at line 84 of file update_version.py.

84  def __init__(
85  self,
86  pattern: Union[str, re.Pattern],
87  replace: Union[str, Callable[[str, Fields], str]],
88  ):
89  self.pattern = re.compile(pattern)
90  if isinstance(replace, str):
91  replace = f"{replace.rstrip()}\n"
92  self.replace = lambda _line, fields: replace.format(**fields.data)
93  else:
94  self.replace = replace
95 

Member Function Documentation

◆ __call__()

str update_version.ReplacementRule.__call__ (   self,
str  line,
Fields  fields 
)

Definition at line 96 of file update_version.py.

96  def __call__(self, line: str, fields: Fields) -> str:
97  if self.pattern.match(line):
98  return self.replace(line, fields)
99  return line
100 
101 

Member Data Documentation

◆ pattern

update_version.ReplacementRule.pattern

Definition at line 85 of file update_version.py.

◆ replace

update_version.ReplacementRule.replace

Definition at line 88 of file update_version.py.


The documentation for this class was generated from the following file: