#!/usr/bin/python

import json
import glob
import sys
import os
import getopt
import fileinput
import re

def clean_jsonified_dir():
  if not os.path.exists('jsonified'):
    os.makedirs('jsonified')
  elif os.path.exists('./jsonified/transactions.json'):
    print "Removing ./jsonified/transactions.json"
    os.remove('./jsonified/transactions.json')

def main(argv):
  clean_jsonified_dir()

  inputdir = '' #'./textified/20130926.pdf_deposits'
  output_filename = './jsonified/transactions.json'
  fp = open(output_filename, 'w')
  try:
    opts, args = getopt.getopt(argv,"hi:o:",["idir="])
  except getopt.GetoptError:
    print "json_maker -i <directory>"
    sys.exit(2)
  for opt, arg in opts:
    if opt == '-h':
      print "json_maker -i <directory>"
      sys.exit()
    elif opt in ("-i", "--idir"):
      inputdir = arg

  print 'Input directory is "',inputdir,'"'

  if not os.path.exists(inputdir):
    print 'Directory "',inputdir,'" does not exist. Exiting.'

  index_count = 0
  for bank_statement in os.listdir(inputdir):
    inputfile = open(inputdir + bank_statement, 'r')
    if bank_statement.endswith('deposits'):
      index_type = "deposits"
    elif bank_statement.endswith('withdrawals'):
      index_type = "withdrawals"


    for line in inputfile:
      date = re.search('^[0-9]{2}\/[0-9]{2}\s+', line).group(0).rstrip()
      date = bank_statement[:4] + '-' + date.replace("/", "-")
      subject = re.search('(?![0-9]*\/)(?![0-9])(?!\s+).*\s(?=\$?[0-9,]+\.[0-9]{2})', line)
      subject = subject.group(0)
      subject = subject.rstrip()
      value = re.search('[0-9,]+\.[0-9]{2}', line).group(0).replace(",","")

      json.dump({"index":{"_index": "transactions","_type": index_type, "_id": index_count}}, fp)
      fp.write('\n')
      json.dump({"date": date, "subject": subject, "value": value}, fp)
      fp.write('\n')

      index_count += 1
    inputfile.close

  fp.close
if __name__ == "__main__":
  main(sys.argv[1:])




@@ -367,32 +367,8 @@
 gv):
-%0A  clean_jsonified_dir()
 %0A%0A  
@@ -399,116 +399,9 @@
 ied/
-20130926.pdf_deposits'%0A  output_filename = './jsonified/transactions.json'%0A  fp = open(output_filename, 'w')
+'
 %0A  t
@@ -742,16 +742,126 @@
 tdir,'%22'
+%0A  clean_jsonified_dir()%0A  output_filename = './jsonified/transactions.json'%0A  fp = open(output_filename, 'w')
 %0A%0A  if n
@@ -952,16 +952,68 @@
 ting.'%0A%0A
+  print %22Processing text entries into JSON format.%22%0A
   index_
@@ -1912,16 +1912,55 @@
 unt += 1
+%0A%0A    print bank_statement, %22complete.%22
 %0A    inp
@@ -1971,16 +1971,18 @@
 le.close
+()
 %0A%0A  fp.c
@@ -1985,16 +1985,108 @@
 fp.close
+()%0A%0A  if os.path.exists(output_filename):%0A    print output_filename, %22successfully created!%22
 %0Aif __na

fix bug when using invalid args or help