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