import re


def parentdir(file_path):

    """
    Get the parent directory of a file or directory.

    Goes by the path alone, so it doesn't follow symlinks.

    On Windows, paths must be converted to use / before passing.

    This function is not the same as os.path.dirname(); for example,
    dirname will not give us the correct answer for any of:
        . ./ .. ../

    Note: still doesn't always correctly handle paths starting with /
    and containing . or .., e.g., parentdir('/foo/..')

    Dependencies:
        modules: re

    """

    # remove trailing /'s
    parentdir = re.sub('/*$', '', file_path)

    # are there no /'s left?
    if '/' not in parentdir:
        if parentdir == '':
            return '/'  # it was /, and / is its own parent
        if parentdir == '.':
            return '..'
        if parentdir == '..':
            return '../..'
        return '.'

    # remove the last component of the path
    parentdir = re.sub('/*[^/]*$', '', parentdir)
    if parentdir == '':
        return '/'

    return parentdir