# HG changeset patch # User Peter Sanchez # Date 1349919219 25200 # Wed Oct 10 18:33:39 2012 -0700 # Node ID 6af728d95c1b18b96b6e47cfcf01da1cdd35105e # Parent 96310db538bc3f06d7170a1bc7afd2d0606cf7e0 # Parent d862ef682abf29bcd0e9aab57392db3430fa53b1 Merged in issue2 branch. diff --git a/zerigodns/__init__.py b/zerigodns/__init__.py --- a/zerigodns/__init__.py +++ b/zerigodns/__init__.py @@ -1,6 +1,6 @@ ''' Copyright 2009 Peter Sanchez . See BSD-LICENSE for license information. ''' -from api import ZerigoError, ZerigoNotFound, NSZone, NSHost, \ - NSZoneTemplate, NSHostTemplate -__version__ = '0.3' +from api import ZerigoError, ZerigoNotFound, NSZone, ZoneHostList, \ + NSHost, NSZoneTemplate, NSHostTemplate +__version__ = '0.4' diff --git a/zerigodns/api.py b/zerigodns/api.py --- a/zerigodns/api.py +++ b/zerigodns/api.py @@ -118,11 +118,6 @@ req_headers.update(headers) if method in ('POST', 'PUT'): req_headers['Content-Type'] = 'application/xml' - # XXX This is a hack. For some reason letting the Python - # HTTPConnection.request() method set the Content-Length - # header (default) was setting the length 2 bytes short. - # Manually set the length for now until we can look into it - #req_headers['Content-Length'] = (len(request) + 2) if self.is_debug: print 'DEBUG (method): ', method @@ -249,6 +244,79 @@ return (not self.has_errors()) +class ZoneHostList(list): + ''' Wrapper around list to paginate through a Zerigo zone's + host list. + ''' + _paginate_by = 100 + + def __init__(self, zone, *args, **kwargs): + self._zone = zone + self._host_paginator = NSHost( + zone.user, + zone.key, + is_secure=zone.is_secure, + is_debug=zone.is_debug, + ) + super(ZoneHostList, self).__init__(*args, **kwargs) + if self.cur_count == 0 and self.host_count > 0: + self.load() + + @property + def host_count(self): + return self._zone.hosts_count + + @property + def cur_count(self): + return len(self) + + @property + def paginate_by(self): + if self._paginate_by <= 0: + self._paginate_by = 100 + elif self._paginate_by > 1000: + self._paginate_by = 1000 + return self._paginate_by + + @property + def cur_page(self): + return (self.cur_count / self.paginate_by) + 1 + + @property + def next_page(self): + if self.cur_count < self.host_count: + if self.cur_count == 0: + return self.cur_page + return self.cur_page + 1 + return -1 + + @property + def num_pages(self): + return (self.host_count / self.paginate_by) + 1 + + @property + def all_pages(self): + return [(x + 1) for x in range(self.num_pages)] + + def load(self): + if self.next_page > 0: + hosts = self._host_paginator.find_all( + self._zone, { + 'page': self.next_page, + 'per_page': self.paginate_by, + }, + ) + self.extend(hosts) + return hosts + return [] + + def load_all(self): + host_tmp = self.load() + while host_tmp: + host_tmp = self.load() + return True + + class NSZone(ZerigoDNS): ''' Class to manage zone entries ''' @@ -262,17 +330,24 @@ def __setattr__(self, name, val): if name == 'hosts' and isinstance(val, (list, tuple)): # Add hosts as NSHost objects - self.vals[name] = [ - NSHost( + self.vals[name] = ZoneHostList( + self, + [NSHost( self.user, self.key, data=zdata['host'], is_secure=self.is_secure, is_debug=self.is_debug, - ) for zdata in val - ] + ) for zdata in val], + ) else: super(NSZone, self).__setattr__(name, val) + + def load(self, data, reset=True): + super(NSZone, self).load(data, reset=reset) + host_count = getattr(self, 'host_count', 0) + if 'hosts' not in self.vals and host_count > 0: + setattr(self, 'hosts', ZoneHostList(self)) def to_xml(self): return self._to_xml({'zone': self.get_xml_data()})