"""
Load data from sequential data file into data base.
Output is a batch of index files.
Normally standalone.
"""


def update ():

    from fileHandler import FileHandler
    from dataHandler import toFloat,toHardness,toInt,toPercent,toPli, \
         toPsi,toRatio,toSeconds,toViscosity

    # input data file
    inFile = FileHandler ()
    polymers = 'polymers.txt'
    # open file
    x = inFile.openFile (polymers)

    mainDict = {}       # main dictionaly - used as data base
    
    # feature accumulators
    nameS = []
    madeByS = []
    websiteS = []
    classS = []
    polymerS = []
    typeS = []
    mixRatioS = []
    potLifeS = []
    demoldTimeS = []
    sizeS = []
    weightS = []
    priceS = []
    viscosityS = []
    hardnessS = []
    specificVolumeS = []
    colorS = []
    shrinkageS = []
    elongationS = []
    tearStrengthS = []
    compStrengthS = []
    tensileStrengthS = []
    
    
    
    
    # cycle on entries and pull required data
    
    while 1:


        name_,madeBy_,website_,class_,polymer_, \
           type_,mixRatio_,potLife_,demoldTime_,size_, \
           weight_,price_,viscosity_,hardness_,specificVolume_, \
           color_,shrinkage_,elongation_,tearStrength_,compStrength_, \
           tensileStrength_ = inFile.readPolymers ()

        
        if name_ == '':
            break

        
        if name_:
        # storage for name_ 0
            # lower case for name_
            temp = name_.lower () 
            nameS.append ((temp, name_))
        
        if madeBy_:
        # storage for madeBy_ 1
            # lower case for madeBy_
            temp = madeBy_.lower () 
            madeByS.append ((temp, name_))
        
        if website_:
        # storage for website_ 2
            # lower case for website_
            temp = website_.lower () 
            websiteS.append ((temp, name_))
        
        if class_:
        # storage for class_ 3
            # lower case for class_
            temp = class_.lower () 
            classS.append ((temp, name_))
        
        if polymer_:
        # storage for polymer_ 4
            # lower case for polymer_
            temp = polymer_.lower () 
            polymerS.append ((temp, name_))
        
        if type_:
        # storage for type_ 5
            # lower case for type_
            temp = type_.lower () 
            typeS.append ((temp, name_))
        
        if mixRatio_:
        # storage for mixRatio_ 6
            # convert to a ratio
            temp = toRatio (mixRatio_)
            mixRatioS.append ((temp, name_))
        
        if potLife_:
        # storage for potLife_ 7
            # convert time to seconds
            temp = toSeconds (potLife_)
            potLifeS.append ((temp, name_))
        
        if demoldTime_:
        # storage for demoldTime_ 8
            # convert time to seconds
            temp = toSeconds (demoldTime_)
            demoldTimeS.append ((temp, name_))
        
        if size_:
        # storage for size_ 9
            # lower case for size_
            temp = size_.lower () 
            sizeS.append ((temp, name_))
        
        if weight_:
        # storage for weight_ 10
            # float weight_
            temp = toFloat (weight_)
            weightS.append ((temp, name_))
        
        
        if price_:
        # storage for price_ 11
            # float price_
            temp = toFloat (price_)
            priceS.append ((temp, name_))
        
        
        if viscosity_:
        # storage for viscosity_ 12
            # convert to normalzed viscosity
            temp = toViscosity (viscosity_) 
            viscosityS.append ((temp, name_))
        
        if hardness_:
        # storage for hardness_ 13
            # convert to normalized hardness
            temp = toHardness (hardness_)
            hardnessS.append ((temp, name_))
        
        if specificVolume_:
        # storage for specificVolume_ 14
            # float specificVolume_
            temp = toFloat (specificVolume_)
            specificVolumeS.append ((temp, name_))
        
        
        if color_:
        # storage for color_ 15
            # lower case for color_
            temp = color_.lower () 
            colorS.append ((temp, name_))
        
        if shrinkage_:
        # storage for shrinkage_ 16
            # float shrinkage_
            temp = toFloat (shrinkage_)
            shrinkageS.append ((temp, name_))
        
        
        if elongation_:
        # storage for elongation_ 17
            # percent elongation_
            temp = toPercent (elongation_)
            elongationS.append ((temp, name_))
        
        if tearStrength_:
        # storage for tearStrength_ 18
            # pli tearStrength_
            temp = toPli (tearStrength_)
            tearStrengthS.append ((temp, name_))
        
        if compStrength_:
        # storage for compStrength_ 19
            # psi compStrength_
            temp = toPsi (compStrength_)
            compStrengthS.append ((temp, name_))
        
        
        if tensileStrength_:
        # storage for tensileStrength_ 20
            # psi tensileStrength_
            temp = toPsi (tensileStrength_)
            tensileStrengthS.append ((temp, name_))
        
        

        # add to mainDict
        mainDict [name_] = name_,madeBy_,website_,class_,polymer_, \
            type_,mixRatio_,potLife_,demoldTime_,size_, \
            weight_,price_,viscosity_,hardness_,specificVolume_, \
            color_,shrinkage_,elongation_,tearStrength_,compStrength_, \
            tensileStrength_

    # close input file
    inFile.closeFile ()
      
    
    # save mainDict:
    fMd = open ('C:/polymers/data/mainDict.txt','w')
    fMd.write (repr (mainDict))
    fMd.close ()
    
    # sort features and put in data-store

    
    nameS.sort ()
    f0 = open ('C:/polymers/data/nameIndex.txt','w')
    f0.write (repr (nameS))
    f0.close ()
    
    madeByS.sort ()
    f1 = open ('C:/polymers/data/madeByIndex.txt','w')
    f1.write (repr (madeByS))
    f1.close ()
    
    websiteS.sort ()
    f2 = open ('C:/polymers/data/websiteIndex.txt','w')
    f2.write (repr (websiteS))
    f2.close ()
    
    classS.sort ()
    f3 = open ('C:/polymers/data/classIndex.txt','w')
    f3.write (repr (classS))
    f3.close ()
    
    polymerS.sort ()
    f4 = open ('C:/polymers/data/polymerIndex.txt','w')
    f4.write (repr (polymerS))
    f4.close ()
    
    typeS.sort ()
    f5 = open ('C:/polymers/data/typeIndex.txt','w')
    f5.write (repr (typeS))
    f5.close ()
    
    mixRatioS.sort ()
    f6 = open ('C:/polymers/data/mixRatioIndex.txt','w')
    f6.write (repr (mixRatioS))
    f6.close ()
    
    potLifeS.sort ()
    f7 = open ('C:/polymers/data/potLifeIndex.txt','w')
    f7.write (repr (potLifeS))
    f7.close ()
    
    demoldTimeS.sort ()
    f8 = open ('C:/polymers/data/demoldTimeIndex.txt','w')
    f8.write (repr (demoldTimeS))
    f8.close ()
    
    sizeS.sort ()
    f9 = open ('C:/polymers/data/sizeIndex.txt','w')
    f9.write (repr (sizeS))
    f9.close ()
    
    weightS.sort ()
    f10 = open ('C:/polymers/data/weightIndex.txt','w')
    f10.write (repr (weightS))
    f10.close ()
    
    priceS.sort ()
    f11 = open ('C:/polymers/data/priceIndex.txt','w')
    f11.write (repr (priceS))
    f11.close ()
    
    viscosityS.sort ()
    f12 = open ('C:/polymers/data/viscosityIndex.txt','w')
    f12.write (repr (viscosityS))
    f12.close ()
    
    hardnessS.sort ()
    f13 = open ('C:/polymers/data/hardnessIndex.txt','w')
    f13.write (repr (hardnessS))
    f13.close ()
    
    specificVolumeS.sort ()
    f14 = open ('C:/polymers/data/specificVolumeIndex.txt','w')
    f14.write (repr (specificVolumeS))
    f14.close ()
    
    colorS.sort ()
    f15 = open ('C:/polymers/data/colorIndex.txt','w')
    f15.write (repr (colorS))
    f15.close ()
    
    shrinkageS.sort ()
    f16 = open ('C:/polymers/data/shrinkageIndex.txt','w')
    f16.write (repr (shrinkageS))
    f16.close ()
    
    elongationS.sort ()
    f17 = open ('C:/polymers/data/elongationIndex.txt','w')
    f17.write (repr (elongationS))
    f17.close ()
    
    tearStrengthS.sort ()
    f18 = open ('C:/polymers/data/tearStrengthIndex.txt','w')
    f18.write (repr (tearStrengthS))
    f18.close ()
    
    compStrengthS.sort ()
    f19 = open ('C:/polymers/data/compStrengthIndex.txt','w')
    f19.write (repr (compStrengthS))
    f19.close ()
    
    tensileStrengthS.sort ()
    f20 = open ('C:/polymers/data/tensileStrengthIndex.txt','w')
    f20.write (repr (tensileStrengthS))
    f20.close ()
     
          
    
    
    
if __name__ == '__main__':
    print 'Starting polymers Datastore update.'
    update ()
    print 'End of polymers Datastore update.'