|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +pattern = r"(?P<FILESTEM_PREFIX>.*?)(?P<FRAMEID_STR>[-._]\d+)?(?P<EXTENSION>\.\w{3,4})" |
| 5 | +compiled_pattern = re.compile(pattern) |
| 6 | +compiled_frameId = re.compile(r"(\D+)?(?P<FRAMEID>\d+$)") |
| 7 | + |
| 8 | +def getFileElements(inputFilePath: str): |
| 9 | + |
| 10 | + filename = os.path.basename(inputFilePath) |
| 11 | + match = compiled_pattern.search(filename) |
| 12 | + frameId_str = match.group("FRAMEID_STR") |
| 13 | + |
| 14 | + fileElements = {} |
| 15 | + if match: |
| 16 | + fileElements = { |
| 17 | + "<PATH>": inputFilePath, |
| 18 | + "<FILENAME>": filename, |
| 19 | + "<FILESTEM>": match.group("FILESTEM_PREFIX"), |
| 20 | + "<FILESTEM_PREFIX>": match.group("FILESTEM_PREFIX"), |
| 21 | + "<EXTENSION>": match.group("EXTENSION"), |
| 22 | + } |
| 23 | + if frameId_str is not None: |
| 24 | + fileElements["<FRAMEID_STR>"] = frameId_str |
| 25 | + fileElements["<FILESTEM>"] += frameId_str |
| 26 | + match_frameId = compiled_frameId.search(frameId_str) |
| 27 | + fileElements["<FRAMEID>"] = match_frameId.group("FRAMEID") |
| 28 | + |
| 29 | + return fileElements |
| 30 | + |
| 31 | + |
| 32 | +def getViewElements(vp): |
| 33 | + |
| 34 | + vpPath = vp.childAttribute("path").value |
| 35 | + |
| 36 | + viewElements = getFileElements(vpPath) |
| 37 | + |
| 38 | + viewElements["<VIEW_ID>"] = str(vp.childAttribute("viewId").value) |
| 39 | + viewElements["<INTRINSIC_ID>"] = str(vp.childAttribute("intrinsicId").value) |
| 40 | + viewElements["<POSE_ID>"] = str(vp.childAttribute("poseId").value) |
| 41 | + |
| 42 | + return viewElements |
| 43 | + |
| 44 | + |
| 45 | +def replacePatterns(input, pattern, replacements): |
| 46 | + # Use all substrings of "input" matching the regex "pattern" as a key to substitute themselves by their value in the dictionary "replacements". |
| 47 | + # If "replacements" does not contain the key, the key is removed from "input" to build the resolved string. |
| 48 | + def replaceMatch(match): |
| 49 | + key = match.group() |
| 50 | + return replacements.get(key, "") |
| 51 | + return pattern.sub(replaceMatch, input) |
| 52 | + |
| 53 | + |
| 54 | +compiled_element = re.compile(r"<\w*>") |
| 55 | + |
| 56 | +def resolvePath(input, outputTemplate: str) -> str: |
| 57 | + |
| 58 | + if isinstance(input, str): |
| 59 | + replacements = getFileElements(input) |
| 60 | + else: |
| 61 | + replacements = getViewElements(input) |
| 62 | + |
| 63 | + resolved = replacePatterns(outputTemplate, compiled_element, replacements) |
| 64 | + |
| 65 | + return resolved |
| 66 | + |
0 commit comments