21 from __future__
import annotations
23 __all__ = [
"FilePathParser"]
26 from typing
import Dict
28 from ..mapping
import Mapping
32 """A callable object that extracts Gen2 data IDs from filenames 33 corresponding to a particular Gen2 DatasetType. 35 External code should use the `fromMapping` method to construct instances. 40 Dictionary mapping Gen2 data ID key to the type of its associated 42 regex : regular expression object 43 Regular expression pattern with named groups for all data ID keys. 45 def __init__(self, keys: Dict[str, type], regex: re.Pattern):
51 TEMPLATE_RE = re.compile(
r"\%\((?P<name>\w+)\)[^\%]*?(?P<type>[idrs])")
55 """Construct a FilePathParser instance from a Gen2 56 `lsst.obs.base.Mapping` instance. 60 mapping : `lsst.obs.base.Mapping` 61 Mapping instance from a Gen2 `CameraMapper`. 65 parser : `FilePathParser` 66 A new `FilePathParser` instance that can extract Gen2 data IDs 67 from filenames with the given mapping's template. 72 Raised if the mapping has no template. 74 template = mapping.template
84 allKeys = mapping.keys()
88 terms.append(re.escape(template[last:match.start()]))
92 name = match.group(
"name")
95 elif match.group(
"type")
in "id":
101 terms.append(
r"(?P<%s>%s)" % (name, pattern))
102 keys[name] = allKeys[name]
104 terms.append(
r"(%s)" % pattern)
109 terms.append(re.escape(template[last:]))
110 return cls(keys, regex=re.compile(
"".join(terms)))
113 """Extract a Gen2 data ID dictionary from the given path. 118 Path and filename relative to the repository root. 123 Dictionary used to identify the dataset in the Gen2 butler, or 124 None if the file was not recognized. 126 m = self.
regex.fullmatch(filePath)
129 return {k: v(m.group(k))
for k, v
in self.
keys.items()}