Skip to content

Commit

Permalink
add docstrings to class and fix ambiguous descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lordlabuckdas committed Aug 24, 2021
1 parent 08da1fa commit 59dd4d2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
20 changes: 14 additions & 6 deletions snare/cloner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@


class BaseCloner:
"""Abstract base class for all core functions of cloner"""

def __init__(self, root: str, max_depth: int, css_validate: bool, default_path: str = "/opt/snare") -> None:
"""Base class for all core functions of the cloner
"""Constructor method
:param root: Website root URL
:type root: str
Expand Down Expand Up @@ -109,7 +111,7 @@ def get_headers(
return headers, content_type

async def process_link(self, url: str, level: int, check_host: bool = False) -> Union[str, None]:
"""Process (relative and absolute) links to make them suitable for serving and add new URLs to the queue
"""Add URL to the queue if new and return its relative URL
:param url: Page URL
:type url: str
Expand Down Expand Up @@ -152,7 +154,7 @@ async def process_link(self, url: str, level: int, check_host: bool = False) ->
return res

async def replace_links(self, data: Union[bytes, str], level: int) -> BeautifulSoup:
"""Replace website links to make them suitable for serving
"""Replace all links present in the page's data with their relative versions
:param data: Page data
:type data: Union[bytes, str]
Expand Down Expand Up @@ -299,10 +301,12 @@ async def get_root_host(self) -> None:


class SimpleCloner(BaseCloner):
"""aiohttp-driven data fetching"""

async def fetch_data(
self, session: aiohttp.ClientSession, current_url: yarl.URL, level: int, try_count: int
) -> Tuple[Union[yarl.URL, None], bytes, List[Dict[str, str]], str]:
"""Fetch data from the given URL using aiohttp
"""Fetch data from the given URL using aiohttp's ClientSession
:param session: aiohttp ClientSession object
:type session: aiohttp.ClientSession
Expand Down Expand Up @@ -335,10 +339,12 @@ async def fetch_data(


class HeadlessCloner(BaseCloner):
"""Pyppeteer-driven data fetching"""

async def fetch_data(
self, browser: pyppeteer.browser.Browser, current_url: yarl.URL, level: int, try_count: int
) -> Tuple[Union[yarl.URL, None], Union[str, bytes], List[Dict[str, str]], Union[str, None]]:
"""Fetch data from the given URL using Pyppeteer
"""Fetch data from the given URL using Pyppeteer's headless browser
:param browser: Pyppeteer Browser object
:type browser: pyppeteer.Browser.browser
Expand Down Expand Up @@ -378,10 +384,12 @@ async def fetch_data(


class CloneRunner:
"""One class to rule them all - Runner class for all cloners"""

def __init__(
self, root: str, max_depth: int, css_validate: bool, default_path: str = "/opt/snare", headless: bool = False
) -> None:
"""Runner class for all cloners
"""Constructor method
:param root: Website root URL
:type root: str
Expand Down
4 changes: 3 additions & 1 deletion snare/html_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@


class HtmlHandler:
"""Handle HTML content of pages"""

def __init__(self, no_dorks: bool, tanner: str):
"""Class to handle HTML contents of pages
"""Constructor method
:param no_dorks: Disable dorks
:type no_dorks: bool
Expand Down
4 changes: 3 additions & 1 deletion snare/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@


class SnareMiddleware:
"""Middleware for Snare's aiohttp web server"""

def __init__(
self, error_404: Union[None, str], headers: List[Dict[str, str]] = [], server_header: str = ""
) -> None:
"""Middleware class for Snare's aiohttp web server
"""Constructor method
:param error_404: 404 page's file name (hash)
:type error_404: Union[None, str]
Expand Down
4 changes: 3 additions & 1 deletion snare/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


class HttpRequestHandler:
"""Handle all HTTP requests to Snare"""

def __init__(
self,
meta: Dict,
Expand All @@ -22,7 +24,7 @@ def __init__(
keep_alive: int = 75,
**kwargs: Dict[str, str]
) -> None:
"""HTTP request handler class
"""Constructor method
:param meta: Meta info from `meta.json`
:type meta: Dict
Expand Down
4 changes: 3 additions & 1 deletion snare/tanner_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@


class TannerHandler:
"""Handle Tanner communication"""

def __init__(self, run_args: argparse.Namespace, meta: Dict, snare_uuid: bytes) -> None:
"""Class for handling Tanner communication
"""Constructor method
:param run_args: Runtime CLI arguments
:type run_args: argparse.Namespace
Expand Down
4 changes: 3 additions & 1 deletion snare/utils/asyncmock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from unittest.mock import Mock


class AsyncMock(Mock): # custom function defined to mock asyncio coroutines
class AsyncMock(Mock):
"""Custom class defined to mock asyncio coroutines"""

def __call__(self, *args, **kwargs):
sup = super(AsyncMock, self)

Expand Down
5 changes: 3 additions & 2 deletions snare/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ def filter(self, record: logging.LogRecord) -> bool:
:return: True if record's level is lesser than the set level
:rtype: bool
"""
# "<" instead of "<=": since logger.setLevel is inclusive, this should be exclusive
return record.levelno < self.level

# "<" instead of "<=": since logger.setLevel is inclusive, this should be exclusive


class Logger:
"""Modify built-in logger's format and handlers for Snare and Cloner"""

@staticmethod
def create_logger(debug_filename: str, err_filename: str, logger_name: str) -> logging.Logger:
"""Create logger with debugging and error level handlers for Snare
Expand Down
10 changes: 7 additions & 3 deletions snare/utils/snare_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@


class VersionManager:
"""Check Snare-Tanner compatibility"""

def __init__(self) -> None:
"""Version manager class for Snare-Tanner compatibility checking"""
"""Constructor method"""
self.logger = logging.getLogger(__name__)
self.version = "0.3.0"
self.version_mapper = {
Expand Down Expand Up @@ -40,13 +42,15 @@ def check_compatibility(self, tanner_version: str) -> None:


class Converter:
"""Convert a website's source files to a Snare-friendly form"""

def __init__(self) -> None:
"""Converter class"""
"""Constructor method"""
self.logger = logging.getLogger(__name__)
self.meta = {}

def convert(self, path: str) -> None:
"""Convert all pages to a Snare-friendly form and write meta info
"""Rename all page files to their MD5 hash and populate meta.json with their hash and Content-Type header
:param path: Page files storage directory
:type path: str
Expand Down

0 comments on commit 59dd4d2

Please sign in to comment.