def __iter__(self):
        for item in self.previous:
            yield item

        cwd = os.getcwd()
        yield {self.pathkey: posixpath.sep,
               self.typekey: self.foldertype}

        try:
            os.chdir(self.dirname)
            for (dirpath, dirnames, filenames) in os.walk(os.curdir):
                os.chdir(cwd)

                # Convert path from os.path to posixpath
                dirpath = posixpath.join(*pathsplit(dirpath, ospath=os.path))

                def sortkey(basename, dirpath=dirpath, sortkey=self.sortkey):
                    return sortkey({}, dirpath=dirpath, basename=basename)

                for basename in sorted(filenames, key=sortkey):
                    yield {self.pathkey: posixpath.relpath(
                        posixpath.join(posixpath.sep, dirpath, basename),
                        posixpath.sep)}
                for basename in sorted(dirnames, key=sortkey):
                    yield {
                        self.pathkey: posixpath.relpath(
                            posixpath.join(posixpath.sep, dirpath, basename),
                            posixpath.sep) + posixpath.sep,
                        self.typekey: self.foldertype}

                os.chdir(self.dirname)

        finally:
            os.chdir(cwd)
Example #2
0
    def __iter__(self):

        for item in self.previous:

            keys = item.keys()
            pathKey = self.pathKey(*keys)[0]

            if not pathKey:  # not enough info
                yield item
                continue

            newPathKey = self.newPathKey or pathKey
            newTypeKey = self.newTypeKey

            path = item[pathKey]
            elems = path.strip('/').rsplit('/', 1)
            container, id = (len(elems) == 1 and ('', elems[0]) or elems)

            # This may be a new container
            if container not in self.seen:

                containerPathItems = list(pathsplit(container))
                if containerPathItems:

                    checkedElements = []

                    # Check each possible parent folder
                    obj = self.context
                    for element in containerPathItems:
                        checkedElements.append(element)
                        currentPath = '/'.join(checkedElements)

                        if currentPath and currentPath not in self.seen:

                            if element and traverse(obj, element) is None:
                                # We don't have this path - yield to create a
                                # skeleton folder
                                yield {
                                    newPathKey: '/' + currentPath,
                                    newTypeKey: self.folderType
                                }
                            if self.cache:
                                self.seen.add(currentPath)

                        obj = traverse(obj, element)

            if self.cache:
                self.seen.add("%s/%s" % (
                    container,
                    id,
                ))

            yield item
    def __iter__(self):

        for item in self.previous:

            keys = item.keys()
            pathKey = self.pathKey(*keys)[0]

            if not pathKey:  # not enough info
                yield item
                continue

            newPathKey = self.newPathKey or pathKey
            newTypeKey = self.newTypeKey

            path = item[pathKey]
            elems = path.strip('/').rsplit('/', 1)
            container, id = (len(elems) == 1 and ('', elems[0]) or elems)

            # This may be a new container
            if container not in self.seen:

                containerPathItems = list(pathsplit(container))
                if containerPathItems:

                    checkedElements = []

                    # Check each possible parent folder
                    obj = self.context
                    for element in containerPathItems:
                        checkedElements.append(element)
                        currentPath = '/'.join(checkedElements)

                        if currentPath and currentPath not in self.seen:

                            if element and traverse(obj, element) is None:
                                # We don't have this path - yield to create a
                                # skeleton folder
                                yield {newPathKey: '/' + currentPath,
                                       newTypeKey: self.folderType}
                            if self.cache:
                                self.seen.add(currentPath)

                        obj = traverse(obj, element)

            if self.cache:
                self.seen.add("%s/%s" % (container, id,))

            yield item
