"""Module containing a transformer that converts outputs in the notebook from 
one format to another.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
import sys
import subprocess

from IPython.utils.tempdir import TemporaryDirectory
from IPython.utils.traitlets import Unicode

from .convertfigures import ConvertFiguresTransformer


#-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------

INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'


#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------

class SVG2PDFTransformer(ConvertFiguresTransformer):
    """
    Converts all of the outputs in a notebook from one format to another.
    """

    from_format = Unicode('svg', config=True, help='Format the converter accepts')
    to_format = Unicode('pdf', config=False, help='Format the converter writes')


    def convert_figure(self, data_format, data):
        """
        Convert a single Svg figure.  Returns converted data.
        """

        #Work in a temporary directory
        with TemporaryDirectory() as tmpdir:
            
            #Write fig to temp file
            input_filename = os.path.join(tmpdir, 'figure.' + data_format)
            with open(input_filename, 'w') as f:
                f.write(data)

            #Determine command (different on Mac OSX)
            command = INKSCAPE_COMMAND 
            if sys.platform == 'darwin':
                command = INKSCAPE_OSX_COMMAND

            #Call conversion application
            output_filename = os.path.join(tmpdir, 'figure.pdf')
            shell = command.format(from_filename=input_filename, 
                                   to_filename=output_filename)
            subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.

            #Read output from drive
            if os.path.isfile(output_filename):
                with open(output_filename, 'rb') as f:
                    return f.read().encode("base64") #PDF is a nb supported binary
                                                     #data type, so base64 encode.
            else:
                return TypeError("Inkscape svg to png conversion failed")

@@ -621,16 +621,30 @@
 ------%0A%0A
+import base64%0A
 import o
@@ -1519,37 +1519,26 @@
 ok from 
-one format to another
+SVG to PDF
 .%0A    %22%22
@@ -1704,16 +1704,628 @@
 rites')%0A
+    command = Unicode(config=True,%0A        help=%22%22%22The command to use for converting SVG to PDF%0A        %0A        This string is a template, which will be formatted with the keys%0A        to_filename and from_filename.%0A        %0A        The conversion call must read the SVG from %7Bfrom_flename%7D,%0A        and write a PDF to %7Bto_filename%7D.%0A        %22%22%22)%0A    %0A    def _command_default(self):%0A        if sys.platform == %22darwin%22:%0A            return INKSCAPE_OSX_COMMAND%0A        elif sys.platform == %22win32%22:%0A            # windows not yet supported%0A            return %22%22%0A        else:%0A            return INKSCAPE_COMMAND%0A
 %0A%0A    de
@@ -2409,17 +2409,24 @@
 le S
-vg
+VG
  figure
+ to PDF
 .  R
@@ -2709,16 +2709,17 @@
 name, 'w
+b
 ') as f:
@@ -2754,191 +2754,8 @@
 a)%0A%0A
-            #Determine command (different on Mac OSX)%0A            command = INKSCAPE_COMMAND %0A            if sys.platform == 'darwin':%0A                command = INKSCAPE_OSX_COMMAND%0A%0A
     
@@ -2876,16 +2876,21 @@
 shell = 
+self.
 command.
@@ -3116,16 +3116,62 @@
 m drive%0A
+            # return value expects a filename%0A
         
@@ -3289,102 +3289,69 @@
     
-return f.read().encode(%22base64%22) #PDF is a nb supported binary%0A                               
+# PDF is a nb supported binary, data type, so base64 encode.%0A
     
@@ -3370,39 +3370,44 @@
     
-  #data type, so
+return
  base64
- 
+.
 encode
-.
+string(f.read())
 %0A   

make inkscape command configurable