-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): enhance CLI with subcommands for database management and a…
…dd schema retrieval functionality - Introduced subcommands for creating databases and retrieving existing database schemas. - Updated argument parsing to support new commands and improved error handling. - Added a new script to retrieve and format the schema of a Notion database. - Updated .gitignore to exclude notion_automation.log from version control.
- Loading branch information
1 parent
5abaeb3
commit 7afec84
Showing
4 changed files
with
111 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,5 @@ venv/ | |
|
||
plugins | ||
|
||
*.log | ||
notion_automation.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import sys | ||
import json | ||
from notion_client import Client | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
def get_database_schema(database_id): | ||
"""Retrieve and format the schema of a Notion database.""" | ||
load_dotenv() | ||
notion = Client(auth=os.getenv("NOTION_TOKEN")) | ||
|
||
try: | ||
database = notion.databases.retrieve(database_id=database_id) | ||
|
||
# Extract and format the properties schema | ||
schema = { | ||
"title": database["title"][0]["plain_text"] if database.get("title") else "", | ||
"properties": {} | ||
} | ||
|
||
for prop_name, prop_data in database["properties"].items(): | ||
prop_type = prop_data["type"] | ||
prop_info = { | ||
"type": prop_type, | ||
"name": prop_name | ||
} | ||
|
||
# Add additional info for specific property types | ||
if prop_type == "select": | ||
prop_info["options"] = [ | ||
option["name"] for option in prop_data["select"]["options"] | ||
] | ||
elif prop_type == "multi_select": | ||
prop_info["options"] = [ | ||
option["name"] for option in prop_data["multi_select"]["options"] | ||
] | ||
|
||
schema["properties"][prop_name] = prop_info | ||
|
||
return schema | ||
|
||
except Exception as e: | ||
print(f"Error retrieving database: {str(e)}", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
print("Usage: python get_database.py <database_id>", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
database_id = sys.argv[1] | ||
schema = get_database_schema(database_id) | ||
|
||
# Pretty print the schema | ||
print(json.dumps(schema, indent=2)) | ||
|
||
if __name__ == "__main__": | ||
main() |