Example #4
0
    def __iter__(self):
        for item in self.previous:
            yield item

        cwd = os.getcwd()
        yield {self.pathkey: posixpath.sep, self.typekey: self.foldertype}

        try:
            os.chdir(self.dirname)
            for (dirpath, dirnames, filenames) in os.walk(os.curdir):
                os.chdir(cwd)

                # Convert path from os.path to posixpath
                dirpath = posixpath.join(*pathsplit(dirpath, ospath=os.path))

                def sortkey(basename, dirpath=dirpath, sortkey=self.sortkey):
                    return sortkey({}, dirpath=dirpath, basename=basename)

                for basename in sorted(filenames, key=sortkey):
                    yield {
                        self.pathkey:
                        posixpath.relpath(
                            posixpath.join(posixpath.sep, dirpath, basename),
                            posixpath.sep)
                    }
                for basename in sorted(dirnames, key=sortkey):
                    yield {
                        self.pathkey:
                        posixpath.relpath(
                            posixpath.join(posixpath.sep, dirpath, basename),
                            posixpath.sep) + posixpath.sep,
                        self.typekey:
                        self.foldertype
                    }

                os.chdir(self.dirname)

        finally:
            os.chdir(cwd)
    def __iter__(self):
        storage = queryUtility(IRedirectionStorage)
        for item in self.previous:
            if storage is None:
                self.logger.warn(
                    'Could not find plone.app.redirector storage utility')
                yield item
                continue

            keys = item.keys()

            # Update paths first

            # If a redirect already exists for _path, it should be
            # updated first so that any new redirects for _old_paths
            # point to the correct _path and it's unlikely that any
            # keys in this item will contain any _old_paths for the
            # same item

            for key in keys:
                if not self.updatepathkeys(key)[1]:
                    continue

                if not self.condition(item, key=key):
                    continue

                multiple = True
                paths = old_paths = item[key]
                if not isinstance(paths, (tuple, list)):
                    multiple = False
                    paths = [paths]

                for idx, obj in enumerate(paths):
                    path = obj
                    is_element = isinstance(obj, (Element, ElementBase))
                    if is_element:
                        for attrib in self.elementattribs:
                            if attrib in obj.attrib:
                                path = obj.attrib[attrib]
                                break
                        else:
                            # No attribute in this element
                            continue

                    leading = path[:-len(path.lstrip('/'))]
                    url = urlparse.urlsplit(path)
                    if self._is_external(url):
                        continue

                    abspath = posixpath.abspath(posixpath.join(
                        posixpath.sep, str(url.path).lstrip('/'))).lstrip('/')
                    new_path = old_path = self.context_path
                    for elem in pathsplit(abspath):
                        old_path = posixpath.join(old_path, elem)
                        new_path = posixpath.join(new_path, elem)
                        new_path = storage.get(old_path, new_path)
                    if not urlparse.urlsplit(new_path).netloc:
                        new_path = leading + new_path[len(self.context_path):]
                    new_path = urlparse.urlunsplit(
                        url[:2] + (new_path, ) + url[3:])

                    if new_path != path.lstrip('/'):
                        if is_element:
                            obj.attrib[attrib] = new_path
                            new_path = obj
                        paths[idx] = new_path

                if not multiple:
                    paths = paths[0]
                if item[key] != paths:
                    self.logger.debug('Updating %r path(s): %r => %r',
                                      key, old_paths, paths)
                    item[key] = paths

            # Collect old paths
            old_paths = set()
            oldpathskey = self.oldpathskey(*keys)[0]
            if oldpathskey and oldpathskey in item:
                paths = item[oldpathskey]
                if isinstance(paths, (str, unicode)):
                    paths = [paths]
                old_paths.update(paths)

            pathkey = self.pathkey(*keys)[0]
            if pathkey:
                path = item[pathkey]
                url = urlparse.urlsplit(path)
                new_path = (
                    self._is_external(url) and path
                    or posixpath.join(self.context_path,
                                      str(path).lstrip('/'))).rstrip('/')
                # Add any new redirects
                for old_path in old_paths:
                    old_path = posixpath.join(
                        self.context_path,
                        str(old_path).lstrip('/')).rstrip('/')
                    if (old_path and old_path != new_path
                            # Avoid recursive redirects
                            and not new_path.startswith(old_path + '/')
                            and not storage.has_path(old_path)
                            and traverse(self.context, old_path) is None):
                        self.logger.debug('Adding %r redirect: %r => %r',
                                          pathkey, old_path, new_path)
                        storage.add(old_path, new_path)

            yield item
