Skip to content

Commit

Permalink
Fix off-by-one padding issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Jan 22, 2024
1 parent 54877be commit 52641f4
Showing 1 changed file with 20 additions and 50 deletions.
70 changes: 20 additions & 50 deletions metricflow/dag/dag_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,9 @@ def _max_width_tracker(self) -> MaxWidthTracker: # noqa: D
return self._thread_local_data.max_width_tracker

def _displayed_property_on_one_line(self, displayed_property: DisplayedProperty) -> str:
return jinja2.Template(
textwrap.dedent(
"""\
<!-- {{ key }} = {{ value }} -->
"""
),
undefined=jinja2.StrictUndefined,
).render(
key=displayed_property.key,
value=mf_pformat(displayed_property.value, max_line_length=self._max_width_tracker.current_max_width),
)
key = displayed_property.key
value = mf_pformat(displayed_property.value, max_line_length=self._max_width_tracker.current_max_width)
return f"<!-- {key} = {value} -->"

def _format_to_text(self, node: DagNode, inner_contents: Optional[str]) -> str:
"""Convert the given node to the text representation.
Expand Down Expand Up @@ -117,50 +109,28 @@ def _format_to_text(self, node: DagNode, inner_contents: Optional[str]) -> str:
max_value_str_length = max([len(x) for x in value_str_split])

# Print the key on multiple lines.
node_fields.append(
jinja2.Template(
textwrap.dedent(
"""\
<!-- {{ key }} = {{ padding }} -->
"""
),
undefined=jinja2.StrictUndefined,
).render(
key=displayed_property.key,
padding=" "
* (
(len("<!-- ") + len(self._value_indent_prefix) + max_value_str_length + len(" -->"))
- len("<!-- ")
- len(displayed_property.key)
- len(" =")
- len(" -->")
),
)
key = displayed_property.key
# Add padding so that all fields of this object have <!-- and --> that align.
key_padding = " " * (
(len("<!-- ") + len(self._value_indent_prefix) + max_value_str_length + len(" -->"))
- len("<!-- ")
- len(key)
- len(" = ")
- len(" -->")
)

node_fields.append(f"<!-- {key} = {key_padding} -->")

# Print the lines for the value in an indented section.
for value_str in value_str_split:
node_fields.append(
jinja2.Template(
textwrap.dedent(
"""\
<!-- {{ indent_prefix }}{{ value }} {{ padding }} -->
"""
),
undefined=jinja2.StrictUndefined,
).render(
indent_prefix=self._value_indent_prefix,
value=value_str,
padding=" "
* (
(len("<!-- ") + len(self._value_indent_prefix) + max_value_str_length + len(" -->"))
- len("<!-- ")
- len(self._value_indent_prefix)
- len(value_str)
- len(" -->")
),
)
value_padding = " " * (
(len("<!-- ") + len(self._value_indent_prefix) + max_value_str_length + len(" -->"))
- len("<!-- ")
- len(self._value_indent_prefix)
- len(value_str)
- len(" -->")
)
node_fields.append(f"<!-- {self._value_indent_prefix}{value_str}{value_padding} -->")

return jinja2.Template(
textwrap.dedent(
Expand Down

0 comments on commit 52641f4

Please sign in to comment.