Compare commits

...

2 Commits

Author SHA1 Message Date
Lukáš Kucharczyk fa6cb6c65e
Fix directory and file structure 2022-03-01 11:07:59 +01:00
Lukáš Kucharczyk 84ba9f45fa
Update README 2022-03-01 11:01:34 +01:00
6 changed files with 70 additions and 62 deletions

View File

@ -1,3 +1,7 @@
# Example package # `frontmatter-to-csv` - Convert YAML front matter to CSV
This is a simple example package. This simple Python script will convert fields from the YAML front matter to your specified CSV fields.
Optionally, it can transform some of the fields:
* convert Python lists to comma-joined lists
* convert varied human-readable durations to unified unit (hours)

View File

@ -1,55 +0,0 @@
from queue import Empty
from typing import Dict
from durations import Duration
import os
import frontmatter
import csv
directory = "games"
outputfile = "test.csv"
filtered = dict()
fields = {
"name": "Name",
"playtime": "Playtime",
"platform": "Platform",
"infinite": "Infinite",
"finished": "Finished",
"refunded": "Refunded",
"dropped": "Dropped",
"date-released": "DateReleased",
"date-purchased": "DatePurchased",
"date-started": "DateStarted",
"date-finished": "DateFinished",
}
with open(outputfile, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fields.values())
writer.writeheader()
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
if os.path.isfile(f):
article = frontmatter.load(f)
for field in fields:
if field not in article.metadata.keys():
newvalue = ""
else:
newvalue = article.metadata[field]
if field == "playtime" and newvalue != 0 and newvalue is not None:
if type(newvalue) is dict:
totalvalue = 0
for key in newvalue:
timevalue = Duration(newvalue[key]).to_hours()
totalvalue += timevalue
newvalue = totalvalue
else:
timevalue = Duration(newvalue)
newvalue = timevalue.to_hours()
if type(newvalue) is list:
print(
f"Field '{field}' for file '{filename}' is a list, joining with commas."
)
newvalue = ",".join(newvalue)
filtered[fields[field]] = newvalue
writer.writerow(filtered)

View File

@ -1,14 +1,14 @@
[metadata] [metadata]
name = example-pkg-YOUR-USERNAME-HERE name = frontmatter-to-csv-lkucharczyk
version = 0.0.1 version = 0.0.1
author = Lukáš Kucharczyk author = Lukáš Kucharczyk
author_email = lukas@kucharczyk.xyz author_email = lukas@kucharczyk.xyz
description = A small example package description = Convert YAML front matter to CSV
long_description = file: README.md long_description = file: README.md
long_description_content_type = text/markdown long_description_content_type = text/markdown
url = https://git.kucharczyk.xyz/templates/python url = https://git.kucharczyk.xyz/lukas/frontmatter-to-csv
project_urls = project_urls =
Bug Tracker = https://git.kucharczyk.xyz/templates/python/issues Bug Tracker = https://git.kucharczyk.xyz/lukas/frontmatter-to-csv/issues
classifiers = classifiers =
Programming Language :: Python :: 3 Programming Language :: Python :: 3
License :: OSI Approved :: MIT License License :: OSI Approved :: MIT License

View File

@ -0,0 +1,60 @@
"""
Convert YAML front matter to CSV
"""
from queue import Empty
from typing import Dict
from durations import Duration
import os
import frontmatter
import csv
directory = "games"
outputfile = "test.csv"
filtered = dict()
fields = {
"name": "Name",
"playtime": "Playtime",
"platform": "Platform",
"infinite": "Infinite",
"finished": "Finished",
"refunded": "Refunded",
"dropped": "Dropped",
"date-released": "DateReleased",
"date-purchased": "DatePurchased",
"date-started": "DateStarted",
"date-finished": "DateFinished",
}
if __name__ == '__main__':
with open(outputfile, "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fields.values())
writer.writeheader()
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
if os.path.isfile(f):
article = frontmatter.load(f)
for field in fields:
if field not in article.metadata.keys():
newvalue = ""
else:
newvalue = article.metadata[field]
if field == "playtime" and newvalue != 0 and newvalue is not None:
if type(newvalue) is dict:
totalvalue = 0
for key in newvalue:
timevalue = Duration(newvalue[key]).to_hours()
totalvalue += timevalue
newvalue = totalvalue
else:
timevalue = Duration(newvalue)
newvalue = timevalue.to_hours()
if type(newvalue) is list:
print(
f"Field '{field}' for file '{filename}' is a list, joining with commas."
)
newvalue = ",".join(newvalue)
filtered[fields[field]] = newvalue
writer.writerow(filtered)

View File

@ -1 +0,0 @@