From 0a7840fdfc99d696dcf581db58467fba9e5f2b18 Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Sun, 9 Jul 2023 11:06:57 +0100 Subject: [PATCH] add command to bstools for retyping posts --- bstools/bstools.py | 33 +++++++++++++++++++++++++++++++++ bstools/poetry.lock | 18 +++++++++++++++++- bstools/pyproject.toml | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/bstools/bstools.py b/bstools/bstools.py index 9a2d3e3..0d56f71 100644 --- a/bstools/bstools.py +++ b/bstools/bstools.py @@ -3,6 +3,7 @@ import dotenv import os import requests import ujson +import frontmatter from urllib.parse import urlparse @click.group() @@ -11,6 +12,38 @@ def cli(): pass +@cli.command() +@click.option("--folder", type=click.Path(dir_okay=True, file_okay=False), required=True) +@click.option("--old_type", type=str, required=True) +@click.option("--new_type", type=str, required=True) +def fix_post_types(folder: str, old_type: str, new_type: str): + """Fix post type metadata""" + + for root, dirs, files in os.walk(folder): + + for file in files: + if file.endswith(".md"): + full_path = os.path.join(root,file) + data = frontmatter.load(full_path) + + print(f"Analysing... {full_path}") + + if 'type' not in data: + print(f"Skipping {full_path} due to incomplete frontmatter") + continue + + if(data['type'] == old_type): + print(f"Update type for {full_path}: {old_type}->{new_type}") + data['type'] = new_type + + with open(full_path,'wb') as f: + frontmatter.dump(data, f) + + + + + + @cli.command() @click.option("--mentions-file", type=click.Path(file_okay=True), required=True) def fetch_mentions(mentions_file: str): diff --git a/bstools/poetry.lock b/bstools/poetry.lock index 2f20249..98af5da 100644 --- a/bstools/poetry.lock +++ b/bstools/poetry.lock @@ -100,6 +100,21 @@ python-versions = ">=3.5" [package.extras] cli = ["click (>=5.0)"] +[[package]] +name = "python-frontmatter" +version = "1.0.0" +description = "Parse and manage posts with YAML (or other) frontmatter" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +PyYAML = "*" + +[package.extras] +docs = ["sphinx"] +test = ["pytest", "toml", "pyaml"] + [[package]] name = "pyyaml" version = "6.0" @@ -186,7 +201,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "e29e6aa9b8aa53841b7b54c35483d1ae5ca4179793ce600d3cf96be58136d303" +content-hash = "37f6b249fc390c867f7f098d6b2a25155384e22a69faa6bb1276e93d559f3450" [metadata.files] beautifulsoup4 = [] @@ -219,6 +234,7 @@ python-dotenv = [ {file = "python-dotenv-0.19.2.tar.gz", hash = "sha256:a5de49a31e953b45ff2d2fd434bbc2670e8db5273606c1e737cc6b93eff3655f"}, {file = "python_dotenv-0.19.2-py2.py3-none-any.whl", hash = "sha256:32b2bdc1873fd3a3c346da1c6db83d0053c3c62f28f1f38516070c4c8971b1d3"}, ] +python-frontmatter = [] pyyaml = [ {file = "PyYAML-6.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53"}, {file = "PyYAML-6.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c"}, diff --git a/bstools/pyproject.toml b/bstools/pyproject.toml index e47d368..014b2d8 100644 --- a/bstools/pyproject.toml +++ b/bstools/pyproject.toml @@ -15,6 +15,7 @@ PyYAML = "^6.0" click = "^8.0.3" python-dotenv = "^0.19.2" markdownify = "^0.11.6" +python-frontmatter = "^1.0.0" [tool.poetry.dev-dependencies]