1
0
Fork 0

updated FOP.py

This commit is contained in:
Spirillen 2021-09-03 21:25:36 +02:00
parent c4e87e27fa
commit 2622a85fca
No known key found for this signature in database
GPG Key ID: 30562ED6FB236E4E
1 changed files with 9 additions and 5 deletions

14
FOP.py
View File

@ -54,7 +54,8 @@ COMMITPATTERN = re.compile(r"^(A|M|P)\:\s(\((.+)\)\s)?(.*)$")
# List the files that should not be sorted, either because they have a special sorting system or because they are not filter files
IGNORE = ("CC-BY-SA.txt", "easytest.txt", "GPL.txt", "MPL.txt",
"enhancedstats-addon.txt", "fanboy-tracking", "firefox-regional", "other")
"enhancedstats-addon.txt", "fanboy-tracking", "firefox-regional", "other",
"easylist_cookie_specific_uBO.txt", "fanboy_annoyance_specific_uBO.txt", "fanboy_notifications_specific_uBO.txt", "fanboy_social_specific_uBO.txt")
# List all Adblock Plus options (excepting domain, which is handled separately), as of version 1.3.9
KNOWNOPTIONS = ("collapse", "csp", "document", "elemhide",
@ -215,6 +216,9 @@ def fopsort (filename):
filterlines = elementlines = 0
outputfile.write("{line}\n".format(line = line))
else:
# Skip filters containing less than three characters
if len(line) < 3:
continue
# Neaten up filters and, if necessary, check their type for the sorting algorithm
elementparts = re.match(ELEMENTPATTERN, line)
if elementparts:
@ -271,7 +275,7 @@ def filtertidy (filterin):
optionlist = sorted(set(filter(lambda option: option not in removeentries, optionlist)), key = lambda option: (option[1:] + "~") if option[0] == "~" else option)
# If applicable, sort domain restrictions and append them to the list of options
if domainlist:
optionlist.append("domain={domainlist}".format(domainlist = "|".join(sorted(set(domainlist), key = lambda domain: domain.strip("~")))))
optionlist.append("domain={domainlist}".format(domainlist = "|".join(sorted(set(filter(lambda domain: domain != "", domainlist)), key = lambda domain: domain.strip("~")))))
# Return the full filter
return "{filtertext}${options}".format(filtertext = filtertext, options = ",".join(optionlist))
@ -379,10 +383,10 @@ def isglobalelement (domains):
def removeunnecessarywildcards (filtertext):
""" Where possible, remove unnecessary wildcards from the beginnings
and ends of blocking filters."""
whitelist = False
allowlist = False
hadStar = False
if filtertext[0:2] == "@@":
whitelist = True
allowlist = True
filtertext = filtertext[2:]
while len(filtertext) > 1 and filtertext[0] == "*" and not filtertext[1] == "|" and not filtertext[1] == "!":
filtertext = filtertext[1:]
@ -394,7 +398,7 @@ def removeunnecessarywildcards (filtertext):
filtertext = "{filtertext}*".format(filtertext = filtertext)
if filtertext == "*":
filtertext = ""
if whitelist:
if allowlist:
filtertext = "@@{filtertext}".format(filtertext = filtertext)
return filtertext