Skip to content

Commit

Permalink
Update linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneSutro committed Feb 12, 2023
1 parent b5e55e6 commit 338060b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ The `Formatter().convertPlainText()` accepts the following:

## Upcoming Support


* Templates
* Choose from a list of templates to send to your board, including calendars, Q\&A, trivia, and more

Expand Down
55 changes: 29 additions & 26 deletions vestaboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
Board - Class
Installable - Class
"""
import warnings
import json
import requests
from vestaboard.formatter import Formatter
import vestaboard.vbUrls as vbUrls
import warnings
import json
import os


class Board:
Expand Down Expand Up @@ -99,14 +98,13 @@ def __init__(
raise ValueError(
"You must supply a read/write enabled API key to use readWrite mode, or instantiate an Installable in readWrite mode to store your credentials."
)
else:
self.apiKey = creds[0]
if self.validateKey:
self.read()
except FileNotFoundError as e:
self.apiKey = creds[0]
if self.validateKey:
self.read()
except FileNotFoundError as exc:
raise FileNotFoundError(
"I couldn't find any saved credentials, and no API key was provided.\nTo use readWrite mode, you must supply a read/write enabled API key, or instantiate an Installable in readWrite mode."
) from e
) from exc
else:
self.apiKey = apiKey
if self.validateKey:
Expand All @@ -123,10 +121,10 @@ def __init__(
raise ValueError(
"Credentials have been saved, but one or more are missing. Create a new installable and pass in saveCredentials=True, or pass in all three parameters when initializing a new Board."
)
except FileNotFoundError as e:
except FileNotFoundError as exc:
raise ValueError(
"You must create an installable first or save credentials by passing saveCredentials=True into installable()."
) from e
) from exc
else:
self.apiKey = apiKey
self.apiSecret = apiSecret
Expand Down Expand Up @@ -159,7 +157,7 @@ def post(self, text):
}
finalText = Formatter()._standard(text)
req = requests.post(
vbUrls.post.format(self.subscriptionId), headers=headers, json=finalText
vbUrls.post.format(self.subscriptionId), headers=headers, json=finalText, timeout=5
)
req.raise_for_status()

Expand Down Expand Up @@ -217,14 +215,15 @@ def raw(self, charList: list, pad=None):
vbUrls.readWrite,
headers=headers,
data=json.dumps(finalText["characters"]),
timeout=5
)
else:
headers = {
"X-Vestaboard-Api-Key": self.apiKey,
"X-Vestaboard-Api-Secret": self.apiSecret,
}
requests.post(
vbUrls.post.format(self.subscriptionId), headers=headers, json=finalText
vbUrls.post.format(self.subscriptionId), headers=headers, json=finalText, timeout=5
)

def _enableLocalApi(
Expand All @@ -233,7 +232,7 @@ def _enableLocalApi(
headers = {"X-Vestaboard-Local-Api-Enablement-Token": enablementKey}
try:
response = requests.post(
vbUrls.enableLocal.format(boardIP), headers=headers
vbUrls.enableLocal.format(boardIP), headers=headers, timeout=5
)
if not response.ok:
print(
Expand All @@ -247,7 +246,7 @@ def _enableLocalApi(
self.localKey = parsed["apiKey"]
print("Success! Here's your local API token:\n", parsed["apiKey"])
if saveToFile:
with open("./local.txt", "w") as local:
with open("./local.txt", "w", encoding="UTF-8") as local:
local.write(parsed["apiKey"])
local.write("\n")
local.write(boardIP)
Expand All @@ -259,7 +258,7 @@ def _enableLocalApi(
raise ConnectionError(
"I couldn't connect to your board. Are you on the same network as the board you're trying to connect to?",
e,
)
) from e

def checkAndEnableLocalAPI(self):
if "enablementToken" not in self.localOptions:
Expand All @@ -284,7 +283,9 @@ def checkAndEnableLocalAPI(self):
)
return True

def read(self, options: dict = {}):
def read(self, options: dict = None):
if options is None:
options = {}
if not self.readWrite:
if self.localOptions is None or "useSavedToken" not in self.localOptions:
raise ValueError(
Expand All @@ -293,14 +294,14 @@ def read(self, options: dict = {}):
if self.localIP and self.localKey:
localHeader = {"X-Vestaboard-Local-Api-Key": self.localKey}
res = requests.get(
vbUrls.postLocal.format(self.localIP), headers=localHeader
vbUrls.postLocal.format(self.localIP), headers=localHeader, timeout=5
)
res.raise_for_status()
response_text = res.json()["message"]
elif self.readWrite and self.apiKey:
readWriteHeader = {"X-Vestaboard-Read-Write-Key": self.apiKey}
print("Getting board message")
res = requests.get(vbUrls.readWrite, headers=readWriteHeader)
res = requests.get(vbUrls.readWrite, headers=readWriteHeader, timeout=5)
res.raise_for_status()
response_text = json.loads(res.json()["currentMessage"]["layout"])
if "print" in options and options["print"]:
Expand All @@ -322,6 +323,7 @@ def _post_local(self, text):
vbUrls.postLocal.format(self.localIP),
headers=localHeader,
data=json.dumps(convertedByDefault),
timeout=5
)
res.raise_for_status()
print(res.text)
Expand All @@ -332,6 +334,7 @@ def _raw_local(self, chars):
vbUrls.postLocal.format(self.localIP),
headers=localHeader,
data=json.dumps(chars),
timeout=5
)
res.raise_for_status()

Expand Down Expand Up @@ -359,7 +362,7 @@ def __init__(
"Installables must have an apiKey and apiSecret parameter."
)
if saveCredentials and apiKey and apiSecret:
with open("./credentials.txt", "w") as cred:
with open("./credentials.txt", "w", encoding='UTF-8') as cred:
cred.write(apiKey + "\n")
cred.write(apiSecret + "\n")
cred.close()
Expand All @@ -377,10 +380,10 @@ def get_subscriptions(self, save=True):
"X-Vestaboard-Api-Key": self.apiKey,
"X-Vestaboard-Api-Secret": self.apiSecret,
}
response = requests.get(vbUrls.subscription, headers=headers)
response = requests.get(vbUrls.subscription, headers=headers, timeout=5)
response.raise_for_status()
if self.saveCredentials or save and response.status_code == 200:
with open("./credentials.txt", "a") as cred:
with open("./credentials.txt", "a", encoding='UTF-8') as cred:
cred.write(response.json()["subscriptions"][0]["_id"] + "\n")
cred.close()

Expand All @@ -389,14 +392,14 @@ def get_subscriptions(self, save=True):


def get_creds():
with open("./credentials.txt", "r") as cred:
with open("./credentials.txt", "r", encoding='UTF-8') as cred:
creds = cred.read().splitlines()
return creds


def get_local_token():
try:
with open("./local.txt", "r") as local:
with open("./local.txt", "r", encoding='UTF-8') as local:
token = local.read().splitlines()
if len(token) == 0:
raise ValueError(
Expand All @@ -411,7 +414,7 @@ def get_local_token():
"Your local.txt file contains more than your key and IP (or has more than two lines of text). Please remove the extra key or line and try again. Your local.txt file should contain only the local key and board IP address, each on their own line."
)
return (token[0], token[1])
except FileNotFoundError:
except FileNotFoundError as exc:
raise FileNotFoundError(
"I couldn't find any stored tokens. If you have already enabled your board's local API, you can pass in your board's IP and token in via the `localApi` dict, or pass in just the enablement token to get and store a new API token."
)
) from exc
24 changes: 11 additions & 13 deletions vestaboard/formatter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from vestaboard.characters import characters
from vestaboard.characters import colors
from vestaboard.characters import reverseCharacters
import warnings
import textwrap
import re
import math
from vestaboard.characters import characters
from vestaboard.characters import colors
from vestaboard.characters import reverseCharacters


class Formatter:
Expand Down Expand Up @@ -72,10 +72,10 @@ def convertLine(self, inputString, justify="center", color=" ", spaceBuffer=Fals
if color != " ":
try:
color = colors[color]
except KeyError:
except KeyError as exc:
raise KeyError(
"Valid colors are red, orange, yellow, green, blue, violet, white, and black (default black)."
)
) from exc
converted = []
if len(inputString) - numCharacterCodes > 22:
raise Exception(
Expand Down Expand Up @@ -147,7 +147,9 @@ def _reverse_convert(charArray, normalize: bool = False):
else:
return convertedArray

def _add_vestaboard_spacing(self, lines, size=[6, 22], justify="left"):
def _add_vestaboard_spacing(self, lines, size=None, justify="left"):
if size is None:
size = [6, 22]
_, maxChars = size
longestLine = 0
for line in lines:
Expand Down Expand Up @@ -181,20 +183,16 @@ def _add_vestaboard_spacing(self, lines, size=[6, 22], justify="left"):

return paddedLines

# Find longest line
# Determine num of extra spaces needed and divide by 2 (take floor)
# Add spaces to left side (or right, if justify == right)
# Add remaining spaces to opposite side up to char limit
pass

def convertPlainText(
self,
text,
size=[6, 22],
size=None,
justify="center",
align="center",
useVestaboardCentering=False,
):
if size is None:
size = [6, 22]
maxRows, maxCols = size
splitLines = []
for linebreak in text.split("\n"):
Expand Down

0 comments on commit 338060b

Please sign in to comment.