{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is experimental work towards a build system for the Jupyter notebooks in the \"remote\" and \"local\" folders."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: gitpython in c:\\users\\paul_000\\anaconda3\\lib\\site-packages\n",
      "Requirement already satisfied: gitdb2>=2.0.0 in c:\\users\\paul_000\\anaconda3\\lib\\site-packages (from gitpython)\n",
      "Requirement already satisfied: smmap2>=2.0.0 in c:\\users\\paul_000\\anaconda3\\lib\\site-packages (from gitdb2>=2.0.0->gitpython)\n"
     ]
    }
   ],
   "source": [
    "!pip install gitpython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import nbformat\n",
    "from traitlets.config import Config\n",
    "from nbconvert import HTMLExporter\n",
    "\n",
    "from os import listdir,mkdir\n",
    "from os.path import join,splitext,isdir\n",
    "from shutil import copy\n",
    "from git import Repo\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "repo_dir=Path(r\"\\Users\\paul_000\\Documents\\GitHub\\o2biz\")\n",
    "repo=Repo(repo_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "assert not repo.bare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#\n",
    "# htmlize and copy both return the name of the file they created,  which in turn gets used in\n",
    "# process_dir\n",
    "#\n",
    "\n",
    "def htmlize(src,dest):\n",
    "    notebook=nbformat.read(src,as_version=4)    \n",
    "    html_exporter = HTMLExporter()\n",
    "    (body, resources) = html_exporter.from_notebook_node(notebook)\n",
    "    dest_file=splitext(dest)[0]+'.html'\n",
    "    with open(dest_file,'wt',encoding=\"utf-8\") as outfile:\n",
    "        outfile.write(body)\n",
    "    return dest_file\n",
    "        \n",
    "extensions={\n",
    "    '.jpg':copy,\n",
    "    '.png':copy,\n",
    "    '.ipynb':htmlize\n",
    "}\n",
    "    \n",
    "\n",
    "def process_dir(source_dir,output_dir):\n",
    "    files=listdir(source_dir)   \n",
    "    for f in files:\n",
    "        src=join(source_dir,f)\n",
    "        dest=join(output_dir,f)\n",
    "        if isdir(src):\n",
    "            if not isdir(dest):\n",
    "                mkdir(dest)\n",
    "            process_dir(src,dest)\n",
    "        else:\n",
    "            fname,ext=splitext(f)\n",
    "            if ext in extensions:\n",
    "                op=extensions[ext]\n",
    "                newfile=Path(op(src,dest)).relative_to(repo_dir)\n",
    "                repo.git.add(newfile)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "nb_source=r\"..\"\n",
    "nb_output=r\"\\Users\\paul_000\\Documents\\GitHub\\o2biz\\o2site\\src\\main\\webapp\\notebooks\"\n",
    "\n",
    "def process(subname):\n",
    "    source_dir=join(nb_source,subname)\n",
    "    output_dir=join(nb_output,subname)\n",
    "    process_dir(source_dir,output_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "process(\"remote\")\n",
    "#process(\"local\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#notebook_path=join(source_dir,'Querying DBpedia.ipynb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#notebook=nbformat.read(notebook_path,as_version=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#html_exporter = HTMLExporter()\n",
    "#(body, resources) = html_exporter.from_notebook_node(notebook)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
