summary refs log tree commit diff stats
path: root/code
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
parent6ca37350f8e6bffe78520fddfc29ca53fa562e87 (diff)
downloadranger-fae694a03ebb52888a747010fcb92fa43b33b4a8.tar.gz
better implementation of directories/files
Diffstat (limited to 'code')
-rw-r--r--code/directory.py72
-rw-r--r--code/file.py6
-rw-r--r--code/fm.py2
-rw-r--r--code/fsobject.py65
4 files changed, 103 insertions, 42 deletions
diff --git a/code/directory.py b/code/directory.py
index d50432d2..10ad163c 100644
--- a/code/directory.py
+++ b/code/directory.py
@@ -1,55 +1,45 @@
-class FrozenException(Exception): pass
-class NotLoadedYet(Exception): pass
+import fsobject
+import file
 
-class Directory():
+class Directory(fsobject.FSObject):
 	def __init__(self, path):
-		self.path = path
-		self.accessible = False
-		self.files_loaded = False
+		fsobject.FSObject.__init__(self, path)
+		self.content_loaded = False
 		self.scheduled = False
-		self.files = None
-		self.mtime = None
-		self.exists = True
-
-		self.frozen = False
+		self.enterable = False
 
-	def load_files(self):
+		self.filenames = None
+		self.files = None
+		self.pointed_index = None
+	
+	def load_content(self):
+		self.stop_if_frozen()
+		self.load_if_outdated()
+		self.content_loaded = True
 		import os
-		if self.frozen: raise FrozenException()
-		try:
-			self.mtime = os.path.getmtime(self.path)
-			self.files = os.listdir(self.path)
-			self.exists = True
-			self.accessible = True
-		except OSError:
-			self.files = None
-			self.exists = False
-			self.accessible = False
-
-		self.files_loaded = True
-
-	def clone(self):
-		clone = Directory(self.path)
-		for key in iter(self.__dict__):
-			clone.__dict__[key] = self.__dict__[key]
-		return clone
-
-	def frozenClone(self):
-		clone = self.clone()
-		clone.frozen = True
-		return clone
+		if self.exists:
+			self.filenames = os.listdir(self.path)
+			self.files = []
+			for name in self.filenames:
+				f = file.File(name)
+				f.load()
+				self.files.append(f)
+	
+	def load_content_once(self):
+		self.stop_if_frozen()
+		if not self.content_loaded: self.load_content()
 
 	def __len__(self):
-		if not self.accessible: raise NotLoadedYet()
-		return len(self.files)
+		if not self.accessible: raise fsobject.NotLoadedYet()
+		return len(self.filenames)
 	
 	def __getitem__(self, key):
-		if not self.accessible: raise NotLoadedYet()
-		return self.files[key]
+		if not self.accessible: raise fsobject.NotLoadedYet()
+		return self.filenames[key]
 
 if __name__ == '__main__':
 	d = Directory('.')
-	d.load_files()
-	print(d.files)
+	d.load_filenames()
+	print(d.filenames)
 	print(d[1])
 
diff --git a/code/file.py b/code/file.py
new file mode 100644
index 00000000..291e53b2
--- /dev/null
+++ b/code/file.py
@@ -0,0 +1,6 @@
+import fsobject
+class File(fsobject.FSObject):
+	pass
+#	def __init__(self, path):
+#		fsobject.FSObject.__init__(self, path)
+		
diff --git a/code/fm.py b/code/fm.py
index 8029d915..c2fe30a4 100644
--- a/code/fm.py
+++ b/code/fm.py
@@ -18,7 +18,7 @@ class FM():
 			self.env.pwd = directory.Directory(path)
 			self.env.directories[path] = self.env.pwd
 
-		self.env.pwd.load_files()
+		self.env.pwd.load_content()
 		if len(self.env.pwd) > 0: self.env.cf = self.env.pwd[0]
 
 	def run(self):
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
+