diff --git a/fimfarchive/fetchers.py b/fimfarchive/fetchers.py index e726b89..780e6b6 100644 --- a/fimfarchive/fetchers.py +++ b/fimfarchive/fetchers.py @@ -27,12 +27,13 @@ import gc import json from copy import deepcopy from io import BytesIO +from typing import FrozenSet from zipfile import ZipFile, BadZipFile import requests from fimfarchive.exceptions import InvalidStoryError, StorySourceError -from fimfarchive.flavors import StorySource, DataFormat, MetaPurity +from fimfarchive.flavors import Flavor, StorySource, DataFormat, MetaPurity from fimfarchive.stories import Story @@ -43,6 +44,7 @@ __all__ = ( ) +FlavorSet = FrozenSet[Flavor] StreamReader = codecs.getreader('utf-8') @@ -53,7 +55,7 @@ class Fetcher: prefetch_meta = False prefetch_data = False - flavors = frozenset() + flavors = frozenset() # type: FlavorSet def __enter__(self): """ diff --git a/fimfarchive/utils.py b/fimfarchive/utils.py index 648c2c0..77c0a6f 100644 --- a/fimfarchive/utils.py +++ b/fimfarchive/utils.py @@ -25,7 +25,7 @@ Various utilities. import json import os import shutil -from collections import UserDict +from typing import Dict, Any __all__ = ( @@ -52,7 +52,7 @@ class Empty(metaclass=EmptyMeta): return False -class PersistedDict(UserDict): +class PersistedDict(Dict[str, Any]): """ Dictionary for simple persistance. """ @@ -75,22 +75,19 @@ class PersistedDict(UserDict): """ Loads data from file as JSON. """ + self.clear() + self.update(self.default) + if os.path.exists(self.path): with open(self.path, 'rt') as fobj: - self.data = json.load(fobj) - else: - self.data = dict() - - for k, v in self.default.items(): - if k not in self.data: - self.data[k] = v + self.update(json.load(fobj)) def save(self): """ Saves data to file as JSON. """ content = json.dumps( - self.data, + self, indent=4, ensure_ascii=False, sort_keys=True, diff --git a/tox.ini b/tox.ini index a2680f1..e19cc5a 100644 --- a/tox.ini +++ b/tox.ini @@ -10,10 +10,12 @@ python = commands = py.test flake8 fimfarchive tests + mypy --ignore-missing-imports fimfarchive deps = -rrequirements.txt pytest flake8 + mypy [pytest] addopts =