# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).

__all__ = ['drive_setup', 'change_dir', 'home_dir', 'clone_new_repo', 'setup_nb']

# Cell
import os

# Cell
#export
os.system('pip install nbdev')
os.system('pip install fastcore')
import nbdev
from nbdev.showdoc import *
from fastcore.test import *

# Cell
from google.colab import drive
from pathlib import Path
from getpass import getpass
import urllib

# Cell
def drive_setup():
  "Connect Google Drive to Colab instance"
  drive.mount('/content/drive', force_remount=True)
  os._exit(00)

# Cell
class _StopExecution(Exception):
    def _render_traceback_(self):
        pass

# Cell
def change_dir(path):
   p = Path(path)
   os.chdir(p)

# Cell
home_dir = "/content/drive/My Drive"

# Cell
def _check_input(type, input):
  if input == "":
    print(f'Error: {type} required')
    raise StopExecution
  else: return input

def _get_dest_dir():
  dir = input(f'Destination directory {home_dir}/')
  dest_dir = home_dir+"/"+dir
  dir_exists = os.path.isdir(dest_dir)
  if dir_exists:
    return dest_dir
  else:
    print(f"Error: Destination directory {dest_dir} does not exist\n")
    raise _StopExecution

def _get_repo(dir):
  repo = input('Repo name: ')
  repo_path = dir+"/"+repo
  path_exists = os.path.exists(repo_path)
  if path_exists:
    print(f"Error: file/folder {repo} already exists at destination {dir}. Function can only be used to create new repo\n")
    raise _StopExecution
  else:
     return repo

# Cell
def clone_new_repo():
  "Clone repo from github to google drive and configure"
  print('\n** nbd_dev does NOT store user details but github username and password \nare stored in the local github repo to allow automatic authentication from Colab.\n')

  dest_dir = _get_dest_dir()
  repo = _get_repo(dest_dir)
  user = input('Username: ')
  user_email = input('User email: ')
  password = getpass('Password: ')

  # converts password into url format
  password = urllib.parse.quote(password)

  confirm = input('Confirm and clone y/n?')
  if confirm == 'y':
    change_dir(dest_dir)
    cmd_string = 'git clone https://{0}:{1}@github.com/{0}/{2}.git'.format(user, password, repo)
    try:
      os.system(cmd_string)
    except:
      print('Error: Clone failed. Please review entries and try again. User details purged')
    finally:
      # purge password containing variables
      cmd_string, password = None, None

    if os.path.exists(dest_dir+"/"+repo):
      print(f'{repo_name} successfully cloned into {dest_dir}:')
      os.system('ls')
    else:
      print('Error: Clone failed. Please review entries and try again. User details purged')
      raise _StopExecution

    # save user email and username into local git repo to identify user for git commands
    os.system('git config user.name user')
    os.system('git config user.email user_email')

    # Install git hooks to clean up notebook metadata for smooth github integration
    os.system('nbdev_install_git_hooks')

  else:
    cmd_string, password = None, None
    print('Clone cancelled\n')
    raise _StopExecution


# Cell
def setup_nb(repo, dir=None):
  "Setup a new module notebook for use with nbdev"
  drive_setup()

  dir = home_dir if dir == None else dir
  change_dir(dir+"/"+repo)
  os.system('ls')