Skip to content

Commit

Permalink
added the actual writing to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 29, 2024
1 parent 1a79a82 commit 81ce161
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def is_expression(_checker: TypeChecker, instance: Any) -> bool:
def is_natural_language_map(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, NaturalLanguageMap)


def is_programming_language_map(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, ProgrammingLanguageMap)

Expand Down
30 changes: 21 additions & 9 deletions tested/nat_translation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from pathlib import Path

import yaml

Expand Down Expand Up @@ -329,13 +330,21 @@ def translate_dsl(dsl_object: YamlObject, language: str) -> YamlObject:
return dsl_object


def parse_yaml(yaml_path: str) -> YamlObject:
def parse_yaml(yaml_path: Path) -> YamlObject:
with open(yaml_path, "r") as stream:
result = _parse_yaml(stream.read())

Check warning on line 335 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L334-L335

Added lines #L334 - L335 were not covered by tests

return result

Check warning on line 337 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L337

Added line #L337 was not covered by tests


def generate_new_yaml(yaml_path: Path, yaml_string: str, language: str):
file_name = yaml_path.name
split_name = file_name.split(".")
path_to_new_yaml = yaml_path.parent / f"{'.'.join(split_name[:-1])}-{language}.yaml"
with open(path_to_new_yaml, "w") as yaml_file:
yaml_file.write(yaml_string)

Check warning on line 345 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L341-L345

Added lines #L341 - L345 were not covered by tests


def convert_to_yaml(yaml_object: YamlObject) -> str:
def oracle_representer(dumper, data):
return dumper.represent_mapping("!oracle", data)
Expand All @@ -349,15 +358,18 @@ def expression_representer(dumper, data):
return yaml.dump(yaml_object, sort_keys=False)


if __name__ == "__main__":
n = len(sys.argv)
assert n > 1, "Expected atleast two argument (path to yaml file and language)."

path = sys.argv[1]
lang = sys.argv[2]
def run(path: Path, language: str):
new_yaml = parse_yaml(path)
validate_pre_dsl(new_yaml)
translated_dsl = translate_dsl(new_yaml, lang)
translated_dsl = translate_dsl(new_yaml, language)
yaml_string = convert_to_yaml(translated_dsl)
print(yaml_string)
_validate_dsl(_parse_yaml(yaml_string))

Check warning on line 366 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L362-L366

Added lines #L362 - L366 were not covered by tests

generate_new_yaml(path, yaml_string, language)

Check warning on line 368 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L368

Added line #L368 was not covered by tests


if __name__ == "__main__":
n = len(sys.argv)
assert n > 1, "Expected atleast two argument (path to yaml file and language)."

Check warning on line 373 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L372-L373

Added lines #L372 - L373 were not covered by tests

run(Path(sys.argv[1]), sys.argv[2])

Check warning on line 375 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L375

Added line #L375 was not covered by tests
9 changes: 7 additions & 2 deletions tests/test_dsl_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
)
from tested.dsl import parse_dsl, translate_to_test_suite
from tested.dsl.translate_parser import _parse_yaml, load_schema_validator
from tested.nat_translation import convert_to_yaml, parse_value, translate_dsl, \
validate_pre_dsl
from tested.nat_translation import (
convert_to_yaml,
parse_value,
translate_dsl,
validate_pre_dsl,
)
from tested.serialisation import (
FunctionCall,
NumberType,
Expand Down Expand Up @@ -1575,6 +1579,7 @@ def test_translate_parse():
parsed_result = parse_value(value, flattened_stack)
assert parsed_result == expected_value


def test_wrong_natural_translation_suite():
yaml_str = """
tabs:
Expand Down

0 comments on commit 81ce161

Please sign in to comment.