-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from WEHI-ResearchComputing/logging
Logging
- Loading branch information
Showing
7 changed files
with
205 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Iterable, TypedDict | ||
|
||
from bs4 import BeautifulSoup | ||
|
||
|
||
class DownloadFile(TypedDict): | ||
client_entropy: str | ||
encrypted: str | ||
encrypted_size: int | ||
fileaead: str | ||
fileiv: str | ||
id: int | ||
key_salt: str | ||
key_version: int | ||
mime: str | ||
#: filename | ||
name: str | ||
password_encoding: str | ||
password_hash_iterations: int | ||
password_version: int | ||
size: int | ||
transferid: int | ||
|
||
def files_from_page(content: bytes) -> Iterable[DownloadFile]: | ||
""" | ||
Yields dictionaries describing the files listed on a FileSender web page | ||
Params: | ||
content: The HTML content of the FileSender download page | ||
""" | ||
for file in BeautifulSoup(content, "html.parser").find_all( | ||
class_="file" | ||
): | ||
yield { | ||
"client_entropy": file.attrs[f"data-client-entropy"], | ||
"encrypted": file.attrs["data-encrypted"], | ||
"encrypted_size": int(file.attrs["data-encrypted-size"]), | ||
"fileaead": file.attrs["data-fileaead"], | ||
"fileiv": file.attrs["data-fileiv"], | ||
"id": int(file.attrs["data-id"]), | ||
"key_salt": file.attrs["data-key-salt"], | ||
"key_version": int(file.attrs["data-key-version"]), | ||
"mime": file.attrs["data-mime"], | ||
"name": file.attrs["data-name"], | ||
"password_encoding": file.attrs["data-password-encoding"], | ||
"password_hash_iterations": int(file.attrs["data-password-hash-iterations"]), | ||
"password_version": int(file.attrs["data-password-version"]), | ||
"size": int(file.attrs["data-size"]), | ||
"transferid": int(file.attrs["data-transferid"]), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Union | ||
from click import ParamType, Context, Parameter | ||
from enum import Enum | ||
import logging | ||
|
||
class LogLevel(Enum): | ||
NOTSET = 0 | ||
DEBUG = 10 | ||
#: Used for verbose logging that the average user wouldn't want | ||
VERBOSE = 15 | ||
INFO = 20 | ||
#: Used for basic feedback that a CLI user would expect | ||
FEEDBACK = 25 | ||
WARNING = 30 | ||
ERROR = 40 | ||
CRITICAL = 50 | ||
|
||
def configure_label(self): | ||
""" | ||
Configures the logging module to understand this log level | ||
""" | ||
logging.addLevelName(self.value, self.name) | ||
|
||
def configure_extra_levels(): | ||
""" | ||
Configures the logging module to understand the additional log levels | ||
""" | ||
for level in (LogLevel.VERBOSE, LogLevel.FEEDBACK): | ||
level.configure_label() | ||
|
||
class LogParam(ParamType): | ||
name = "LogParam" | ||
|
||
def convert(self, value: Union[int, str], param: Union[Parameter, None], ctx: Union[Context, None]) -> int: | ||
if isinstance(value, int): | ||
return value | ||
|
||
# Convert string representation to int | ||
if not hasattr(LogLevel, value): | ||
self.fail(f"{value!r} is not a valid log level", param, ctx) | ||
|
||
return LogLevel[value].value | ||
|
||
def get_metavar(self, param: Parameter) -> Union[str, None]: | ||
# Print out the choices | ||
return "|".join(LogLevel._member_map_) |
Oops, something went wrong.