Skip to content

Commit

Permalink
feat(cli): support creating empty databases with schema-only option
Browse files Browse the repository at this point in the history
  • Loading branch information
atxtechbro committed Oct 19, 2024
1 parent 7c1f1ca commit 9b8d405
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ To create a database with tasks, run:
python cli.py --schema plugins/schema.json --tasks plugins/tasks.json
```

This will automatically create a Notion database and add your tasks.
If you want to create an empty database without adding tasks, simply omit the --tasks argument:

```bash
python cli.py --schema plugins/schema.json
```

This will create a Notion database based on your schema without adding any tasks.

## Core Files

Expand Down
71 changes: 46 additions & 25 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# Load environment variables from .env
load_dotenv()

def create_database(schema_path, tasks_path):
"""Creates a Notion database and adds tasks."""
def create_database(schema_path, tasks_path=None):
"""Creates a Notion database and optionally adds tasks."""
notion_api_key = os.getenv("NOTION_API_KEY")
notion_page_id = os.getenv("NOTION_PAGE_ID")

Expand All @@ -32,24 +32,6 @@ def create_database(schema_path, tasks_path):

schema_config = SchemaConfig(title=schema_data["title"], properties=properties)

with open(tasks_path, "r") as tasks_file:
tasks_data = json.load(tasks_file)

tasks_config = []
for task_data in tasks_data.get("tasks", []):
task_properties = {}
if "properties" in task_data:
# Existing format
for name, prop in task_data["properties"].items():
task_properties[name] = TaskProperty(**prop)
else:
# Simplified format
for name, value in task_data.items():
task_properties[name] = TaskProperty.from_value(
name, value, properties
)
tasks_config.append(TaskConfig(properties=task_properties))

except FileNotFoundError as e:
error_message = f"Error: {e}"
logger.error(error_message)
Expand All @@ -61,21 +43,60 @@ def create_database(schema_path, tasks_path):
print(error_message)
sys.exit(1)
except Exception as e:
error_message = f"Error processing schema or tasks: {e}"
error_message = f"Error processing schema: {e}"
logger.error(error_message)
print(error_message)
sys.exit(1)

# Initialize tasks_config to an empty list
tasks_config = []

if tasks_path:
try:
with open(tasks_path, "r") as tasks_file:
tasks_data = json.load(tasks_file)

for task_data in tasks_data.get("tasks", []):
task_properties = {}
if "properties" in task_data:
# Existing format
for name, prop in task_data["properties"].items():
task_properties[name] = TaskProperty(**prop)
else:
# Simplified format
for name, value in task_data.items():
task_properties[name] = TaskProperty.from_value(
name, value, properties
)
tasks_config.append(TaskConfig(properties=task_properties))

except FileNotFoundError as e:
error_message = f"Error: {e}"
logger.error(error_message)
print(error_message)
sys.exit(1)
except json.JSONDecodeError as e:
error_message = f"Error parsing JSON: {e}"
logger.error(error_message)
print(error_message)
sys.exit(1)
except Exception as e:
error_message = f"Error processing tasks: {e}"
logger.error(error_message)
print(error_message)
sys.exit(1)

try:
notion_client = NotionClient(api_key=notion_api_key)
database_id = notion_client.create_database(
parent_id=notion_page_id, schema=schema_config
)
print(f"Database created successfully with ID: {database_id}")

for task in tasks_config:
notion_client.create_task(database_id=database_id, task=task)
print("Tasks added successfully.")
if tasks_config:
for task in tasks_config:
notion_client.create_task(database_id=database_id, task=task)
print("Tasks added successfully.")
except Exception as e:
error_message = f"Error creating database or tasks: {e}"
logger.error(error_message)
Expand Down Expand Up @@ -161,7 +182,7 @@ def parse_natural_language_properties(property_descriptions):
description="Create a Notion database and add tasks."
)
parser.add_argument("--schema", required=True, help="Path to the JSON schema file.")
parser.add_argument("--tasks", required=True, help="Path to the JSON tasks file.")
parser.add_argument("--tasks", required=False, help="Path to the JSON tasks file.")

# Parse the arguments
args = parser.parse_args()
Expand Down

0 comments on commit 9b8d405

Please sign in to comment.