Skip to content

Commit

Permalink
redo command engine
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi-arya1 committed Jul 6, 2024
1 parent 9bae9a5 commit e3add20
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 42 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This will make `tuna` executable from anywhere on your machine.
**1. Initialize**

```bash
tuna --init
tuna init

# Initializes a `.tuna` folder
# Authenticates your GitHub credentials
Expand All @@ -45,16 +45,21 @@ tuna --init
**2. Serve**

```bash
tuna --serve
tuna serve
# Runs a Local Jupyter Environment with the
# autogenerated notebook and dataset,
# with CPU and Memory monitoring

# By default, this doesn't open the browser
# automatically. Run:
tuna serve --open
# to do that
```

**3. Refresh**

```bash
tuna --refresh
tuna refresh
# Recreates the dataset after updating
# from your GitHub project, in case you made
# edits after initializing with Tuna
Expand Down
24 changes: 19 additions & 5 deletions tuna/cli/constants.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from pathlib import Path
from os import getcwd


CHECK_ICON = "\u2714" # Check mark
CROSS_ICON = "\u2716" # Cross mark
WARNING_ICON = "\u26A0" # Warning sign
LOADING_ICON = "\u27F3" # Loading sign
INFO_ICON = "\u24D8" # Information sign



CWD = Path(getcwd())

HELLO = """
🎣 Welcome to Tuna
Commands:
tuna --init : Initialize Tuna in your current directory
tuna --serve : Run Tuna in JupyterLab
tuna --refresh : Refresh GitHub cache in your current directory
tuna --edit : Edit the Tuna notebook in your current directory
tuna : Redisplay this message
⋅ tuna init : Initialize Tuna in your current directory
⋅ tuna serve : Run Tuna in JupyterLab, no automatic browser opening
⋅ tuna serve --open : Same as above with automatic browser opening
⋅ tuna serve --no-open : Same as "tuna serve"
⋅ tuna refresh : Refresh GitHub cache in your current directory
⋅ tuna : Redisplay this message
Help : Contact [email protected]
Expand All @@ -23,6 +33,10 @@
CONFIG_FILE = TUNA_DIR / 'tuna.config.json'
AUTH_FILE = TUNA_DIR / 'auth.config.json'

TUNA_GITIGNORE = """
ipynb_checkpoints/
auth.config.json
"""

IPYNB_REQUIREMENTS = """
transformers
Expand Down
15 changes: 5 additions & 10 deletions tuna/cli/jupyter_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@
import psutil
import sys
from .util import clear_terminal
from .constants import TUNA_DIR
from .constants import TUNA_DIR, CHECK_ICON, CROSS_ICON, WARNING_ICON, LOADING_ICON, INFO_ICON

CHECK_ICON = "\u2714" # Check mark
CROSS_ICON = "\u2716" # Cross mark
WARNING_ICON = "\u26A0" # Warning sign
LOADING_ICON = "\u27F3" # Loading sign
INFO_ICON = "\u24D8" # Information sign


def start_lab():
def start_lab(browser: bool):
print(f"[{LOADING_ICON} Starting TunaLab...")
process = subprocess.Popen(['jupyter', 'lab'], cwd=(TUNA_DIR), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
command = ['jupyter', 'lab'] if browser else ['jupyter', 'lab', '--no-browser']
process = subprocess.Popen(command, cwd=(TUNA_DIR), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return process


def monitor_lab(process):
clear_terminal()
print(f"[{INFO_ICON}] Running TunaLab on \033[1mhttp://localhost:8888/lab\033[0m")
print(f"[{INFO_ICON}] Running TunaLab on \033[1mhttp://localhost:8888/lab/tree/tuna.ipynb\033[0m")
print(f"[{INFO_ICON}] Type CTRL+C to end lab. Monitoring CPU and Memory usage...\n\n\n\n")

try:
Expand Down
82 changes: 58 additions & 24 deletions tuna/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .github_fs import fetch
from .jupyter_fs import start_lab, monitor_lab, kill_lab
from .constants import TUNA_DIR, AUTH_FILE, HELLO
from .constants import TUNA_DIR, AUTH_FILE, HELLO, CROSS_ICON, INFO_ICON, WARNING_ICON
import json
import os
import inquirer
Expand All @@ -18,8 +18,8 @@ def load_credentials():
def save_credentials(username, token):
with open(AUTH_FILE, 'w') as f:
json.dump({
'message': 'This file contains your GitHub credentials. Do not share this file with anyone! If you think your credentials have been compromised, delete this file and run `tuna --init` again.',
'data_usage': 'We do not store this file. You can delete it whenever you want to revoke the Tuna CLI\'s access, and run `tuna --init` again to reauthenticate.',
'message': 'This file contains your GitHub credentials. Do not share this file with anyone! If you think your credentials have been compromised, delete this file and run `tuna init` again.',
'data_usage': 'We do not store this file. You can delete it whenever you want to revoke the Tuna CLI\'s access, and run `tuna init` again to reauthenticate.',
'username': username,
'token': token
}, f)
Expand All @@ -40,34 +40,68 @@ def authenticate():
return username, token


def validate():
username, token = load_credentials()
if not username or not token:
print(f"[{CROSS_ICON}] You haven't set up a repository yet, run `tuna init` to start")
exit(1)



def init():
print(f"[{INFO_ICON}] Let's get started...")
TUNA_DIR.mkdir(exist_ok=True)
username, token = load_credentials()
if not username or not token:
username, token = authenticate()
save_credentials(username, token)
repo = fetch(username, token)
print(repo)



def serve(browser: bool=False):
validate()
lab = start_lab(browser)
try:
monitor_lab(lab)
except KeyboardInterrupt:
kill_lab(lab)



def refresh():
validate()
print(f"[{INFO_ICON}] Refreshing the Tuna Cache in your current directory")



def main():
if len(argv) == 1:
print(HELLO)
exit(0)

if argv[1] == "--init":
print("Let's get started...")
TUNA_DIR.mkdir(exist_ok=True)
username, token = load_credentials()
if not username or not token:
username, token = authenticate()
save_credentials(username, token)
repo = fetch(username, token)
print(repo)

elif argv[1] == "--serve":
lab = start_lab()
try:
monitor_lab(lab)
except KeyboardInterrupt:
kill_lab(lab)

elif argv[1] == "--refresh":
print("Refreshing GitHub cache in your current directory")
elif argv[1] == "--edit":
if argv[1] == "init":
init()

elif argv[1] == "serve":
if(len(argv)) > 2:
if argv[2] == "--open":
serve(browser=True)
elif argv[2] == "--no-open":
serve()
else:
print(f"[{WARNING_ICON}] Invalid flag '{argv[2]}'. Run 'tuna' for help")
else:
serve()

elif argv[1] == "refresh":
validate()

elif argv[1] == "edit":
print("Editing the Tuna notebook in your current directory")
else:
print("Invalid flag. Run 'tuna' for help")
print(f"[{WARNING_ICON}] Invalid option '{argv[1]}'. Run 'tuna' for help")

exit(0)

Expand Down

0 comments on commit e3add20

Please sign in to comment.