mirror of
https://github.com/Hopiu/linkchecker.git
synced 2026-04-22 23:24:44 +00:00
fix file parsing, ignore comments and empty lines
git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@1222 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
parent
8a474914f3
commit
fa9023d9f8
1 changed files with 22 additions and 15 deletions
|
|
@ -15,7 +15,7 @@
|
|||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
import os
|
||||
import sys, os
|
||||
from Logger import Logger
|
||||
from linkcheck.Config import norm
|
||||
|
||||
|
|
@ -28,9 +28,17 @@ class BlacklistLogger (Logger):
|
|||
super(BlacklistLogger, self).__init__(**args)
|
||||
self.errors = 0
|
||||
self.blacklist = {}
|
||||
self.filename = norm(args['filename'])
|
||||
if os.path.exists(self.filename):
|
||||
self.readBlacklist()
|
||||
if args.has_key('fileoutput'):
|
||||
self.fileoutput = True
|
||||
filename = args['filename']
|
||||
if os.path.exists(filename):
|
||||
self.readBlacklist(file(filename, "r"))
|
||||
self.fd = file(filename, "w")
|
||||
elif args.has_key('fd'):
|
||||
self.fd = args['fd']
|
||||
else:
|
||||
self.fileoutput = False
|
||||
self.fd = sys.stdout
|
||||
|
||||
|
||||
def newUrl (self, urlData):
|
||||
|
|
@ -50,23 +58,22 @@ class BlacklistLogger (Logger):
|
|||
self.writeBlacklist()
|
||||
|
||||
|
||||
def readBlacklist (self):
|
||||
fd = file(self.filename)
|
||||
def readBlacklist (self, fd):
|
||||
for line in fd:
|
||||
value, key = line.split(1)
|
||||
line = line.rstrip()
|
||||
if line.startswith('#') or not line:
|
||||
continue
|
||||
value, key = line.split(None, 1)
|
||||
self.blacklist[key] = int(value)
|
||||
fd.close()
|
||||
|
||||
|
||||
def writeBlacklist (self):
|
||||
"""write the blacklist"""
|
||||
oldmask = None
|
||||
if not os.path.exists(self.filename):
|
||||
oldmask = os.umask(0077)
|
||||
fd = file(self.filename, "w")
|
||||
oldmask = os.umask(0077)
|
||||
for key, value in self.blacklist.items():
|
||||
fd.write("%d %s\n" % (value, key))
|
||||
fd.close()
|
||||
self.fd.write("%d %s\n" % (value, key))
|
||||
if self.fileoutput:
|
||||
self.fd.close()
|
||||
# restore umask
|
||||
if oldmask is not None:
|
||||
os.umask(oldmask)
|
||||
os.umask(oldmask)
|
||||
|
|
|
|||
Loading…
Reference in a new issue