Skip to content

Commit

Permalink
fix: Indicate parameters explicitly in Config Object
Browse files Browse the repository at this point in the history
  • Loading branch information
echo724 committed Feb 19, 2022
1 parent 4869455 commit 7ea65a4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
32 changes: 20 additions & 12 deletions notion2md/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,41 @@ class Config(object):
"path_name",
)

def __init__(self, **kargs):
if "url" in kargs and kargs["url"]:
self.target_id = get_id(kargs["url"])
elif "id" in kargs and kargs["id"]:
self.target_id = kargs["id"]
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,
):
if block_url:
self.target_id = get_id(block_url)
elif block_id:
self.target_id = block_id
else:
self.target_id = ""

if "name" in kargs and kargs["name"]:
self.file_name = kargs["name"]
if output_filename:
self.file_name = output_filename
else:
self.file_name = self.target_id

if "path" in kargs and kargs["path"]:
self.path_name = kargs["path"]
self.output_path = os.path.abspath(kargs["path"])
if output_path:
self.path_name = output_path
self.output_path = os.path.abspath(output_path)

else:
self.path_name = "notion2md-output"
self.output_path = os.path.join(os.getcwd(), "notion2md-output")

if "download" in kargs and kargs["download"]:
if download:
self.download = True
else:
self.download = False

if "unzipped" in kargs and kargs["unzipped"]:
if unzipped:
self.unzipped = True
self.tmp_path = self.output_path
else:
Expand Down
18 changes: 17 additions & 1 deletion notion2md/console/commands/export_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
from notion2md.util import zip_dir


ARGS_NEW_KEY_MAP = {
"id": "block_id",
"url": "block_url",
"name": "output_filename",
"path": "output_path",
"download": "download",
"unzipped": "unzipped",
}


class ExportBlockCommand(Command):
name = "block"
description = "Export a Notion block object to markdown."
Expand Down Expand Up @@ -71,8 +81,14 @@ def error(self, msg):
sys.exit(1)

def handle(self):
args = {}
for k, v in self.io.input.options.items():
if k in ARGS_NEW_KEY_MAP:
args[ARGS_NEW_KEY_MAP[k]] = v
else:
pass
try:
config = Config(**self.io.input.options)
config = Config(**args)
except UnInitializedConfigException as e:
self.error(e)
if not config.target_id:
Expand Down
26 changes: 22 additions & 4 deletions notion2md/exporter/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
from notion2md.util import zip_dir


def block_markdown_exporter(**kargs):
config = Config(**kargs)
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,
):
config = Config(
block_id, block_url, output_filename, output_path, download, unzipped
)
# Directory Checking and Creating
if not os.path.exists(config.tmp_path):
os.makedirs(config.tmp_path)
Expand All @@ -32,8 +41,17 @@ def block_markdown_exporter(**kargs):
shutil.rmtree(config.tmp_path)


def block_string_exporter(**kargs):
config = Config(**kargs)
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,
):
config = Config(
block_id, block_url, output_filename, output_path, download, unzipped
)
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):
Expand Down

0 comments on commit 7ea65a4

Please sign in to comment.