forked from guilherme1guy/rasa-model-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.py
64 lines (42 loc) · 1.84 KB
/
filesystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from datetime import datetime
from os import scandir, listdir, DirEntry
from os.path import isfile, isdir, relpath, join, getsize
from config import download_folder
class Scaner:
def __init__(self, path):
self.entries = [Entry(entry) for entry in scandir(path.encode())]
self.entries.sort(
key=lambda e: e.modified_time.timestamp(),
reverse=True
)
self.latest_entry = max(self.entries, key=lambda e: e.modified_time.timestamp())
class Entry:
def __init__(self, entry: DirEntry):
self.name = entry.name.decode()
self.path = entry.path.decode()
self.rel_path = relpath(self.path, download_folder)
self.is_dir = entry.is_dir()
# save created_time and modified_time as date objects to provide
# better date comparison
self.created_time = datetime.fromtimestamp(entry.stat().st_ctime)
self.modified_time = datetime.fromtimestamp(entry.stat().st_mtime)
self.size = self._human_readable_size(self._get_size(entry.path))
def _get_size(self, path):
total_size = getsize(path)
if isdir(path):
for item in listdir(path):
item_path = join(path, item)
if isfile(item_path):
total_size += getsize(item_path)
elif isdir(item_path):
total_size += self._get_size(item_path)
return total_size
def _human_readable_size(self, size):
units = ['B', 'KB', 'MB', 'GB', 'TB']
human_fmt = '{0:.2f} {1}'
human_radix = 1024.
for unit in units[:-1]:
if size < human_radix:
return human_fmt.format(size, unit)
size /= human_radix
return human_fmt.format(size, units[-1])