Initial Commit
[Projects.git] / sinks / source / sink.py
blob3b84a0c7ce1db26641c29fe192acededa0706de3
1 def isdict(a):
2 try:
3 a['test']
4 return True
5 except(TypeError):
6 return False
7 except(KeyError):
8 return True
9 def equals(a,b):
10 equal=False
11 different=False
12 difference = []
13 for ka,va in a.iteritems():
14 for kb,vb in b.iteritems():
15 if ka==kb and vb!=va and vb!='' and va!='' and vb!=[] and va!=[]:
16 different=True
17 difference.append(ka)
18 elif ka==kb and vb==va and vb!='' and vb!=[] and va!=[]: equal=True
19 elif ka==kb and isdict(vb) and isdict(va):
20 if equals(vb,va): equal=True
21 else: different=True
22 if different and 'title' not in difference: print difference
23 if equal and not different: return True #they are exactly the same
24 else: return False #they have no relation at all
25 def init(files):
26 for sinks in files:
27 for sink in sinks: sink.files=sink.get_files()
28 for sink in sinks:
29 for file in sink.files:
30 for s in sinks:
31 if s.name!=sink.name:
32 found=False
33 for f in s.files:
34 if equals(f,file):
35 found=True
36 if not found:
37 print 'Creating %s to %s' % (file['title'],s.name)
38 file.update({'content':sink.getContent(file)})
39 s.create(file)
40 def loop(files):
41 for sinks in files:
42 for sink in sinks: sink.file = sink.get_files()
43 while 1:
44 for sinks in files:
45 for sink in sinks:
46 get_files=sink.get_files()
48 if get_files==None: continue
50 #check if file deleted or edited
51 for file in sink.files:
52 found=False
53 for f in get_files:
54 if equals(file,f):
55 found=True
56 if not found:
57 sink.files.remove(file)
58 for s in sinks:
59 if s.name!=sink.name:
60 print "Removing %s from %s" % (file['title'],s.name)
61 for fi in s.files:
62 if equals(fi,file):
63 s.remove(fi)
64 s.files.remove(fi)
66 #check if files added
67 for file in get_files:
68 found=False
69 for f in sink.files:
70 if equals(file,f):
71 found=True
72 if not found: # file has been added
73 file.update({'content':sink.getContent(file)})
74 sink.files.append(file)
75 for s in sinks:
76 if s.name!=sink.name:
77 print "Creating %s to %s" % (file['title'],s.name)
78 s.create(file)
79 s.files.append(file)