Returns a :class:`MatchType` for the directory, expressed as a list of path
        elements, match for the :class:`Pattern`.

        If ``self.bound_start`` is True, the first :class:`Section` must match
        from the first directory element.

        If ``self.bound_end`` is True, the last :class:`Section` must match
        the last contiguous elements of *path_elements*.

def match_directory(self, path_elements):
        """Returns a :class:`MatchType` for the directory, expressed as a list of path
        elements, match for the :class:`Pattern`.

        If ``self.bound_start`` is True, the first :class:`Section` must match
        from the first directory element.

        If ``self.bound_end`` is True, the last :class:`Section` must match
        the last contiguous elements of *path_elements*.
        """

        def match_recurse(is_start, sections, path_elements, location):
            """A private function for implementing the recursive search.

            The function takes the first section from sections and tries to
            match this against the elements in path_elements, starting from
            the location'th element in that list.

            If sections is empty, this is taken to mean all sections have
            been previously matched, therefore a match has been found.

            * is_start: True if this is the call starting the recursion. False if
              this call is recursing
            * sections: A list of the remaining sections (sections not yet matched)
            * path_elements: A list of directory names, each element being a single directory
            * location: index into path_elements for where the search should start
            """
            if sections:
                section = sections[0]
                any_match = False
                for end in section.match_iter(path_elements, location):
                    any_match = True
                    match = match_recurse(False, sections[1:], path_elements, end)
                    if match | MatchType.MATCH:
                        return match

                # No match found
                if is_start and self.bound_start and not any_match:
                    # This this is the start of the recursion AND the pattern
                    # is bound to the start of the path ("/start/**") AND this
                    # did not match, then no subdirectories are possible either

                    if len(path_elements) >= len(section.elements):
                        return MatchType.NO_MATCH_NO_SUBDIRECTORIES
                    else:
                        # Optimization: Don't search subdirectories when
                        #  i) we have an fixed start to the pattern, eg "/Users/myuser/**"
                        #  ii) We have a path not matching the first, anchored, section, eg "/usr" or "/Users/another"
                        # Need to check whether the last path element matches the corresponding element in section
                        # If it does, return NO_MATCH (it's incomplete)
                        # If, however, the element's don't match, then no further match is possible,
                        # So return NO_MATCH_NO_SUBDIRECTORIES
                        if section.length > len(path_elements) > 0:
                            if not section.elements[len(path_elements) - 1].match(
                                                    path_elements[-1]):
                                return MatchType.NO_MATCH_NO_SUBDIRECTORIES
                        return MatchType.NO_MATCH
                else:
                    return MatchType.NO_MATCH
            else:
                # Termination of the recursion after FINDING the match.
                if len(self.sections) == 1 and self.bound_start and self.bound_end:
                    # If this pattern is of the form "/test/*" it matches
                    # just THIS directory and no subdirectories
                    return MatchType.MATCH_BUT_NO_SUBDIRECTORIES
                elif self.bound_end:
                    # "**/test/*" matches just this directory
                    # and allows subdirectories to also match
                    return MatchType.MATCH
                else:
                    # If the pattern is not bound to the end of the path (eg
                    # NOT "**/term/**") the pattern matches all subdirectories
                    return MatchType.MATCH_ALL_SUBDIRECTORIES
            # End of: def match_recurse(is_start, sections, path_elements, location):

        if self.sections:
            return match_recurse(True, self.sections, path_elements, 0)
        else:
            # Catches directory-less patterns like "*.py" and "/*.py".
            if self.bound_start:
                if len(path_elements) == 0:
                    # Eg "*/*.py" in the root directory
                    return MatchType.MATCH_BUT_NO_SUBDIRECTORIES
                else:
                    # Eg "/*.py" meets directory "/test/" - nothing happening
                    return MatchType.NO_MATCH_NO_SUBDIRECTORIES
            else:
                # Eg "**/*.py" - match all directories
                return MatchType.MATCH_ALL_SUBDIRECTORIES