#!/usr/bin/env python
# encoding: utf-8
"""
    executable to convert yaml input into groceries.app
    compatible shopping lists
"""

import yaml
import sys

from os.path import expanduser
from optparse import OptionParser
from lib.__version__ import __version__
from lib.groceries_exporter import GroceriesExporter

GROCERIES_PATH = expanduser("~")+"/.groceries_exporter/groceries.json"
GROCERIES_TEMPLATE = expanduser("~")+"/.groceries_exporter/groceries.mustache"


def init_parser():
    """method to init options parser"""
        # initialize parser
    usage = "usage: %prog [-j groceries_dump] [-t template] groceries.yaml"
    parser = OptionParser(usage, version="%prog "+__version__)
    parser.add_option("-j", "--groceries_dump", action="store",
                      dest="groceries_json", help="groceries json dump")
    parser.add_option("-t", "--template", action="store", dest="template",
                      help="mustache template to fill")

    return parser.parse_args()

def main():
    """ main function
    """
    (options, args) = init_parser()

    if not options.groceries_json:
        groceries_file = GROCERIES_PATH
    else:
        groceries_file = options.groceries_json
    print groceries_file

    gc = GroceriesExporter(groceries_file)
    if gc == None:
        sys.exit(-1)
    groceries = yaml.safe_load(open(args[0]).read())
    print gc.export_list(groceries)

if __name__ == '__main__':
    main()

@@ -150,16 +150,32 @@
 port sys
+%0Aimport pystache
 %0A%0Afrom o
@@ -1222,32 +1222,147 @@
 son%0A
+%0A
     
-print groceries_file
+if not options.template:%0A        template = open(GROCERIES_TEMPLATE).read()%0A    else:%0A        template = open(options.template).read()
 %0A%0A  
@@ -1503,13 +1503,19 @@
     
-print
+data_json =
  gc.
@@ -1537,16 +1537,268 @@
 ceries)%0A
+    items = gc.data%5B'items'%5D%0A    ctx = %7B%7D%0A    ctx%5B'groceries'%5D = data_json%0A    ctx%5B'listname'%5D = gc.data%5B'name'%5D%0A    ctx%5B'itemcount'%5D = len(gc.data%5B'items'%5D)%0A    ctx%5B%22items%22%5D = gc.list_data%0A    output = pystache.render(template, ctx)%0A    print output%0A%0A
 %0Aif __na

add pystache support