sonu.kumar | c8f7a70 | 2016-04-29 21:07:16 +0900 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2015 Rackspace |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | """ |
| 16 | |
| 17 | |
| 18 | class ZoneFile(object): |
| 19 | |
| 20 | def __init__(self, origin, ttl, records): |
| 21 | self.origin = origin |
| 22 | self.ttl = ttl |
| 23 | self.records = records |
| 24 | |
| 25 | def __str__(self): |
| 26 | return str(self.__dict__) |
| 27 | |
| 28 | def __repr__(self): |
| 29 | return str(self) |
| 30 | |
| 31 | def __eq__(self, other): |
| 32 | return self.__dict__ == other.__dict__ |
| 33 | |
gecong1973 | 4f173f3 | 2016-10-16 09:27:14 +0800 | [diff] [blame] | 34 | def __ne__(self, other): |
| 35 | return not self.__eq__(other) |
| 36 | |
sonu.kumar | c8f7a70 | 2016-04-29 21:07:16 +0900 | [diff] [blame] | 37 | @classmethod |
| 38 | def from_text(cls, text): |
| 39 | """Return a ZoneFile from a string containing the zone file contents""" |
| 40 | # filter out empty lines and strip all leading/trailing whitespace. |
| 41 | # this assumes no multiline records |
| 42 | lines = [x.strip() for x in text.split('\n') if x.strip()] |
| 43 | |
| 44 | assert lines[0].startswith('$ORIGIN') |
| 45 | assert lines[1].startswith('$TTL') |
| 46 | |
| 47 | return ZoneFile( |
| 48 | origin=lines[0].split(' ')[1], |
| 49 | ttl=int(lines[1].split(' ')[1]), |
| 50 | records=[ZoneFileRecord.from_text(x) for x in lines[2:]], |
| 51 | ) |
| 52 | |
| 53 | |
| 54 | class ZoneFileRecord(object): |
| 55 | |
| 56 | def __init__(self, name, type, data): |
| 57 | self.name = str(name) |
| 58 | self.type = str(type) |
| 59 | self.data = str(data) |
| 60 | |
| 61 | def __str__(self): |
| 62 | return str(self.__dict__) |
| 63 | |
| 64 | def __repr__(self): |
| 65 | return str(self) |
| 66 | |
| 67 | def __eq__(self, other): |
| 68 | return self.__dict__ == other.__dict__ |
| 69 | |
gecong1973 | 4f173f3 | 2016-10-16 09:27:14 +0800 | [diff] [blame] | 70 | def __ne__(self, other): |
| 71 | return not self.__eq__(other) |
| 72 | |
sonu.kumar | c8f7a70 | 2016-04-29 21:07:16 +0900 | [diff] [blame] | 73 | def __hash__(self): |
| 74 | return hash(tuple(sorted(self.__dict__.items()))) |
| 75 | |
| 76 | @classmethod |
| 77 | def from_text(cls, text): |
| 78 | """Create a ZoneFileRecord from a line of text of a zone file, like: |
| 79 | |
| 80 | mydomain.com. IN NS ns1.example.com. |
| 81 | """ |
| 82 | # assumes records don't have a TTL between the name and the class. |
| 83 | # assumes no parentheses in the record, all on a single line. |
| 84 | parts = [x for x in text.split(' ', 4) if x.strip()] |
| 85 | name, rclass, rtype, data = parts |
| 86 | assert rclass == 'IN' |
| 87 | return cls(name=name, type=rtype, data=data) |