--- FSCounter/FSCounter.py.orig 2004-07-14 12:16:31.000000000 +0900 +++ FSCounter/FSCounter.py 2004-11-09 20:32:57.000000000 +0900 @@ -42,6 +42,7 @@ import string import Globals # so I can get Globals data_dir import time +import fcntl from numbers import num2eng @@ -76,6 +77,7 @@ """initialise a new instance of FSCounter""" self.id = id self.title = title + self.last_count = 0 filename = '%s.counter' % id # try id but if it exists already then choose a unique file name @@ -107,6 +109,7 @@ self.reset() else: self.reset(initialvalue=initialvalue) + self.last_count = int(initialvalue) def manage_beforeDelete(self, item, container): ''' clean up ''' @@ -144,6 +147,7 @@ self.ignore_addrs=tuple(ignAdTmp) if newvalue != '': + self.last_count = int(newvalue) self.__write_(newvalue) if REQUEST is not None: @@ -153,6 +157,11 @@ ''' checks to see if we can read from the file ''' return self.read(failread = 1) != None + def getLastCount(self): + if not hasattr(self, 'last_count'): + self.last_count = self.read() + return self.last_count + def __write_(self, newvalue=''): ''' write newvalue to file or increment ''' # allow a failing read if we aren't setting a new value @@ -164,12 +173,16 @@ value = value + 1 else: value = int(newvalue) - #try: - outfile = open(str(Globals.data_dir) + os.sep + str(self.filename), 'wb') - outfile.write(str(value)) - outfile.close() - #except: - # pass + + if not hasattr(self, 'last_count'): + self.last_count = 0 + if value >= self.last_count: + outfile = open(str(Globals.data_dir) + os.sep + str(self.filename), 'wb') + fcntl.flock(outfile.fileno(), fcntl.LOCK_EX) + outfile.write(str(value)) + outfile.flush() + fcntl.flock(outfile.fileno(), fcntl.LOCK_UN) + outfile.close() return int(value) def read(self, failread = 0): @@ -307,6 +320,11 @@ outfile.write(time.strftime('%Y-%m-%d')+'\n') outfile.write(ip) outfile.close() + last = self.read() + if not hasattr(self, 'last_count'): + self.last_count = 0 + if last > self.last_count: + self.last_count = last except: pass elif not found: @@ -330,12 +348,17 @@ ''' check if the ip should be ignored ''' ignore = 0 if self.ignore_addrs: - rAdrSplit=string.split(self.REQUEST.get('HTTP_X_FORWARDED_FOR', '') or self.REQUEST.get('REMOTE_ADDR', ''), '.') - rAdrInt=(int(rAdrSplit[0])<<24)+(int(rAdrSplit[1])<<16)+(int(rAdrSplit[2])<<8)+int(rAdrSplit[3]) - for addr_range in self.ignore_addrs: - if rAdrInt&addr_range[1] == addr_range[0]: - ignore = 1 - break + urls = self.REQUEST.get('HTTP_X_FORWARDED_FOR', '') or self.REQUEST.get('REMOTE_ADDR', '') + rAdrSplit = string.split(urls, '.') + if len(rAdrSplit) <= 4: + try: + rAdrInt=(int(rAdrSplit[0])<<24)+(int(rAdrSplit[1])<<16)+(int(rAdrSplit[2])<<8)+int(rAdrSplit[3]) + for addr_range in self.ignore_addrs: + if rAdrInt&addr_range[1] == addr_range[0]: + ignore = 1 + break + except: + pass return ignore # index_html = __call__