2009-08-05 08:59:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/05/02
|
2015-01-03 21:30:57 +00:00
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2014-10-13 19:08:59 +00:00
|
|
|
#
|
2015-01-03 21:33:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
2014-10-13 19:08:59 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2010-08-15 12:42:55 +00:00
|
|
|
from xml.etree import ElementTree as ET
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-11 12:36:05 +00:00
|
|
|
from hscommon.util import FileOrPath
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2011-01-11 12:36:05 +00:00
|
|
|
class IgnoreList:
|
2009-06-01 09:55:11 +00:00
|
|
|
"""An ignore list implementation that is iterable, filterable and exportable to XML.
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
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.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# ---Override
|
2009-06-01 09:55:11 +00:00
|
|
|
def __init__(self):
|
2021-08-25 06:11:24 +00:00
|
|
|
self.clear()
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __iter__(self):
|
2014-10-13 19:08:59 +00:00
|
|
|
for first, seconds in self._ignored.items():
|
2009-06-01 09:55:11 +00:00
|
|
|
for second in seconds:
|
2014-10-13 19:08:59 +00:00
|
|
|
yield (first, second)
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __len__(self):
|
|
|
|
return self._count
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# ---Public
|
2021-08-25 06:11:24 +00:00
|
|
|
def are_ignored(self, first, second):
|
2014-10-13 19:08:59 +00:00
|
|
|
def do_check(first, second):
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
|
|
|
matches = self._ignored[first]
|
|
|
|
return second in matches
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2014-10-13 19:08:59 +00:00
|
|
|
|
|
|
|
return do_check(first, second) or do_check(second, first)
|
|
|
|
|
2021-08-25 06:11:24 +00:00
|
|
|
def clear(self):
|
2009-06-01 09:55:11 +00:00
|
|
|
self._ignored = {}
|
|
|
|
self._count = 0
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2021-08-25 06:11:24 +00:00
|
|
|
def filter(self, func):
|
2009-06-01 09:55:11 +00:00
|
|
|
"""Applies a filter on all ignored items, and remove all matches where func(first,second)
|
|
|
|
doesn't return True.
|
|
|
|
"""
|
|
|
|
filtered = IgnoreList()
|
2014-10-13 19:08:59 +00:00
|
|
|
for first, second in self:
|
|
|
|
if func(first, second):
|
2021-08-25 06:11:24 +00:00
|
|
|
filtered.ignore(first, second)
|
2009-06-01 09:55:11 +00:00
|
|
|
self._ignored = filtered._ignored
|
|
|
|
self._count = filtered._count
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2021-08-25 06:11:24 +00:00
|
|
|
def ignore(self, first, second):
|
|
|
|
if self.are_ignored(first, second):
|
2009-06-01 09:55:11 +00:00
|
|
|
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
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2012-03-14 16:47:21 +00:00
|
|
|
def remove(self, first, second):
|
|
|
|
def inner(first, second):
|
|
|
|
try:
|
|
|
|
matches = self._ignored[first]
|
|
|
|
if second in matches:
|
|
|
|
matches.discard(second)
|
|
|
|
if not matches:
|
|
|
|
del self._ignored[first]
|
|
|
|
self._count -= 1
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2021-08-21 23:02:02 +00:00
|
|
|
if not inner(first, second) and not inner(second, first):
|
|
|
|
raise ValueError()
|
2014-10-13 19:08:59 +00:00
|
|
|
|
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.
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
infile can be a file object or a filename.
|
|
|
|
"""
|
|
|
|
try:
|
2010-08-15 12:42:55 +00:00
|
|
|
root = ET.parse(infile).getroot()
|
2009-06-01 09:55:11 +00:00
|
|
|
except Exception:
|
|
|
|
return
|
2020-01-01 02:16:27 +00:00
|
|
|
file_elems = (e for e in root if e.tag == "file")
|
2010-08-15 12:42:55 +00:00
|
|
|
for fn in file_elems:
|
2020-01-01 02:16:27 +00:00
|
|
|
file_path = fn.get("path")
|
2010-03-01 11:21:43 +00:00
|
|
|
if not file_path:
|
2009-06-01 09:55:11 +00:00
|
|
|
continue
|
2020-01-01 02:16:27 +00:00
|
|
|
subfile_elems = (e for e in fn if e.tag == "file")
|
2010-08-15 12:42:55 +00:00
|
|
|
for sfn in subfile_elems:
|
2020-01-01 02:16:27 +00:00
|
|
|
subfile_path = sfn.get("path")
|
2010-03-01 11:21:43 +00:00
|
|
|
if subfile_path:
|
2021-08-25 06:11:24 +00:00
|
|
|
self.ignore(file_path, subfile_path)
|
2014-10-13 19:08:59 +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.
|
2014-10-13 19:08:59 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
outfile can be a file object or a filename.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
root = ET.Element("ignore_list")
|
2010-03-01 11:21:43 +00:00
|
|
|
for filename, subfiles in self._ignored.items():
|
2020-01-01 02:16:27 +00:00
|
|
|
file_node = ET.SubElement(root, "file")
|
|
|
|
file_node.set("path", filename)
|
2010-03-01 11:21:43 +00:00
|
|
|
for subfilename in subfiles:
|
2020-01-01 02:16:27 +00:00
|
|
|
subfile_node = ET.SubElement(file_node, "file")
|
|
|
|
subfile_node.set("path", subfilename)
|
2010-08-15 12:42:55 +00:00
|
|
|
tree = ET.ElementTree(root)
|
2020-01-01 02:16:27 +00:00
|
|
|
with FileOrPath(outfile, "wb") as fp:
|
|
|
|
tree.write(fp, encoding="utf-8")
|