From 501f8284d3dfacc6e51704c391a59cbbcc554a30 Mon Sep 17 00:00:00 2001 From: Nicholas FitzRoy-Dale Date: Wed, 18 Oct 2023 12:16:44 +0100 Subject: [PATCH] Move filesystem data classes to separate module --- rime/filesystem/base.py | 37 +++++++++++++++++++++++++++++ rime/filesystem/devicefilesystem.py | 28 ++++------------------ 2 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 rime/filesystem/base.py diff --git a/rime/filesystem/base.py b/rime/filesystem/base.py new file mode 100644 index 0000000..d70e95d --- /dev/null +++ b/rime/filesystem/base.py @@ -0,0 +1,37 @@ +# This software is released under the terms of the GNU GENERAL PUBLIC LICENSE. +# See LICENSE.txt for full details. +# Copyright 2023 Telemarq Ltd + +from dataclasses import dataclass +import os +import stat +from typing import Optional + + +@dataclass(frozen=True, unsafe_hash=True) +class File: + """ + """ + pathname: str + mime_type: Optional[str] = None + + +@dataclass(frozen=True, unsafe_hash=True) +class DirEntry: + """ + Mimic os.DirEntry for the scandir() method. Stores metadata at time of instantiation rather than + querying the filesystem the first time (unlike os.DirEntry). + """ + # Ideally we'd use os.DirEntry, but these can't be instantiated. + name: str + path: str + stat_val: os.stat_result + + def is_dir(self): + return stat.S_ISDIR(self.stat_val.st_mode) + + def is_file(self): + return stat.S_ISREG(self.stat_val.st_mode) + + def stat(self): + return self.stat_val diff --git a/rime/filesystem/devicefilesystem.py b/rime/filesystem/devicefilesystem.py index fd06edb..d514c58 100644 --- a/rime/filesystem/devicefilesystem.py +++ b/rime/filesystem/devicefilesystem.py @@ -1,29 +1,11 @@ +# This software is released under the terms of the GNU GENERAL PUBLIC LICENSE. +# See LICENSE.txt for full details. +# Copyright 2023 Telemarq Ltd + from abc import ABC, abstractmethod -import os -import stat from typing import Optional -from dataclasses import dataclass - - -@dataclass(frozen=True, unsafe_hash=True) -class DirEntry: - """ - Mimic os.DirEntry for the scandir() method. Stores metadata at time of instantiation rather than - querying the filesystem the first time (unlike os.DirEntry). - """ - # Ideally we'd use os.DirEntry, but these can't be instantiated. - name: str - path: str - stat_val: os.stat_result - - def is_dir(self): - return stat.S_ISDIR(self.stat_val.st_mode) - - def is_file(self): - return stat.S_ISREG(self.stat_val.st_mode) - def stat(self): - return self.stat_val +from .base import DirEntry class DeviceFilesystem(ABC):