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
136
137
138
139
|
# -*- coding: utf-8 -*-
import StringIO
import hashlib
import os
import os.path
import zlib
import datetime
import fileinput
import permission
class Object :
def _getPath( self ) :
return self.name[:2] + '/' + self.name[2:]
def _createDir( self ):
if ( not os.path.exists( self.name[:2] ) ) :
os.mkdir( self.name[:2] )
if ( not os.path.exists( self.path ) ) :
os.mkdir( self.path )
##
# Save the object on the disk.
def save( self ) :
self._createDir()
header = "author " + self.data['author'] + "\n"
header += "parent " + self.data['parent'] + "\n"
header += "created " + self.data['created'] + "\n"
header += "#\n" #end of header
store = header + self.data['content']
file = open( self.path + "/object", 'w' )
file.write( zlib.compress( store ) )
file.close()
os.mknod(self.path + "/childs")
os.mknod(self.path + "/permissions")
self.saved = True
##
# Class constructor.
def __init__( self, init ) :
if isinstance( init, dict ) :
if 'author' not in init :
init['author'] = 'None'
if 'parent' not in init :
init['parent'] = 'None'
if 'content' not in init :
init['content'] = 'None'
self.data = init
self.data['created'] = str( datetime.datetime.now() )
self.name = hashlib.sha1( str( self.data ) ).hexdigest()
self.path = self._getPath()
self.saved = False
elif isinstance( init, str ) :
self.name = init
self.path = self._getPath()
self.saved = True
if ( os.path.exists( self.path ) ) :
file = open( self.path + '/object','r')
contentStream = StringIO.StringIO( zlib.decompress( file.read() ) )
data = {}
for line in contentStream :
if ( line == "#\n") :
data['content'] = contentStream.read()
break
else :
key, sep, value = line.rstrip('\n').partition(' ')
data[key] = value
self.data = data
file.close()
def appendTo(self, father) :
father.appendChild(self)
def appendChild(self, child) :
if not self.saved :
self.save()
file = open( self.path + "/childs", 'a' )
file.write( child.name + "\n")
file.close()
def getPermission( self, user ) :
file = open( self.path + '/permissions', 'r' )
for line in file :
name, sep, perm = line.rstrip('\n').partition(' ')
if name == user :
return perm
else :
return 0
def setPermission(self, user, perm ) :
if not self.saved :
self.save()
sentinel = False
for line in fileinput.input( self.path + "/permissions", inplace=1 ) :
name, sep, perm = line.rstrip('\n').partition(' ')
if name == user :
print name + sep + perm
sentinel = True
else :
print line
if not sentinel :
file = open( self.path + '/permissions', 'a' )
file.write( user + ' ' + str( perm ) )
file.close()
if __name__== '__main__' :
x = Object({'parent' : 'toto', 'author' : 'Zaran'})
x.setPermission("test", permission.READ)
child = Object({'parent' : x.name, 'author' : 'Zaran'})
child.save()
x.appendChild(child)
y = Object(x.name)
print y.data['author']
print y.data['parent']
print y.data['content']
print y.data['created']
|