Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various bug fixes in frontend and backend #55

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/spex/jsonspec/extractors/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from spex.jsonspec.extractors.regular_expressions import (
ELLIPSIS_LABEL_REGEX,
LABEL_NUMERICAL_REARRANGE_REGEX,
LABEL_VALIDATION_REGEX,
STRUCT_LABEL_REGEX,
)
Expand Down Expand Up @@ -65,6 +66,22 @@ def generate_acronym(text: str) -> str:
return "".join(w[0] for w in text.split()).lower()


def rearrange_num_label(label: str) -> str:
# Use regex to check if the string starts with a number followed by an underscore
match = LABEL_NUMERICAL_REARRANGE_REGEX.match(label)

if match:
# Extract the number and the rest of the string
num = match.group(1)
rest_of_string = match.group(2).strip("_")

# Return the rearranged string with the number at the end
return f"{rest_of_string}_{num}".strip("_")
else:
# If the string doesn't match the pattern, return the original string
return label


def normalize_label(label: str) -> str:
"""Normalize a input string by replacing specific characters

Expand Down
2 changes: 2 additions & 0 deletions src/spex/jsonspec/extractors/regular_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ def match(self, text: str) -> Tuple[Optional[str], Optional[str], Optional[str]]

LABEL_VALIDATION_REGEX = compile(r"^[a-zA-Z][\w]*$")

LABEL_NUMERICAL_REARRANGE_REGEX = compile(r"^(\d+)(.*)")

TABLE_ID_REGEX = compile(r"^Figure (?P<id>\d+):.*")
3 changes: 2 additions & 1 deletion src/spex/jsonspec/extractors/valuetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extract_content,
mapping_incomplete,
normalize_label,
rearrange_num_label,
validate_label,
)
from spex.jsonspec.extractors.regular_expressions import VALUE_LABEL_REGEX
Expand Down Expand Up @@ -267,7 +268,7 @@ def _extract_label(self, row: Element, row_key: str, data: Element) -> str:
else:
self.add_issue(LintErr.LBL_EXTRACT_ERR, row_key=row_key)
label = text

label = rearrange_num_label(label)
label = normalize_label(label)
validate_label(label, self.fig_id, row_key, self.linter)
return label
Expand Down
4 changes: 0 additions & 4 deletions src/spexsrv/templates/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ <h2 class="title">Figures:</h2>
<span x-text="v"></span>
</span>
</template>
<template x-if="lint_err.context.label">
<span class="tag is-size-6 has-text-weight-bold is-info"
x-text="lint_err.context.label"></span>
</template>
</div>
</div>
</div>
Expand Down
54 changes: 54 additions & 0 deletions tests/test_value_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,57 @@ def test_label_value_table_without_brief(
"fields": [{"val": "00b", "label": "TERTIARY_VERSION"}],
}
]


def test_label_value_table_with_num_prefix(
html_parser: Callable[[str, bool], List[EntityMeta]],
html_doc: Callable[[str], str],
html_table: Callable[[List[List[str]]], str],
):
table = [
["Value", "Definition"],
["0h", "1 microsecond"],
["1h", "10 microseconds"],
["2h", "100 microseconds"],
["3h", "1 millisecond"],
["4h", "10 milliseconds"],
["5h", "100 milliseconds"],
["6h", "1 second"],
["7h", "10 seconds"],
["8h", "100 seconds"],
["9h", "1,000 seconds"],
["Ah", "10,000 seconds"],
["Bh", "100,000 seconds"],
["Ch", "1,000,000 seconds"],
["Dh", "Reserved"],
["Eh", "Reserved"],
["Fh", "Reserved"],
]
html = html_doc(html_table(table))

json = html_parser(html, False)
assert json == [
{
"title": "Figure 0: Test",
"fig_id": "0",
"fields": [
{"val": "0h", "label": "MICROSECOND_1"},
{"val": "1h", "label": "MICROSECONDS_10"},
{"val": "2h", "label": "MICROSECONDS_100"},
{"val": "3h", "label": "MILLISECOND_1"},
{"val": "4h", "label": "MILLISECONDS_10"},
{"val": "5h", "label": "MILLISECONDS_100"},
{"val": "6h", "label": "SECOND_1"},
{"val": "7h", "label": "SECONDS_10"},
{"val": "8h", "label": "SECONDS_100"},
{"val": "9h", "label": "1"},
{"val": "ah", "label": "10"},
{"val": "bh", "label": "100"},
{"val": "ch", "label": "1"},
{"val": "dh", "label": "RESERVED"},
{"val": "eh", "label": "RESERVED"},
{"val": "fh", "label": "RESERVED"},
],
"type": "values",
}
]