Helpviewer/AdBlock/AdBlockSubscription.py

changeset 1960
d8c45fe8a1b9
parent 1955
9e6da33c1c10
child 1963
9c5b3235abf9
equal deleted inserted replaced
1957:2fed7bc4ad83 1960:d8c45fe8a1b9
55 55
56 self.__rules = [] # list containing all AdBlock rules 56 self.__rules = [] # list containing all AdBlock rules
57 57
58 self.__networkExceptionRules = [] 58 self.__networkExceptionRules = []
59 self.__networkBlockRules = [] 59 self.__networkBlockRules = []
60 self.__pageRules = []
61 60
62 self.__parseUrl(url) 61 self.__parseUrl(url)
63 62
64 def __parseUrl(self, url): 63 def __parseUrl(self, url):
65 """ 64 """
86 85
87 self.__loadRules() 86 self.__loadRules()
88 87
89 def url(self): 88 def url(self):
90 """ 89 """
91 Public method to generate the url for this subscription. 90 Public method to generate the URL for this subscription.
92 91
93 @return AdBlock URL for the subscription (QUrl) 92 @return AdBlock URL for the subscription (QUrl)
94 """ 93 """
95 url = QUrl() 94 url = QUrl()
96 url.setScheme("abp") 95 url.setScheme("abp")
322 textStream = QTextStream(f) 321 textStream = QTextStream(f)
323 textStream << "[Adblock Plus 0.7.1]\n" 322 textStream << "[Adblock Plus 0.7.1]\n"
324 for rule in self.__rules: 323 for rule in self.__rules:
325 textStream << rule.filter() << "\n" 324 textStream << rule.filter() << "\n"
326 325
327 def pageRules(self): 326 def match(self, req, urlDomain, urlString):
328 """ 327 """
329 Public method to get the page rules of the subscription. 328 Public method to check the subscription for a matching rule.
330 329
331 @return list of rule objects (list of AdBlockRule) 330 @param req reference to the network request (QNetworkRequest)
332 """ 331 @param urlDomain domain of the URL (string)
333 return self.__pageRules[:] 332 @param urlString URL (string)
334
335 def allow(self, urlString):
336 """
337 Public method to check, if the given URL is allowed.
338
339 @return reference to the rule object or None (AdBlockRule) 333 @return reference to the rule object or None (AdBlockRule)
340 """ 334 """
341 for rule in self.__networkExceptionRules: 335 for rule in self.__networkExceptionRules:
342 if rule.networkMatch(urlString): 336 if rule.networkMatch(req, urlDomain, urlString):
337 return None
338
339 for rule in self.__networkBlockRules:
340 if rule.networkMatch(req, urlDomain, urlString):
343 return rule 341 return rule
344 342
345 return None 343 return None
346 344
347 def block(self, urlString): 345 def adBlockDisabledForUrl(self, url):
348 """ 346 """
349 Public method to check, if the given URL should be blocked. 347 Public method to check, if AdBlock is disabled for the given URL.
350 348
351 @return reference to the rule object or None (AdBlockRule) 349 @param url URL to check (QUrl)
352 """ 350 @return flag indicating disabled state (boolean)
353 for rule in self.__networkBlockRules: 351 """
354 if rule.networkMatch(urlString): 352 for rule in self.__documentRules:
355 return rule 353 if rule.urlMatch(url):
356 354 return True
357 return None 355
356 return False
357
358 def elemHideDisabledForUrl(self, url):
359 """
360 Public method to check, if element hiding is disabled for the given URL.
361
362 @param url URL to check (QUrl)
363 @return flag indicating disabled state (boolean)
364 """
365 if self.adBlockDisabledForUrl(url):
366 return True
367
368 for rule in self.__elemhideRules:
369 if rule.urlMatch(url):
370 return True
371
372 return False
373
374 def elementHidingRules(self):
375 """
376 Public method to get the element hiding rules.
377
378 @return element hiding rules (string)
379 """
380 return self.__elementHidingRules
381
382 def elementHidingRulesForDomain(self, domain):
383 """
384 Public method to get the element hiding rules for the given domain.
385
386 @param domain domain name (string)
387 @return element hiding rules (string)
388 """
389 rules = ""
390
391 for rule in self.__domainRestrictedCssRules:
392 if rule.matchDomain(domain):
393 rules += rule.cssSelector() + ","
394
395 return rules
358 396
359 def allRules(self): 397 def allRules(self):
360 """ 398 """
361 Public method to get the list of rules. 399 Public method to get the list of rules.
362 400
400 438
401 def __populateCache(self): 439 def __populateCache(self):
402 """ 440 """
403 Private method to populate the various rule caches. 441 Private method to populate the various rule caches.
404 """ 442 """
443 self.__networkExceptionRules = []
405 self.__networkBlockRules = [] 444 self.__networkBlockRules = []
406 self.__networkExceptionRules = [] 445 self.__domainRestrictedCssRules = []
407 self.__pageRules = [] 446 self.__elementHidingRules = ""
408 if not self.isEnabled(): 447 self.__documentRules = []
409 return 448 self.__elemhideRules = []
410 449
411 for rule in self.__rules: 450 for rule in self.__rules:
412 if not rule.isEnabled(): 451 if not rule.isEnabled():
413 continue 452 continue
414 453
415 if rule.isCSSRule(): 454 if rule.isCSSRule():
416 self.__pageRules.append(rule) 455 if rule.isDomainRestricted():
456 self.__domainRestrictedCssRules.append(rule)
457 else:
458 self.__elementHidingRules += rule.cssSelector() + ","
459 elif rule.isDocument():
460 self.__documentRules.append(rule)
461 elif rule.isElementHiding():
462 self.__elemhideRules.append(rule)
417 elif rule.isException(): 463 elif rule.isException():
418 self.__networkExceptionRules.append(rule) 464 self.__networkExceptionRules.append(rule)
419 else: 465 else:
420 self.__networkBlockRules.append(rule) 466 self.__networkBlockRules.append(rule)

eric ide

mercurial