class FileHandler:

    import string


    def clearFeatures (self):
        # blank out all Casting Polymers features
        
        name_ = ''
        madeBy_ = ''
        website_ = ''
        class_ = ''
        polymer_ = ''
        type_ = ''
        mixRatio_ = ''
        potLife_ = ''
        demoldTime_ = ''
        size_ = ''
        weight_ = ''
        price_ = ''
        viscosity_ = ''
        hardness_ = ''
        specificVolume_ = ''
        color_ = ''
        shrinkage_ = ''
        elongation_ = ''
        tearStrength_ = ''
        compStrength_ = ''
        tensileStrength_ = ''
    
        return name_, madeBy_, website_, class_, polymer_,  \
               type_, mixRatio_, potLife_, demoldTime_, size_,  \
               weight_, price_, viscosity_, hardness_, specificVolume_,  \
               color_, shrinkage_, elongation_, tearStrength_, compStrength_,  \
               tensileStrength_

    
    
    def closeFile (self):

        # close open file

        self.file.close ()



    def getBetween (self, startString, endString):
        
        # get section of line delimited by startString and endString

        # get first delimiter
        loc = self.locate (startString)
        # check for not found and return
        if loc == -1:
            return -1
        
        # now check for end delimiter
        string = self.getUntil (endString)
        #print 'fileHandler: string, endString: ',  string, ' ', endString
        # check for not found and return
        if string == -1:
            return -1
        # string found
        return string



    def getLine (self):

        # get next line

        self.line = self.file.readline ()
        # check for end of file return
        if self.line == '':
            return -1
        # check to see if there is an end flag
        if self.endFlag:
            if self.endFlag in self.line:
                return -1
        #replace return char with blank
        self.line = self.line.replace ('\r', ' ')
        # replace tab char with blank
        self.line = self.line.replace ('\t', ' ')
        self.col = 0



    def getNext (self, noChars):

        # get next noChars characters

        string = ''
        start = self.col
        end = self.col + noChars
        
        while 1:
            line = self.line.rstrip ('\n')
            eol = len (line)
            if end < eol + 1:
                # pick up string and return
                string = string + line [start: end]
                self.col = end
                return string
            else:
                # pick up current string then get another line
                string = string + line [start: eol]
                end -= eol
                y = self.getLine ()
                if y == -1:
                    return -1
                #replace return char with blank
                self.line = self.line.replace ('\r', ' ')
                # replace tab char with blank
                self.line = self.line.replace ('\t', ' ')
                start = 0

    def getPrevious (self, noChars):

        # get previous noChars 
        # do not change column location self.col
        
        # note that this only goes back to the begining of the
        # current line

        # start location
        # note that self.col is already one beyond
        # a previously searched field
        start = self.col - (noChars + 1)
        # check for no less than 0
        if start < 0:
            start = 0
      
        return self.line [start: self.col - 1]
                
                
    def getUntil (self, delimit):

        # get next field from the current location until
        # delimit string

        string = ''
        start = self.col
        
        while 1:
            # remove controls at end of line
            line = self.line.rstrip ('\n')
            line = line.rstrip ('\r')
            line = line.rstrip ('\t')
            
            eol = len (line) - 1
            #print '\nfileHandler eol, line: ', eol,' ',line
            loc = line.find (delimit, start)
            if loc == -1:
                # pick up current string then get another line
                string = string + line [start: eol]
                y = self.getLine ()
                if y == -1:
                    return -1
                #replace return char with blank
                #self.line = self.line.replace ('\r', ' ')
                # replace tab char with blank
                #self.line = self.line.replace ('\t', ' ')
                start = 0
            else:
                # pick up string and return
                string = string + line [start: loc]
                self.col = loc + len (delimit)
                return string                
                
        

    def locate (self, string):

    # Find string. Read more lines if needed
    # and flag = 1.

        while 1:
            loc = self.line.find (string, self.col)
            if loc == -1:
                y = self.getLine ()
                if y == -1:
                    return -1
                if self.line == '':
                    return -1
                #replace return char with blank
                self.line = self.line.replace ('\r', ' ')
                # replace tab char with blank
                self.line = self.line.replace ('\t', ' ')
                self.col = 0
                continue
        
            self.col = loc + len (string)
            return loc

    
    def openFile (self, fileName, ioFlag='r', endFlag=''):

        # open a file for input or output
        
        self.file = open (fileName,ioFlag)


        # note - first line read has been removed
        
        # set endFlag
        self.endFlag = endFlag
        



    def openURL (self, URLname, endFlag=''):

        import urllib

        # open URL for reading

        self.file = urllib.urlopen (URLname)
        self.endFlag = endFlag
        y = self.getLine ()
        if y == -1:
            return -1
        


    def readPolymers (self):
        # pick up data for the next polmer from sequential file
        # if this comes right after open, then the first record
        # has been read in

        name_, madeBy_, website_, class_, polymer_,  \
               type_, mixRatio_, potLife_, demoldTime_, size_,  \
               weight_, price_, viscosity_, hardness_, specificVolume_,  \
               color_, shrinkage_, elongation_, tearStrength_, compStrength_,  \
               tensileStrength_ = self.clearFeatures ()
            
        while 1:
            self.getLine ()
            
            # end file ??
            if self.line == '':
                name = ''
                break
                
            # check for end block
            if self.line.startswith ('---'):
                break
            # bypass count info
            if self.line.startswith ('count:'):
                continue
            
            # check for name_
            if self.line.startswith ("name:"):
                loc = self.line.index (':')+1
                name_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for madeBy_
            if self.line.startswith ("madeBy:"):
                loc = self.line.index (':')+1
                madeBy_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for website_
            if self.line.startswith ("website:"):
                loc = self.line.index (':')+1
                website_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for class_
            if self.line.startswith ("class:"):
                loc = self.line.index (':')+1
                class_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for polymer_
            if self.line.startswith ("polymer:"):
                loc = self.line.index (':')+1
                polymer_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for type_
            if self.line.startswith ("type:"):
                loc = self.line.index (':')+1
                type_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for mixRatio_
            if self.line.startswith ("mixRatio:"):
                loc = self.line.index (':')+1
                mixRatio_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for potLife_
            if self.line.startswith ("potLife:"):
                loc = self.line.index (':')+1
                potLife_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for demoldTime_
            if self.line.startswith ("demoldTime:"):
                loc = self.line.index (':')+1
                demoldTime_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for size_
            if self.line.startswith ("size:"):
                loc = self.line.index (':')+1
                size_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for weight_
            if self.line.startswith ("weight:"):
                loc = self.line.index (':')+1
                weight_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for price_
            if self.line.startswith ("price:"):
                loc = self.line.index (':')+1
                price_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for viscosity_
            if self.line.startswith ("viscosity:"):
                loc = self.line.index (':')+1
                viscosity_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for hardness_
            if self.line.startswith ("hardness:"):
                loc = self.line.index (':')+1
                hardness_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for specificVolume_
            if self.line.startswith ("specificVolume:"):
                loc = self.line.index (':')+1
                specificVolume_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for color_
            if self.line.startswith ("color:"):
                loc = self.line.index (':')+1
                color_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for shrinkage_
            if self.line.startswith ("shrinkage:"):
                loc = self.line.index (':')+1
                shrinkage_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for elongation_
            if self.line.startswith ("elongation:"):
                loc = self.line.index (':')+1
                elongation_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for tearStrength_
            if self.line.startswith ("tearStrength:"):
                loc = self.line.index (':')+1
                tearStrength_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for compStrength_
            if self.line.startswith ("compStrength:"):
                loc = self.line.index (':')+1
                compStrength_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
            # check for tensileStrength_
            if self.line.startswith ("tensileStrength:"):
                loc = self.line.index (':')+1
                tensileStrength_ = self.line [loc:].rstrip ().lstrip ()
                continue
            
                                                     
        return name_, madeBy_, website_, class_, polymer_,  \
               type_, mixRatio_, potLife_, demoldTime_, size_,  \
               weight_, price_, viscosity_, hardness_, specificVolume_,  \
               color_, shrinkage_, elongation_, tearStrength_, compStrength_,  \
               tensileStrength_

    
        
    def rewind (self):
        
        # positon at start of file
        # doesn't work for URLs ??
        
        self.file.seek (0)
   
   
    def setEndFlag (self, endFlag):
  
       # set "endFlag" to be used as end of section marker 
       
        self.endFlag = endFlag
       
       
        
    def writePolymers (self,count, \
               name_, madeBy_, website_, class_, polymer_,  \
               type_, mixRatio_, potLife_, demoldTime_, size_,  \
               weight_, price_, viscosity_, hardness_, specificVolume_,  \
               color_, shrinkage_, elongation_, tearStrength_, compStrength_,  \
               tensileStrength_ ,printFlag=False):
        
        # write polmer data "record" into a file

        temp = 'count: ' + str (count) + '\n'
        if printFlag:
            print temp
        self.file.write (temp)

        
        if name_:
            temp = 'name: ' + name_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if madeBy_:
            temp = 'madeBy: ' + madeBy_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if website_:
            temp = 'website: ' + website_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if class_:
            temp = 'class: ' + class_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if polymer_:
            temp = 'polymer: ' + polymer_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if type_:
            temp = 'type: ' + type_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if mixRatio_:
            temp = 'mixRatio: ' + mixRatio_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if potLife_:
            temp = 'potLife: ' + potLife_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if demoldTime_:
            temp = 'demoldTime: ' + demoldTime_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if size_:
            temp = 'size: ' + size_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if weight_:
            temp = 'weight: ' + weight_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if price_:
            temp = 'price: ' + price_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if viscosity_:
            temp = 'viscosity: ' + viscosity_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if hardness_:
            temp = 'hardness: ' + hardness_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if specificVolume_:
            temp = 'specificVolume: ' + specificVolume_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if color_:
            temp = 'color: ' + color_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if shrinkage_:
            temp = 'shrinkage: ' + shrinkage_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if elongation_:
            temp = 'elongation: ' + elongation_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if tearStrength_:
            temp = 'tearStrength: ' + tearStrength_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if compStrength_:
            temp = 'compStrength: ' + compStrength_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            
        if tensileStrength_:
            temp = 'tensileStrength: ' + tensileStrength_ + '\n'
            if printFlag:
                print temp
            self.file.write (temp)
            

               
        temp = '---' + '\n'
        if printFlag:
                print temp
        self.file.write (temp)