aboutsummaryrefslogtreecommitdiffstats
path: root/object.py
blob: ad76c4120c5f9642fced4db67d91d94a4a9d638d (plain)
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
# -*- coding: utf-8 -*-
import StringIO
import hashlib
import os
import os.path
import zlib
import datetime

class Object :
    
    def __create_dir(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.__create_dir()
        
        header = "author " + self.author + "\n"
        header += "parent " + self.parent + "\n"
        header += "created " + self.created + "\n"
        header += "#\n" #end of header
        store = header + self.content

        file = open( self.path + "/object", 'w' )
        file.write( zlib.compress( store ) )
        file.close()
        self.saved = True
        return self.name
    
    ##
    # Class constructor.
    def __init__( self, author = None, parent = None, content = None ) :
        self.author = str(author)
        self.parent = str(parent)
        self.created = str( datetime.datetime.now() )
        self.content = str(content)
        self.name = hashlib.sha1( self.author + self.parent + self.created ).hexdigest()
        self.path = self.name[:2] + '/' + self.name[2:]
        self.saved = False
    
    ##
    # Create an object from a file object
    # @param name The name of the object to load
    @staticmethod  
    def load( name ) :
        result = Object()
        result.name = name
        result.path = name[:2] + '/' + name[2:]
        result.saved = True
            
        if ( os.path.exists( result.path ) ) :
            
            file = open( result.path + '/object','r')
            contentStream = StringIO.StringIO( zlib.decompress( file.read() ) )
            
            for line in contentStream :

                if ( line == "#\n") :
                    result.content = contentStream.read()
                    break

                key, sep, value = line.rstrip('\n').partition(' ')
                setattr(result, key, value)
                
            file.close()
                
        return result
                    
    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()
        
        
            
    
if __name__== '__main__' :
    x = Object("test parent", "Zaran", "father")
    name = x.save()
    
    child = Object(name,"Zaran", "child")
    child.save()
    
    x.appendChild(child)
    
    print name
    y = Object.load(name)
    print y.author
    print y.parent
    print y.content
    print y.created