forked from monofox/SugarSync-Python-Client
/
SugarSyncDirectory.py
161 lines (127 loc) · 4.49 KB
/
SugarSyncDirectory.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# author: Lukas Schreiner
#
# For editors/contributors: please add an author tag above -
# for a full list ;-)
#
# For debugging: please use the pudb from
# https://github.com/orsenthil/pudb.git
# because the original does not support python3!
from XMLElement import XMLElement
from SugarSyncFile import SugarSyncFile
from Printer import Printer
from SugarSync import SugarSync
from SugarSyncInstance import SugarSyncInstance
class SugarSyncDirectory:
# this is mainly for directories. But collections are nearly identicaly.
# so we will handle it normal and will create subdirs
def __init__(self, link):
# link can be a link or an dsid
# we have to find it out. I think the best is via collection=True or False
if link is not None:
self.setLink(link)
else:
self.link = link
# it could be interesting on 'cd ..' (going level up)
self.parent = None
# when was the folder created?
self.ctime = None
# we need the name.
self.name = ''
# nearly i've forgotten the children :D
self.children = {} # or dict ( with id : object ) ?
if not self.retrieveInfo():
print('Error retrieving information for \'%s\'' % link)
# adding is simple. But removing? xD
def addChild(self, child):
self.children[child.getName()] = child
def getSortList(self):
return self.sortList
def setLink(self, link):
if isinstance(link, XMLElement):
link = link.getValue()
# we have to remove the api url
links = link.split('/')
link = links[len(links)-1]
self.link = str(link)
def getLink(self):
return self.link
# easy too ;-)
def removeChild(self, child):
try:
del self.children.remove[child]
except:
print("Child doesn't exist!")
def getChildren(self):
return self.children
def setTime(self, time):
dt = SugarSync.parseDate(time)
if dt is not None:
self.ctime = dt
else:
print('Error on setting creation time.')
def getTime(self):
return self.ctime
def getParent(self):
return self.parent
def retrieveInfo(self):
# first folder informations
informations = SugarSync.getFolderInfo(self.link)
if informations is not None:
self.setName(informations.displayName)
self.setTime(informations.timeCreated.getValue())
self.retrieveChildren()
return True
else:
return False
def retrieveChildren(self):
data = SugarSync.getFolderContents(self.link)
# we have to split all elements and check whether its a folder or a file
childs = data.getChildren()
if childs is not None:
for c in childs:
if c.getName() == 'collection' and c.getAttribute('type') == 'folder':
elm = SugarSyncDirectory(c.ref)
else:
elm = SugarSyncFile(c.ref)
elm.setName(str(c.displayName))
elm.setParent(self)
self.addChild(elm)
def searchChild(self, filename):
i = 0
found = False
while i < len(self.children) and not found:
if isinstance(self.children[i], SugarSyncFile) \
and self.children[i].getName() == filename:
found = True
i += 1
return self.children[i-1]
def refresh(self):
self.children = {}
self.retrieveChildren()
def getName(self):
return self.name
def setName(self, name):
if isinstance(name, XMLElement):
self.name = name.getValue()
else:
self.name = name
def setParent(self, parent):
self.parent = parent
def printInfo(self):
create = self.ctime.strftime('%d/%m/%Y %H:%M')
output = '%s: directory, created on %s' % (self.name, create)
print(output)
def notifyCreate(self, evt):
# if its a directory, we will set a new notify item ;-)
# (after creating the dirctory)
pass
def notifyDelete(self, evt):
# if its a directory, the directory will remove the watcher - if there exist any.
pass
def notifyModify(self, evt):
# we have to upload it / update it.
child = self.searchChild(evt.name)
child.update(evt.pathname)