def path_to_key(datastore, path):
    """
    Translates a file system path to a datastore key. The basename becomes the
    key name and the extension becomes the kind.

    Examples:
        /file.ext -> key(ext, file)
        /parent.ext/file.ext -> key(ext, parent, ext, file)
    """
    key_parts = []
    path_parts = path.strip(u'/').split(u'/')
    for n, x in enumerate(path_parts):
        name, ext = x.rsplit('.', 1)
        key_parts.extend([ext, name])

    return datastore.key(*key_parts)