| Total Complexity | 4 |
| Total Lines | 24 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | """Mixin class for types that need a data directory""" |
||
| 2 | |||
| 3 | from __future__ import annotations |
||
| 4 | |||
| 5 | import os |
||
| 6 | import os.path |
||
| 7 | |||
| 8 | |||
| 9 | class DatadirMixin: |
||
| 10 | """Mixin class for types that need a data directory for storing files""" |
||
| 11 | |||
| 12 | def __init__(self, datadir: str, typename: str, identifier: str) -> None: |
||
| 13 | self._datadir_path = os.path.join(datadir, typename, identifier) |
||
| 14 | |||
| 15 | @property |
||
| 16 | def datadir(self) -> str: |
||
| 17 | if not os.path.exists(self._datadir_path): |
||
| 18 | try: |
||
| 19 | os.makedirs(self._datadir_path) |
||
| 20 | except FileExistsError: |
||
| 21 | # apparently the datadir was created by another thread! |
||
| 22 | pass |
||
| 23 | return self._datadir_path |
||
| 24 |