Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathobject_registry.py
More file actions
Latest commit
79 lines (61 loc) · 2.41 KB
/
Copy pathobject_registry.py
File metadata and controls
79 lines (61 loc) · 2.41 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
importPythonBridge.bridge_globals
fromuuidimportuuid1
defensure_global_registry():
ifnothasattr(PythonBridge.bridge_globals, 'ObjectRegistry'):
PythonBridge.bridge_globals.ObjectRegistry=Registry()
defregistry():
returnPythonBridge.bridge_globals.ObjectRegistry
primitive= (int, str, bool)
defis_primitive(obj):
returnisinstance(obj, primitive)
classRegistry():
def__init__(self):
self.idToObjMap= {}
self.objToIdMap= {}
defhasId(self, anId):
returnanIdinself.idToObjMap
defcreateNewObjId(self):
returnuuid1().hex
defregister(self, obj):
ifobjisNoneoris_primitive(obj):
return0
ifid(obj) inself.objToIdMap:
returnself.objToIdMap[id(obj)]
else:
returnself._register(obj, self.createNewObjId())
defregister_with_id(self, obj, newObjId):
ifobjisNoneoris_primitive(obj):
returnRegisterForbiddenObject(obj)
ifid(obj) inself.objToIdMap:
objId=self.objToIdMap[id(obj)]
ifobjId==newObjId:
returnnewObjId
else:
raiseRegisterWithDifferentIdError(obj, newObjId)
else:
returnself._register(obj, newObjId)
defresolve(self, objId):
ifobjIdinself.idToObjMap:
returnself.idToObjMap[objId]
else:
raiseResolveUnknownObject(objId)
def_register(self, obj, newObjId):
self.idToObjMap[newObjId] =obj
self.objToIdMap[id(obj)] =newObjId
returnnewObjId
defclean(self, objId):
obj=self.idToObjMap[objId]
delself.idToObjMap[objId]
delself.objToIdMap[id(obj)]
classRegistryError(Exception):
pass
classRegisterWithDifferentIdError(RegistryError):
def__init__(self, obj, newId):
RegistryError.__init__(self,"Attempting to register object {0} with ID {1} with different ID {2}.".format(type(obj).__name__, registry().register(obj), newId))
classResolveUnknownObject(RegistryError):
def__init__(self, objId):
RegistryError.__init__(self,"Attempting to resolve unknown object with id {0}.".format(objId))
classRegisterForbiddenObject(RegistryError):
def__init__(self, obj):
RegistryError.__init__(self,"Attempting to register forbidden object of type {0}.".format(type(obj).__name__))
ensure_global_registry()