BlockIt
scanners.py
Go to the documentation of this file.
00001 #............................................................................
00002 # Copyright (c) 2009,2010,2011 David Car, david.car7@gmail.com
00003 #
00004 # This program is free software; you can redistribute it and/or modify it under
00005 # the terms of the GNU General Public License as published by the Free Software
00006 # Foundation; either version 2 of the License, or (at your option) any later
00007 # version.
00008 #
00009 # This program is distributed in the hope that it will be useful, but WITHOUT
00010 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License along with
00014 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00015 # Place, Suite 330, Boston, MA 02111-1307 USA
00016 #..........................................................................
00017 from cStringIO import StringIO as SrcBuffer
00018 import copy
00019 import hashlib
00020 
00021 class LineScanner(object):
00022     def __init__(self):
00023         self._buff = SrcBuffer()
00024         self.reset()
00025 
00026     def reset(self):
00027         self._buff.reset()
00028         self._buff.truncate()
00029         self._curLine = ''
00030         self._lineNumber = 0
00031         self._curByte = 0
00032         self._lastByte = 0
00033         self._lineByteMap = []
00034         self._name = ''
00035 
00036     def __str__(self):
00037         return self._name
00038 
00039     def _makeMap(self):
00040         buff = self._buff
00041         lineByteMap = self._lineByteMap
00042         buff.seek(0)
00043         for line in buff:
00044             lineByteMap.append(buff.tell())
00045         buff.seek(0)
00046 
00047     def nextLine(self):
00048         buff = self._buff
00049         self._lastByte = buff.tell()
00050         self._curLine = buff.readline()
00051 
00052         if self._curLine == '':
00053             raise EOFError("EOF")
00054 
00055         self._lineNumber += 1
00056         self._curByte = buff.tell()
00057         return self._curLine
00058 
00059     def prevLine(self):
00060         _n = max(1, self._lineNumber-2)
00061         self._buff.seek(self._lineByteMap[_n])
00062         return self.nextLine()
00063 
00064     def backup(self, n=1):
00065         _lineNum = max(self._lineNumber-n, 1)
00066         _lineNum = min(_lineNum, len(self._lineByteMap))
00067         self._lineNumber = _lineNum
00068         self._buff.seek(self._lineByteMap[_lineNum-1])
00069 
00070     def curLine(self):
00071         return self._curLine
00072 
00073     def gotoLine(self, n):
00074         self._buff.seek(self._lineByteMap[n-1])
00075         self._lineNumber = n
00076         return self.nextLine()
00077 
00078     def lineNumber(self):
00079         return self._lineNumber
00080 
00081     def load(self, name):
00082         '''Load a file into the buffer.
00083 
00084         '''
00085         self.reset()
00086         
00087         try:
00088             fd = open(name)
00089             try:
00090                 self._name = name
00091                 self._buff.write(fd.read())
00092                 self._makeMap()
00093             finally:
00094                 fd.close()
00095         except:
00096             print "Could not read %s"%(name,)
00097 
00098     def loadBuff(self, buff, name):
00099         self.reset()
00100         self._name = name
00101         self._buff.write(buff.read())
00102         self._makeMap()
00103 
00104     def loadString(self, s, name):
00105         self.reset()
00106         self._name = name
00107         self._buff.write(s)
00108         self._makeMap()
00109 
00110     def hash(self):
00111         cur = self._buff.tell()
00112         self._buff.seek(0)
00113         hsh = hashlib.sha256(self._buff.read()).hexdigest()
00114         self._buff.seek(cur)
00115         return hsh
00116 
00117     def name(self):
00118         return self._name
00119 
 All Classes Namespaces Files Functions Variables Properties