diff options
author | Camil Staps | 2016-05-02 15:36:05 +0200 |
---|---|---|
committer | Camil Staps | 2016-05-02 15:36:05 +0200 |
commit | 7120add95d0c5b8a97af861785ec9fe1cfc7eaee (patch) | |
tree | 986d3801536e5d45937ee3d22474bc6eca120819 /project2/proj2_s4498062/dns/cache.py | |
parent | Assignment 5 (diff) |
dos2unix
Diffstat (limited to 'project2/proj2_s4498062/dns/cache.py')
-rw-r--r-- | project2/proj2_s4498062/dns/cache.py | 182 |
1 files changed, 91 insertions, 91 deletions
diff --git a/project2/proj2_s4498062/dns/cache.py b/project2/proj2_s4498062/dns/cache.py index 1b64b51..eab51c5 100644 --- a/project2/proj2_s4498062/dns/cache.py +++ b/project2/proj2_s4498062/dns/cache.py @@ -1,91 +1,91 @@ -#!/usr/bin/env python2
-
-"""A cache for resource records
-
-This module contains a class which implements a cache for DNS resource records,
-you still have to do most of the implementation. The module also provides a
-class and a function for converting ResourceRecords from and to JSON strings.
-It is highly recommended to use these.
-"""
-
-import json
-
-from dns.resource import ResourceRecord, RecordData
-from dns.types import Type
-from dns.classes import Class
-
-
-class ResourceEncoder(json.JSONEncoder):
- """ Conver ResourceRecord to JSON
-
- Usage:
- string = json.dumps(records, cls=ResourceEncoder, indent=4)
- """
- def default(self, obj):
- if isinstance(obj, ResourceRecord):
- return {
- "name": obj.name,
- "type": Type.to_string(obj.type_),
- "class": Class.to_string(obj.class_),
- "ttl": obj.ttl,
- "rdata": obj.rdata.data
- }
- return json.JSONEncoder.default(self, obj)
-
-
-def resource_from_json(dct):
- """ Convert JSON object to ResourceRecord
-
- Usage:
- records = json.loads(string, object_hook=resource_from_json)
- """
- name = dct["name"]
- type_ = Type.from_string(dct["type"])
- class_ = Class.from_string(dct["class"])
- ttl = dct["ttl"]
- rdata = RecordData.create(type_, dct["rdata"])
- return ResourceRecord(name, type_, class_, ttl, rdata)
-
-
-class RecordCache(object):
- """ Cache for ResourceRecords """
-
- def __init__(self, ttl):
- """ Initialize the RecordCache
-
- Args:
- ttl (int): TTL of cached entries (if > 0)
- """
- self.records = []
- self.ttl = ttl
-
- def lookup(self, dname, type_, class_):
- """ Lookup resource records in cache
-
- Lookup for the resource records for a domain name with a specific type
- and class.
-
- Args:
- dname (str): domain name
- type_ (Type): type
- class_ (Class): class
- """
- pass
-
- def add_record(self, record):
- """ Add a new Record to the cache
-
- Args:
- record (ResourceRecord): the record added to the cache
- """
- pass
-
- def read_cache_file(self):
- """ Read the cache file from disk """
- pass
-
- def write_cache_file(self):
- """ Write the cache file to disk """
- pass
-
-
+#!/usr/bin/env python2 + +"""A cache for resource records + +This module contains a class which implements a cache for DNS resource records, +you still have to do most of the implementation. The module also provides a +class and a function for converting ResourceRecords from and to JSON strings. +It is highly recommended to use these. +""" + +import json + +from dns.resource import ResourceRecord, RecordData +from dns.types import Type +from dns.classes import Class + + +class ResourceEncoder(json.JSONEncoder): + """ Conver ResourceRecord to JSON + + Usage: + string = json.dumps(records, cls=ResourceEncoder, indent=4) + """ + def default(self, obj): + if isinstance(obj, ResourceRecord): + return { + "name": obj.name, + "type": Type.to_string(obj.type_), + "class": Class.to_string(obj.class_), + "ttl": obj.ttl, + "rdata": obj.rdata.data + } + return json.JSONEncoder.default(self, obj) + + +def resource_from_json(dct): + """ Convert JSON object to ResourceRecord + + Usage: + records = json.loads(string, object_hook=resource_from_json) + """ + name = dct["name"] + type_ = Type.from_string(dct["type"]) + class_ = Class.from_string(dct["class"]) + ttl = dct["ttl"] + rdata = RecordData.create(type_, dct["rdata"]) + return ResourceRecord(name, type_, class_, ttl, rdata) + + +class RecordCache(object): + """ Cache for ResourceRecords """ + + def __init__(self, ttl): + """ Initialize the RecordCache + + Args: + ttl (int): TTL of cached entries (if > 0) + """ + self.records = [] + self.ttl = ttl + + def lookup(self, dname, type_, class_): + """ Lookup resource records in cache + + Lookup for the resource records for a domain name with a specific type + and class. + + Args: + dname (str): domain name + type_ (Type): type + class_ (Class): class + """ + pass + + def add_record(self, record): + """ Add a new Record to the cache + + Args: + record (ResourceRecord): the record added to the cache + """ + pass + + def read_cache_file(self): + """ Read the cache file from disk """ + pass + + def write_cache_file(self): + """ Write the cache file to disk """ + pass + + |