30 @classmethod |
31 @classmethod |
31 def fromApiEntry(cls, entry): |
32 def fromApiEntry(cls, entry): |
32 """ |
33 """ |
33 Class method to instantiate a threat list given a threat list entry |
34 Class method to instantiate a threat list given a threat list entry |
34 dictionary. |
35 dictionary. |
35 |
36 |
36 @param entry threat list entry dictionary |
37 @param entry threat list entry dictionary |
37 @type dict |
38 @type dict |
38 @return instantiated object |
39 @return instantiated object |
39 @rtype ThreatList |
40 @rtype ThreatList |
40 """ |
41 """ |
41 return cls(entry['threatType'], entry['platformType'], |
42 return cls(entry["threatType"], entry["platformType"], entry["threatEntryType"]) |
42 entry['threatEntryType']) |
|
43 |
43 |
44 def asTuple(self): |
44 def asTuple(self): |
45 """ |
45 """ |
46 Public method to convert the object to a tuple. |
46 Public method to convert the object to a tuple. |
47 |
47 |
48 @return tuple containing the threat list info |
48 @return tuple containing the threat list info |
49 @rtype tuple of (str, str, str) |
49 @rtype tuple of (str, str, str) |
50 """ |
50 """ |
51 return (self.threatType, self.platformType, self.threatEntryType) |
51 return (self.threatType, self.platformType, self.threatEntryType) |
52 |
52 |
53 def __repr__(self): |
53 def __repr__(self): |
54 """ |
54 """ |
55 Special method to generate a printable representation. |
55 Special method to generate a printable representation. |
56 |
56 |
57 @return printable representation |
57 @return printable representation |
58 @rtype str |
58 @rtype str |
59 """ |
59 """ |
60 return '/'.join(self.asTuple()) |
60 return "/".join(self.asTuple()) |
61 |
61 |
62 |
62 |
63 class HashPrefixList: |
63 class HashPrefixList: |
64 """ |
64 """ |
65 Class implementing a container for threat list data. |
65 Class implementing a container for threat list data. |
66 """ |
66 """ |
|
67 |
67 def __init__(self, prefixLength, rawHashes): |
68 def __init__(self, prefixLength, rawHashes): |
68 """ |
69 """ |
69 Constructor |
70 Constructor |
70 |
71 |
71 @param prefixLength length of each hash prefix |
72 @param prefixLength length of each hash prefix |
72 @type int |
73 @type int |
73 @param rawHashes raw hash prefixes of given length concatenated and |
74 @param rawHashes raw hash prefixes of given length concatenated and |
74 sorted in lexicographical order |
75 sorted in lexicographical order |
75 @type str |
76 @type str |
76 """ |
77 """ |
77 self.__prefixLength = prefixLength |
78 self.__prefixLength = prefixLength |
78 self.__rawHashes = rawHashes |
79 self.__rawHashes = rawHashes |
79 |
80 |
80 def __len__(self): |
81 def __len__(self): |
81 """ |
82 """ |
82 Special method to calculate the number of entries. |
83 Special method to calculate the number of entries. |
83 |
84 |
84 @return length |
85 @return length |
85 @rtype int |
86 @rtype int |
86 """ |
87 """ |
87 return len(self.__rawHashes) // self.__prefixLength |
88 return len(self.__rawHashes) // self.__prefixLength |
88 |
89 |
89 def __iter__(self): |
90 def __iter__(self): |
90 """ |
91 """ |
91 Special method to iterate over the raw hashes. |
92 Special method to iterate over the raw hashes. |
92 |
93 |
93 @return iterator object |
94 @return iterator object |
94 @rtype iterator |
95 @rtype iterator |
95 """ |
96 """ |
96 n = self.__prefixLength |
97 n = self.__prefixLength |
97 return (self.__rawHashes[index:index + n] |
98 return ( |
98 for index in range(0, len(self.__rawHashes), n) |
99 self.__rawHashes[index : index + n] |
99 ) |
100 for index in range(0, len(self.__rawHashes), n) |
|
101 ) |