diff --git a/core/TESTS/test_drawings.py b/core/TESTS/test_drawings.py index cfffa6e19..41337078e 100644 --- a/core/TESTS/test_drawings.py +++ b/core/TESTS/test_drawings.py @@ -126,3 +126,22 @@ class DrawingsPathlibTests(TestCase): di = DataIssue.objects.filter(parser='Tunnel', message__contains="files named 'unique'") self.assertFalse(di.exists()) + + def test_extension_helpers_and_constants(self): + # Helpers should recognise supported/image suffixes (case-insensitive) + self.assertTrue(drawings._is_supported_suffix('.png')) + self.assertTrue(drawings._is_supported_suffix('.xml')) + self.assertTrue(drawings._is_supported_suffix('.TH')) + self.assertFalse(drawings._is_supported_suffix('')) + self.assertFalse(drawings._is_supported_suffix('.exe')) + + self.assertTrue(drawings._is_image_suffix('.png')) + self.assertTrue(drawings._is_image_suffix('.JPEG')) + self.assertFalse(drawings._is_image_suffix('.xml')) + self.assertFalse(drawings._is_image_suffix('')) + + # Constants should include expected values and be consistent + self.assertIn('.png', drawings.IMAGE_EXTS) + self.assertEqual(set(drawings.IMAGE_LIKE_EXTS), set(drawings.IMAGE_EXTS)) + self.assertIn('.th', drawings.SUPPORTED_EXTENSIONS) + self.assertIn('.png', drawings.SUPPORTED_EXTENSIONS) diff --git a/parsers/drawings.py b/parsers/drawings.py index 0796650cf..3bfa31075 100644 --- a/parsers/drawings.py +++ b/parsers/drawings.py @@ -12,8 +12,7 @@ for tunnel and therion files """ todo = """ -- Rename functions more consistently between tunnel and therion variants - +- Fix lost links to 1999 wallets now they have been renamed - implement: findimportinsert(therionfile, imp) Tries to link the scrap (Therion format) to the referenced therion scrap @@ -30,7 +29,31 @@ rx_pcpath = re.compile(r' bool: + """Return True if `suffix` (e.g. '.png') is a supported drawings extension.""" + if not suffix: + return False + return suffix.lower() in SUPPORTED_EXTENSIONS + + +def _is_image_suffix(suffix: str) -> bool: + """Return True if `suffix` looks like an image/scan-type suffix.""" + if not suffix: + return False + return suffix.lower() in IMAGE_EXTS + rx_wallet = re.compile(r""" # r"(\d\d\d\d#X?\d+\w?|1995-96kh|92-94Surveybookkh|1991surveybook|smkhs)/(.*?(?:png|jpg|pdf|jpeg|gif|txt))$", path # This regex is designed to extract a specific directory prefix and a filename @@ -154,7 +177,7 @@ def _process_reference(dwgfile, path, parser_label="Tunnel"): # Not a wallet reference; check image extension and possibly drawing-to-drawing reference suffix = Path(path).suffix.lower() - if suffix in IMAGE_EXTS: + if _is_image_suffix(suffix): # It's an image/scanned file type; we don't treat it as a referenced drawing return None, None @@ -325,8 +348,6 @@ def load_drawings_files(): if os.path.isfile("therionrefs.log"): os.remove("therionrefs.log") - supported_extensions = {".txt", ".xml", ".th", ".th2", ".pdf", ".png", ".svg", ".jpg"} - # Walk the tree with pathlib, skip hidden and backup files files_meta = [] # list of tuples (ext, rel_path, dwgname, pathobj) for p in drawdatadir.rglob('*'): @@ -339,7 +360,7 @@ def load_drawings_files(): continue suffix = p.suffix.lower() - if suffix in supported_extensions or suffix == '': + if _is_supported_suffix(suffix) or suffix == '': rel = p.relative_to(drawdatadir).as_posix() if suffix == '': dwgname = p.name