2009-08-05 08:59:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/05/02
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the "HS" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.hardcoded.net/licenses/hs_license
|
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
from lxml import etree
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
from hsutil.files import FileOrPath
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
class IgnoreList(object):
|
|
|
|
"""An ignore list implementation that is iterable, filterable and exportable to XML.
|
|
|
|
|
|
|
|
Call Ignore to add an ignore list entry, and AreIgnore to check if 2 items are in the list.
|
|
|
|
When iterated, 2 sized tuples will be returned, the tuples containing 2 items ignored together.
|
|
|
|
"""
|
|
|
|
#---Override
|
|
|
|
def __init__(self):
|
|
|
|
self._ignored = {}
|
|
|
|
self._count = 0
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for first,seconds in self._ignored.iteritems():
|
|
|
|
for second in seconds:
|
|
|
|
yield (first,second)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self._count
|
|
|
|
|
|
|
|
#---Public
|
|
|
|
def AreIgnored(self,first,second):
|
|
|
|
def do_check(first,second):
|
|
|
|
try:
|
|
|
|
matches = self._ignored[first]
|
|
|
|
return second in matches
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return do_check(first,second) or do_check(second,first)
|
|
|
|
|
|
|
|
def Clear(self):
|
|
|
|
self._ignored = {}
|
|
|
|
self._count = 0
|
|
|
|
|
|
|
|
def Filter(self,func):
|
|
|
|
"""Applies a filter on all ignored items, and remove all matches where func(first,second)
|
|
|
|
doesn't return True.
|
|
|
|
"""
|
|
|
|
filtered = IgnoreList()
|
|
|
|
for first,second in self:
|
|
|
|
if func(first,second):
|
|
|
|
filtered.Ignore(first,second)
|
|
|
|
self._ignored = filtered._ignored
|
|
|
|
self._count = filtered._count
|
|
|
|
|
|
|
|
def Ignore(self,first,second):
|
|
|
|
if self.AreIgnored(first,second):
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
matches = self._ignored[first]
|
|
|
|
matches.add(second)
|
|
|
|
except KeyError:
|
|
|
|
try:
|
|
|
|
matches = self._ignored[second]
|
|
|
|
matches.add(first)
|
|
|
|
except KeyError:
|
|
|
|
matches = set()
|
|
|
|
matches.add(second)
|
|
|
|
self._ignored[first] = matches
|
|
|
|
self._count += 1
|
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
def load_from_xml(self, infile):
|
2009-06-01 09:55:11 +00:00
|
|
|
"""Loads the ignore list from a XML created with save_to_xml.
|
|
|
|
|
|
|
|
infile can be a file object or a filename.
|
|
|
|
"""
|
|
|
|
try:
|
2010-03-01 11:21:43 +00:00
|
|
|
root = etree.parse(infile).getroot()
|
2009-06-01 09:55:11 +00:00
|
|
|
except Exception:
|
|
|
|
return
|
2010-03-01 11:21:43 +00:00
|
|
|
for fn in root.iterchildren('file'):
|
|
|
|
file_path = fn.get('path')
|
|
|
|
if not file_path:
|
2009-06-01 09:55:11 +00:00
|
|
|
continue
|
2010-03-01 11:21:43 +00:00
|
|
|
for sfn in fn.iterchildren('file'):
|
|
|
|
subfile_path = sfn.get('path')
|
|
|
|
if subfile_path:
|
|
|
|
self.Ignore(file_path, subfile_path)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
def save_to_xml(self, outfile):
|
2009-06-01 09:55:11 +00:00
|
|
|
"""Create a XML file that can be used by load_from_xml.
|
|
|
|
|
|
|
|
outfile can be a file object or a filename.
|
|
|
|
"""
|
2010-03-01 11:21:43 +00:00
|
|
|
root = etree.Element('ignore_list')
|
|
|
|
for filename, subfiles in self._ignored.items():
|
|
|
|
file_node = etree.SubElement(root, 'file')
|
|
|
|
file_node.set('path', filename)
|
|
|
|
for subfilename in subfiles:
|
|
|
|
subfile_node = etree.SubElement(file_node, 'file')
|
|
|
|
subfile_node.set('path', subfilename)
|
|
|
|
tree = etree.ElementTree(root)
|
2009-06-01 09:55:11 +00:00
|
|
|
with FileOrPath(outfile, 'wb') as fp:
|
2010-03-01 11:21:43 +00:00
|
|
|
tree.write(fp, encoding='utf-8')
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
|