Skip to content

Commit

Permalink
Added helper functions in helper.py. Added unittests for said helper …
Browse files Browse the repository at this point in the history
…functions. Fixed license notice being a doc string.
  • Loading branch information
WyvernIXTL committed May 8, 2024
1 parent ffb871e commit 4c46c55
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load_template(path: str) -> Template:

return env.from_string(template_str)

def get_error_message(parsed_template) -> bool:
def get_error_message(parsed_template: str) -> str|None:
reg = re.compile(".*\[\[ERROR\]\]\s*(?P<errmsg>.*?)\s*\[\[ERROR\]\].*", re.S)
matches = reg.search(parsed_template)
if matches is None:
Expand Down
22 changes: 10 additions & 12 deletions installation_instruction/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
"""
Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege
# Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from importlib import metadata
Expand Down
78 changes: 78 additions & 0 deletions installation_instruction/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from jinja2 import Environment, Template


def _get_error_message_from_string(string: str) -> str|None:
"""
Parses via regex and returns error message enquoted in "[[ERROR]]". if no error message is found returns `None`.
:param string: This is the raw input string where an error message might be.
:type string: str
:return: Error message if found else None.
:rtpye: str or None
"""
reg = re.compile(r".*\[\[ERROR\]\]\s*(?P<errmsg>.*?)\s*\[\[ERROR\]\].*", re.S)
matches = reg.search(string)
if matches is None:
return None
return matches.group("errmsg")

def _replace_whitespace_in_string(string: str) -> str:
"""
Replaces eol and whitespaces of a string with a single whitespace.
:param string: String to be processed.
:type string: str
:return: String where whitespace and eol is replaced with one whitespace and whitspace before and after are stripped.
:rtype: str
"""
return re.sub(r"\s{1,}", " ", string, 0, re.S).strip()

def _split_string_at_delimiter(string: str) -> tuple[str, str]:
"""
Extracts part before and after the delimiter "------" or more.
:param string: The string with a delimiter.
:type string: str
:raise Exception: If no delimiter is found.
:return: Returns a tuple with the part before and after the delimiter.
:rtype: tuple[str, str]
"""
reg = re.compile(r"^\s*(?P<schema>.*?)\s*\-{6,}\s*(?P<template>.*?)\s*$", re.S)
matches = reg.search(string)
if matches is None:
raise Exception("No delimiter (------) found.")
print("HERE ", matches.group("template"))
return (
matches.group("schema"),
matches.group("template")
)

def _load_template_from_string(string: str) -> Template:
"""
Returns `jinja2.Template`.
:param string: String to be processed.
:type string: str
:return: jinja2 Template object.
:rtype: jinja2.Template
"""
env = Environment(
trim_blocks=True,
lstrip_blocks=True
)
return env.from_string(string)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ classifiers = [
"License :: OSI Approved :: Apache-2.0 License",
]
dependencies = [
"jinja2",
"jsonschema",
"yaml"
]

[project.optional-dependencies]
Expand Down
80 changes: 80 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2024 Adam McKellar, Kanushka Gupta, Timo Ege

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from jinja2 import Template
from installation_instruction import helpers


def test_get_error_message_from_string_with_err_message():
err_string = """
abcd
[[ERROR]]
Mac does not support ROCm or CUDA!
[[ERROR]]
efg
"""
msg = helpers._get_error_message_from_string(err_string)
assert msg == "Mac does not support ROCm or CUDA!"

def test__get_error_message_from_string_without_err_message():
err_string = """
abcd
efg
"""
msg = helpers._get_error_message_from_string(err_string)
assert msg is None

def test_replace_whitespace_in_string():
string = """
python -m pip
install
installation_instruction
"""
res = helpers._replace_whitespace_in_string(string)
assert res == "python -m pip install installation_instruction"

def test_split_string_at_delimiter_with_delimiter():
string = """
os:
- win
- mac
something:
------
{% if os == 'win' %}
do stuff
{% endif %}
"""

schema = """os:
- win
- mac
something:"""

template = """{% if os == 'win' %}
do stuff
{% endif %}"""

(parsed_schema, parsed_template) = helpers._split_string_at_delimiter(string)
assert schema == parsed_schema
assert template == parsed_template


0 comments on commit 4c46c55

Please sign in to comment.