Initial Commit
[Projects.git] / sinks / source / .svn / text-base / sink.py.svn-base
blob00d273f6db9ff328e99e45245dc5e61ad947dbcd
1 <<<<<<< .mine
2 def isdict(a):
3     try:
4         a['test']
5         return True
6     except(TypeError):
7         return False
8     except(KeyError):
9         return True
10 def equals(a,b):
11     equal=False
12     different=False
13     difference=[]
14     for ka,va in a.iteritems():
15         for kb,vb in b.iteritems():
16             if ka==kb and vb!=va and vb!='' and va!='':
17                 different=True
18             elif ka==kb and vb==va and vb!='':
19                 equal=True
20             elif ka==kb and isdict(vb) and isdict(va):
21                 if equals(vb,va):
22                     equal=True
23                 else:
24                     different=True
25     if equal and not different: return True #they are exactly the same
26     else: return False #they have no relation at all
27 def init(files):
28     for sinks in files:
29         for sink in sinks: sink.files=sink.get_files()
30         for sink in sinks:
31             for file in sink.files:
32                 for s in sinks:
33                     if s.name!=sink.name:
34                         found=False
35                         for f in s.files:
36                             if equals(f,file):
37                                 found=True
38                         if not found:
39                             print 'creating %s to %s' % (file['title'],s.name)
40                             file.update({'content':sink.getContent(file)})
41                             s.create(file)
42 def loop(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
49                 
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)=======
80 def isdict(a):
81     try:
82         a['test']
83         return True
84     except(TypeError):
85         return False
86     except(KeyError):
87         return True
88 def equals(a,b):
89     equal=False
90     different=False
91     difference=[]
92     for ka,va in a.iteritems():
93         for kb,vb in b.iteritems():
94             if ka==kb and vb!=va and vb!='' and va!='':
95                 different=True
96             elif ka==kb and vb==va and vb!='':
97                 equal=True
98             elif ka==kb and isdict(vb) and isdict(va):
99                 if equals(vb,va):
100                     equal=True
101                 else:
102                     different=True
103     if equal and not different: return True #they are exactly the same
104     else: return False #they have no relation at all
105 def init(files):
106     for sinks in files:
107         for sink in sinks: sink.files=sink.get_files()
108         for sink in sinks:
109             for file in sink.files:
110                 for s in sinks:
111                     if s.name!=sink.name:
112                         found=False
113                         for f in s.files:
114                             if equals(f,file):
115                                 found=True
116                             elif f['title']==file['title'] and f['title']!='':
117                                 found=True
118                                 print '%s must need updating...' % f['title']
119                         if not found:
120                             print 'creating %s to %s' % (file['title'],s.name)
121                             file.update({'content':sink.getContent(file)})
122                             s.create(file)
123 def loop(files):
124     while 1:
125         for sinks in files:
126             for sink in sinks:
127                 get_files=sink.get_files()
128                 
129                 #check if file deleted or edited
130                 for file in sink.files:
131                     found=False
132                     for f in get_files:
133                         if equals(file,f):
134                             found=True
135                     if not found:
136                         sink.files.remove(file)
137                         for s in sinks:
138                             if s.name!=sink.name:
139                                 print "removing %s from %s" % (file['title'],s.name)
140                                 for fi in s.files:
141                                     if equals(fi,file):
142                                         s.remove(fi)
143                                         s.files.remove(fi)
145                 #check if files added
146                 for file in get_files:
147                     found=False
148                     for f in sink.files:
149                         if equals(file,f):
150                             found=True
151                     if not found: # file has been added
152                         file.update({'content':sink.getContent(file)})
153                         sink.files.append(file)
154                         for s in sinks:
155                             if s.name!=sink.name:
156                                 print "creating %s to %s" % (file['title'],s.name)
157                                 s.create(file)
158                                 s.files.append(file)>>>>>>> .r14