Skip to content

Commit

Permalink
fix language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sumpfork committed Oct 17, 2024
1 parent fccb62c commit cb1ee8b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/domdiv/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import copy
import functools
import importlib.resources
import json
import os

Expand All @@ -20,18 +19,18 @@
@functools.lru_cache()
def get_languages(path="card_db"):
languages = []
for name in importlib.resources.files(f"domdiv").joinpath(path).iterdir():
dir_path = os.path.join(path, name)
if importlib.resources.files("domdiv").joinpath(dir_path).is_dir():
cards_file = os.path.join(dir_path, f"cards_{name}.json.gz")
sets_file = os.path.join(dir_path, f"sets_{name}.json.gz")
types_file = os.path.join(dir_path, f"types_{name}.json.gz")
for name in resource_handling.iter_resource_dir(path):
lang = os.path.basename(name)
if resource_handling.is_resource_dir(name):
cards_file = os.path.join(name, f"cards_{lang}.json.gz")
sets_file = os.path.join(name, f"sets_{lang}.json.gz")
types_file = os.path.join(name, f"types_{lang}.json.gz")
if (
resource_handling.resource_exists(cards_file)
and resource_handling.resource_exists(sets_file)
and resource_handling.resource_exists(types_file)
):
languages.append(name)
languages.append(lang)
if LANGUAGE_XX in languages:
languages.remove(LANGUAGE_XX)
return languages
Expand Down
8 changes: 8 additions & 0 deletions src/domdiv/resource_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
import os


def iter_resource_dir(path):
return importlib.resources.files(f"domdiv").joinpath(path).iterdir()


def is_resource_dir(path):
return importlib.resources.files(f"domdiv").joinpath(path).is_dir()


@contextlib.contextmanager
def get_resource_stream(path):
ref = importlib.resources.files("domdiv").joinpath(path)
Expand Down

0 comments on commit cb1ee8b

Please sign in to comment.