-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automatic censoring to logging and refactor utils to folder (#96)
* Add automatic censoring to logging and refactor utils to folder * Use asterisks instead of CENSORED * Add extra key to check that it only modifies what is intended * Remove censor_location and associated tests
- Loading branch information
1 parent
f0b3f4e
commit 1eee49c
Showing
17 changed files
with
205 additions
and
97 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
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
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 was deleted.
Oops, something went wrong.
Empty file.
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,24 @@ | ||
"""Conversion utilities used""" | ||
import logging | ||
|
||
_LOGGER: logging.Logger = logging.getLogger(__package__) | ||
|
||
|
||
def convert_to_miles(kilometers: float) -> float: | ||
"""Convert kilometers to miles""" | ||
_LOGGER.debug(f"Converting {kilometers} to miles...") | ||
return round(kilometers * 0.621371192, 4) | ||
|
||
|
||
def convert_to_liter_per_100_miles(liters: float) -> float: | ||
"""Convert liters per 100 km to liters per 100 miles""" | ||
_LOGGER.debug("Converting to L/100miles...") | ||
return round(liters * 1.609344, 4) | ||
|
||
|
||
def convert_to_mpg(liters_per_100_km: float) -> float: | ||
"""Convert to miles per UK gallon (MPG)""" | ||
_LOGGER.debug("Converting to MPG...") | ||
if liters_per_100_km > 0.0: | ||
return round(282.5 / liters_per_100_km, 4) | ||
return 0.0 |
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,12 @@ | ||
"""Formatters used""" | ||
|
||
|
||
def format_odometer(raw: list) -> dict: | ||
"""Formats odometer information from a list to a dict.""" | ||
instruments: dict = {} | ||
for instrument in raw: | ||
instruments[instrument["type"]] = instrument["value"] | ||
if "unit" in instrument: | ||
instruments[instrument["type"] + "_unit"] = instrument["unit"] | ||
|
||
return instruments |
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,14 @@ | ||
"""Locale validation utilities""" | ||
from langcodes import Language | ||
from langcodes.tag_parser import LanguageTagError | ||
|
||
|
||
def is_valid_locale(locale: str) -> bool: | ||
"""Is locale string valid.""" | ||
valid = False | ||
if locale: | ||
try: | ||
valid = Language.get(locale).is_valid() | ||
except LanguageTagError: | ||
pass | ||
return valid |
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,53 @@ | ||
"""Log utilities""" | ||
|
||
|
||
def censor(text) -> str: | ||
""" | ||
Replaces characters in a str with the asterisks | ||
text: The text to censure. | ||
""" | ||
char = "*" | ||
text = text if text else "" | ||
return text[0] + (len(text) - 1) * char if text else text | ||
|
||
|
||
def censor_cookie(cookie) -> str: | ||
""" | ||
Replaces characters in a str with the asterisks | ||
text: The text to censure. | ||
""" | ||
string = cookie.split("=") | ||
return string[0] + "=" + censor(string[1]) | ||
|
||
|
||
def censor_vin(vin) -> str: | ||
""" | ||
Replaces parts of the vin number with asterisks. | ||
""" | ||
vin = vin if vin else "" | ||
return vin[:-8] + "********" if vin else vin | ||
|
||
|
||
def censor_dict(dictionary) -> dict: | ||
""" | ||
Censors token, vin and other private info in a dict. | ||
""" | ||
if "vin" in dictionary: | ||
dictionary["vin"] = censor_vin(dictionary["vin"]) | ||
|
||
if "VIN" in dictionary: | ||
dictionary["VIN"] = censor_vin(dictionary["VIN"]) | ||
|
||
if "X-TME-TOKEN" in dictionary: | ||
dictionary["X-TME-TOKEN"] = censor(dictionary["X-TME-TOKEN"]) | ||
|
||
if "uuid" in dictionary: | ||
dictionary["uuid"] = censor(dictionary["uuid"]) | ||
|
||
if "id" in dictionary: | ||
dictionary["id"] = censor(str(dictionary["id"])) | ||
|
||
if "Cookie" in dictionary: | ||
dictionary["Cookie"] = censor_cookie(dictionary["Cookie"]) | ||
|
||
return dictionary |
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,13 @@ | ||
"""Token validation utilities""" | ||
from mytoyota.const import TOKEN_LENGTH | ||
from mytoyota.exceptions import ToyotaInvalidToken | ||
|
||
|
||
def is_valid_token(token: str) -> bool: | ||
"""Checks if token is the correct length""" | ||
if token and len(token) == TOKEN_LENGTH and token.endswith("..*"): | ||
return True | ||
|
||
raise ToyotaInvalidToken( | ||
f"Token must end with '..*' and be {TOKEN_LENGTH} characters long." | ||
) |
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
Oops, something went wrong.