Unverified Commit 6fa89a1e authored by Noé Lopez's avatar Noé Lopez
Browse files

Add DocumentWriter class

parent 2648491a
import os
import json
from document import Document
class DocumentWriter():
def __init__(self, target_directory) -> None:
self.target = target_directory
os.makedirs(self.target, exist_ok=True)
os.makedirs(os.path.join(self.target, ".meta"), exist_ok=True)
def write_document(self, document: Document) -> None:
main_file_name = os.path.join(self.target, document.get_name())
with open(main_file_name, "w") as f:
f.write(document.get_main_content())
meta_file_name = os.path.join(self.target, ".meta", document.get_name() + ".json")
with open(meta_file_name, "w") as f:
f.write(json.dumps({
"title": document.get_title(),
"backlink": main_file_name,
"properties": document.get_attributes()
}))
import os
import json
from document_writer import DocumentWriter
from document import Document
def test_document_writer(tmp_path):
content = """beep
boop
bop
bim"""
name = "test.txt"
title = "hello !"
creation_date = 1761763703219
test_document = Document()
test_document.set_name(name)
test_document.set_title(title)
test_document.set_main_content(content)
test_document.set_attribute("creation_date", creation_date)
writer = DocumentWriter(tmp_path)
writer.write_document(test_document)
with open(os.path.join(tmp_path, name)) as f:
assert f.read() == content
with open(os.path.join(tmp_path, ".meta", name + ".json")) as f:
meta = json.loads(f.read())
assert meta["title"] == title
assert meta["backlink"] == os.path.join(tmp_path, name)
assert meta["properties"] != None
assert meta["properties"]["creation_date"] == 1761763703219
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment