def __len__(self): if self.isCollection: return super(NavigationCollection, self).__len__() else: # This is clumsy as we grab the entity itself entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = unicode( self.filter) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: # if we got a 404 from the underlying system we're done return 0 elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) return 1 else: raise core.InvalidEntryDocument(str(entityURL))
def __getitem__(self, key): sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = "%s and %s" % ( core.ODataURI.key_dict_to_query( self.entity_set.key_dict(key)), unicode(self.filter)) entityURL = str(self.baseURI) else: entityURL = ( str(self.baseURI) + core.ODataURI.FormatKeyDict(self.entity_set.GetKeyDict(key))) if self.expand is not None: sysQueryOptions[core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) if self.filter: request.set_header('Accept', 'application/atom+xml') else: request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: raise KeyError(key) elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) return entity elif isinstance(doc.root, atom.Feed): nresults = len(doc.root.Entry) if nresults == 0: raise KeyError(key) elif nresults == 1: e = doc.root.Entry[0] entity = core.Entity(self.entity_set) entity.exists = True e.GetValue(entity) return entity else: raise UnexpectedHTTPResponse( "%i entities returned from %s" % nresults, entityURL) elif isinstance(doc.root, core.Error): raise KeyError(key) else: raise core.InvalidEntryDocument(str(entityURL))
def __getitem__(self, key): if self.isCollection: return super(NavigationCollection, self).__getitem__(key) else: # The baseURI points to a single entity already, we must not add # the key entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[ core.SystemQueryOption.filter] = unicode(self.filter) if self.expand is not None: sysQueryOptions[ core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[ core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URIFactory.URI( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.HTTPRequest(str(entityURL)) request.SetHeader('Accept', 'application/atom+xml;type=entry') self.client.ProcessRequest(request) if request.status == 404: raise KeyError(key) elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.resBody) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) if entity.Key() == key: return entity else: raise KeyError(key) elif isinstance(doc.root, core.Error): raise KeyError(key) else: raise core.InvalidEntryDocument(str(entityURL))
def entity_generator(self): if self.isCollection: for entity in super(NavigationCollection, self).entity_generator(): yield entity else: # The baseURI points to a single entity already, we must not add # the key entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = unicode( self.filter) if self.expand is not None: sysQueryOptions[ core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[ core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: return elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) yield entity else: raise core.InvalidEntryDocument(str(entityURL))