Skip to content

Commit

Permalink
feature: Replace methods to Exporter Classes
Browse files Browse the repository at this point in the history
Add
Exporter BaseClass

Replace
markdown_exporter -> MarkdownExporter(...).export()
string_exporter -> StringExporter(...).export()
  • Loading branch information
echo724 committed Mar 22, 2022
1 parent 77f4833 commit 2b35818
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 55 deletions.
2 changes: 0 additions & 2 deletions notion2md/exporter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from .block import markdown_exporter
from .block import string_exporter
130 changes: 77 additions & 53 deletions notion2md/exporter/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,88 @@

from notion2md.config import Config
from notion2md.convertor.block import BlockConvertor
from notion2md.notion_api import get_children
from notion2md.notion_api import NotionClient
from notion2md.util import zip_dir


def markdown_exporter(
block_id: str = None,
block_url: str = None,
output_filename: str = None,
output_path: str = None,
download: bool = False,
unzipped: bool = False,
):
args = locals()
config = Config(**args)
# Directory Checking and Creating
if not os.path.exists(config.tmp_path):
os.makedirs(config.tmp_path)
if not os.path.exists(config.output_path):
os.mkdir(config.output_path)
# Get actual blocks
blocks = get_children(config.target_id)
# Write(Export) Markdown file
with open(
os.path.join(config.output_path, config.file_name + ".md"),
"w",
encoding="utf-8",
) as output:
output.write(BlockConvertor(config).convert(blocks))
# Make Zip file and Delete tmp
if not config.unzipped:
zip_dir(
os.path.join(config.output_path, config.file_name) + ".zip",
config.tmp_path,
class Exporter:
def __init__(
self,
block_id: str = None,
block_url: str = None,
output_filename: str = None,
output_path: str = None,
download: bool = False,
unzipped: bool = False,
):
self._config = Config(
block_id=block_id,
block_url=block_url,
output_filename=output_filename,
output_path=output_path,
download=download,
unzipped=unzipped,
)
shutil.rmtree(config.tmp_path)
self._client = NotionClient()
self._io = None
self._block_convertor = None

@property
def block_convertor(self):
if not self._block_convertor:
self._block_convertor = BlockConvertor(
self._config, self._client, self._io
)
return self._block_convertor

@property
def config(self):
return self._config

@property
def io(self):
return self._io

@io.setter
def io(self, io):
self._io = io

def create_directories(self):
if not os.path.exists(self._config.tmp_path):
os.makedirs(self._config.tmp_path)
if not os.path.exists(self._config.output_path):
os.mkdir(self._config.output_path)

def get_blocks(self):
return self._client.get_children(self._config.target_id)

def string_exporter(
block_id: str = None,
block_url: str = None,
output_filename: str = None,
output_path: str = None,
download: bool = False,
unzipped: bool = False,
):
args = locals()
config = Config(**args)
if config.download and not os.path.exists(config.output_path):
os.mkdir(config.output_path)
if not config.unzipped and not os.path.exists(config.tmp_path):
os.makedirs(config.tmp_path)
blocks = get_children(config.target_id)
md = BlockConvertor(config).to_string(blocks)
# Make Zip file and Delete tmp
if not config.unzipped:
def make_zip(self):
zip_dir(
os.path.join(config.output_path, config.file_name) + ".zip",
config.tmp_path,
os.path.join(self._config.output_path, self._config.file_name)
+ ".zip",
self._config.tmp_path,
)
shutil.rmtree(config.tmp_path)
return md
shutil.rmtree(self._config.tmp_path)

def export(self):
pass


class MarkdownExporter(Exporter):
def export(self):
self.create_directories()
with open(
os.path.join(
self._config.output_path, self._config.file_name + ".md"
),
"w",
encoding="utf-8",
) as output:
output.write(self.block_convertor.convert(self.get_blocks()))
if not self._config.unzipped:
self.make_zip()


class StringExporter(Exporter):
def export(self):
return self.block_convertor.to_string(self.get_blocks())

0 comments on commit 2b35818

Please sign in to comment.