From fa6cb6c65ed7d16dd05b6ce010f8f9eef3948c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Tue, 1 Mar 2022 11:07:59 +0100 Subject: [PATCH] Fix directory and file structure --- convert.py | 55 ----------------- setup.cfg | 8 +-- .../__init__.py | 0 src/frontmatter-to-csv/main.py | 60 +++++++++++++++++++ src/package-name/main.py | 1 - 5 files changed, 64 insertions(+), 60 deletions(-) delete mode 100755 convert.py rename src/{package-name => frontmatter-to-csv}/__init__.py (100%) create mode 100644 src/frontmatter-to-csv/main.py delete mode 100644 src/package-name/main.py diff --git a/convert.py b/convert.py deleted file mode 100755 index 742c020..0000000 --- a/convert.py +++ /dev/null @@ -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) diff --git a/setup.cfg b/setup.cfg index cb39809..40da6b3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,14 +1,14 @@ [metadata] -name = example-pkg-YOUR-USERNAME-HERE +name = frontmatter-to-csv-lkucharczyk version = 0.0.1 author = Lukáš Kucharczyk author_email = lukas@kucharczyk.xyz -description = A small example package +description = Convert YAML front matter to CSV long_description = file: README.md long_description_content_type = text/markdown -url = https://git.kucharczyk.xyz/templates/python +url = https://git.kucharczyk.xyz/lukas/frontmatter-to-csv project_urls = - Bug Tracker = https://git.kucharczyk.xyz/templates/python/issues + Bug Tracker = https://git.kucharczyk.xyz/lukas/frontmatter-to-csv/issues classifiers = Programming Language :: Python :: 3 License :: OSI Approved :: MIT License diff --git a/src/package-name/__init__.py b/src/frontmatter-to-csv/__init__.py similarity index 100% rename from src/package-name/__init__.py rename to src/frontmatter-to-csv/__init__.py diff --git a/src/frontmatter-to-csv/main.py b/src/frontmatter-to-csv/main.py new file mode 100644 index 0000000..c6b9125 --- /dev/null +++ b/src/frontmatter-to-csv/main.py @@ -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) diff --git a/src/package-name/main.py b/src/package-name/main.py deleted file mode 100644 index 8d1c8b6..0000000 --- a/src/package-name/main.py +++ /dev/null @@ -1 +0,0 @@ -