localzone package

Submodules

localzone.context module

localzone.context

This module implements the methods for localzone context management.

copyright:
  1. 2018 Andrew Grant Spencer
license:

BSD, see LICENSE for more details.

localzone.context.load(filename, origin=None)[source]

Read a master zone file and construct a Zone object.

Parameters:
  • filename (string) – The path to the zone’s master file.
  • origin (string) – (optional) The zone’s origin domain
Returns:

Zone object

Return type:

localzone.Zone

localzone.context.manage(filename, origin=None, autosave=False)[source]

A context manager that yields a Zone object.

Parameters:
  • filename (string) – The path to the zone’s master file.
  • origin (string) – (optional) The zone’s origin domain
  • autosave (bool) – (optional) controls whether or not the zone’s master file is written to disk upon exit
Returns:

Zone object

Return type:

localzone.models.Zone

localzone.models module

localzone.models

This module contains the primary objects that power localzone.

copyright:
  1. 2018 Andrew Grant Spencer
license:

BSD, see LICENSE for more details.

class localzone.models.Record(origin, name, node, rdataset, rdata)[source]

Bases: object

Initialize a Record object.

Parameters:
  • origin (dns.name.Name object) – The record’s parent domain.
  • name (dns.name.Name object) – The record’s name.
  • node (dns.node.Node object) – The record’s node.
  • rdataset (dns.rdataset.Rdataset object) – The record’s rdataset.
  • rdata (dns.rdata.Rdata object) – The record’s rdata.
content
hashid
name
node
origin
rdata
rdataset
rdclass
rdtype
to_text()[source]
ttl
class localzone.models.Zone(origin, rdclass=1, relativize=True)[source]

Bases: dns.zone.Zone

Initialize a Zone object.

The Zone class extends its base class dns.zone.Zone with additional abstractions (or denormalizations) for dealing with DNS zone records.

Parameters:
  • origin (dns.name.Name object or string) – The zone’s origin.
  • rdclass (int) – The zone’s rdata class; the default is class dns.rdataclass.IN.
  • relativize (bool) – Should the zone’s names be relativized to the origin?
add_record(name, rdtype, content, rdclass='IN', ttl=None)[source]

Add a resource record to the zone.

Parameters:
  • name (string) – The record’s name.
  • rdtype (string) – The record’s type, e.g. “CNAME”.
  • content (string) – The record’s content.
  • rdclass (string) – The record’s class.
  • ttl (ttl) – The record’s TTL.
Returns:

Record object

Return type:

localzone.models.Record

filename
find_record(rdtype='ANY', name=None, content=None)[source]

Create and return a list of each resource record in the zone matching the search criteria.

Parameters:
  • rdtype (string) – The record’s type.
  • name (string) – The record’s name.
  • content (string) – The record’s content.
Returns:

list of Record objects

Return type:

list

get_record(hashid)[source]

Get a resource record via ID. If no record is found, raise a KeyError.

Parameters:hashid (string) – The record’s ID.
Returns:Record object
Return type:localzone.models.Record
get_records(rdtype)[source]

Create and return a list of each resource record in the zone matching the specified type. If rdtype is “ANY”, all zone records are returned.

Parameters:rdtype (string) – The record’s type.
Returns:list of Record objects
Return type:list
records

Return a list of Record objects for each resource record in the zone. If the zone is very large, be aware of memory constraints.

Returns:list of Record objects
Return type:list
remove_record(hashid, cascade=True)[source]

Remove a resource record from the zone. A KeyError is raised by the get_record() method if the supplied hashid is not found in the zone.

If cascade is True and the`rdataset` is empty after removing the record, the rdataset is also removed. If the node only contains the empty rdataset, then the node is removed.

Parameters:
  • hashid (string) – The record’s ID.
  • cascade (bool) – (optional) Also remove the rdataset and node if empty?
save(filename=None, autoserial=True)[source]

Write the zone master file to disk. If filename is not provided, the file from which the zone was originally loaded will be written.

NB: this will replace the file located at filename.

Parameters:
  • filename (string) – The location to where the zone master file will be written.
  • autoserial (bool) – Should the zone’s serial be updated automatically?
soa

Return the SOA record of the zone’s origin.

Returns:Record object
Return type:localzone.models.Record
ttl
update_record(hashid, content)[source]

Update the content of a resource record. A KeyError is raised by the get_record() method if the supplied hashid is not found in the zone.

Parameters:
  • hashid (string) – The record’s ID.
  • content (string) – The new content of the record.

localzone.util module

localzone.util

This module contains general purpose utilities used by localzone.

localzone.util.checksum(word, size=32)[source]

Implements Fletcher’s checksum to create 16, 32, or 64-bit checksums.

Parameters:
  • word (string) – The data to checksum.
  • size (int) – The checksum’s size.
Returns:

Fletcher’s checksum represented as a hexadecimal digest.

Return type:

string

localzone.util.group(iterable, n, padvalue=None)[source]

Create n-length sets from an iterable.

Parameters:
  • iterable (iterable) – An iterable object.
  • n (int) – The number of items in the set.
  • padvalue – The value used, if necessary, to pad the last set.
Returns:

An iterable set.

Return type:

iterator

localzone.util.pack(tup)[source]

Packs a tuple of 8-bit integers.

Parameters:tup (tuple) – Tuple of 8-bit integers.
Returns:The packed n-bit value, where n is len(tup) * 8.
Return type:int

Module contents

The localzone DNS Library

A simple DNS library, written in Python, for managing zone files.

Basic usage:

>>> import localzone
>>> with localzone.manage("db.example.com") as z:
...     r = z.add_record("greeting", "TXT", "hello, world!")
...     r.name    # the record name, i.e. 'greeting'
...     r.rdtype  # the record type, i.e. 'TXT'
...     r.content # the record content, i.e. '"hello," "world!"'
...

Print the zone’s resource records:

>>> import localzone
>>> with localzone.manage("db.example.com") as z:
...     print(*z.records, sep="\n")
...
@ 3600 IN SOA ns username 2007120710 86400 7200 2419200 3600
@ 3600 IN NS ns
@ 3600 IN NS ns.somewhere.example.
@ 3600 IN MX 10 mail
@ ...

or:

>>> import localzone
>>> with localzone.manage("db.example.com") as z:
>>>     for r in z.records:
>>>         print(r)
...

The full documentation is available at <https://localzone.iomaestro.com>.

copyright:
  1. 2018 by Andrew Grant Spencer.
license:

BSD, see LICENSE for more details.