Skip to content

Commit

Permalink
Move filesystem data classes to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas FitzRoy-Dale committed Oct 18, 2023
1 parent 0cce9df commit 501f828
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
37 changes: 37 additions & 0 deletions rime/filesystem/base.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 5 additions & 23 deletions rime/filesystem/devicefilesystem.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 501f828

Please sign in to comment.