-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added helper functions in helper.py. Added unittests for said helper …
…functions. Fixed license notice being a doc string.
- Loading branch information
1 parent
ffb871e
commit 4c46c55
Showing
5 changed files
with
172 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|