"""
The module is intended to be an abstraction that helps the user find the
local ip address that will be used for broadcasting.
"""
import netifaces
from twisted.python import filepath
import json


def getLiveInterface():
    """
    Return the LAN address of this machine.
    """
    ifaces = netifaces.interfaces()
    for layer in ifaces:
        addrs = netifaces.ifaddresses(layer)
        link = addrs.get(netifaces.AF_INET)
        # this bit needs cleaning up. It doesn't cover enough cases
        if link:
            subaddr = link[0]
            octet = subaddr['addr']
            if octet[:3] != '127':
                return subaddr['addr']


def getFilenames(path):
    """ Given a path, provide all of the files occuring at or under the
    given path.

    This function expects that path is a directory and not a single
    file path.
    """
    # XXX throw an argument exception if a filename is passed in!
    path = filepath.FilePath(path)
    names = ['/'.join(subpath.segmentsFrom(path.parent()))
             for subpath in path.walk()]
    return names


def sortedDump(data):
    """
    return the data object as serialized json, with the keys sorted
    alphabetically.

    :param data: any data structure that can be serialized.
    :returns: a serialized object encoded in utf-8 with all keys sorted
    alphabetically.
    """
    return json.dumps(data, sort_keys=True)


if __name__ == '__main__':
    address = getLiveInterface()
    print 'addr: %s' % address

@@ -686,16 +686,20 @@
 %0A    %22%22%22
+%0A   
  Given a
@@ -709,15 +709,12 @@
 th, 
-provide
+find
  all
@@ -729,49 +729,61 @@
 file
-s occuring at or under the%0A    given path
+ and directory names that are%0A    the path's children
 .%0A%0A 
@@ -868,81 +868,99 @@
 th.%0A
-    %22%22%22%0A    # XXX throw an argument exception if a filename is passed in!
+%0A    :returns: a list of directories and a list of filenames%0A    :rtype: list, list%0A    %22%22%22
 %0A   
@@ -995,16 +995,20 @@
 th)%0A    
+file
 names = 
@@ -1012,114 +1012,274 @@
  = %5B
-'/'.join(subpath.segmentsFrom(path.parent()))%0A             for subpath in path.walk()%5D%0A    return 
+%5D%0A    dirs = set()%0A    for subpath in path.walk():%0A        name = '/'.join(subpath.segmentsFrom(path.parent()))%0A        if subpath.isfile():%0A            filenames.append(name)%0A        if subpath.isdir():%0A            dirs.add(name)%0A    return list(dirs), file
 names%0A%0A%0A
 def 
@@ -1274,16 +1274,17 @@
 names%0A%0A%0A
+%0A
 def sort

return both the filenames and directory names from getFilenames