emmental: Add support for an application-specific temporary directory
And include a function to help extract embedded cover.jpg files. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
000dbd7018
commit
7ff1a3d60c
|
@ -0,0 +1,21 @@
|
|||
# Copyright 2022 (c) Anna Schumaker.
|
||||
"""Special handling for temporary directories."""
|
||||
import tempfile
|
||||
import pathlib
|
||||
from . import gsetup
|
||||
|
||||
TMP_DIR = pathlib.Path(tempfile.gettempdir()) / gsetup.APPLICATION_ID
|
||||
TMP_DIR.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
def cover_jpg(data: bytes | None = None) -> pathlib.Path:
|
||||
"""Get the path to the temporary cover.jpg file.
|
||||
|
||||
If 'data' is provided, then the file will be created with the
|
||||
contents initialized to 'data'.
|
||||
"""
|
||||
cover = TMP_DIR / "cover.jpg"
|
||||
if data is not None:
|
||||
with cover.open("wb") as f:
|
||||
f.write(data)
|
||||
return cover
|
|
@ -0,0 +1,33 @@
|
|||
# Copyright 2022 (c) Anna Schumaker.
|
||||
"""Test our temporary directory handling code."""
|
||||
import tempfile
|
||||
import pathlib
|
||||
import unittest
|
||||
import emmental.tmpdir
|
||||
|
||||
TMP_DIR = pathlib.Path(tempfile.gettempdir())
|
||||
|
||||
|
||||
class TestTmpDir(unittest.TestCase):
|
||||
"""Our teporary directory test case."""
|
||||
|
||||
def setUp(self):
|
||||
"""Prepare for testing."""
|
||||
emmental.tmpdir.cover_jpg().unlink(missing_ok=True)
|
||||
|
||||
def test_tmpdir(self):
|
||||
"""Test TMP_DIR location and existence."""
|
||||
self.assertEqual(emmental.tmpdir.TMP_DIR,
|
||||
TMP_DIR / emmental.gsetup.APPLICATION_ID)
|
||||
self.assertTrue(emmental.tmpdir.TMP_DIR.is_dir())
|
||||
|
||||
def test_cover_jpg(self):
|
||||
"""Test temporary cover.jpg handling."""
|
||||
cover = emmental.tmpdir.cover_jpg()
|
||||
self.assertEqual(cover, emmental.tmpdir.TMP_DIR / "cover.jpg")
|
||||
self.assertFalse(cover.is_file())
|
||||
|
||||
cover = emmental.tmpdir.cover_jpg("Test Text".encode("utf-8"))
|
||||
self.assertTrue(cover.is_file())
|
||||
with cover.open("rb") as f:
|
||||
self.assertEqual(f.read(), "Test Text".encode("utf-8"))
|
Loading…
Reference in New Issue