made confidence level configurable, added auto accept / decline

This commit is contained in:
Tobias Diekershoff 2023-01-07 17:33:42 +01:00
commit bb4843710c

View file

@ -20,18 +20,22 @@ class BrewBlocklist():
* rank the blocklists by their trustworthyness * rank the blocklists by their trustworthyness
* compile a the resulting blocklist * compile a the resulting blocklist
""" """
def __init__(self, configfile, outputfile): def __init__(self, configfile, outputfile, auto_accept = False,
auto_accept_direction = True, confidence = 100):
""" """
Initialise the cauldron with the filenames of the config file Initialise the cauldron with the filenames of the config file
and the outputfilename. and the outputfilename.
""" """
self.sources = [] self.sources = []
self.auto_accept = auto_accept
self.auto_accept_direction = auto_accept_direction
config = configparser.RawConfigParser() config = configparser.RawConfigParser()
config.read(configfile) config.read(configfile)
for section in config.sections(): for section in config.sections():
section_values = dict(config.items(section)) section_values = dict(config.items(section))
self.sources.append({'url': section, 'trust': int(section_values['trust'])}) self.sources.append({'url': section, 'trust': int(section_values['trust'])})
self.outputfile = outputfile self.outputfile = outputfile
self.confidence = confidence
self.blocklist = {} self.blocklist = {}
self.reasons = {} self.reasons = {}
@ -51,12 +55,40 @@ class BrewBlocklist():
self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust'] self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust']
self.reasons[pattern] = self.reasons.get(pattern, reason) self.reasons[pattern] = self.reasons.get(pattern, reason)
def clean_list(self):
"""
Go through the list of blocklist items and ask the user if they want
to keep the item or not.
Interaction will be over-written when the user provided either the
--yes-to-all or --no-to-all commandline parameter.
"""
c_blocklist = {}
c_reasons = {}
for key, value in self.blocklist.items():
if value < self.confidence:
if not self.auto_accept:
print('Domain: {} [total trust {}]'.format(key, value))
print('Reason: {}'.format(self.reasons[key]))
keep = input('Keep that entry? [Y/n] > ')
if keep not in ['n', 'N']:
c_blocklist[key] = value
c_reasons[key] = self.reasons[key]
else:
if self.auto_accept_direction:
c_blocklist[key] = value
c_reasons[key] = self.reasons[key]
else:
c_blocklist[key] = value
c_reasons[key] = self.reasons[key]
self.blocklist = c_blocklist
self.reasons = c_reasons
def serve_meal(self): def serve_meal(self):
""" """
Print the CSV list of the collected blocklist into either STDOUT or Print the CSV list of the collected blocklist into either STDOUT or
the output file that was defined as command line parameter. the output file that was defined as command line parameter.
""" """
print(self.outputfile, not self.outputfile)
if self.outputfile: if self.outputfile:
out_file = open(self.outputfile, 'w') out_file = open(self.outputfile, 'w')
orig_stdout = sys.stdout orig_stdout = sys.stdout
@ -77,10 +109,28 @@ if __name__ == '__main__':
dest='outputfile', dest='outputfile',
default=None, default=None,
help='specify the output file. STDOUT if none given') help='specify the output file. STDOUT if none given')
parser.add_argument('-y', '--auto-accept',
dest='auto_accept_direction',
action='store_true',
default=None,
help='accept all blocklist items with trust values < confidence')
parser.add_argument('-n', '--auto-decline',
dest='auto_accept_direction',
action='store_false',
default=None,
help='decline all blocklist items with trust values < confidence')
parser.add_argument('-C', '--confidence',
type=int,
dest='confidence',
default=100,
help='set the needed confidence level for automatically keep item')
args = parser.parse_args() args = parser.parse_args()
arg_auto_accept = not args.auto_accept_direction is None
if not exists(args.configfile): if not exists(args.configfile):
print('The config file {} was not found.'.format(args.configfile)) print('The config file {} was not found.'.format(args.configfile))
sys.exit() sys.exit()
brew = BrewBlocklist(args.configfile, args.outputfile) brew = BrewBlocklist(args.configfile, args.outputfile, arg_auto_accept,
args.auto_accept_direction)
brew.collect_ingrediens() brew.collect_ingrediens()
brew.clean_list()
brew.serve_meal() brew.serve_meal()