diff options
| author | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-26 17:11:37 +0100 | 
|---|---|---|
| committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2021-12-26 18:28:21 +0100 | 
| commit | d592880c4bc4c581d8847d96ff73bc319ca1fac2 (patch) | |
| tree | ac4996aa4d81ef55e68b209d4c541c206f1efe61 | |
| parent | 794292740c30bfea48dc23eb62fb6aa531bfdca7 (diff) | |
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 <olivier.gayot@sigexec.com>
| -rw-r--r-- | README | 5 | ||||
| -rw-r--r-- | debian/control | 3 | ||||
| -rw-r--r-- | debian/copyright | 2 | ||||
| -rw-r--r-- | setup.cfg | 11 | ||||
| -rw-r--r-- | swiftstory/cards.py | 12 | ||||
| -rw-r--r-- | swiftstory/data/lang/en/cards/black.json (renamed from usr/share/swiftstory/lang/en/cards/black.json) | 0 | ||||
| -rw-r--r-- | swiftstory/data/lang/en/cards/white.json (renamed from usr/share/swiftstory/lang/en/cards/white.json) | 0 | ||||
| -rw-r--r-- | swiftstory/data/lang/fr/cards/black.json (renamed from usr/share/swiftstory/lang/fr/cards/black.json) | 0 | ||||
| -rw-r--r-- | swiftstory/data/lang/fr/cards/white.json (renamed from usr/share/swiftstory/lang/fr/cards/white.json) | 0 | ||||
| -rw-r--r-- | swiftstory/game_manager.py | 4 | ||||
| -rw-r--r-- | tests/test_cards.py | 6 | 
11 files changed, 22 insertions, 21 deletions
| @@ -1,15 +1,14 @@  Installation of the Server  ========================== -    $ python setup.py build -    $ [sudo] python setup.py install +    $ python3 -m pip install .  Execution  =========  Start the python server (it will listen to any connection on port 1236) -    $ chdir / && swiftstoryd & +    $ swiftstoryd &  Installation of the web application  =================================== diff --git a/debian/control b/debian/control index 33b1e60..bdb5941 100644 --- a/debian/control +++ b/debian/control @@ -9,12 +9,13 @@ Build-Depends: python3-setuptools,                 debhelper-compat (= 13),                 python3-pytest,                 python3-websockets, +               python3-pkg-resources,                 python3  Standards-Version: 3.9.1  Package: swiftstory  Architecture: all  Pre-Depends: ${misc:Pre-Depends} -Depends: ${misc:Depends}, ${python3:Depends}, python3-websockets +Depends: ${misc:Depends}, ${python3:Depends}, python3-websockets, python3-pkg-resources  Description: SwiftStory game: We're not out of the woods yet. diff --git a/debian/copyright b/debian/copyright index a9f62d4..c9331ff 100644 --- a/debian/copyright +++ b/debian/copyright @@ -7,6 +7,6 @@ Files: *  Copyright: Copyright (c) 2021 Olivier Gayot  License: BSD-3-clause -Files: usr/share/swiftstory/lang/*/cards +Files: swiftstory/data/lang/*/cards/*.json  Copyright: Copyright (c) Cards Against Humanity (https://cardsagainsthumanity.com)  License: CC-BY-SA-2.0 @@ -14,6 +14,7 @@ classifiers =  [options]  packages = find:  python_requires = >=3.7 +include_package_data = True  [options.data_files]  share/swiftstory/www/ = @@ -26,13 +27,9 @@ share/swiftstory/www/ =      usr/share/swiftstory/www/swiftstory-mobile.js  # TODO install jQuery using libjs-jquery      usr/share/swiftstory/www/jquery.js -# TODO install cards as package data instead -share/swiftstory/lang/en/cards = -    usr/share/swiftstory/lang/en/cards/black.json -    usr/share/swiftstory/lang/en/cards/white.json -share/swiftstory/lang/fr/cards = -    usr/share/swiftstory/lang/fr/cards/black.json -    usr/share/swiftstory/lang/fr/cards/white.json + +[options.package_data] +swiftstory = data/lang/*/cards/*.json  [options.entry_points]  console_scripts = 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) diff --git a/usr/share/swiftstory/lang/en/cards/black.json b/swiftstory/data/lang/en/cards/black.json index 8b841a1..8b841a1 100644 --- a/usr/share/swiftstory/lang/en/cards/black.json +++ b/swiftstory/data/lang/en/cards/black.json diff --git a/usr/share/swiftstory/lang/en/cards/white.json b/swiftstory/data/lang/en/cards/white.json index 9b5af22..9b5af22 100644 --- a/usr/share/swiftstory/lang/en/cards/white.json +++ b/swiftstory/data/lang/en/cards/white.json diff --git a/usr/share/swiftstory/lang/fr/cards/black.json b/swiftstory/data/lang/fr/cards/black.json index 0fb334c..0fb334c 100644 --- a/usr/share/swiftstory/lang/fr/cards/black.json +++ b/swiftstory/data/lang/fr/cards/black.json diff --git a/usr/share/swiftstory/lang/fr/cards/white.json b/swiftstory/data/lang/fr/cards/white.json index ff74cb4..ff74cb4 100644 --- a/usr/share/swiftstory/lang/fr/cards/white.json +++ b/swiftstory/data/lang/fr/cards/white.json diff --git a/swiftstory/game_manager.py b/swiftstory/game_manager.py index 6c2da1f..a69704c 100644 --- a/swiftstory/game_manager.py +++ b/swiftstory/game_manager.py @@ -3,6 +3,8 @@ import logging  import os  from typing import Dict, List +import pkg_resources +  from swiftstory.exception import UnsupportedLanguage  from swiftstory.game import Game  from swiftstory.cards import Cards @@ -39,7 +41,7 @@ class GameManager:      def list_languages(self) -> List[str]:          """ List available languages based on FS. """ -        return next(os.walk("usr/share/swiftstory/lang"))[1] +        return pkg_resources.resource_listdir(__name__, "data/lang")      def find_by_name(self, game_name: str, lang: str, create=True) -> Game:          """ Find and return the game that matches the name and language diff --git a/tests/test_cards.py b/tests/test_cards.py index 73f42a0..605984b 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -13,13 +13,13 @@ class TestCards(unittest.TestCase):          ])          black_cards = json.dumps(["Black Card 1", "Black Card 2"]) -        with patch("swiftstory.cards.open", mock_open(read_data=white_cards)): +        with patch("swiftstory.cards.pkg_resources.resource_stream", mock_open(read_data=white_cards)):              self.assertIn("White Card 2", Cards.get_white_cards("en")) -        with patch("swiftstory.cards.open", mock_open(read_data=black_cards)): +        with patch("swiftstory.cards.pkg_resources.resource_stream", mock_open(read_data=black_cards)):              self.assertIn("Black Card 1", Cards.get_black_cards("en"))      def test_get_cards_unknown_language(self): -        with patch("swiftstory.cards.open", side_effect=FileNotFoundError): +        with patch("swiftstory.cards.pkg_resources.resource_stream", side_effect=FileNotFoundError):              with self.assertRaises(FileNotFoundError):                  Cards.get_white_cards("zz")              with self.assertRaises(FileNotFoundError): | 
