summary refs log tree commit diff stats
path: root/code/directory.py
blob: 2554ca7251c913a60a5d1d35f76e8f899d8d3397 (plain) (blame)
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
65
66
67
68
69
import fsobject
import file, debug

class Directory(fsobject.FSObject):
	def __init__(self, path):
		fsobject.FSObject.__init__(self, path)
		self.content_loaded = False
		self.scheduled = False
		self.enterable = False

		self.filenames = None
		self.files = None
		self.filter = None
		self.pointed_index = None
		self.pointed_file = None
	
	def load_content(self):
		self.stop_if_frozen()
		self.load_if_outdated()
		self.content_loaded = True
		import os
		if self.exists:
			basenames = os.listdir(self.path)
			mapped = map(lambda name: os.path.join(self.path, name), basenames)
			self.filenames = list(mapped)
			self.infostring = ' %d' % len(self.filenames) # update the infostring
			self.files = []
			for name in self.filenames:
				if os.path.isdir(name):
					f = Directory(name)
				else:
					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()
			return True
		return False

	def load_content_if_outdated(self):
		self.stop_if_frozen()
		if self.load_content_once(): return True

		import os
		real_mtime = os.stat(self.path).st_mtime
		cached_mtime = self.stat.st_mtime

		if real_mtime != cached_mtime:
			self.load_content()
			return True
		return False

	def __len__(self):
		if not self.accessible: raise fsobject.NotLoadedYet()
		return len(self.filenames)
	
	def __getitem__(self, key):
		if not self.accessible: raise fsobject.NotLoadedYet()
		return self.files[key]

if __name__ == '__main__':
	d = Directory('.')
	d.load_filenames()
	print(d.filenames)
	print(d[1])