forked from cottyard/Yeste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
134 lines (104 loc) · 3.55 KB
/
data.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
import pickle
import base64
import os
def printTree(node):
'''function for testing'''
print node.name
if node.type == Entry.TYPE_DIR:
for x in node.children.values():
printTree(x)
class NoteManager:
def __init__(self):
self.rootDir = Dir('root')
self.load()
self.path = [self.rootDir]
self.pwd = self.rootDir
# Query Methods
def getPath(self):
return map(lambda x: x.name, self.path)
def getNoteIter(self):
return self.pwd.childrenIter(\
lambda x: self.pwd.getChild(x).type == Entry.TYPE_NOTE)
def getDirIter(self):
return self.pwd.childrenIter(\
lambda x: self.pwd.getChild(x).type == Entry.TYPE_DIR)
def getNoteContent(self, name):
return self.pwd.getChild(name).getContent()
def getDirEntries(self, name):
return self.pwd.getChild(name).childrenIter()
def isNote(self, name):
return self.pwd.getChild(name).type == Entry.TYPE_NOTE
def isEncrypted(self, name):
if self.pwd.getChild(name).type == Entry.TYPE_NOTE:
return False
return self.pwd.getChild(name).getPassword() != ''
def matchPassword(self, name, password):
return self.pwd.getChild(name).getPassword() == password
def retrieveEntry(self, name):
return self.pwd.getChild(name)
# Setting Methods
def enterDir(self, name):
dir = self.pwd.getChild(name)
if dir.type == Entry.TYPE_DIR:
self.path.append(dir)
self.pwd = dir
def exitDir(self):
if len(self.path) > 1:
self.path.pop()
self.pwd = self.path[-1]
def delEntry(self, name):
self.pwd.delChild(name)
def newEntry(self, entry):
self.pwd.addChild(entry.name, entry)
def newNote(self, name, content):
self.pwd.addChild(name, Note(name, content))
def newDir(self, name, password = ''):
self.pwd.addChild(name, Dir(name, password))
# File Methods
def load(self):
try:
p = os.getcwd() + "\\note.dat"
print 'working path: ' + p
length = os.path.getsize(p)
except WindowsError:
f = open("note.dat",'wb')
f.close()
length = 0
if length != 0:
with open("note.dat",'rb') as f:
s = f.read()
s = base64.b64decode(s)
self.rootDir = pickle.loads(s)
def save(self):
with open("note.dat",'wb') as f:
s = pickle.dumps(self.rootDir)
s = base64.b64encode(s)
f.write(s)
# Data Classes
class Entry(object):
TYPE_NOTE = 0
TYPE_DIR = 1
def __init__(self, type, name):
self.type = type
self.name = name
class Note(Entry):
def __init__(self, name, content = ''):
super(Note, self).__init__(Entry.TYPE_NOTE, name)
self.content = content
def getContent(self):
return self.content
class Dir(Entry):
def __init__(self, name, password = ''):
super(Dir, self).__init__(Entry.TYPE_DIR, name)
self.children = dict()
self.password = password
def addChild(self, name, child):
self.children[name] = child
def delChild(self, name):
del self.children[name]
def getChild(self, name):
return self.children[name]
def getPassword(self):
return self.password
def childrenIter(self, filt = None):
return filter(filt, self.children)