Skip to content

Commit d73a629

Browse files
committed
Included generation of folder
1 parent 121c5a4 commit d73a629

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.env
22
__pycache__/
33
.bashrc
4-
.bash_history
4+
.bash_history
5+
wiki_processed_files/

m_aux/outputs.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import shutil
3+
4+
def is_folder(path):
5+
"""
6+
Check if the given path points to a folder.
7+
8+
Parameters:
9+
- path (str): The path to check.
10+
11+
Returns:
12+
- bool: True if the path points to a folder, False otherwise.
13+
"""
14+
# Check if the path exists and is a directory
15+
return os.path.exists(path) and os.path.isdir(path)
16+
17+
def prepare_output_folder(folder_path):
18+
"""
19+
Prepare the output folder by ensuring the specified folder exists and is empty.
20+
21+
Parameters:
22+
- folder_path (str): The path to the folder to prepare.
23+
"""
24+
# Check if the path is indeed a folder
25+
if not is_folder(folder_path):
26+
if os.path.exists(folder_path):
27+
# Path exists but is not a folder (likely a file), operation is aborted
28+
print(f"Error: The path '{folder_path}' is not a folder.")
29+
return
30+
else:
31+
# Path does not exist, create the folder
32+
os.makedirs(folder_path)
33+
print(f"Folder '{folder_path}' created.")
34+
return
35+
36+
# If the folder exists, empty it
37+
try:
38+
shutil.rmtree(folder_path)
39+
os.makedirs(folder_path)
40+
print(f"Folder '{folder_path}' was emptied and recreated.")
41+
except Exception as e:
42+
print(f"An error occurred while preparing the folder: {e}")

m_search/notion_blocks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def get_all_children_blocks(notion_client:Client,page_id: str):
1313
list: A list of children blocks.
1414
"""
1515
blocks = notion_client.blocks.children.list(block_id=page_id)
16+
pretty_print(blocks)
1617
return blocks.get("results",[])
1718

1819
def get_block_type(block:dict):
@@ -25,5 +26,6 @@ def get_block_type(block:dict):
2526
Returns:
2627
str: The type of the block.
2728
"""
29+
pretty_print(f'Block: {block.get("id")}, Type: {block.get("type")}')
2830
return block.get("type")
2931

main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from notion_client import Client
44
from notion_client import APIResponseError
55
from m_search.notion_blocks import get_all_children_blocks, get_block_type
6+
from m_aux.outputs import prepare_output_folder
67

78
def main():
89
# Parse command line arguments
910
parser = argparse.ArgumentParser(description="Notion Exporter Application",epilog="NOTE: This program requires the NOTION_TOKEN environment variable to be set.")
1011
parser.add_argument("-l", "--log_level", help="Set the log level", default="INFO", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
12+
parser.add_argument("-o", "--outputs_dir", help="Set the output directory", default="wiki_processed_files")
1113

1214
# Subparsers for commands
1315
subparsers = parser.add_subparsers(dest='command', help='Commands (list or export)')
@@ -31,6 +33,9 @@ def main():
3133
# Here you would set the log level for the Notion client based on args.log_level
3234
# Notion client setup code to apply log level goes here (depends on the client's capabilities)
3335

36+
# Prepare the output folder
37+
prepare_output_folder(args.outputs_dir)
38+
3439
# Handle commands
3540
blocks = get_all_children_blocks(notion,args.page_id)
3641
for block in blocks:

0 commit comments

Comments
 (0)