import re


def get_import_target(line):
    """Canonicalize import statements into a reliable form.

    All of

        import foo.bar.baz
        from foo.bar import baz
        from foo.bar import baz as quux

    are canonicalized to

        foo.bar.baz

    Returns None if line does not contain an import statement. Note that we will
    not catch imports done with advanced techniques (__import__, etc.).
    """
    target = None

    match = re.search(r'^import\ (.+)$', line)
    if match:
        target = match.groups()[0]
    else:
        match = re.search(r'^from\ (\S+)\ import\ (\S+)(\ as\ .+)?$', line)
        if match:
            target = '%s.%s' % tuple(match.groups()[0:2])

    return target