]>
Commit | Line | Data |
---|---|---|
1 | import os | |
2 | from pathlib import Path | |
3 | from typing import Union | |
4 | ||
5 | ||
6 | class FileIO: | |
7 | def __init__(self, path: Union[os.PathLike, str]): | |
8 | if isinstance(path, str): | |
9 | path = Path(path) | |
10 | self.path = path | |
11 | ||
12 | def read(self): # Returns bytes/str | |
13 | # We would like to read unicode here, but we cannot, because we are not | |
14 | # sure if it is a valid unicode file. Therefore just read whatever is | |
15 | # here. | |
16 | with open(self.path, 'rb') as f: | |
17 | return f.read() | |
18 | ||
19 | def get_last_modified(self): | |
20 | """ | |
21 | Returns float - timestamp or None, if path doesn't exist. | |
22 | """ | |
23 | try: | |
24 | return os.path.getmtime(self.path) | |
25 | except FileNotFoundError: | |
26 | return None | |
27 | ||
28 | def __repr__(self): | |
29 | return '%s(%s)' % (self.__class__.__name__, self.path) | |
30 | ||
31 | ||
32 | class KnownContentFileIO(FileIO): | |
33 | def __init__(self, path, content): | |
34 | super().__init__(path) | |
35 | self._content = content | |
36 | ||
37 | def read(self): | |
38 | return self._content |