Browse Source

permit shared index with subject ids

Bastien Sevajol 6 years ago
parent
commit
a9014cf1f8
2 changed files with 32 additions and 8 deletions
  1. 30 6
      synergine2/share.py
  2. 2 2
      tests/test_share.py

+ 30 - 6
synergine2/share.py View File

8
 from synergine2.exceptions import SynergineException
8
 from synergine2.exceptions import SynergineException
9
 from synergine2.exceptions import UnknownSharedData
9
 from synergine2.exceptions import UnknownSharedData
10
 
10
 
11
+if typing.TYPE_CHECKING:
12
+    from synergine2.simulation import Subject
13
+
11
 
14
 
12
 class NoSharedDataInstance(SynergineException):
15
 class NoSharedDataInstance(SynergineException):
13
     pass
16
     pass
22
         self.shared_data_manager = shared_data_manager
25
         self.shared_data_manager = shared_data_manager
23
         self.key = key
26
         self.key = key
24
 
27
 
25
-    def add(self, value: typing.Any) -> None:
28
+    def add(self, subject: 'Subject', value: typing.Any) -> None:
26
         raise NotImplementedError()
29
         raise NotImplementedError()
27
 
30
 
28
-    def remove(self, value: typing.Any) -> None:
31
+    def remove(self, subject: 'Subject', value: typing.Any) -> None:
29
         raise NotImplementedError()
32
         raise NotImplementedError()
30
 
33
 
34
+    def get_final_key(self, subject: 'Subject', value: typing.Any) -> str:
35
+        return self.key.format(shared_key=value, subject_id=subject.id)
36
+
31
 
37
 
32
 class SharedData(object):
38
 class SharedData(object):
33
     def __init__(
39
     def __init__(
226
             try:
232
             try:
227
                 previous_value = self.get(final_key)
233
                 previous_value = self.get(final_key)
228
                 for index in indexes:
234
                 for index in indexes:
229
-                    index.remove(previous_value)
235
+                    index.remove(instance, previous_value)
230
             except UnknownSharedData:
236
             except UnknownSharedData:
231
                 pass  # If no shared data, no previous value to remove
237
                 pass  # If no shared data, no previous value to remove
232
 
238
 
236
                 self.set(final_key, shared_data.type(value_))
242
                 self.set(final_key, shared_data.type(value_))
237
 
243
 
238
             for index in indexes:
244
             for index in indexes:
239
-                index.add(value_)
245
+                index.add(instance, value_)
240
 
246
 
241
         def fdel(self_):
247
         def fdel(self_):
242
             raise SynergineException('You cannot delete a shared data: not implemented yet')
248
             raise SynergineException('You cannot delete a shared data: not implemented yet')
258
 shared = SharedDataManager()
264
 shared = SharedDataManager()
259
 
265
 
260
 
266
 
267
+class SubjectListIndex(SharedDataIndex):
268
+    def add(self, subject: 'Subject', value):
269
+        final_key = self.get_final_key(subject, value)
270
+        try:
271
+            values = self.shared_data_manager.get(final_key)
272
+        except UnknownSharedData:
273
+            values = []
274
+
275
+        values.append(subject.id)
276
+        self.shared_data_manager.set(final_key, values)
277
+
278
+    def remove(self, subject: 'Subject', value):
279
+        final_key = self.get_final_key(subject, value)
280
+        values = self.shared_data_manager.get(final_key)
281
+        values.remove(subject.id)
282
+        self.shared_data_manager.set(final_key, values)
283
+
284
+
261
 class ListIndex(SharedDataIndex):
285
 class ListIndex(SharedDataIndex):
262
-    def add(self, value):
286
+    def add(self, subject: 'Subject', value):
263
         try:
287
         try:
264
             values = self.shared_data_manager.get(self.key)
288
             values = self.shared_data_manager.get(self.key)
265
         except UnknownSharedData:
289
         except UnknownSharedData:
268
         values.append(value)
292
         values.append(value)
269
         self.shared_data_manager.set(self.key, values)
293
         self.shared_data_manager.set(self.key, values)
270
 
294
 
271
-    def remove(self, value):
295
+    def remove(self, subject: 'Subject', value):
272
         values = self.shared_data_manager.get(self.key)
296
         values = self.shared_data_manager.get(self.key)
273
         values.remove(value)
297
         values.remove(value)
274
         self.shared_data_manager.set(self.key, values)
298
         self.shared_data_manager.set(self.key, values)

+ 2 - 2
tests/test_share.py View File

71
 
71
 
72
     def test_position_index(self):
72
     def test_position_index(self):
73
         class ListIndex(share.SharedDataIndex):
73
         class ListIndex(share.SharedDataIndex):
74
-            def add(self, value):
74
+            def add(self, subject, value):
75
                 try:
75
                 try:
76
                     values = self.shared_data_manager.get(self.key)
76
                     values = self.shared_data_manager.get(self.key)
77
                 except UnknownSharedData:
77
                 except UnknownSharedData:
80
                 values.append(value)
80
                 values.append(value)
81
                 self.shared_data_manager.set(self.key, values)
81
                 self.shared_data_manager.set(self.key, values)
82
 
82
 
83
-            def remove(self, value):
83
+            def remove(self, subject, value):
84
                 values = self.shared_data_manager.get(self.key)
84
                 values = self.shared_data_manager.get(self.key)
85
                 values.remove(value)
85
                 values.remove(value)
86
                 self.shared_data_manager.set(self.key, values)
86
                 self.shared_data_manager.set(self.key, values)