-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.py
More file actions
205 lines (162 loc) · 4.31 KB
/
sync.py
File metadata and controls
205 lines (162 loc) · 4.31 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import database
from datetime import *
import error
from bson.objectid import ObjectId
db = database.db
def flatten ( l ):
out = []
if isinstance(l, (list, tuple)):
for item in l:
out.extend(flatten(item))
elif isinstance(l, (dict)):
for dictkey in l.keys():
out.extend(flatten(l[dictkey]))
elif isinstance(l, (str, int, unicode)):
if isinstance(l, int):
l = str(l)
out.append(l)
elif isinstance(l, datetime):
out.append(l.isoformat(' '))
elif isinstance(l, ObjectId):
out.append(str(l))
elif l is None:
out.append("")
else:
out.append(l)
return out
# Inserts the data if it doesn't exits, skips if a match exists and updates if it exists but in an older version
def sync ( table, query, document, unset=["_updated", "_id", "_created"] ):
existsing = table.find(query).limit(1)
if existsing.count() is 0:
document["_created"] = datetime.now()
document["_updated"] = datetime.now()
try:
_id = table.insert(document, manipulate=True)
except Exception, e:
error.log(__file__, False, str(e))
return {
"status" : True,
"action" : "created",
"_id" : _id
}
else:
existsing = existsing[0]
difference = None
unsettedRows = {}
_id = None
try:
for item in unset:
if item in existsing:
unsettedRows[item] = existsing[item]
existsing.pop(item, None)
if item in document:
document.pop(item, None)
existingRows = []
for row in document:
if row in existsing:
existingRows.append(existsing[row])
existingItems = []
documentItems = []
for row in document:
row = ""
try:
row = " ".join(flatten(document[row]))
except Exception, e:
error.log(__file__, False, str(e))
documentItems.append(row)
for row in existingRows:
row = " ".join(flatten(row))
existingItems.append(row)
difference = set(documentItems)-set(existingItems)
for row in existsing:
if not row in document:
document[row] = existsing[row]
if len(difference) == 0 or difference == None:
return {
"status" : True,
"action" : "existsing",
"_id" : unsettedRows["_id"]
}
except Exception, e:
error.log(__file__, False, str(e))
for item in unsettedRows:
if item in unsettedRows and not unsettedRows[item] == None:
document[item] = unsettedRows[item]
# Assign updated Time
document["_updated"] = datetime.now()
# If no created field, create it
if not "_created" in document:
document["_created"] = datetime.now()
# Update Table
try:
table.update(query, document, upsert=True)
_id = unsettedRows["_id"]
except Exception, e:
error.log(__file__, False, str(e))
return {
"status" : True,
"action" : "updated",
"difference" : difference,
"_id" : _id
}
# Checks if to fire an event based on the status of
def check_action_event ( status ):
if not "status" in status or not "action" in status:
return False
if status["status"] == False or status["action"] == "existsing":
return False
return True
'''
Retrieves a list of event listeners for the specific object type,
and where the data matches the quer, a list of URLs is returned
'''
def find_listeners ( type, query ):
listeners = db.event_listeners.find({
"type" : type,
"query" : query
})
urls = []
for listeners in listeners:
urls = urls + listeners["urls"]
return urls
def find_general_listeners ( type ):
listeners = db.event_listeners.find({
"type" : type
})
urls = []
for listeners in listeners:
urls = urls + listeners["urls"]
return urls
def send_event ( url, event, data ):
pass
def find_deleted ( table, query, uniqueRows, current ):
deleted = []
existsing = table.find(query)
if existsing is None:
return deleted
for row in existsing:
found = False
for element in current:
if same(uniqueRows, element, row):
found = True
if not found:
table.remove({
"_id" : row["_id"]
})
deleted.append(row)
return deleted
def same ( uniqueRows, element1, element2 ):
same = True
for row in uniqueRows:
if not row in element1 and row in element2:
same = False
if not row in element2 and row in element1:
same = False
if row in element1 and row in element2:
if type(element2[row]) == type(element1[row]):
if not element2[row] == element1[row]:
same = False
else:
if not str(element2[row]) == str(element1[row]):
same = False
return same