~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/names/hosts.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
from twisted.names import dns
 
6
from twisted.persisted import styles
 
7
from twisted.python import failure
 
8
from twisted.internet import defer
 
9
 
 
10
from twisted.names import common
 
11
 
 
12
def searchFileFor(file, name):
 
13
    try:
 
14
        fp = open(file)
 
15
    except:
 
16
        return None
 
17
 
 
18
    lines = fp.readlines()
 
19
    for line in lines:
 
20
        idx = line.find('#')
 
21
        if idx != -1:
 
22
            line = line[:idx]
 
23
        if not line:
 
24
            continue
 
25
        parts = line.split()
 
26
        if name.lower() in [s.lower() for s in parts[1:]]:
 
27
            return parts[0]
 
28
    return None
 
29
 
 
30
 
 
31
 
 
32
class Resolver(common.ResolverBase, styles.Versioned):
 
33
    """A resolver that services hosts(5) format files."""
 
34
    #TODO: IPv6 support
 
35
 
 
36
    persistenceVersion = 1
 
37
 
 
38
    def upgradeToVersion1(self):
 
39
        # <3 exarkun
 
40
        self.typeToMethod = {}
 
41
        for (k, v) in common.typeToMethod.items():
 
42
            self.typeToMethod[k] = getattr(self, v)
 
43
 
 
44
 
 
45
    def __init__(self, file='/etc/hosts', ttl = 60 * 60):
 
46
        common.ResolverBase.__init__(self)
 
47
        self.file = file
 
48
        self.ttl = ttl
 
49
 
 
50
 
 
51
    def lookupAddress(self, name, timeout = None):
 
52
        res = searchFileFor(self.file, name)
 
53
        if res:
 
54
            return defer.succeed([
 
55
                (dns.RRHeader(name, dns.A, dns.IN, self.ttl, dns.Record_A(res, self.ttl)),), (), ()
 
56
            ])
 
57
        return defer.fail(failure.Failure(dns.DomainError(name)))
 
58
 
 
59
 
 
60
    # When we put IPv6 support in, this'll need a real impl
 
61
    lookupAllRecords = lookupAddress