- peer_id = self['peer_id']
-
- # we get the whole table just in case
- # a host would have switched from one plc to the other
- local_foreign_slices = ForeignSlices (self.api)
- # index it by name for searching later
- local_foreign_slices_index = local_foreign_slices.dict('name')
-
- ### mark entries for this peer outofdate
- old_count=0;
- for foreign_slice in local_foreign_slices:
- if foreign_slice['peer_id'] == peer_id:
- foreign_slice.uptodate=False
- old_count += 1
-
- ### these fields get copied through
- remote_fields = ['instantiation', 'url', 'description',
- 'max_nodes', 'created', 'expires']
-
- ### scan the new entries, and mark them uptodate
- new_count=0
- for slice in peer_get_slices:
- ### ignore system-wide slices
- if slice['creator_person_id'] == 1:
- continue
-
- name = slice['name']
-
- # create or update
- try:
- foreign_slice = local_foreign_slices_index[name]
- if foreign_slice['peer_id'] != peer_id:
- ### the slice has changed its plc, needs to update peer_slice
- old_peer_id = foreign_slice['peer_id']
- old_peers=Peers(self.api,[peer_id])
- assert old_peer[0]
- # remove from previous peer
- old_peers[0].manage_slice(foreign_slice,False,False)
- # add to new peer
- self.manage_slice(foreign_slice,True,True)
- foreign_slice['peer_id'] = peer_id
- except:
- foreign_slice = ForeignSlice(self.api, {'name':name})
-# ### xxx temporary
-# foreign_slice['site_id']=1
- ### need to sync so we get a slice_id
- foreign_slice.sync()
- self.manage_slice(foreign_slice,True,True)
- # insert in index
- local_foreign_slices_index[name]=foreign_slice
-
- # go on with update
- for field in remote_fields:
- foreign_slice[field]=slice[field]
- # this row is now valid
- foreign_slice.uptodate=True
- new_count += 1
- foreign_slice.sync()
-
- ### handle node_ids
- # in slice we get a set of node_ids
- # but these ids are RELATIVE TO THE PEER
- # so we need to figure the local node_id for these nodes
- # we do this through peer_foreign_nodes
- # dictify once
- peer_foreign_nodes_dict = {}
- for foreign_node in peer_foreign_nodes:
- peer_foreign_nodes_dict[foreign_node['node_id']]=foreign_node
- updated_node_ids = []
- for alien_node_id in slice['node_ids']:
- try:
- local_node_id=self.locate_alien_node_id_in_foreign_nodes(peer_foreign_nodes_dict,alien_node_id)
- updated_node_ids.append(local_node_id)
- except:
- # this node_id is not in our scope
- pass
- foreign_slice.update_slice_nodes (updated_node_ids)
-
- ### delete entries that are not uptodate
- for foreign_slice in local_foreign_slices:
- if not foreign_slice.uptodate:
- foreign_slice.delete()
-
- return new_count-old_count
+ try:
+ # Figure out if the specified attribute is the name of a
+ # PLCAPI function. If so and the function requires an
+ # authentication structure as its first argument, return a
+ # callable that automagically adds an auth struct to the
+ # call.
+ methodname = attr
+ api_function = self.api.callable(methodname)
+ if api_function.accepts and \
+ (isinstance(api_function.accepts[0], PLC.Auth.Auth) or \
+ (isinstance(api_function.accepts[0], Mixed) and \
+ filter(lambda param: isinstance(param, Auth), api_function.accepts[0]))):
+ function = getattr(self.server, methodname)
+ return self.add_auth(function, methodname)
+ except Exception, err:
+ pass
+
+ if hasattr(self, attr):
+ return getattr(self, attr)
+ else:
+ raise AttributeError, "type object 'Peer' has no attribute '%s'" % attr