Example #6
0
    def __iter__(self):  # noqa: C901
        storage = queryUtility(IRedirectionStorage)
        for item in self.previous:
            if storage is None:
                self.logger.warn(
                    'Could not find plone.app.redirector storage utility')
                yield item
                continue

            keys = item.keys()

            # Update paths first

            # If a redirect already exists for _path, it should be
            # updated first so that any new redirects for _old_paths
            # point to the correct _path and it's unlikely that any
            # keys in this item will contain any _old_paths for the
            # same item

            for key in keys:
                if not self.updatepathkeys(key)[1]:
                    continue

                if not self.condition(item, key=key):
                    continue

                multiple = True
                paths = old_paths = item[key]
                if not isinstance(paths, (tuple, list)):
                    multiple = False
                    paths = [paths]

                for idx, obj in enumerate(paths):
                    if obj is None:
                        continue  # no object at this location

                    path = obj
                    is_element = isinstance(obj, (Element, ElementBase))
                    if is_element:
                        for attrib in self.elementattribs:
                            if attrib in obj.attrib:
                                path = obj.attrib[attrib]
                                break
                        else:
                            # No attribute in this element
                            continue

                    leading = path[:-len(path.lstrip('/'))]
                    url = urlparse.urlsplit(path)
                    if self._is_external(url):
                        continue

                    abspath = posixpath.abspath(
                        posixpath.join(posixpath.sep,
                                       str(url.path).lstrip('/'))).lstrip('/')
                    new_path = old_path = self.context_path
                    for elem in pathsplit(abspath):
                        old_path = posixpath.join(old_path, elem)
                        new_path = posixpath.join(new_path, elem)
                        new_path = storage.get(old_path, new_path)
                    if not urlparse.urlsplit(new_path).netloc:
                        new_path = leading + new_path[len(self.context_path):]
                    new_path = urlparse.urlunsplit(url[:2] + (new_path, ) +
                                                   url[3:])

                    if new_path != path.lstrip('/'):
                        if is_element:
                            obj.attrib[attrib] = new_path
                            new_path = obj
                        paths[idx] = new_path

                if not multiple:
                    paths = paths[0]
                if item[key] != paths:
                    self.logger.debug('Updating %r path(s): %r => %r', key,
                                      old_paths, paths)
                    item[key] = paths

            # Collect old paths
            old_paths = set()
            oldpathskey = self.oldpathskey(*keys)[0]
            if oldpathskey and oldpathskey in item:
                paths = item[oldpathskey]
                if isinstance(paths, (str, unicode)):
                    paths = [paths]
                old_paths.update(paths)

            pathkey = self.pathkey(*keys)[0]
            if pathkey:
                path = item[pathkey]
                url = urlparse.urlsplit(path)
                new_path = (self._is_external(url) and path or posixpath.join(
                    self.context_path,
                    str(path).lstrip('/'))).rstrip('/')
                # Add any new redirects
                for old_path in old_paths:
                    old_path = posixpath.join(
                        self.context_path,
                        str(old_path).lstrip('/')).rstrip('/')
                    if (old_path and old_path != new_path and
                            # Avoid recursive redirects
                            not new_path.startswith(old_path + '/')
                            and not storage.has_path(old_path)
                            and traverse(self.context, old_path) is None):
                        self.logger.debug('Adding %r redirect: %r => %r',
                                          pathkey, old_path, new_path)
                        storage.add(old_path, new_path)

            yield item