Add Path.assert_disk_space.

This commit is contained in:
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

View file

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