Compare commits
8 Commits
409b8d4a2a
...
0.1.0
Author | SHA1 | Date | |
---|---|---|---|
5cf265f09c | |||
91743d1c6d | |||
516e08a707 | |||
ff645425f9 | |||
0f01ef80ec | |||
7ce32bac41 | |||
fa6cb6c65e | |||
84ba9f45fa |
22
.drone.yml
Normal file
22
.drone.yml
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: build and publish
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
image: registry.kucharczyk.tech/python-pypi
|
||||
commands:
|
||||
- python -m build
|
||||
- name: publish
|
||||
image: registry.kucharczyk.tech/python-pypi
|
||||
commands:
|
||||
- python -m twine upload -u $USERNAME -p $PASSWORD dist/*
|
||||
environment:
|
||||
USERNAME:
|
||||
from_secret: pypi_username
|
||||
PASSWORD:
|
||||
from_secret: pypi_password
|
||||
trigger:
|
||||
event:
|
||||
- tag
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -139,3 +139,4 @@ dmypy.json
|
||||
cython_debug/
|
||||
|
||||
testfiles
|
||||
*.csv
|
||||
|
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"editor.formatOnSave": true,
|
||||
"python.formatting.provider": "black"
|
||||
}
|
1
Pipfile
1
Pipfile
@ -9,6 +9,7 @@ durations = "*"
|
||||
|
||||
[dev-packages]
|
||||
black = "*"
|
||||
build = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.10"
|
||||
|
33
Pipfile.lock
generated
33
Pipfile.lock
generated
@ -1,7 +1,7 @@
|
||||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "5b36fe9398efd9d93cb2e2c1b2984de30b9791bb9b76859a692595f911d9d2e1"
|
||||
"sha256": "4a6a85639db632b62a977f88ab0a77557d1fe6477398675f6d24f006c59c0ff9"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
@ -101,6 +101,14 @@
|
||||
"index": "pypi",
|
||||
"version": "==22.1.0"
|
||||
},
|
||||
"build": {
|
||||
"hashes": [
|
||||
"sha256:1aaadcd69338252ade4f7ec1265e1a19184bf916d84c9b7df095f423948cb89f",
|
||||
"sha256:21b7ebbd1b22499c4dac536abc7606696ea4d909fd755e00f09f3c0f2c05e3c8"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.7.0"
|
||||
},
|
||||
"click": {
|
||||
"hashes": [
|
||||
"sha256:6a7a62563bbfabfda3a38f3023a1db4a35978c0abd76f6c9605ecd6554d6d9b1",
|
||||
@ -116,6 +124,14 @@
|
||||
],
|
||||
"version": "==0.4.3"
|
||||
},
|
||||
"packaging": {
|
||||
"hashes": [
|
||||
"sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb",
|
||||
"sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==21.3"
|
||||
},
|
||||
"pathspec": {
|
||||
"hashes": [
|
||||
"sha256:7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a",
|
||||
@ -123,6 +139,13 @@
|
||||
],
|
||||
"version": "==0.9.0"
|
||||
},
|
||||
"pep517": {
|
||||
"hashes": [
|
||||
"sha256:931378d93d11b298cf511dd634cf5ea4cb249a28ef84160b3247ee9afb4e8ab0",
|
||||
"sha256:dd884c326898e2c6e11f9e0b64940606a93eb10ea022a2e067959f3a110cf161"
|
||||
],
|
||||
"version": "==0.12.0"
|
||||
},
|
||||
"platformdirs": {
|
||||
"hashes": [
|
||||
"sha256:7535e70dfa32e84d4b34996ea99c5e432fa29a708d0f4e394bbcb2a8faa4f16d",
|
||||
@ -131,6 +154,14 @@
|
||||
"markers": "python_version >= '3.7'",
|
||||
"version": "==2.5.1"
|
||||
},
|
||||
"pyparsing": {
|
||||
"hashes": [
|
||||
"sha256:18ee9022775d270c55187733956460083db60b37d0d0fb357445f3094eed3eea",
|
||||
"sha256:a6c06a88f252e6c322f65faf8f418b16213b51bdfaece0524c1c1bc30c63c484"
|
||||
],
|
||||
"markers": "python_version >= '3.6'",
|
||||
"version": "==3.0.7"
|
||||
},
|
||||
"tomli": {
|
||||
"hashes": [
|
||||
"sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc",
|
||||
|
@ -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)
|
55
convert.py
55
convert.py
@ -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)
|
65
frontmatter_to_csv/convert.py
Normal file
65
frontmatter_to_csv/convert.py
Normal file
@ -0,0 +1,65 @@
|
||||
"""
|
||||
Convert YAML front matter to CSV
|
||||
"""
|
||||
|
||||
from queue import Empty
|
||||
from typing import Dict
|
||||
from durations import Duration
|
||||
import os
|
||||
import frontmatter
|
||||
import 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",
|
||||
}
|
||||
|
||||
|
||||
def convert(directory="f", outputfile="test.csv"):
|
||||
if not os.path.isdir(directory):
|
||||
exit(f"The directory {directory} does not exist!")
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
convert()
|
24
setup.cfg
24
setup.cfg
@ -1,24 +1,26 @@
|
||||
[metadata]
|
||||
name = example-pkg-YOUR-USERNAME-HERE
|
||||
version = 0.0.1
|
||||
name = frontmatter-to-csv
|
||||
version = 0.1.0
|
||||
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
|
||||
Environment :: Console
|
||||
Intended Audience :: End Users/Desktop
|
||||
License :: OSI Approved :: GNU General Public License (GPL)
|
||||
Operating System :: OS Independent
|
||||
Topic :: Utilities
|
||||
|
||||
[options]
|
||||
package_dir =
|
||||
= src
|
||||
packages = find:
|
||||
packages = frontmatter_to_csv
|
||||
python_requires = >=3.6
|
||||
|
||||
[options.packages.find]
|
||||
where = src
|
||||
[options.entry_points]
|
||||
console_scripts =
|
||||
frontmatter-to-csv = frontmatter_to_csv.convert:convert
|
@ -1 +0,0 @@
|
||||
|
Reference in New Issue
Block a user