#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import csv
import json
import shutil

try:
  import pyopencc
  cc = pyopencc.OpenCC('zhs2zht.ini')
  jtof = cc.convert
except:
  print('cannot import opencc, import jianfan')
  sys.path.append(os.path.join(os.path.dirname(__file__), '../gae/libs'))
  from jianfan import jtof


dictBooksJsonPath = os.path.join(os.path.dirname(__file__), 'books.json')
with open(dictBooksJsonPath, 'r') as f:
  dicIndex = json.loads(f.read())


dictWordsCSV1Path = os.path.join(os.path.dirname(__file__),
    "../../../data/pali/common/dictionary/dict_words_1.csv")
dictWordsCSV2Path = os.path.join(os.path.dirname(__file__),
    "../../../data/pali/common/dictionary/dict_words_2.csv")

dictWordsJsonDir = os.path.join(os.path.dirname(__file__), 'paliwords')


def processWordCSV(csvPath):
  with open(csvPath, "r") as wordsCsvfile:
    wordreader = csv.reader(wordsCsvfile, delimiter=',', quotechar='"')
    for row in wordreader:

      if len(row) != 7:
        raise Exception('len(row) != 7')

      if row[0] == 'db_id': continue

      """
      print(dicIndex[row[2]]['locale'])
      print(dicIndex[row[2]]['data'][2])
      print(dicIndex[row[2]]['data'][3])
      print(row[4])
      print(row[6])
      """

      #path = os.path.join(output_dir, '%s.json' % 
      path = os.path.join(dictWordsJsonDir, '%s' % 
          row[4].decode('utf-8').lower())
      print(path)
      #print([dicIndex[row[2]]['data'][1], row[6]])

      if os.path.exists(path):
        # append new data to existing data
        with open(path, 'r') as f:
          data = json.loads(f.read())

        if dicIndex[row[2]]['locale'] == 'zh':
          data.append([dicIndex[row[2]]['data'][1], jtof(row[6])])
        else:
          data.append([dicIndex[row[2]]['data'][1], row[6]])

        with open(path, 'w') as f:
          f.write(json.dumps(data))
      else:
        # create new data file
        if dicIndex[row[2]]['locale'] == 'zh':
          data = [ [dicIndex[row[2]]['data'][1], jtof(row[6])] ]
        else:
          data = [ [dicIndex[row[2]]['data'][1], row[6]] ]

        with open(path, 'w') as f:
          f.write(json.dumps(data))


if __name__ == '__main__':
  if os.path.exists(dictWordsJsonDir):
    shutil.rmtree(dictWordsJsonDir)
    os.makedirs(dictWordsJsonDir)
  else:
    os.makedirs(dictWordsJsonDir)

  processWordCSV(dictWordsCSV1Path)
  processWordCSV(dictWordsCSV2Path)

@@ -332,500 +332,61 @@
 %0A%0A%0Ad
-ictBooksJsonPath = os.path.join(os.path.dirname(__file__), 'books.json')%0Awith open(dictBooksJsonPath, 'r') as f:%0A  dicIndex = json.loads(f.read())%0A%0A%0Adict
+ef process
 Word
-s
 CSV
-1Path = os.path.join(os.path.dirname(__file__),%0A    %22../../../data/pali/common/dictionary/dict_words_1.csv%22)%0AdictWordsCSV2Path = os.path.join(os.path.dirname(__file__),%0A    %22../../../data/pali/common/dictionary/dict_words_2.csv%22)%0A%0AdictWordsJsonDir = os.path.join(os.path.dirname(__file__), 'paliwords')%0A%0A%0Adef processWordCSV(csvPath
+(csvPath, dicIndex, dictWordsJsonDir
 ):%0A 
@@ -1775,155 +1775,693 @@
 :%0A  
-if os.path.exists(dictWordsJsonDir):%0A    shutil.rmtree(dictWordsJsonDir)%0A    os.makedirs(dictWordsJsonDir)%0A  else:%0A    os.makedirs(dictWordsJsonDir
+# read index of dictionary books%0A  dictBooksJsonPath = os.path.join(os.path.dirname(__file__),%0A      '../gae/libs/json/books.json')%0A  with open(dictBooksJsonPath, 'r') as f:%0A    dicIndex = json.loads(f.read())%0A%0A  dictWordsJsonDir = os.path.join(os.path.dirname(__file__), 'paliwords')%0A  if os.path.exists(dictWordsJsonDir):%0A    shutil.rmtree(dictWordsJsonDir)%0A    os.makedirs(dictWordsJsonDir)%0A  else:%0A    os.makedirs(dictWordsJsonDir)%0A%0A  dictWordsCSV1Path = os.path.join(os.path.dirname(__file__),%0A      %22../../../data/pali/common/dictionary/dict_words_1.csv%22)%0A  dictWordsCSV2Path = os.path.join(os.path.dirname(__file__),%0A      %22../../../data/pali/common/dictionary/dict_words_2.csv%22
 )%0A%0A 
@@ -2493,16 +2493,44 @@
 CSV1Path
+, dicIndex, dictWordsJsonDir
 )%0A  proc
@@ -2557,10 +2557,38 @@
 CSV2Path
+, dicIndex, dictWordsJsonDir
 )%0A

update parsing words script