Skip to content

Commit

Permalink
Fixed field names with subfields
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Mar 17, 2023
1 parent 2c6b1e4 commit 7dee3ed
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,84 @@ The mdinfo command line tool can output selected metadata in CSV and JSON format
Currently tested on Linux and macOS.
## Synopsis

```bash
$ mdinfo -p "{audio:artist}" -p "{audio:album}" -p "{audio:track}" -p "{size}" music/*.mp3
track1.mp3: The Piano Guys Wonders 1 8806978
track10.mp3: The Piano Guys Wonders 10 5765646
track11.mp3: The Piano Guys Wonders 11 8048782
track12.mp3: The Piano Guys Wonders 12 7834054
track2.mp3: The Piano Guys Wonders 2 8563796
track3.mp3: The Piano Guys Wonders 3 6162443
track4.mp3: The Piano Guys Wonders 4 7863944
track5.mp3: The Piano Guys Wonders 5 8194232
track6.mp3: The Piano Guys Wonders 6 8794087
track7.mp3: The Piano Guys Wonders 7 8873454
track8.mp3: The Piano Guys feat. Shweta Subram Wonders 8 8582158
track9.mp3: The Piano Guys Wonders 9 9011851
```

CSV output:

```bash
$ mdinfo -p "{audio:artist}" -p "{audio:album}" -p "{audio:track}" -p "{size}" music/*.mp3 --csv
filename,audio:artist,audio:album,audio:track,size
track1.mp3,The Piano Guys,Wonders,1,8806978
track10.mp3,The Piano Guys,Wonders,10,5765646
track11.mp3,The Piano Guys,Wonders,11,8048782
track12.mp3,The Piano Guys,Wonders,12,7834054
track2.mp3,The Piano Guys,Wonders,2,8563796
track3.mp3,The Piano Guys,Wonders,3,6162443
track4.mp3,The Piano Guys,Wonders,4,7863944
track5.mp3,The Piano Guys,Wonders,5,8194232
track6.mp3,The Piano Guys,Wonders,6,8794087
track7.mp3,The Piano Guys,Wonders,7,8873454
track8.mp3,The Piano Guys feat. Shweta Subram,Wonders,8,8582158
track9.mp3,The Piano Guys,Wonders,9,9011851
```

JSON output:

```bash
$ mdinfo -p "{audio:artist}" -p "{audio:album}" -p "{audio:track}" -p "{size}" music/*.mp3 --json
{
"audio:album": "Wonders",
"audio:artist": "The Piano Guys",
"audio:track": "1",
"filename": "track1.mp3",
"size": "8806978"
}
{
"audio:album": "Wonders",
"audio:artist": "The Piano Guys",
"audio:track": "10",
"filename": "track10.mp3",
"size": "5765646"
}
...
```

JSON array output:

```bash
$ mdinfo -p "{audio:artist}" -p "{audio:album}" -p "{audio:track}" -p "{size}" music/*.mp3 --json --array
[
{
"audio:album": "Wonders",
"audio:artist": "The Piano Guys",
"audio:track": "1",
"filename": "track1.mp3",
"size": "8806978"
},
{
"audio:album": "Wonders",
"audio:artist": "The Piano Guys",
"audio:track": "10",
"filename": "track10.mp3",
"size": "5765646"
},
...
]
```

## Command Line Usage

Expand Down
9 changes: 7 additions & 2 deletions mdinfo/mdinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def print_templates(
else (f"{filepath}: " if path else f"{pathlib.Path(filepath).name}: ")
)
rendered_templates = [
str(t).replace( NONE_STR_SENTINEL, undefined or "") for t in rendered_templates
str(t).replace(NONE_STR_SENTINEL, undefined or "") for t in rendered_templates
]
separator = "\0" if null_separator else " "
print(f"{header}{separator.join(rendered_templates)}")
Expand Down Expand Up @@ -184,7 +184,12 @@ def get_field_name(template: str) -> str:

parser = MTLParser(get_field_values=lambda *x: x)
if template_statements := parser.parse_statement(template):
return template_statements[0].field or template
if template_statements[0].field:
field_name = template_statements[0].field
if template_statements[0].subfield:
field_name += f":{template_statements[0].subfield}"
return field_name
return template

raise ValueError(f"Could not find field in template: {template}")

Expand Down

0 comments on commit 7dee3ed

Please sign in to comment.