@@ -834,6 +834,8 @@ class Collection(Endpoint):
834834 def __init__ (self , service , path , item = Entity ):
835835 Endpoint .__init__ (self , service , path )
836836 self .item = item # Item accessor
837+ self .null_count = - 1
838+
837839
838840 def __contains__ (self , name ):
839841 """Is there at least one entry called *name* in this collection?
@@ -927,7 +929,7 @@ def __iter__(self):
927929 for entity in saved_searches:
928930 print "Saved search named %s" % entity.name
929931 """
930- for item in self .list ():
932+ for item in self .iter ():
931933 yield item
932934
933935 def __len__ (self ):
@@ -1140,8 +1142,39 @@ def itemmeta(self):
11401142 content = _load_atom (response , MATCH_ENTRY_CONTENT )
11411143 return _parse_atom_metadata (content )
11421144
1145+ def iter (self , offset = 0 , count = None , pagesize = None , ** kwargs ):
1146+ """Iterate (possibly lazily) over the collection.
1147+
1148+ This is equivalent to the :meth:`list` method, but
1149+ it returns an iterator, and can load the entities a few at a
1150+ time from the server (the number so loaded is controlled by
1151+ the *pagesize* argument).
1152+
1153+ **Example**::
1154+
1155+ import splunklib.client as client
1156+ s = client.connect(...)
1157+ for saved_search in s.saved_searches.iter(pagesize=10):
1158+ # Loads 10 saved searches at a time from the
1159+ # server.
1160+ ...
1161+ """
1162+ if count is None :
1163+ count = self .null_count
1164+ fetched = 0
1165+ while count == self .null_count or fetched < count :
1166+ response = self .get (count = pagesize or count , offset = offset , ** kwargs )
1167+ items = self ._load_list (response )
1168+ N = len (items )
1169+ fetched += N
1170+ for item in items :
1171+ yield item
1172+ if pagesize is None or N < pagesize :
1173+ break
1174+ offset += N
1175+
11431176 # kwargs: count, offset, search, sort_dir, sort_key, sort_mode
1144- def list (self , count = - 1 , ** kwargs ):
1177+ def list (self , count = None , ** kwargs ):
11451178 """Fetch a list of the entities in this collection.
11461179
11471180 There is no laziness in this function. The entire collection
@@ -1162,8 +1195,9 @@ def list(self, count=-1, **kwargs):
11621195 This function always makes a roundtrip to the server, and
11631196 makes no attempt at caching.
11641197 """
1165- response = self .get (count = count , ** kwargs )
1166- return self ._load_list (response )
1198+ # response = self.get(count=count, **kwargs)
1199+ # return self._load_list(response)
1200+ return list (self .iter (count = count , ** kwargs ))
11671201
11681202class ConfigurationFile (Collection ):
11691203 """This class contains a single configuration, which is a collection of
@@ -1487,6 +1521,14 @@ def list(self, *args):
14871521
14881522 return entities
14891523
1524+ def __iter__ (self , ** kwargs ):
1525+ for item in self .list (** kwargs ):
1526+ yield item
1527+
1528+ def iter (self , ** kwargs ):
1529+ for item in self .list (** kwargs ):
1530+ yield item
1531+
14901532 def oneshot (self , ** kwargs ):
14911533 pass
14921534
@@ -1712,6 +1754,9 @@ class Jobs(Collection):
17121754 """This class represents a collection of search jobs."""
17131755 def __init__ (self , service ):
17141756 Collection .__init__ (self , service , PATH_JOBS , item = Job )
1757+ # The count value to say list all the contents of this
1758+ # Collection is 0, not -1 as it is on most.
1759+ self .null_count = 0
17151760
17161761 def create (self , query , ** kwargs ):
17171762 if kwargs .get ("exec_mode" , None ) == "oneshot" :
@@ -1798,9 +1843,6 @@ def oneshot(self, query, **params):
17981843 else :
17991844 raise
18001845
1801- def list (self , count = 0 , ** kwargs ):
1802- return Collection .list (self , count , ** kwargs )
1803-
18041846class Loggers (Collection ):
18051847 """This class represents a collection of service logging categories."""
18061848 def __init__ (self , service ):
@@ -2054,15 +2096,16 @@ class NotSupportedError(Exception):
20542096class DeploymentCollection (Collection ):
20552097 def __init__ (self , service , path , item ):
20562098 Collection .__init__ (self , service , path , item = item )
2099+ # The count value to say list all the contents of this
2100+ # Collection is 0, not -1 as it is on most.
2101+ self .null_count = 0
20572102
20582103 def create (self , name , ** kwargs ):
20592104 raise NotSupportedError ("Cannot create %s with the REST API." % self .__class__ .__name__ )
20602105
20612106 def delete (self , name ):
20622107 raise NotSupportedError ("Cannot delete %s with the REST API." % self .__class__ .__name__ )
20632108
2064- def list (self , count = 0 , ** kwargs ):
2065- return Collection .list (self , count = count , ** kwargs )
20662109
20672110class DeploymentTenant (Entity ):
20682111 """Binding for /deployments/tenants/{name}."""
@@ -2148,10 +2191,9 @@ class DeploymentServers(DeploymentCollection):
21482191 """Binding for /deployment/server"""
21492192 def __init__ (self , service ):
21502193 Collection .__init__ (self , service , PATH_DEPLOYMENT_SERVERS , item = DeploymentServer )
2151-
2152- # Override this because Collection defaults to count=-1
2153- def list (self , count = 0 , ** kwargs ):
2154- return Collection .list (self , count = count , ** kwargs )
2194+ # The count value to say list all the contents of this
2195+ # Collection is 0, not -1 as it is on most.
2196+ self .null_count = 0
21552197
21562198 def create (self , name , ** kwargs ):
21572199 raise NotSupportedError ("Cannot create deployment servers with the REST API." )
0 commit comments