Skip to content

Commit

Permalink
add pydocstyle docstrings to missing functions and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
taddes committed Jan 30, 2024
1 parent f438d7d commit 72b094a
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 32 deletions.
4 changes: 1 addition & 3 deletions scripts/fernet_key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
A command-line utility that generates endpoint encryption keys.
"""
"""A command-line utility that generates endpoint encryption keys."""

from __future__ import print_function
from cryptography.fernet import Fernet
Expand Down
5 changes: 4 additions & 1 deletion scripts/gendpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Module to process configuration from cli arguments and environment
variables.
"""
#! env python3

import argparse
import os

Expand Down Expand Up @@ -35,6 +37,7 @@ def config(env_args: os._Environ) -> argparse.Namespace:


def main():
"""Process environment arguments/variables and set key."""
args = config(os.environ)
if isinstance(args.key, list):
key = args.key[0]
Expand Down
1 change: 1 addition & 0 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""__init__.py for integration tests."""
31 changes: 23 additions & 8 deletions tests/integration/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ItemNotFound(Exception):


class DynamoDBResource(threading.local):
"""DynamoDBResource class subclassing threading.local"""

def __init__(self, **kwargs):
conf = kwargs
if not conf.get("endpoint_url"):
Expand Down Expand Up @@ -87,7 +89,7 @@ def __getattr__(self, name):
def get_latest_message_tablenames(
self, prefix: str = "message", previous: int = 1
) -> list[str]:
"""Fetches the name of the last message table"""
"""Fetch the name of the last message table."""
client = self._resource.meta.client
paginator = client.get_paginator("list_tables")
tables = []
Expand All @@ -102,11 +104,13 @@ def get_latest_message_tablenames(
return tables[0 - previous :]

def get_latest_message_tablename(self, prefix: str = "message") -> str:
"""Fetches the name of the last message table"""
"""Fetch the name of the last message table."""
return self.get_latest_message_tablenames(prefix=prefix, previous=1)[0]


class DynamoDBTable(threading.local):
"""DynamoDBTable class."""

def __init__(self, ddb_resource: DynamoDBResource, *args, **kwargs) -> None:
self._table = ddb_resource.Table(*args, **kwargs)

Expand All @@ -118,13 +122,24 @@ def __getattr__(self, name):
def generate_hash(key: bytes, payload: bytes) -> str:
"""Generate a HMAC for the uaid using the secret
:returns: HMAC hash and the nonce used as a tuple (nonce, hash).
:param key: key
:type: bytes
:param payload: payload
:type: bytes
:returns: A hexadecimal string of the HMAC hash and the nonce, used as a tuple (nonce, hash)
:rtype: str
"""
h = hmac.new(key=key, msg=payload, digestmod=hashlib.sha256)
return h.hexdigest()


def normalize_id(ident: uuid.UUID | str) -> str:
"""Normalize and return ID as string
:param ident: uuid.UUID or str identifier
:returns: string representation of UUID
:raises ValueError: raises an exception if UUID is invalid
"""
if isinstance(ident, uuid.UUID):
return str(ident)
try:
Expand All @@ -134,9 +149,10 @@ def normalize_id(ident: uuid.UUID | str) -> str:


def base64url_encode(value: bytes | str) -> str:
"""Encode an unpadded Base64 URL-encoded string per RFC 7515."""
if isinstance(value, str):
value = bytes(value, "utf-8")
"""Encodes an unpadded Base64 URL-encoded string per RFC 7515."""

return base64.urlsafe_b64encode(value).strip(b"=").decode("utf-8")


Expand All @@ -155,9 +171,7 @@ def base64url_encode(value: bytes | str) -> str:


def get_month(delta: int = 0) -> datetime.date:
"""Basic helper function to get a datetime.date object iterations months
ahead/behind of now.
"""
"""Get a datetime.date object iterations months ahead/behind of now."""
new = last = datetime.date.today()
# Move until we hit a new month, this avoids having to manually
# check year changes as we push forward or backward since the Python
Expand Down Expand Up @@ -308,7 +322,8 @@ def get_router_table(

def track_provisioned(func: Callable[..., T]) -> Callable[..., T]:
"""Tracks provisioned exceptions and increments a metric for them named
after the function decorated"""
after the function decorated.
"""

@wraps(func)
def wrapper(self, *args, **kwargs):
Expand Down
Loading

0 comments on commit 72b094a

Please sign in to comment.