'''Version 1 Base Classes======================These classes are what pyTenable < 1.2 used for all interactions. They are hereas most of the library will still use it until these have been phased out infavor of the newer RESTfly-derived classes.As these classes exist only as a basis for the application packages, it isn'trecommended to use this directly. Further if you're looking for a generic APIinterface to use for your own uses, take a look at the RESTfly library... autoclass:: APIResultsIterator :members:'''from__future__importabsolute_importimportlogging
[docs]classAPIResultsIterator:''' The API iterator provides a scalable way to work through result sets of any size. The iterator will walk through each page of data, returning one record at a time. If it reaches the end of a page of records, then it will request the next page of information and then continue to return records from the next page (and the next, and the next) until the counter reaches the total number of records that the API has reported. Note that this Iterator is used as a base model for all of the iterators, and while the mechanics of each iterator may vary, they should all behave to the user in a similar manner. Attributes: count (int): The current number of records that have been returned page (list): The current page of data being walked through. pages will be cycled through as the iterator requests more information from the API. page_count (int): The number of record returned from the current page. total (int): The total number of records that exist for the current request. '''count=0page_count=0total=1page=[]# The API will be grafted on here._api=None# The page size limit_limit=None# The current record offset_offset=None# The number of pages that may be requested before bailing. If set to# None, then there is no limitation to the number of pages that may be# requested._pages_total=None_pages_requested=0def__init__(self,api,**kw):self._api=apiself._log=logging.getLogger('{}.{}'.format(self.__module__,self.__class__.__name__))self.__dict__.update(kw)def_get_page(self):passdef__iter__(self):returnselfdef__next__(self):returnself.next()
[docs]defnext(self):''' Ask for the next record '''# If there are no more agent records to return, then we should raise# a StopIteration exception to end the madness.ifself.count>=self.total:raiseStopIteration()# If we have worked through the current page of records and we still# haven't hit to the total number of available records, then we should# query the next page of records.ifself.page_count>=len(self.page)andself.count<=self.total:self._get_page()iflen(self.page)==0:raiseStopIteration()# Get the relevant record, increment the counters, and return the# record.item=self.page[self.page_count]self.count+=1self.page_count+=1returnitem