Skip to content

Commit

Permalink
fix(code): Specify encoding
Browse files Browse the repository at this point in the history
Specify encoding to ensure cross-platform support when opening file
  • Loading branch information
quevon24 committed Jun 28, 2023
1 parent c930df2 commit 84521c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
16 changes: 11 additions & 5 deletions courts_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
from glob import iglob
from io import open
from string import Template, punctuation
from string import Template

db_root = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -133,24 +133,30 @@ def load_courts_db():
:return: A python object containing the rendered courts DB
"""
with open(os.path.join(db_root, "data", "variables.json"), "r") as v:
with open(
os.path.join(db_root, "data", "variables.json"), "r", encoding="utf-8"
) as v:
variables = json.load(v)

for path in iglob(os.path.join(db_root, "data", "places", "*.txt")):
with open(path, "r") as p:
with open(path, "r", encoding="utf-8") as p:
places = f"({'|'.join(p.read().splitlines())})"
variables[path.split(os.path.sep)[-1].split(".txt")[0]] = places

# Add in code to allow ordinal creation for many many judicial district numbers
# for example 1 to 56 judicial districts
with open(os.path.join(db_root, "data", "courts.json"), "r") as f:
with open(
os.path.join(db_root, "data", "courts.json"), "r", encoding="utf-8"
) as f:
temp = f.read()
ord_arrays = re.findall(r"\${(\d+)-(\d+)}", temp)
for ord in ord_arrays:
re_ord = f"(({')|('.join(ordinals[int(ord[0])-1: int(ord[1])])}))"
temp = temp.replace(f"${{{ord[0]}-{ord[1]}}}", re_ord)

with open(os.path.join(db_root, "data", "courts.json"), "r") as f:
with open(
os.path.join(db_root, "data", "courts.json"), "r", encoding="utf-8"
) as f:
s = Template(temp).substitute(**variables)
s = s.replace("\\", "\\\\")
data = json.loads(s)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def read(*parts):
Build an absolute path from *parts* and and return the contents of the
resulting file. Assume UTF-8 encoding.
"""
with codecs.open(os.path.join(HERE, *parts), "rb", "utf-8") as f:
with codecs.open(os.path.join(HERE, *parts), "rb", encoding="utf-8") as f:
return f.read()


Expand Down
6 changes: 5 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def test_json(self):
"""Does our json load properly, and if not where are the issues"""
try:
# Load entire json to shortcircuit testing
with open(os.path.join(db_root, "data", "courts.json"), "r") as f:
with open(
os.path.join(db_root, "data", "courts.json"),
"r",
encoding="utf-8",
) as f:
data = f.read()
json.loads(data)
return
Expand Down

0 comments on commit 84521c2

Please sign in to comment.