---
# System prepended metadata

title: fw-meta - import meta templates/patterns

---

# fw-meta - import meta templates/patterns

```python
# easiest to experiment w/ extraction behavior in fw-meta:
# > pip install fw-meta ipython && ipython
from fw_meta import extract_meta

# the input data can be a simple dict - for now we are limited to the path field
data = {"path": "2023-04-05/projectlbl=emory_6_retrospective-form/naccid=NACC001504/visitlbl=FORMS-VISIT-10_UDSv3/my-filename-i-don't-care-about.json"}

# let's start simple and build up to full solutions in steps
# 1. just grab the things from the projectlbl=... directory
# - use the default glob style (* and . have a different meanings in regex!)
# - capture groups don't allow '/' chars by default to not cross dir boundaries
# - note the double star '**' at the end - it's glob for "anything, even /"
# - note that the uploader doesn't support setting project level fields!
extract_meta(data, mappings=[
    ("path", "*/projectlbl={grp}_{prj.info.ADCID}_{prj}/**"),
])

# 2/a it's straightforward to add the next 2 levels
# - note single star ending glob - only matches if the file is at that level!
extract_meta(data, mappings=[
    ("path", "*/projectlbl={grp}_{prj.info.ADCID}_{prj}/naccid={sub}/visitlbl={ses}_{acq}/*"),
])

# 2/b note that the mappings is list[tuple] - we could split the extractions
# - note that the left hand side (LHS) doesn't need to be a single data field
# - it's a template for building a string to then extract from via a pattern
# - docs: https://gitlab.com/flywheel-io/tools/lib/fw-utils/-/blob/main/fw_utils/formatters.py#L123
extract_meta(data, mappings=[
    ("path", "*/projectlbl={grp}_*_{prj}/**"),
    ("{path}", "**/naccid={sub}/**"),
    ("{path} foo", "**/visitlbl={ses}_{acq}/**"),
])

# 3/a you can use previously extracted !meta fields on the LHS
extract_meta(data, mappings=[
    ("path", "*/projectlbl={grp}_*_{prj}/naccid={sub}/visitlbl={ses}_{acq}/*"),
    ("{!sub}-{!ses}-{!acq}.json", "file"),
])

# 3/b or you could use a re.sub pattern on the LHS, but escaping for paths is hard
# - we should adjust the available templating options to be more CLI-friendly (/, $, ...)
# - need customer / SSE feedback on needs - bash param expansions could give ideas:
#   https://wiki.bash-hackers.org/syntax/pe
extract_meta(data, mappings=[
    ("path", "*/projectlbl={grp}_*_{prj}/naccid={sub}/visitlbl={ses}_{acq}/*"),
    (r"{path/.*naccid=(.*?)\/visitlbl=(.*?)_(.*?)\/.*/\1-\2-\3}.json", "file"),
])
```