# ------------------------------------------------------------------
# File generated by save_this_cell
# Notebook: colab.ipynb
# Saved: 2026-03-06 19:42:16
# ------------------------------------------------------------------

import os

def require_gdrive(drivepath = "/content/drive/", verbose=False):
  if not  os.path.exists(drivepath):
    from google.colab import drive
    drive.mount("/content/drive")
  else:
    if verbose:
      print("Google drive already mounted.")


from pathlib import Path
from IPython import get_ipython
from datetime import datetime

def save_this_cell(filename, *, below_only=False, notebook=None, encoding="utf-8"):
    """
    Save the contents of the current notebook cell to `filename`.

    Default behaviour:
        Save the whole cell but remove lines whose first non-whitespace
        text is 'save_this_cell'.

    If below_only=True:
        Save only the lines below the first such call.
    """
    ip = get_ipython()
    if ip is None:
        raise RuntimeError("This function requires an IPython/Jupyter environment.")

    parent = ip.get_parent()
    content = parent.get("content", {})
    code = content.get("code")

    if code is None:
        raise RuntimeError("Could not retrieve current cell source.")

    lines = code.splitlines()

    if below_only:
        for i, line in enumerate(lines):
            if line.lstrip().startswith("save_this_cell"):
                lines = lines[i+1:]
                break
    else:
        lines = [line for line in lines
                 if not line.lstrip().startswith("save_this_cell")]

    # Determine notebook name
    if notebook is None:
        try:
            notebook = content.get("metadata", {}).get("notebook")
        except Exception:
            notebook = None

    if not notebook:
        notebook = "unknown_notebook"

    t = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    header = [
        "# ------------------------------------------------------------------",
        "# File generated by save_this_cell",
        f"# Notebook: {notebook}",
        f"# Saved: {t}",
        "# ------------------------------------------------------------------",
        ""
    ]

    text = "\n".join(header + lines) + ("\n" if lines else "")
    Path(filename).write_text(text, encoding=encoding)


