def fill_each(field_inner_content: str, field_value: str, template: Template, indenting: bool = False) -> int: """ Populate all matching template fields in the template. Matches are determined by comparing the inner content of each field with the provided content. """ # make sure that we have a sane value field_value = field_value if field_value is not None else '' # template fields are always represented by wrapping {{ }}'s, # however, both {{my_field}} and {{ my_field }} should be valid; # i.e. any leading or trailing whitespace should simply be ignored field_search = r'{{\s*' + field_inner_content + r'\s*}}' # find any occurences of the field (case-sensitive) search = re.compile(field_search) if indenting: match = search.search(template.content) if match is not None: # we only need the start index start_index = match.span()[0] field_value = get_padded_string(field_value, template.content, start_index) # finally replace any found occurences of the template field with its value template.content, occurences = search.subn(field_value, template.content) return occurences
def fill(field: TemplateField, field_value: str, template: Template, indenting: bool=False) -> None: """ Populate a single template field in the template. """ start_index = field.indices.start end_index = field.indices.stop if ((start_index < 0 or start_index > len(template.content)) or (end_index < 0 or end_index > len(template.content))): raise ValueError('Template field \'{0}\' out of range ({1}-{2}).' .format(field.inner_content, start_index, end_index)) if indenting: field_value = get_padded_string( field_value, template.content, field.indices.start) template.content = template.content[:start_index] + field_value + template.content[end_index:]
def fill(field: TemplateField, field_value: str, template: Template, indenting: bool = False) -> None: """ Populate a single template field in the template. """ start_index = field.indices.start end_index = field.indices.stop if ((start_index < 0 or start_index > len(template.content)) or (end_index < 0 or end_index > len(template.content))): raise ValueError( 'Template field \'{0}\' out of range ({1}-{2}).'.format( field.inner_content, start_index, end_index)) if indenting: field_value = get_padded_string(field_value, template.content, field.indices.start) template.content = template.content[: start_index] + field_value + template.content[ end_index:]
def fill_each(field_inner_content: str, field_value: str, template: Template, indenting: bool=False) -> int: """ Populate all matching template fields in the template. Matches are determined by comparing the inner content of each field with the provided content. """ # make sure that we have a sane value field_value = field_value if field_value is not None else '' # template fields are always represented by wrapping {{ }}'s, # however, both {{my_field}} and {{ my_field }} should be valid; # i.e. any leading or trailing whitespace should simply be ignored field_search = r'{{\s*' + field_inner_content + r'\s*}}' # find any occurences of the field, using a case-insensitive # comparison, to ensure that e.g. {{name}} is populated with the # value from column "Name", even though the casing might differ search = re.compile(field_search, re.IGNORECASE) if indenting: match = search.search(template.content) if match is not None: # we only need the start index start_index = match.span()[0] field_value = get_padded_string(field_value, template.content, start_index) # finally replace any found occurences of the template field with its value template.content, occurences = search.subn(field_value, template.content) return occurences