-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
135 lines (93 loc) · 2.75 KB
/
files.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from random import randint
import mimetypes
import os
class File(object):
def __init__(self, name):
'''
File properties
'''
self.name = name
self.id = str(randint(0,10000000))
self.type = 'file'
self.data = ''
self.size = '0'
self.mimetype = mimetypes.guess_type(self.name)
def set_data(self,data):
'''
Adds data and corrects file size
#TODO: Add correct size calculation (data is just str, size now is just len(str) )
'''
self.data = str(data)
self.size = str(len(data))
def get_data(self):
return self.data
def list(self):
'''
Lists properties of file
'''
return {'link':self,
'name':self.name,
'id':self.id,
'data':self.data,
'size':self.size,
'type':self.type,
'mimetype':self.mimetype[0]
}
class Directory(object):
def __init__(self, name):
'''
Defining directory properties
'''
self.name = name
self.includes = []
self.type = 'directory'
def push(self,links_to_obj):
'''
Adds file to directory
'''
self.includes.extend(links_to_obj)
def pop(self,links_to_obj):
'''
Removes file from directory
'''
del self.includes[includes.index(links_to_obj)]
def list(self):
'''
Lists a directory. Returns a dictionary in format:
d = {
'id_of_file' = {
'prop_name':prop_value
}
}
'''
list_result = {}
list_result['link'] = self
for item in self.includes:
list_result[item.id] = item.list()
return list_result
def find(self, name):
'''
Finds entity by name in directory.
Returns output of its list method.
'''
for item in self.includes:
if item.name == str(name):
return item.list()
def make_files():
'''
Makes directory webdav and 3 files in it
'''
a = File('test1.png')
a.set_data("hello")
b = File('test2.txt')
b.set_data("sometext")
c = File('test3.jpg')
path = os.path.join(os.path.join(os.path.dirname(__file__),'static'),'test_image' )
with open(path, mode='r') as image_file:
c.set_data(image_file.read())
image_file.close()
d = Directory('webdav')
# In the form of ARRAY!
d.push([a, b, c])
#print("List" + str(d.list() ))
return d.list()