feature: parse entire source directory

This commit is contained in:
thomasabishop 2024-11-11 14:45:00 +00:00
parent 2dbfea8cd7
commit b023531dc8

View file

@ -1,39 +1,40 @@
import os import os
from datetime import datetime from datetime import datetime
from typing import List, TypedDict from pathlib import Path
from hurry.filesize import size from hurry.filesize import size
from models.entry import Entry
from services.parse_markdown_service import ParseMarkdownService from services.parse_markdown_service import ParseMarkdownService
class Entry(TypedDict):
title: str
last_modified: str
size: str
tags: List[str]
links: List[str]
body: str
class ParseFileService: class ParseFileService:
def __init__(self, file): def __init__(self, source_directory):
self.eolas_file = file self.source_directory = source_directory
self.info = os.stat(file) self.parse_markdown_service = ParseMarkdownService()
self.parse_markdown_service = ParseMarkdownService(file)
def __get_title(self): def __get_title(self, file):
return os.path.basename(self.eolas_file) return os.path.basename(file)
def parse(self) -> Entry: def __parse_file(self, file) -> Entry:
markdown_data = self.parse_markdown_service.parse() markdown_data = self.parse_markdown_service.parse(file)
return { return {
"title": self.__get_title(), "title": self.__get_title(file),
"last_modified": datetime.fromtimestamp(self.info.st_mtime).strftime( "last_modified": datetime.fromtimestamp(os.stat(file).st_mtime).strftime(
"%Y-%m-%d %H:%M:%S" "%Y-%m-%d %H:%M:%S"
), ),
"size": size(self.info.st_size), "size": os.stat(file).st_size,
"tags": markdown_data.get("tags", []), "tags": markdown_data.get("tags", []),
"links": markdown_data.get("links", []), "links": markdown_data.get("links", []),
"body": markdown_data.get("body", []), "body": markdown_data.get("body", []),
} }
def parse_source_directory(self):
print("INFO Indexing entries in source directory")
parsed_entries = []
with os.scandir(self.source_directory) as dir:
for file in dir:
if Path(file).suffix == ".md":
parsed = self.__parse_file(file)
parsed_entries.append(parsed)
return parsed_entries