Skip to content

Commit

Permalink
refactor(cli): Move init functions to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrada Irribarra, Rodrigo Andres committed Oct 28, 2024
1 parent dac7e96 commit 2ddfb77
Showing 1 changed file with 2 additions and 145 deletions.
147 changes: 2 additions & 145 deletions storycraftr/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import click
import os
import sys
import json
import requests
from rich.console import Console
from pathlib import Path

Expand Down Expand Up @@ -40,12 +38,9 @@ def load_openai_api_key():
load_openai_api_key()

# Import statements grouped together for clarity
import storycraftr.templates.folder_story
import storycraftr.templates.folder_paper
from storycraftr.state import debug_state
from storycraftr.cmd.publish import publish
from storycraftr.cmd.chat import chat
from storycraftr.templates.tex import TEMPLATE_TEX
from storycraftr.agent.story.agents import create_or_get_assistant, update_agent_files
from storycraftr.utils.core import load_book_config

Expand All @@ -62,6 +57,8 @@ def load_openai_api_key():
from storycraftr.cmd.paper.analyze import analyze as paper_analyze
from storycraftr.cmd.paper.finalize import finalize as paper_finalize

from storycraftr.init import init_structure_story, init_structure_paper


# Detect CLI (storycraftr or papercraftr)
def detect_invocation():
Expand All @@ -75,33 +72,6 @@ def detect_invocation():
cli_name = detect_invocation()


# Download files from a URL to a specified directory
def download_file(url, save_dir, filename):
"""
Downloads a file from a URL and saves it in the specified directory.
Args:
url (str): The URL to download the file from.
save_dir (str): The directory where the file will be saved.
filename (str): The name of the file.
Raises:
SystemExit: If the file download fails.
"""
save_dir = Path(save_dir)
save_dir.mkdir(parents=True, exist_ok=True)
save_path = save_dir / filename

try:
response = requests.get(url, timeout=10)
response.raise_for_status()
save_path.write_text(response.text, encoding="utf-8")
console.print(f"[green]File downloaded successfully from {url}[/green]")
except requests.exceptions.RequestException as e:
console.print(f"[red]Error downloading the file from {url}: {e}[/red]")
sys.exit(1)


# Verify if the directory contains storycraftr.json
def verify_book_path(book_path=None):
"""
Expand Down Expand Up @@ -155,119 +125,6 @@ def project_not_initialized_error(book_path):
)


# Function to initialize StoryCraftr
def init_structure_story(
book_path,
license,
primary_language,
alternate_languages,
default_author,
genre,
behavior_content,
reference_author,
):
"""
Initializes the StoryCraftr project structure by creating necessary files and folders.
"""
book_name = Path(book_path).name
console.print(
f"[blue]Initializing StoryCraftr project structure: {book_name}[/blue]"
)

# Create project structure based on StoryCraftr templates
for file in storycraftr.templates.folder_story.files_to_create:
file_path = Path(book_path) / file["folder"] / file["filename"]
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(file["content"], encoding="utf-8")
console.print(f"[green]File created: {file_path}[/green]")

# Create configuration file
config_data = {
"book_path": book_path,
"book_name": book_name,
"primary_language": primary_language,
"alternate_languages": alternate_languages,
"default_author": default_author,
"genre": genre,
"license": license,
"reference_author": reference_author,
}
config_file = Path(book_path) / "storycraftr.json"
config_file.write_text(json.dumps(config_data, indent=4), encoding="utf-8")
console.print(f"[green]Configuration file created: {config_file}[/green]")

# Create behavior file
behaviors_dir = Path(book_path) / "behaviors"
behaviors_dir.mkdir(exist_ok=True)
behavior_file = behaviors_dir / "default.txt"
behavior_file.write_text(behavior_content, encoding="utf-8")
console.print(f"[green]Behavior file created: {behavior_file}[/green]")

# Create LaTeX template
template_dir = Path(book_path) / "templates"
template_dir.mkdir(exist_ok=True)
template_file = template_dir / "template.tex"
template_file.write_text(TEMPLATE_TEX, encoding="utf-8")
console.print(f"[green]LaTeX template created: {template_file}[/green]")

# Download additional files
urls = [
"https://raw.githubusercontent.com/raestrada/storycraftr/refs/heads/main/docs/getting_started.md",
"https://raw.githubusercontent.com/raestrada/storycraftr/refs/heads/main/docs/iterate.md",
"https://raw.githubusercontent.com/raestrada/storycraftr/refs/heads/main/docs/chat.md",
]
filenames = ["getting_started.md", "iterate.md", "chat.md"]
for url, filename in zip(urls, filenames):
download_file(url, Path(book_path) / "storycraftr", filename)

create_or_get_assistant(book_path)


# Function to initialize PaperCraftr
def init_structure_paper(
paper_path,
primary_language,
author,
reference_author,
keywords,
behavior_content,
):
"""
Initializes the PaperCraftr project structure by creating necessary files and folders.
"""
paper_name = Path(paper_path).name
console.print(
f"[blue]Initializing PaperCraftr project structure: {paper_name}[/blue]"
)

# Create project structure based on PaperCraftr templates
for file in storycraftr.templates.folder_paper.files_to_create:
file_path = Path(paper_path) / file["folder"] / file["filename"]
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(file["content"], encoding="utf-8")
console.print(f"[green]File created: {file_path}[/green]")

# Create configuration file
config_data = {
"book_path": paper_path,
"book_name": paper_name,
"primary_language": primary_language,
"default_author": author,
"reference_author": reference_author,
"keywords": keywords,
}
config_file = Path(paper_path) / "papercraftr.json"
config_file.write_text(json.dumps(config_data, indent=4), encoding="utf-8")
console.print(f"[green]Configuration file created: {config_file}[/green]")

# Create behavior file
behaviors_dir = Path(paper_path) / "behaviors"
behaviors_dir.mkdir(exist_ok=True)
behavior_file = behaviors_dir / "default.txt"
behavior_file.write_text(behavior_content, encoding="utf-8")
console.print(f"[green]Behavior file created: {behavior_file}[/green]")


@click.group()
@click.option("--debug", is_flag=True, help="Enable debug mode.")
def cli(debug):
Expand Down

0 comments on commit 2ddfb77

Please sign in to comment.