A helper function that expands a given line that contains a pattern
    specified in top docstring, and returns a list that consists of the
    expanded version.

    The '[' and ']' characters are used to maintain the pseudo-code
    appearance. They are replaced in this function with '|' to ease
    string splitting.

    References: http://ansible.github.com/patterns.html#hosts-and-groups

def expand_hostname_range(line = None):
    '''
    A helper function that expands a given line that contains a pattern
    specified in top docstring, and returns a list that consists of the
    expanded version.

    The '[' and ']' characters are used to maintain the pseudo-code
    appearance. They are replaced in this function with '|' to ease
    string splitting.

    References: http://ansible.github.com/patterns.html#hosts-and-groups
    '''
    all_hosts = []
    if line:
        # A hostname such as db[1:6]-node is considered to consists
        # three parts:
        # head: 'db'
        # nrange: [1:6]; range() is a built-in. Can't use the name
        # tail: '-node'

        (head, nrange, tail) = line.replace('[','|').replace(']','|').split('|')
        bounds = nrange.split(":")
        if len(bounds) != 2:
            raise errors.AnsibleError("host range incorrectly specified")
        beg = bounds[0]
        end = bounds[1]
        if not beg:
            beg = "0"
        if not end:
            raise errors.AnsibleError("host range end value missing")
        if beg[0] == '0' and len(beg) > 1:
            rlen = len(beg) # range length formatting hint
            if rlen != len(end):
                raise errors.AnsibleError("host range format incorrectly specified!")
            fill = lambda _: str(_).zfill(rlen)  # range sequence
        else:
            fill = str

        try:
            i_beg = string.ascii_letters.index(beg)
            i_end = string.ascii_letters.index(end)
            if i_beg > i_end:
                raise errors.AnsibleError("host range format incorrectly specified!")
            seq = string.ascii_letters[i_beg:i_end+1]
        except ValueError:  # not a alpha range
            seq = range(int(beg), int(end)+1)

        for rseq in seq:
            hname = ''.join((head, fill(rseq), tail))
            all_hosts.append(hname)

        return all_hosts