-
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.
refactor(cli): Move init functions to its own module
- Loading branch information
Estrada Irribarra, Rodrigo Andres
committed
Oct 28, 2024
1 parent
2ddfb77
commit e4f4b92
Showing
1 changed file
with
162 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
import sys | ||
import json | ||
import requests | ||
from rich.console import Console | ||
from pathlib import Path | ||
import storycraftr.templates.folder_story | ||
from storycraftr.agent.story.agents import create_or_get_assistant | ||
from storycraftr.templates.tex import TEMPLATE_TEX | ||
|
||
console = Console() | ||
|
||
|
||
# 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) | ||
|
||
|
||
# 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]") | ||
|
||
# Download additional files | ||
urls = [ | ||
"https://raw.githubusercontent.com/raestrada/papercraftr/refs/heads/main/docs/getting_started.md", | ||
"https://raw.githubusercontent.com/raestrada/papercraftr/refs/heads/main/docs/iterate.md", | ||
"https://raw.githubusercontent.com/raestrada/papercraftr/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(paper_path) / "papercraftr", filename) | ||
|
||
create_or_get_assistant(paper_path) |