2024-11-01 14:54:50 +00:00
|
|
|
import os
|
|
|
|
from datetime import datetime
|
2024-11-11 14:45:00 +00:00
|
|
|
from pathlib import Path
|
2024-11-01 14:54:50 +00:00
|
|
|
|
2024-11-11 16:14:01 +00:00
|
|
|
from termcolor import colored
|
2024-11-01 14:54:50 +00:00
|
|
|
|
2024-11-11 14:45:00 +00:00
|
|
|
from models.entry import Entry
|
2024-11-01 14:54:50 +00:00
|
|
|
from services.parse_markdown_service import ParseMarkdownService
|
|
|
|
|
|
|
|
|
|
|
|
class ParseFileService:
|
2024-11-11 14:45:00 +00:00
|
|
|
def __init__(self, source_directory):
|
|
|
|
self.source_directory = source_directory
|
|
|
|
self.parse_markdown_service = ParseMarkdownService()
|
2024-11-01 14:54:50 +00:00
|
|
|
|
2024-11-11 14:45:00 +00:00
|
|
|
def __get_title(self, file):
|
2024-11-14 08:50:06 +00:00
|
|
|
return os.path.splitext(os.path.basename(file))[0]
|
2024-11-01 14:54:50 +00:00
|
|
|
|
2024-11-11 14:45:00 +00:00
|
|
|
def __parse_file(self, file) -> Entry:
|
|
|
|
markdown_data = self.parse_markdown_service.parse(file)
|
2024-11-01 14:54:50 +00:00
|
|
|
return {
|
2024-11-11 14:45:00 +00:00
|
|
|
"title": self.__get_title(file),
|
|
|
|
"last_modified": datetime.fromtimestamp(os.stat(file).st_mtime).strftime(
|
2024-11-01 14:54:50 +00:00
|
|
|
"%Y-%m-%d %H:%M:%S"
|
|
|
|
),
|
2024-11-11 14:45:00 +00:00
|
|
|
"size": os.stat(file).st_size,
|
2024-11-01 14:54:50 +00:00
|
|
|
"tags": markdown_data.get("tags", []),
|
2024-11-01 16:11:16 +00:00
|
|
|
"links": markdown_data.get("links", []),
|
2024-11-01 14:54:50 +00:00
|
|
|
"body": markdown_data.get("body", []),
|
|
|
|
}
|
2024-11-11 14:45:00 +00:00
|
|
|
|
2024-11-14 08:50:06 +00:00
|
|
|
def parse_source_directory(self) -> list[Entry]:
|
2024-11-11 16:14:01 +00:00
|
|
|
print(colored("INFO Indexing entries in source directory", "light_blue"))
|
2024-11-11 14:45:00 +00:00
|
|
|
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
|