Skip to content

Commit

Permalink
Issue544 link check (#545)
Browse files Browse the repository at this point in the history
* Added method that searches for broken hyperlinks

For #544
  • Loading branch information
mwetter committed Apr 15, 2024
1 parent 8f63e91 commit bb97ae1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
6 changes: 5 additions & 1 deletion buildingspy/CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
BuildingsPy Changelog
---------------------

Version 5.1.1, xxxx
Version 5.2.0, xxxx
^^^^^^^^^^^^^^^^^^^

- In buildingspy/development/validator.py, added method validateHyperlinks()
that searches for broken hyperlinks.
(https://github.com/lbl-srg/BuildingsPy/issues/544)
- For Optimica regression tests, added check for connector to itself
(https://github.com/lbl-srg/BuildingsPy/issues/504)
- In buildingspy/development/refactor.py, updated writing the Modelica
Expand Down
2 changes: 1 addition & 1 deletion buildingspy/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.1.dev1
5.2.dev0
55 changes: 55 additions & 0 deletions buildingspy/development/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,58 @@ def validateExperimentSetup(self, root_dir):
s = ("The number of tolerances in the mos files={!s} does no match " +
"the number of mo files={!s}.\n").format(n_tols, n_mo_files)
raise ValueError(s)

def validateHyperlinks(self, root_dir):
"""
This function recursively searches in all ``.mo`` files
in a package for broken Modelica hyperlinks.
It searches for strings of the form
``\"modelica://some/file/name.png\"``
and then checks whether `some/file/name.png` exits.
If it does not exist, it returns an entry in the
string of error messages `str`.
:param rootDir: The root directory of the package.
:return: str[] Error messages.
Usage: Type
>>> import os
>>> import buildingspy.development.validator as v
>>> val = v.Validator()
>>> myMoLib = os.path.join(\
"buildingspy", "tests", "MyModelicaLibrary")
>>> # Check the library for broken links
>>> errStr = val.validateHyperlinks(myMoLib)
"""

def _validateHyperlinks(root_dir, extensions):
import re
import os

# Get all mo files
files = self._recursive_glob(root_dir, '.mo')

errMsg = list()
for extension in extensions:
for ff in files:
with open(ff, 'r') as file:
lines = file.readlines()

line_no = 0

for line in lines:
line_no += 1
match = re.search(fr'\\"modelica:\/\/(?P<image>.*{extension})\\"', line)
if match is not None:
fn = (match.group('image'))
if not os.path.isfile(os.path.join("..", fn)):
msg = f"{ff}:{line_no}: Referenced file does not exist: {fn}"
errMsg.append(msg)

return errMsg

errMsg = _validateHyperlinks(root_dir, [".png", ".pdf"])
return errMsg
38 changes: 38 additions & 0 deletions buildingspy/tests/test_development_Validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,44 @@ def test_validateExperimentSetup(self):
"tolerance=1e-6, startTime=-2147483649, stopTime=0,",
"Integer overflow: Integers can be -2147483648 to 2147483647, received")

def test_validateHyperlinks_lib(self):
"""
Test whether the `.mo` files point only have valid hyperlinks
"""
# Test My ModelicaLibrary
import buildingspy.development.validator as v
val = v.Validator()
myMoLib = os.path.join("buildingspy", "tests", "MyModelicaLibrary")
# Get a list whose elements are the error strings
errStr = val.validateHyperlinks(myMoLib)
self.assertEqual(len(errStr), 0)

def test_validateHyperlinks_new_file(self):
"""
Test whether the `.mo` files point only have valid hyperlinks
"""
import tempfile
# Test My ModelicaLibrary
import buildingspy.development.validator as v

# Create a file with broken hyperlink
with tempfile.TemporaryDirectory(prefix="tmp-buildingspy-") as tmpdirname:

test_file = os.path.join(tmpdirname, "tmp_validate_hyperlinks.mo")
with open(test_file, 'w') as f:
f.write("""
some text
some other text
<img href=\\"modelica://link/to/nonexisting/image.png\\"/>
some other text
""")

val = v.Validator()
myMoLib = tmpdirname
# Get a list whose elements are the error strings
errStr = val.validateHyperlinks(myMoLib)
self.assertEqual(len(errStr), 1)


if __name__ == '__main__':
unittest.main()

0 comments on commit bb97ae1

Please sign in to comment.