jekyll-to-hugo/app/io/writer.py

51 lines
983 B
Python
Raw Normal View History

2023-05-31 15:32:42 +00:00
from abc import ABCMeta, abstractmethod
from pathlib import Path
2023-05-31 15:32:42 +00:00
from app import utils
class IoWriter(metaclass=ABCMeta):
"""
Abstract class for writing posts.
"""
@abstractmethod
def write(self, data: str):
"""
Write a post
Parameters
----------
data: str
The post data to write
"""
raise NotImplementedError
class FileWriter(IoWriter):
"""
Writes a post to a file.
"""
def __init__(self, output_path: Path):
utils.guard_against_none(output_path, "output_path")
self.output_path = output_path
output_path.parent.mkdir(parents=True, exist_ok=True)
def write(self, data: str):
with open(self.output_path, "w") as fo:
fo.write(data)
2023-06-04 16:27:29 +00:00
class MockWriter(IoWriter):
"""
Writes a post to a string.
"""
def __init__(self):
self.content = ""
def write(self, data: str):
self.content += data