
from _hesiod import bind, resolve


from _hesiod import bind, resolve

from pwd import struct_passwd

class HesiodParseError(Exception):
    pass

class Lookup(object):
    """
    A Generic Hesiod lookup
    """
    def __init__(self, hes_name, hes_type):
        self.results = resolve(hes_name, hes_type)
        self.parseRecords()
    
    def parseRecords(self):
        pass

class FilsysLookup(Lookup):
    def __init__(self, name):
        Lookup.__init__(self, name, 'filsys')
    
    def parseRecords(self):
        Lookup.parseRecords(self)
        
        self.filsys = []
        self.multiRecords = (len(self.results) > 1)
        
        for result in self.results:
            priority = 0
            if self.multiRecords:
                result, priority = result.rsplit(" ", 1)
                priority = int(priority)
            
            parts = result.split(" ")
            type = parts[0]
            if type == 'AFS':
                self.filsys.append(dict(type=type,
                                        location=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            elif type == 'NFS':
                self.filsys.append(dict(type=type,
                                        remote_location=parts[1],
                                        server=parts[2],
                                        mode=parts[3],
                                        mountpoint=parts[4],
                                        priority=priority))
            elif type == 'ERR':
                self.filsys.append(dict(type=type,
                                        message=parts[1],
                                        priority=priority))
            elif type == 'UFS':
                self.filsys.append(dict(type=type,
                                        device=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            elif type == 'LOC':
                self.filsys.append(dict(type=type,
                                        location=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            else:
                raise HesiodParseError('Unknown filsys type: %s' % type)

class PasswdLookup(Lookup):
    def __init__(self, name):
        Lookup.__init__(self, name, 'passwd')
    
    def parseRecords(self):
        self.passwd = struct_passwd(self.results[0].split(':'))

class UidLookup(PasswdLookup):
    def __init__(self, uid):
        Lookup.__init__(self, uid, 'uid')

__all__ = ['bind', 'resolve',
           'Lookup', 'FilsysLookup', 'PasswdLookup', 'UidLookup',
           'HesiodParseError']

Add object-oriented-style lookups for filsys, passwd, and uid lookups
Add object-oriented-style lookups for filsys, passwd, and uid lookups

The filsys entry parsing code is taken from pyHesiodFS and was explicitly
relicensed under the MIT license by Quentin Smith <quentin@mit.edu>

  
  from _hesiod import bind, resolve
  
+ from pwd import struct_passwd
+ 
+ class HesiodParseError(Exception):
+     pass
+ 
+ class Lookup(object):
+     """
+     A Generic Hesiod lookup
+     """
+     def __init__(self, hes_name, hes_type):
+         self.results = resolve(hes_name, hes_type)
+         self.parseRecords()
+     
+     def parseRecords(self):
+         pass
+ 
+ class FilsysLookup(Lookup):
+     def __init__(self, name):
+         Lookup.__init__(self, name, 'filsys')
+     
+     def parseRecords(self):
+         Lookup.parseRecords(self)
+         
+         self.filsys = []
+         self.multiRecords = (len(self.results) > 1)
+         
+         for result in self.results:
+             priority = 0
+             if self.multiRecords:
+                 result, priority = result.rsplit(" ", 1)
+                 priority = int(priority)
+             
+             parts = result.split(" ")
+             type = parts[0]
+             if type == 'AFS':
+                 self.filsys.append(dict(type=type,
+                                         location=parts[1],
+                                         mode=parts[2],
+                                         mountpoint=parts[3],
+                                         priority=priority))
+             elif type == 'NFS':
+                 self.filsys.append(dict(type=type,
+                                         remote_location=parts[1],
+                                         server=parts[2],
+                                         mode=parts[3],
+                                         mountpoint=parts[4],
+                                         priority=priority))
+             elif type == 'ERR':
+                 self.filsys.append(dict(type=type,
+                                         message=parts[1],
+                                         priority=priority))
+             elif type == 'UFS':
+                 self.filsys.append(dict(type=type,
+                                         device=parts[1],
+                                         mode=parts[2],
+                                         mountpoint=parts[3],
+                                         priority=priority))
+             elif type == 'LOC':
+                 self.filsys.append(dict(type=type,
+                                         location=parts[1],
+                                         mode=parts[2],
+                                         mountpoint=parts[3],
+                                         priority=priority))
+             else:
+                 raise HesiodParseError('Unknown filsys type: %s' % type)
+ 
+ class PasswdLookup(Lookup):
+     def __init__(self, name):
+         Lookup.__init__(self, name, 'passwd')
+     
+     def parseRecords(self):
+         self.passwd = struct_passwd(self.results[0].split(':'))
+ 
+ class UidLookup(PasswdLookup):
+     def __init__(self, uid):
+         Lookup.__init__(self, uid, 'uid')
+ 
+ __all__ = ['bind', 'resolve',
+            'Lookup', 'FilsysLookup', 'PasswdLookup', 'UidLookup',
+            'HesiodParseError']
+ 
Add object-oriented-style lookups for filsys, passwd, and uid lookups
## Code Before:

from _hesiod import bind, resolve

## Instruction:
Add object-oriented-style lookups for filsys, passwd, and uid lookups
## Code After:

from _hesiod import bind, resolve

from pwd import struct_passwd

class HesiodParseError(Exception):
    pass

class Lookup(object):
    """
    A Generic Hesiod lookup
    """
    def __init__(self, hes_name, hes_type):
        self.results = resolve(hes_name, hes_type)
        self.parseRecords()
    
    def parseRecords(self):
        pass

class FilsysLookup(Lookup):
    def __init__(self, name):
        Lookup.__init__(self, name, 'filsys')
    
    def parseRecords(self):
        Lookup.parseRecords(self)
        
        self.filsys = []
        self.multiRecords = (len(self.results) > 1)
        
        for result in self.results:
            priority = 0
            if self.multiRecords:
                result, priority = result.rsplit(" ", 1)
                priority = int(priority)
            
            parts = result.split(" ")
            type = parts[0]
            if type == 'AFS':
                self.filsys.append(dict(type=type,
                                        location=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            elif type == 'NFS':
                self.filsys.append(dict(type=type,
                                        remote_location=parts[1],
                                        server=parts[2],
                                        mode=parts[3],
                                        mountpoint=parts[4],
                                        priority=priority))
            elif type == 'ERR':
                self.filsys.append(dict(type=type,
                                        message=parts[1],
                                        priority=priority))
            elif type == 'UFS':
                self.filsys.append(dict(type=type,
                                        device=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            elif type == 'LOC':
                self.filsys.append(dict(type=type,
                                        location=parts[1],
                                        mode=parts[2],
                                        mountpoint=parts[3],
                                        priority=priority))
            else:
                raise HesiodParseError('Unknown filsys type: %s' % type)

class PasswdLookup(Lookup):
    def __init__(self, name):
        Lookup.__init__(self, name, 'passwd')
    
    def parseRecords(self):
        self.passwd = struct_passwd(self.results[0].split(':'))

class UidLookup(PasswdLookup):
    def __init__(self, uid):
        Lookup.__init__(self, uid, 'uid')

__all__ = ['bind', 'resolve',
           'Lookup', 'FilsysLookup', 'PasswdLookup', 'UidLookup',
           'HesiodParseError']
