# -*- coding: utf-8 -*-
"""
nbtutor - a small utility to indicate which cells should be cleared (exercises).
"""

try:
    from nbconvert.preprocessors.base import Preprocessor
except ImportError:
    from IPython.nbconvert.preprocessors.base import Preprocessor


class ClearExercisePreprocessor(Preprocessor):

    def preprocess_cell(self, cell, resources, index):

        if 'clear_cell' in cell.metadata and cell.metadata.clear_cell:
            fname = 'snippets/' + resources['unique_key'] + str(cell['execution_count']) + '.py'
            with open(fname, 'w') as f:
                f.write(cell['source'])
            cell['source'] = ["# %load {0}".format(fname)]
            #cell['source'] = []
            cell['execution_count'] = None
            cell['outputs'] = []

        return cell, resources

@@ -107,16 +107,27 @@
 ).%0A%22%22%22%0A%0A
+import os%0A%0A
 try:%0A   
@@ -468,43 +468,79 @@
 e = 
-'snippets/' + resources%5B'unique_key
+os.path.join(%0A                '_solutions', resources%5B'metadata'%5D%5B'name
 '%5D +
@@ -576,16 +576,17 @@
  + '.py'
+)
 %0A       
@@ -733,21 +733,21 @@
     
-#
 cell%5B'
-s
 ou
-rce
+tputs
 '%5D =
@@ -765,65 +765,23 @@
     
+ #
  cell%5B'
-execution_count'%5D = None%0A            cell%5B'outputs
+source
 '%5D =

Fix the output file name for solution