Skip to content

Commit

Permalink
Cosmetic changes: docstrings formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
afilipovich committed Aug 26, 2018
1 parent 101e602 commit 6159b18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
3 changes: 1 addition & 2 deletions gglsbl/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def _verify_threat_list_checksum(self, threat_list, remote_checksum):
return remote_checksum == local_checksum

def update_hash_prefix_cache(self):
"""Update locally cached threat lists.
"""
"""Update locally cached threat lists."""
try:
self.storage.cleanup_full_hashes()
self.storage.commit()
Expand Down
13 changes: 7 additions & 6 deletions gglsbl/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def fair_use_delay(self):

@autoretry
def get_threats_lists(self):
"""Retrieve all available threat lists
"""
"""Retrieve all available threat lists"""
response = self.service.threatLists().list().execute()
self.set_wait_duration(response.get('minimumWaitDuration'))
return response['threatLists']
Expand Down Expand Up @@ -153,13 +152,13 @@ def get_full_hashes(self, prefixes, client_state):
return response

class URL(object):
"URL representation suitable for lookup"
"""URL representation suitable for lookup"""
def __init__(self, url):
self.url = str(url)

@property
def hashes(self):
"Hashes of all possible permutations of the URL in canonical form"
"""Hashes of all possible permutations of the URL in canonical form"""
for url_variant in self.url_permutations(self.canonical):
url_hash = self.digest(url_variant)
yield url_hash
Expand Down Expand Up @@ -225,7 +224,9 @@ def quote(s):
@staticmethod
def url_permutations(url):
"""Try all permutations of hostname and path which can be applied
to blacklisted URLs"""
to blacklisted URLs
"""
def url_host_permutations(host):
if re.match(r'\d+\.\d+\.\d+\.\d+', host):
yield host
Expand Down Expand Up @@ -263,7 +264,7 @@ def url_path_permutations(path):

@staticmethod
def digest(url):
"Hash the URL"
"""Hash the URL"""
return hashlib.sha256(url.encode('utf-8')).digest()


Expand Down
31 changes: 11 additions & 20 deletions gglsbl/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from gglsbl.utils import to_hex

class ThreatList(object):
"""Represents threat list name.
"""
"""Represents threat list name."""
def __init__(self, threat_type, platform_type, threat_entry_type):
self.threat_type = threat_type
self.platform_type = platform_type
Expand All @@ -31,8 +30,7 @@ def __repr__(self):


class HashPrefixList(object):
"""Wrapper object for threat list data.
"""
"""Wrapper object for threat list data."""
def __init__(self, prefix_size, raw_hashes):
self.prefix_size = prefix_size
self.raw_hashes = raw_hashes
Expand All @@ -46,9 +44,9 @@ def __iter__(self):


class SqliteStorage(object):
"""Storage abstraction for local GSB cache"""
schema_version = '1.1'

"""Storage abstraction for local GSB cache"""
def __init__(self, db_path, timeout=10):
self.db_path = db_path
do_init_db = not os.path.isfile(db_path)
Expand Down Expand Up @@ -182,7 +180,7 @@ def lookup_hash_prefix(self, cues):
return output

def store_full_hash(self, threat_list, hash_value, cache_duration, malware_threat_type):
"Store full hash found for the given hash prefix"
"""Store full hash found for the given hash prefix"""

log.info('Storing full hash {} to list {} with cache duration {}'.format(to_hex(hash_value), str(threat_list), cache_duration))
qi = '''INSERT OR IGNORE INTO full_hash
Expand Down Expand Up @@ -211,8 +209,7 @@ def delete_hash_prefix_list(self, threat_list):
dbc.execute(q, parameters)

def cleanup_full_hashes(self, keep_expired_for=60*60*12):
"""Remove long expired full_hash entries.
"""
"""Remove long expired full_hash entries."""
q = '''DELETE FROM full_hash WHERE expires_at < datetime(current_timestamp, '-{} SECONDS')
'''
log.info('Cleaning up full_hash entries expired more than {} seconds ago.'.format(keep_expired_for))
Expand All @@ -227,8 +224,7 @@ def update_hash_prefix_expiration(self, prefix_value, negative_cache_duration):
dbc.execute(q.format(int(negative_cache_duration)), parameters)

def get_threat_lists(self):
"""Get a list of known threat lists.
"""
"""Get a list of known threat lists."""
q = '''SELECT threat_type,platform_type,threat_entry_type FROM threat_list'''
output = []
with self.get_cursor() as dbc:
Expand All @@ -240,8 +236,7 @@ def get_threat_lists(self):
return output

def get_client_state(self):
"""Get a dict of known threat lists including clientState values.
"""
"""Get a dict of known threat lists including clientState values."""
q = '''SELECT threat_type,platform_type,threat_entry_type,client_state FROM threat_list'''
output = {}
with self.get_cursor() as dbc:
Expand All @@ -253,8 +248,7 @@ def get_client_state(self):
return output

def add_threat_list(self, threat_list):
"""Add threat list entry if it does not exist.
"""
"""Add threat list entry if it does not exist."""
q = '''INSERT OR IGNORE INTO threat_list
(threat_type, platform_type, threat_entry_type, timestamp)
VALUES
Expand All @@ -265,8 +259,7 @@ def add_threat_list(self, threat_list):
dbc.execute(q, params)

def delete_threat_list(self, threat_list):
"""Delete threat list entry.
"""
"""Delete threat list entry."""
log.info('Deleting cached threat list "{}"'.format(repr(threat_list)))
q = '''DELETE FROM threat_list
WHERE threat_type=? AND platform_type=? AND threat_entry_type=?
Expand All @@ -284,8 +277,7 @@ def update_threat_list_client_state(self, threat_list, client_state):
dbc.execute(q, params)

def hash_prefix_list_checksum(self, threat_list):
"""Returns SHA256 checksum for alphabetically-sorted concatenated list of hash prefixes
"""
"""Returns SHA256 checksum for alphabetically-sorted concatenated list of hash prefixes"""
q = '''SELECT value FROM hash_prefix
WHERE threat_type=? AND platform_type=? AND threat_entry_type=?
ORDER BY value
Expand Down Expand Up @@ -329,8 +321,7 @@ def get_hash_prefix_values_to_remove(self, threat_list, indices):
return values_to_remove

def remove_hash_prefix_indices(self, threat_list, indices):
"""Remove records matching idices from a lexicographically-sorted local threat list.
"""
"""Remove records matching idices from a lexicographically-sorted local threat list."""
batch_size = 40
q = '''DELETE FROM hash_prefix
WHERE threat_type=? AND platform_type=? AND threat_entry_type=? AND value IN ({})
Expand Down

0 comments on commit 6159b18

Please sign in to comment.