summary refs log tree commit diff stats
path: root/code/directory.py
blob: e28bf35c913c2cd7d76bf01b44a293041d563790 (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
class FrozenException(Exception): pass
class NotLoadedYet(Exception): pass

class Directory():
	def __init__(self, path):
		self.path = path
		self.accessible = False
		self.files_loaded = False
		self.scheduled = False
		self.files = None
		self.mtime = None
		self.exists = True

		self.frozen = False

	def load_files(self):
		import os
		if self.frozen: raise FrozenException()
		try:
			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

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

if __name__ == '__main__':
	d = Directory('.')
	d.load_files()
	print(d.files)
	print(d[1])