summary refs log tree commit diff stats
path: root/code/fsobject.py
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-11-24 00:41:16 +0100
committerhut <hut@lavabit.com>2009-11-24 00:41:16 +0100
commitfae694a03ebb52888a747010fcb92fa43b33b4a8 (patch)
treeccd0d4e8d6a759eb8086d6a7da80316787b868ea /code/fsobject.py
parent6ca37350f8e6bffe78520fddfc29ca53fa562e87 (diff)
downloadranger-fae694a03ebb52888a747010fcb92fa43b33b4a8.tar.gz
better implementation of directories/files
Diffstat (limited to 'code/fsobject.py')
-rw-r--r--code/fsobject.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/code/fsobject.py b/code/fsobject.py
new file mode 100644
index 00000000..d1cd1410
--- /dev/null
+++ b/code/fsobject.py
@@ -0,0 +1,65 @@
+class FrozenException(Exception): pass
+class NotLoadedYet(Exception): pass
+
+class FSObject(object):
+	def __init__(self, path):
+		self.path = path
+		self.exists = False
+		self.accessible = False
+		self.marked = False
+		self.tagged = False
+		self.frozen = False
+		self.loaded = False
+		self.stat = None
+
+	def clone(self):
+		clone = type(self)(self.path)
+		for key in iter(self.__dict__):
+			clone.__dict__[key] = self.__dict__[key]
+		return clone
+
+	def load(self):
+		self.stop_if_frozen()
+		self.loaded = True
+
+		import os
+		try:
+			self.stat = os.stat(self.path)
+			self.exists = True
+			self.accessible = True
+		except OSError:
+			self.exists = False
+			self.accessible = False
+
+	def load_once(self):
+		self.stop_if_frozen()
+		if not self.loaded: self.load()
+
+	def load_if_outdated(self):
+		self.stop_if_frozen()
+		import os
+
+		if not self.loaded:
+			self.load()
+			return True
+
+		real_mtime = os.stat(self.path).st_mtime
+		cached_mtime = self.stat.st_mtime
+
+		if real_mtime != cached_mtime:
+			self.load()
+			return True
+
+		return False
+
+	def frozen_clone(self):
+		self.stop_if_frozen()
+		return self.clone().freeze()
+
+	def stop_if_frozen(self):
+		if self.frozen: raise FrozenException()
+
+	def freeze(self):
+		self.frozen = True
+		return self
+