def matches(window, window_spec):
    """Return True if the given window matches the given window spec.

    False otherwise.

    A window spec is a dict containing items that will be matched against
    the window object's attributes, for example:

        {'window_id': '0x02a00001',
         'desktop': '0',
         'pid': '4346',
         'wm_class': '.Firefox',
         'machine': 'mistakenot',
         'title': 'The Mock Class - Mock 1.0.1 documentation - Firefox'}

    A window object matches a spec if it has an attribute matching each of
    the items in the spec.

    A spec doesn't have to contain all of the attributes. For example
    {'wm_class': '.Firefox'} will match all windows with a wm_class
    attribute matching ".Firefox".

    Attribute matching is done by looking for substrings. For example a
    wm_class of ".Firefox" will match a window with a wm_class of
    "Navigator.Firefox".

    Window specs can also contain a "command" key (the command to be run to
    launch the app if it doesn't have any open windows) - this key will be
    ignored and the window will match the spec as long as all the other keys
    match.

    """
    for key in window_spec.keys():
        if key == 'command':
            continue
        if window_spec[key].lower() not in getattr(window, key, '').lower():
            return False
    return True
