-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfilesystem.py
66 lines (51 loc) · 1.74 KB
/
filesystem.py
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
# -*- coding: utf-8 -*-
from genericpath import isfile
import os
from os.path import join, basename, splitext, isdir, dirname
from action import View
import string
class Node(object):
def __init__(self, root, path):
splitetPath = string.split(path,"/")
self.path = os.path.sep.join(splitetPath)
self.root = root
self._basename = basename(self.path)
def __unicode__(self):
return self.name
def get_actions(self):
return self.avaliable_actions
def apply_action(self, action_class):
action = action_class(self)
return action.apply()
class File(Node):
avaliable_actions = [View, ]
def __unicode__(self):
return self.name
@property
def extension(self):
return splitext(self._basename)[1]
@property
def name(self):
return self._basename
def get_path(self):
return dirname(self.path)
class Folder(Node):
def __init__(self, root, path):
super(Folder, self).__init__(root, path)
self.files = []
self.folders = []
@property
def name(self):
return basename(self.path)
def chunks(self):
chunk_path = ''
for chunk in self.path.split(os.sep):
chunk_path = join(chunk_path, chunk)
yield {'chunk': chunk, 'path': chunk_path}
def read(self):
for node in os.listdir(join(self.root, self.path)):
full_path = join(self.path, node)
if isdir(join(self.root, full_path)):
self.folders.append(Folder(self.root, full_path))
if isfile(join(self.root, full_path)):
self.files.append(File(self.root, full_path))