From d592880c4bc4c581d8847d96ff73bc319ca1fac2 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sun, 26 Dec 2021 17:11:37 +0100 Subject: Make cards be part of the Python package data We now load the cards using pkg_resources. This is way better in many aspects: * we can install the package using pip without having issues loading cards * we don't need to chdir to / before running swiftstoryd * we don't need to rely on data_files which is deprecated. Signed-off-by: Olivier Gayot --- swiftstory/cards.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'swiftstory/cards.py') diff --git a/swiftstory/cards.py b/swiftstory/cards.py index d570f87..62f1e37 100644 --- a/swiftstory/cards.py +++ b/swiftstory/cards.py @@ -1,16 +1,18 @@ import json +import os + +import pkg_resources class Cards: @staticmethod def get_white_cards(lang): ''' Read the file containing the white cards and return a list of cards ''' - with open(f'usr/share/swiftstory/lang/{lang}/cards/white.json', encoding='utf-8') as fh: - return json.load(fh) + fh = pkg_resources.resource_stream(__name__, os.path.join("data/lang", lang, "cards/white.json")) + return json.load(fh) @staticmethod def get_black_cards(lang): ''' Read the file containing the black cards and return a list of cards ''' - - with open(f'usr/share/swiftstory/lang/{lang}/cards/black.json', encoding='utf-8') as fh: - return json.load(fh) + fh = pkg_resources.resource_stream(__name__, os.path.join("data/lang", lang, "cards/black.json")) + return json.load(fh) -- cgit v1.2.3