Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add some type annotation to the code #2067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions sherlock/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
This module defines the objects for notifying the caller about the
results of queries.
"""
import typing as T
from typing import overload
from result import QueryStatus
from colorama import Fore, Style
import webbrowser

# Global variable to count the number of results.
globvar = 0

if T.TYPE_CHECKING:
from result import QueryResult

class QueryNotify:
"""Query Notify Object.
Expand All @@ -20,7 +24,7 @@ class QueryNotify:
override the methods to implement specific functionality.
"""

def __init__(self, result=None):
def __init__(self, result: T.Optional["QueryResult"] = None):
"""Create Query Notify Object.

Contains information about a specific method of notifying the results
Expand All @@ -39,7 +43,7 @@ def __init__(self, result=None):

# return

def start(self, message=None):
def start(self, message: T.Optional["QueryResult"] = None):
"""Notify Start.

Notify method for start of query. This method will be called before
Expand All @@ -58,7 +62,7 @@ def start(self, message=None):

# return

def update(self, result):
def update(self, result: T.Optional["QueryResult"] = None):
"""Notify Update.

Notify method for query result. This method will typically be
Expand All @@ -77,7 +81,7 @@ def update(self, result):

# return

def finish(self, message=None):
def finish(self, message: T.Optional["QueryResult"] = None):
"""Notify Finish.

Notify method for finish of query. This method will be called after
Expand Down Expand Up @@ -114,7 +118,13 @@ class QueryNotifyPrint(QueryNotify):
Query notify class that prints results.
"""

def __init__(self, result=None, verbose=False, print_all=False, browse=False):
def __init__(
self,
result: T.Optional["QueryResult"] = None,
verbose: bool = False,
print_all: bool = False,
browse: bool = False
):
"""Create Query Notify Print Object.

Contains information about a specific method of notifying the results
Expand All @@ -139,7 +149,7 @@ def __init__(self, result=None, verbose=False, print_all=False, browse=False):

return

def start(self, message):
def start(self, message: str) -> None:
"""Notify Start.

Will print the title to the standard output.
Expand Down Expand Up @@ -179,7 +189,7 @@ def countResults(self):
globvar += 1
return globvar

def update(self, result):
def update(self, result: T.Optional["QueryResult"] = None):
"""Notify Update.

Will print the query result to the standard output.
Expand Down Expand Up @@ -247,7 +257,7 @@ def update(self, result):

return

def finish(self, message="The processing has been finished."):
def finish(self, message: str = "The processing has been finished.") -> None:
"""Notify Start.
Will print the last line to the standard output.
Keyword Arguments:
Expand Down
12 changes: 10 additions & 2 deletions sherlock/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module defines various objects for recording the results of queries.
"""
from enum import Enum
import typing as T


class QueryStatus(Enum):
Expand Down Expand Up @@ -31,8 +32,15 @@ class QueryResult():

Describes result of query about a given username.
"""
def __init__(self, username, site_name, site_url_user, status,
query_time=None, context=None):
def __init__(
self,
username: str,
site_name: str,
site_url_user: str,
status: QueryStatus,
query_time: T.Optional[float] = None,
context: T.Optional[str] = None
):
"""Create Query Result Object.

Contains information about a specific method of detecting usernames on
Expand Down
25 changes: 13 additions & 12 deletions sherlock/sherlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
from argparse import ArgumentParser, RawDescriptionHelpFormatter
from time import monotonic
import typing as T

import requests

Expand All @@ -33,7 +34,7 @@


class SherlockFuturesSession(FuturesSession):
def request(self, method, url, hooks=None, *args, **kwargs):
def request(self, method: str, url: str, hooks: T.Optional[T.Dict] = None, *args, **kwargs):
"""Request URL.

This extends the FuturesSession request method to calculate a response
Expand Down Expand Up @@ -127,7 +128,7 @@ def get_response(request_future, error_type, social_network):
return response, error_context, exception_text


def interpolate_string(input_object, username):
def interpolate_string(input_object: T.Union[str, T.Dict, T.List], username: str):
if isinstance(input_object, str):
return input_object.replace("{}", username)
elif isinstance(input_object, dict):
Expand All @@ -137,7 +138,7 @@ def interpolate_string(input_object, username):
return input_object


def check_for_parameter(username):
def check_for_parameter(username: str):
"""checks if {?} exists in the username
if exist it means that sherlock is looking for more multiple username"""
return "{?}" in username
Expand All @@ -147,7 +148,7 @@ def check_for_parameter(username):
checksymbols = ["_", "-", "."]


def multiple_usernames(username):
def multiple_usernames(username: str):
"""replace the parameter with with symbols and return a list of usernames"""
allUsernames = []
for i in checksymbols:
Expand All @@ -156,13 +157,13 @@ def multiple_usernames(username):


def sherlock(
username,
site_data,
query_notify,
tor=False,
unique_tor=False,
proxy=None,
timeout=60,
username: str,
site_data: T.Dict,
query_notify: QueryNotifyPrint,
tor: bool = False,
unique_tor: bool = False,
proxy: T.Optional[str] = None,
timeout: int = 60,
):
"""Run Sherlock Analysis.

Expand Down Expand Up @@ -455,7 +456,7 @@ def sherlock(
return results_total


def timeout_check(value):
def timeout_check(value: float):
"""Check Timeout Argument.

Checks timeout for validity.
Expand Down
15 changes: 12 additions & 3 deletions sherlock/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
import json
import requests
import secrets
import typing as T

class SiteInformation:
def __init__(self, name, url_home, url_username_format, username_claimed,
information, is_nsfw, username_unclaimed=secrets.token_urlsafe(10)):
def __init__(
self,
name: str,
url_home: str,
url_username_format: str,
username_claimed: str,
information: T.Dict,
is_nsfw: bool,
username_unclaimed: str = secrets.token_urlsafe(10)
):
"""Create Site Information Object.

Contains information about a specific website.
Expand Down Expand Up @@ -72,7 +81,7 @@ def __str__(self):


class SitesInformation:
def __init__(self, data_file_path=None):
def __init__(self, data_file_path: T.Optional[str] = None):
"""Create Sites Information Object.

Contains information about all supported websites.
Expand Down