Add Path.assert_disk_space.

master
voussoir 2022-01-19 19:23:11 -08:00
parent 77769aa2ef
commit bcab9b34d6
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 14 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import glob import glob
import os import os
import shutil
_glob = glob _glob = glob
@ -41,6 +42,13 @@ class NotDirectory(PathclassException):
class NotExists(PathclassException): class NotExists(PathclassException):
pass pass
class NotEnoughSpace(PathclassException):
def __init__(self, free, reserve, path):
self.free = free
self.reserve = reserve
self.path = path
class NotFile(PathclassException): class NotFile(PathclassException):
pass pass
@ -207,6 +215,12 @@ class Path:
self._absolute_path = absolute self._absolute_path = absolute
return self._absolute_path return self._absolute_path
def assert_disk_space(self, reserve):
free = shutil.disk_usage(self).free
if free < reserve:
raise NotEnoughSpace(path=self, reserve=reserve, free=free)
return free
def assert_exists(self): def assert_exists(self):
if not self.exists: if not self.exists:
raise NotExists(self) raise NotExists(self)