feat: add typer & new command
Signed-off-by: Konstantin Fickel <mail@konstantinfickel.de>
This commit is contained in:
parent
e826101a24
commit
87be5c22a2
5 changed files with 248 additions and 94 deletions
|
|
@ -1,28 +1,22 @@
|
|||
import glob
|
||||
import os
|
||||
import typer # type: ignore [reportMissingImports]
|
||||
import click # type: ignore [reportMissingImports]
|
||||
from shutil import move
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from streamer.parse import Shard
|
||||
from streamer.parse.parse import parse_markdown_file
|
||||
from streamer.query.task import find_by_markers
|
||||
|
||||
cwd = os.getcwd()
|
||||
app = typer.Typer()
|
||||
|
||||
streamer_directory = os.getcwd()
|
||||
|
||||
|
||||
def find_by_markers(shard: Shard, has: list[str], has_not: list[str]) -> list[Shard]:
|
||||
found_shards = []
|
||||
|
||||
if any(tag in has for tag in shard.markers) and not any(
|
||||
tag in has_not for tag in shard.markers
|
||||
):
|
||||
found_shards.append(shard)
|
||||
|
||||
for child in shard.children:
|
||||
found_shards.extend(find_by_markers(child, has, has_not))
|
||||
|
||||
return found_shards
|
||||
|
||||
|
||||
def run() -> None:
|
||||
for file_name in glob.glob(f"{glob.escape(cwd)}/*.md"):
|
||||
@app.command()
|
||||
def todo() -> None:
|
||||
for file_name in glob.glob(f"{glob.escape(streamer_directory)}/*.md"):
|
||||
with open(file_name, "r") as file:
|
||||
file_content = file.read()
|
||||
sharded_document = parse_markdown_file(file_name, file_content)
|
||||
|
|
@ -38,3 +32,38 @@ def run() -> None:
|
|||
)
|
||||
)
|
||||
print(f" {file_name}:{task_shard.start_line}")
|
||||
|
||||
|
||||
@app.command()
|
||||
def new() -> None:
|
||||
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
preliminary_file_name = f"{timestamp}_wip.md"
|
||||
prelimary_path = os.path.join(streamer_directory, preliminary_file_name)
|
||||
|
||||
content = "# "
|
||||
with open(prelimary_path, "w") as file:
|
||||
file.write(content)
|
||||
|
||||
click.edit(None, filename=prelimary_path)
|
||||
|
||||
with open(prelimary_path, "r") as file:
|
||||
content = file.read()
|
||||
parsed_content = parse_markdown_file(prelimary_path, content)
|
||||
|
||||
final_file_name = f"{timestamp}.md"
|
||||
if parsed_content.shard and len(markers := parsed_content.shard.markers):
|
||||
final_file_name = f"{timestamp} {' '.join(markers)}.md"
|
||||
|
||||
final_path = os.path.join(streamer_directory, final_file_name)
|
||||
move(prelimary_path, final_path)
|
||||
click.echo(f"Saved as {click.style(final_file_name, fg='yellow')}")
|
||||
|
||||
|
||||
@app.callback(invoke_without_command=True)
|
||||
def main(ctx: typer.Context):
|
||||
if ctx.invoked_subcommand is None:
|
||||
new()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app()
|
||||
|
|
|
|||
15
src/streamer/query/task.py
Normal file
15
src/streamer/query/task.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from streamer.parse.shard import Shard
|
||||
|
||||
|
||||
def find_by_markers(shard: Shard, has: list[str], has_not: list[str]) -> list[Shard]:
|
||||
found_shards = []
|
||||
|
||||
if any(tag in has for tag in shard.markers) and not any(
|
||||
tag in has_not for tag in shard.markers
|
||||
):
|
||||
found_shards.append(shard)
|
||||
|
||||
for child in shard.children:
|
||||
found_shards.extend(find_by_markers(child, has, has_not))
|
||||
|
||||
return found_shards
|
||||
Loading…
Add table
Add a link
Reference in a new issue