--- a/WebBrowser/AdBlock/AdBlockSubscription.py Thu Dec 14 19:25:34 2017 +0100 +++ b/WebBrowser/AdBlock/AdBlockSubscription.py Mon Dec 18 18:09:39 2017 +0100 @@ -32,10 +32,13 @@ @signal changed() emitted after the subscription has changed @signal rulesChanged() emitted after the subscription's rules have changed @signal enabledChanged(bool) emitted after the enabled state was changed + @signal rulesEnabledChanged() emitted after a rule enabled state was + changed """ changed = pyqtSignal() rulesChanged = pyqtSignal() enabledChanged = pyqtSignal(bool) + rulesEnabledChanged = pyqtSignal() def __init__(self, url, custom, parent=None, default=False): """ @@ -65,13 +68,6 @@ self.__rules = [] # list containing all AdBlock rules - self.__networkExceptionRules = [] - self.__networkBlockRules = [] - self.__domainRestrictedCssRules = [] - self.__elementHidingRules = "" - self.__documentRules = [] - self.__elemhideRules = [] - self.__checksumRe = re.compile( r"""^\s*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n""", re.IGNORECASE | re.MULTILINE) @@ -105,7 +101,8 @@ """ Private method to parse the AdBlock URL for the subscription. - @param url AdBlock URL for the subscription (QUrl) + @param url AdBlock URL for the subscription + @type QUrl """ if url.scheme() != "abp": return @@ -142,7 +139,8 @@ """ Public method to generate the URL for this subscription. - @return AdBlock URL for the subscription (QUrl) + @return AdBlock URL for the subscription + @rtype QUrl """ url = QUrl() url.setScheme("abp") @@ -169,7 +167,8 @@ """ Public method to check, if the subscription is enabled. - @return flag indicating the enabled status (boolean) + @return flag indicating the enabled status + @rtype bool """ return self.__enabled @@ -177,7 +176,8 @@ """ Public method to set the enabled status. - @param enabled flag indicating the enabled status (boolean) + @param enabled flag indicating the enabled status + @type bool """ if self.__enabled == enabled: return @@ -189,7 +189,8 @@ """ Public method to get the subscription title. - @return subscription title (string) + @return subscription title + @rtype string """ return self.__title @@ -197,7 +198,8 @@ """ Public method to set the subscription title. - @param title subscription title (string) + @param title subscription title + @type str """ if self.__title == title: return @@ -209,7 +211,8 @@ """ Public method to get the subscription location. - @return URL of the subscription location (QUrl) + @return URL of the subscription location + @rtype QUrl """ return QUrl.fromEncoded(self.__location) @@ -217,7 +220,8 @@ """ Public method to set the subscription location. - @param url URL of the subscription location (QUrl) + @param url URL of the subscription location + @type QUrl """ if url == self.location(): return @@ -230,7 +234,8 @@ """ Public method to get the location of a required subscription. - @return location of a required subscription (string) + @return location of a required subscription + @rtype str """ return self.__requiresLocation @@ -238,7 +243,8 @@ """ Public method to get the date and time of the last update. - @return date and time of the last update (QDateTime) + @return date and time of the last update + @rtype QDateTime """ return self.__lastUpdate @@ -246,7 +252,8 @@ """ Public method to get the name of the rules file. - @return name of the rules file (string) + @return name of the rules file + @rtype str """ if self.location().scheme() == "file": return self.location().toLocalFile() @@ -322,7 +329,6 @@ if time: self.__remoteModified.setTime( QTime(int(hour), int(minute))) - self.__populateCache() self.changed.emit() elif not fileName.endswith("_custom"): self.__lastUpdate = QDateTime() @@ -445,11 +451,13 @@ """ Private method to check the subscription file's checksum. - @param fileName name of the file containing the subscription (string) - @return flag indicating a valid file (boolean). A file is considered + @param fileName name of the file containing the subscription + @type str + @return flag indicating a valid file. A file is considered valid, if the checksum is OK, the file does not contain a checksum (i.e. cannot be checked) or we are using the limited EasyList (because we fiddled with the original). + @rtype bool """ try: f = open(fileName, "r", encoding="utf-8") @@ -515,84 +523,14 @@ for rule in self.__rules: textStream << rule.filter() << "\n" - def match(self, req, urlDomain, urlString): - """ - Public method to check the subscription for a matching rule. - - @param req reference to the network request (QWebEngineUrlRequestInfo) - @param urlDomain domain of the URL (string) - @param urlString URL (string) - @return reference to the rule object or None (AdBlockRule) - """ - for rule in self.__networkExceptionRules: - if rule.networkMatch(req, urlDomain, urlString): - return None - - for rule in self.__networkBlockRules: - if rule.networkMatch(req, urlDomain, urlString): - return rule - - return None - - def adBlockDisabledForUrl(self, url): - """ - Public method to check, if AdBlock is disabled for the given URL. - - @param url URL to check (QUrl) - @return flag indicating disabled state (boolean) - """ - for rule in self.__documentRules: - if rule.urlMatch(url): - return True - - return False - - def elemHideDisabledForUrl(self, url): - """ - Public method to check, if element hiding is disabled for the given - URL. - - @param url URL to check (QUrl) - @return flag indicating disabled state (boolean) - """ - if self.adBlockDisabledForUrl(url): - return True - - for rule in self.__elemhideRules: - if rule.urlMatch(url): - return True - - return False - - def elementHidingRules(self): - """ - Public method to get the element hiding rules. - - @return element hiding rules (string) - """ - return self.__elementHidingRules - - def elementHidingRulesForDomain(self, domain): - """ - Public method to get the element hiding rules for the given domain. - - @param domain domain name (string) - @return element hiding rules (string) - """ - rules = "" - - for rule in self.__domainRestrictedCssRules: - if rule.matchDomain(domain): - rules += rule.cssSelector() + "," - - return rules - def rule(self, offset): """ Public method to get a specific rule. - @param offset offset of the rule (integer) - @return requested rule (AdBlockRule) + @param offset offset of the rule + @type int + @return requested rule + @rtype AdBlockRule """ if offset >= len(self.__rules): return None @@ -603,7 +541,8 @@ """ Public method to get the list of rules. - @return list of rules (list of AdBlockRule) + @return list of rules + @rtype list of AdBlockRule """ return self.__rules[:] @@ -611,11 +550,12 @@ """ Public method to add a rule. - @param rule reference to the rule to add (AdBlockRule) - @return offset of the rule (integer) + @param rule reference to the rule to add + @type AdBlockRule + @return offset of the rule + @rtype int """ self.__rules.append(rule) - self.__populateCache() self.rulesChanged.emit() return len(self.__rules) - 1 @@ -624,66 +564,40 @@ """ Public method to remove a rule given the offset. - @param offset offset of the rule to remove (integer) + @param offset offset of the rule to remove + @type int """ if offset < 0 or offset > len(self.__rules): return del self.__rules[offset] - self.__populateCache() self.rulesChanged.emit() def replaceRule(self, rule, offset): """ Public method to replace a rule given the offset. - @param rule reference to the rule to set (AdBlockRule) - @param offset offset of the rule to remove (integer) - @return requested rule (AdBlockRule) + @param rule reference to the rule to set + @type AdBlockRule + @param offset offset of the rule to remove + @type int + @return requested rule + @rtype AdBlockRule """ if offset >= len(self.__rules): return None self.__rules[offset] = rule - self.__populateCache() self.rulesChanged.emit() return self.__rules[offset] - def __populateCache(self): - """ - Private method to populate the various rule caches. - """ - self.__networkExceptionRules = [] - self.__networkBlockRules = [] - self.__domainRestrictedCssRules = [] - self.__elementHidingRules = "" - self.__documentRules = [] - self.__elemhideRules = [] - - for rule in self.__rules: - if not rule.isEnabled(): - continue - - if rule.isCSSRule(): - if rule.isDomainRestricted(): - self.__domainRestrictedCssRules.append(rule) - else: - self.__elementHidingRules += rule.cssSelector() + "," - elif rule.isDocument(): - self.__documentRules.append(rule) - elif rule.isElementHiding(): - self.__elemhideRules.append(rule) - elif rule.isException(): - self.__networkExceptionRules.append(rule) - else: - self.__networkBlockRules.append(rule) - def canEditRules(self): """ Public method to check, if rules can be edited. - @return flag indicating rules may be edited (boolean) + @return flag indicating rules may be edited + @rtype bool """ return self.__custom @@ -691,7 +605,8 @@ """ Public method to check, if the subscription can be removed. - @return flag indicating removal is allowed (boolean) + @return flag indicating removal is allowed + @rtype bool """ return not self.__custom and not self.__defaultSubscription @@ -699,18 +614,22 @@ """ Public method to enable a specific rule. - @param offset offset of the rule (integer) - @param enabled new enabled state (boolean) - @return reference to the changed rule (AdBlockRule) + @param offset offset of the rule + @type int + @param enabled new enabled state + @type bool + @return reference to the changed rule + @rtype AdBlockRule """ if offset >= len(self.__rules): return None rule = self.__rules[offset] rule.setEnabled(enabled) + self.rulesEnabledChanged.emit() + if rule.isCSSRule(): from WebBrowser.WebBrowserWindow import WebBrowserWindow - self.__populateCache() WebBrowserWindow.mainWindow().reloadUserStyleSheet() return rule