Skip to content

Commit

Permalink
Ensure that JSON files are proprely closed in localization
Browse files Browse the repository at this point in the history
  • Loading branch information
loichuder committed Oct 11, 2024
1 parent bad9db8 commit f5cfd1f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions orangecanvas/utils/localization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def pl(n: int, forms: str) -> str: # pylint: disable=invalid-name
word = word.upper()
return word

def _load_json(path):
with open(path) as handle:
return json.load(handle)

@lru_cache
def get_languages(package=None):
Expand All @@ -52,7 +55,7 @@ def get_languages(package=None):
for name, ext in map(os.path.splitext, os.listdir(msgs_path)):
if ext == ".json":
try:
msgs = json.load(open(os.path.join(msgs_path, name + ext)))
msgs = _load_json(os.path.join(msgs_path, name + ext))
except json.JSONDecodeError:
warnings.warn("Invalid language file "
+ os.path.join(msgs_path, name + ext))
Expand Down Expand Up @@ -102,7 +105,7 @@ def __init__(self, package, organization="biolab.si", application="Orange"):
if not os.path.exists(path):
path = os.path.join(package_path, "i18n", f"{DEFAULT_LANGUAGE}.json")
assert os.path.exists(path), f"Missing language file {path}"
self.m = _list(json.load(open(path)))
self.m = _list(_load_json(path))

# Extra argument(s) can give the original string or any other relevant data
def c(self, idx, *_):
Expand Down

0 comments on commit f5cfd1f

Please sign in to comment.