{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# File Register Example\n",
    "How to use an XML based file register class. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import pyMez \n",
    "The XMLModels module (pyMez.Code.DataHandlers.XMLModels) has the FileRegister class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Importing pyMez, this should take roughly 30 seconds\n",
      "Importing Code.DataHandlers.GeneralModels\n",
      "It took 6.065 s to import Code.DataHandlers.GeneralModels\n",
      "Importing Code.DataHandlers.HTMLModels\n",
      "It took 0.464 s to import Code.DataHandlers.HTMLModels\n",
      "Importing Code.DataHandlers.NISTModels\n",
      "It took 16.802 s to import Code.DataHandlers.NISTModels\n",
      "Importing Code.DataHandlers.TouchstoneModels\n",
      "It took 0.013 s to import Code.DataHandlers.TouchstoneModels\n",
      "Importing Code.DataHandlers.XMLModels\n",
      "It took 0.649 s to import Code.DataHandlers.XMLModels\n",
      "Importing Code.DataHandlers.ZipModels\n",
      "It took 0.066 s to import Code.DataHandlers.ZipModels\n",
      "Importing Code.InstrumentControl.Experiments\n",
      "It took 2.243 s to import Code.InstrumentControl.Experiments\n",
      "Importing Code.InstrumentControl.Instruments\n",
      "It took 0.017 s to import Code.InstrumentControl.Instruments\n",
      "Importing Code.Utils.Names\n",
      "It took 0.012 s to import Code.Utils.Names\n",
      "It took 26.332 s to import all of the active modules\n"
     ]
    }
   ],
   "source": [
    "from pyMez import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Create a new file register\n",
    "new_file_register=FileRegister()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# now add all the files in the current working directory\n",
    "new_file_register.add_tree(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Date': '2018-11-14T17:08:34.082000',\n",
       " 'Host': '672PIF-AS1.campus.nist.gov',\n",
       " 'Id': '132.163.48.125.11.11.11.11.11.11.11.11.11.11',\n",
       " 'Type': 'Directory',\n",
       " 'URL': 'file:C:/ProgramData/Anaconda2/Lib/site-packages/pyMez/Documentation/Examples/jupyter/.ipynb_checkpoints'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# now we have created an xml sheet with all of the current files and directories and a ip like id\n",
    "new_file_register.get_data_dictionary_list()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can create html and show it\n",
    "html_text=new_file_register.to_HTML(os.path.join(TESTS_DIRECTORY,\"../XSL/FR_STYLE.xsl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "html=HTMLBase(html_text=html_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file://c:/users/sandersa/appdata/local/temp/1/tmpibiagb.html\n"
     ]
    }
   ],
   "source": [
    "html.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# we can add another file\n",
    "new_file_register.add_entry(os.path.join(PYMEASURE_ROOT,\"Documentation/pyMez_Documentation.html\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Date': '2018-11-14T17:09:02.630000',\n",
       " 'Host': '672PIF-AS1.campus.nist.gov',\n",
       " 'Id': '132.163.48.125.11.11.11.12.11.11.11.11.11.11.11.21',\n",
       " 'Type': 'Ordinary',\n",
       " 'URL': 'file:C:/ProgramData/Anaconda2/lib/site-packages/pyMez/Code/InstrumentControl/../../Documentation/pyMez_Documentation.html'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# now we have a dictionary representation\n",
    "new_file_register.get_data_dictionary_list()[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2018-11-14T17:08:34.082000',\n",
       " 'file:C:/ProgramData/Anaconda2/Lib/site-packages/pyMez/Documentation/Examples/jupyter/.ipynb_checkpoints',\n",
       " '672PIF-AS1.campus.nist.gov',\n",
       " 'Directory',\n",
       " '132.163.48.125.11.11.11.11.11.11.11.11.11.11']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A list representation\n",
    "new_file_register.get_data()[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<File Date=\"2018-11-14T17:08:34.082000\" Host=\"672PIF-AS1.campus.nist.gov\" Id=\"132.163.48.125.11.11.11.11.11.11.11.11.11.11\" Type=\"Directory\" URL=\"file:C:/ProgramData/Anaconda2/Lib/site-packages/pyMez/Documentation/Examples/jupyter/.ipynb_checkpoints\"/>\n"
     ]
    }
   ],
   "source": [
    "# and we can use xpath syntax\n",
    "print(new_file_register[\".//File\"][0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
