#Depends:wineObject
#
#Description: Basically this is just a script that when given a CSPC on the 
#command line, it will look up a bunch of details on the LCBO printer-friendly
#product page and return it to stdout as fields delimited by the "|" character
#
#Usage: python lcboonelookup.py 216 (would look up cspc 216) 
################################################################################
import sys
import codecs
import wineObject
import re
import csv
from wineObject import wineObjectClass

x=wineObjectClass(sys.argv[-1])
if not x.fail:
    a=[x.name,
        x.cspc,
        x.price,
        x.ptype,
        x.sugar,
        x.style,
        x.sweetness,
        x.madeIn,
        x.by,
        x.save,
        x.airmiles,
        x.until,
        x.VQA,
        x.description,
# Nothing really of interest except SQLite cursor usage maybe for reference
# All the real magic is in wineObject.py

import wineObject
from wineObject import wineObjectClass

count = 0
for line in f:
    try:
        count += 1
        tempcspc = line.strip().lstrip("0")
        print str(count) + "  /  " + str(num_lines) + "  =  " + str(
            round(100 * float(count) / float(num_lines), 2)
        ) + "% completed"
        x = wineObject.wineObjectClass(tempcspc)

        if not x.fail:
            # NI have to manually encode string fields
            # this is because csv.writer doesn't natively support unicode.
            # Also could create a sanitizing function for this and map with it.
            row = (
                tempcspc,
                x.name.encode("iso-8859-1"),
                x.by.encode("iso-8859-1"),
                x.price,
                x.madeIn.encode("iso-8859-1"),
                x.ptype.encode("iso-8859-1"),
                x.save,
                x.airmiles,
                x.until.encode("iso-8859-1"),
#Depends on MySQLdb

f=open('activeproducts.txt','r');
g=open('done.tab','w');
import MySQLdb
import sys
conn=MySQLdb.connect(
		host = "localhost",
		user= sys.argv[1],
		passwd = sys.argv[2],
		db = sys.argv[3])
cursor=conn.cursor()
import wineObject
from wineObject import cspclook
for line in f:
	tempcspc=line.strip().lstrip('0');
	print tempcspc
	x=wineObject.wineObjectClass(cspclook(tempcspc),tempcspc)
        cursor.execute ("SELECT DISTINCT * from lcbo_typeconv WHERE name = %s",x.ptype)
	if cursor.fetchone() is None:
        	cursor.execute ("INSERT INTO lcbo_typeconv VALUES (%s,\" \")",str(x.ptype))
        cursor.execute ("SELECT DISTINCT * from lcbo_countryconv WHERE name = \""+ x.country+"\"")
	if cursor.fetchone() is None:
        	cursor.execute ("INSERT INTO lcbo_countryconv VALUES (%s,\" \")",str(x.country))
	g.write(str(tempcspc)+"\n")
f.close();
cursor.close()

#OK. So it reads in a ptype. If it's been seen before, it does nothing, if not, it adds it. Same thing for country. So do a select for it, if no result, do an add. Record the last CSPC used in case of error and loop until all CSPCs have been checked. Export result to filemaker. BANG DONE. (Localization?)