#!/usr/bin/python

import sys
from os import listdir
from os.path import isfile, splitext, basename
from subprocess import call

# Config
CLI_PATH = '/Applications/calibre.app/Contents/MacOS/'
CONVERTER = 'ebook-convert'

args = sys.argv
if len(args) < 4:
   print "Error: This needs to be input and output folders and target format to process."
   print "Usage: ebook-reformat {input folder} {output folder} {target format}"
   exit()





def process_ebook(source, output):
   print "converting: "+source
   call([CLI_PATH+CONVERTER, source, output])


def generateOutputFilepath(source, output, targetFormat):
   filename = splitext(basename(source))[0]
   return output.rstrip('/')+'/'+filename+'.'+targetFormat

def generateIOFilePairs(source, output, targetFormat):
   fileList = []
   basedir = source.rstrip('/')+'/';
   for f in listdir(basedir):
      fullPath = basedir+f;
      if isfile(fullPath) and f[0] != '.':
         fileList.append({
              'source': fullPath,
              'output': generateOutputFilepath(fullPath, output, targetFormat)
            });
   return fileList


files = generateIOFilePairs(args[1], args[2], args[3])

for info in files:
   process_ebook(info['source'], info['output'])

@@ -213,16 +213,133 @@
 convert'
+%0APARAMS = %5B%0A   '--custom-size', '6x8',%0A   '--base-font-size', '7',%0A   '--font-size-mapping', '7,7,8,10,12,14,16,18'%0A%5D
 %0A%0Aargs =
@@ -348,16 +348,17 @@
 ys.argv%0A
+%0A
 if len(a
@@ -549,19 +549,16 @@
 xit()%0A%0A%0A
-%0A%0A%0A
 def proc
@@ -623,12 +623,23 @@
    c
-all(
+li_task_args = 
 %5BCLI
@@ -670,18 +670,48 @@
  output%5D
+ + PARAMS%0A   call(cli_task_args
 )
-%0A
 %0A%0Adef ge

add conversion config params