WebBrowser/SafeBrowsing/SafeBrowsingCache.py

changeset 6229
72b40274a3b6
parent 6186
83451f10f3eb
child 6645
ad476851d7e0
equal deleted inserted replaced
6228:9c3fbf39ec9b 6229:72b40274a3b6
21 from PyQt5.QtCore import QObject, QByteArray, QCryptographicHash, \ 21 from PyQt5.QtCore import QObject, QByteArray, QCryptographicHash, \
22 QCoreApplication, QEventLoop 22 QCoreApplication, QEventLoop
23 from PyQt5.QtSql import QSql, QSqlDatabase, QSqlQuery 23 from PyQt5.QtSql import QSql, QSqlDatabase, QSqlQuery
24 24
25 from .SafeBrowsingUtilities import toHex 25 from .SafeBrowsingUtilities import toHex
26 26 from .SafeBrowsingThreatList import ThreatList
27
28 class ThreatList(object):
29 """
30 Class implementing the threat list info.
31 """
32 def __init__(self, threatType, platformType, threatEntryType):
33 """
34 Constructor
35
36 @param threatType threat type
37 @type str
38 @param platformType platform type
39 @type str
40 @param threatEntryType threat entry type
41 @type str
42 """
43 self.threatType = threatType
44 self.platformType = platformType
45 self.threatEntryType = threatEntryType
46
47 @classmethod
48 def fromApiEntry(cls, entry):
49 """
50 Class method to instantiate a threat list given a threat list entry
51 dictionary.
52
53 @param entry threat list entry dictionary
54 @type dict
55 @return instantiated object
56 @rtype ThreatList
57 """
58 return cls(entry['threatType'], entry['platformType'],
59 entry['threatEntryType'])
60
61 def asTuple(self):
62 """
63 Public method to convert the object to a tuple.
64
65 @return tuple containing the threat list info
66 @rtype tuple of (str, str, str)
67 """
68 return (self.threatType, self.platformType, self.threatEntryType)
69
70 def __repr__(self):
71 """
72 Special method to generate a printable representation.
73
74 @return printable representation
75 @rtype str
76 """
77 return '/'.join(self.asTuple())
78
79
80 class HashPrefixList(object):
81 """
82 Class implementing a container for threat list data.
83 """
84 def __init__(self, prefixLength, rawHashes):
85 """
86 Constructor
87
88 @param prefixLength length of each hash prefix
89 @type int
90 @param rawHashes raw hash prefixes of given length concatenated and
91 sorted in lexicographical order
92 @type str
93 """
94 self.__prefixLength = prefixLength
95 self.__rawHashes = rawHashes
96
97 def __len__(self):
98 """
99 Special method to calculate the number of entries.
100
101 @return length
102 @rtype int
103 """
104 return len(self.__rawHashes) // self.__prefixLength
105
106 def __iter__(self):
107 """
108 Special method to iterate over the raw hashes.
109
110 @return iterator object
111 @rtype iterator
112 """
113 n = self.__prefixLength
114 return (self.__rawHashes[index:index + n]
115 for index in range(0, len(self.__rawHashes), n)
116 )
117 27
118 28
119 class SafeBrowsingCache(QObject): 29 class SafeBrowsingCache(QObject):
120 """ 30 """
121 Class implementing a cache for Google Safe Browsing. 31 Class implementing a cache for Google Safe Browsing.

eric ide

mercurial