Exemple #1
0
    def all_pootle_paths(self):
        """Get cache_key for all parents up to virtual folder location.

        We only return the paths for the VirtualFolderTreeItem tree since we
        don't want to mess with regular CachedTreeItem stats.
        """
        return [p for p in get_all_pootle_paths(self.get_cachekey()) if p.count("/") > self.vfolder.location.count("/")]
Exemple #2
0
    def all_pootle_paths(self):
        """Get cache_key for all parents up to virtual folder location.

        We only return the paths for the VirtualFolderTreeItem tree since we
        don't want to mess with regular CachedTreeItem stats.
        """
        return [
            p for p in get_all_pootle_paths(self.get_cachekey())
            if p.count('/') > self.vfolder.location.count('/')
        ]
Exemple #3
0
def test_get_all_pootle_paths():
    """Tests all paths are properly extracted."""
    assert get_all_pootle_paths('') == ['']
    assert get_all_pootle_paths('/') == ['/']
    assert get_all_pootle_paths('/projects/') == ['/projects/']
    assert get_all_pootle_paths('/projects/tutorial/') == \
        ['/projects/tutorial/']
    assert get_all_pootle_paths('/pt/tutorial/') == \
        ['/pt/tutorial/', '/projects/tutorial/']
    assert get_all_pootle_paths('/pt/tutorial/tutorial.po') == \
        ['/pt/tutorial/tutorial.po', '/pt/tutorial/', '/projects/tutorial/']
def test_get_all_pootle_paths():
    """Tests all paths are properly extracted."""
    assert get_all_pootle_paths('') == ['']
    assert get_all_pootle_paths('/') == ['/']
    assert get_all_pootle_paths('/projects/') == ['/projects/']
    assert get_all_pootle_paths('/projects/tutorial/') == \
        ['/projects/tutorial/']
    assert get_all_pootle_paths('/pt/tutorial/') == \
        ['/pt/tutorial/', '/projects/tutorial/']
    assert get_all_pootle_paths('/pt/tutorial/tutorial.po') == \
        ['/pt/tutorial/tutorial.po', '/pt/tutorial/', '/projects/tutorial/']
Exemple #5
0
def test_get_all_pootle_paths():
    """Tests all paths are properly extracted."""
    assert get_all_pootle_paths("") == [""]
    assert get_all_pootle_paths("/") == ["/"]
    assert get_all_pootle_paths("/projects/") == ["/projects/"]
    assert get_all_pootle_paths("/projects/tutorial/") == ["/projects/tutorial/"]
    assert get_all_pootle_paths("/pt/tutorial/") == [
        "/pt/tutorial/",
        "/projects/tutorial/",
    ]
    assert get_all_pootle_paths("/pt/tutorial/tutorial.po") == [
        "/pt/tutorial/tutorial.po",
        "/pt/tutorial/",
        "/projects/tutorial/",
    ]
Exemple #6
0
def extract_vfolder_from_path(request_path):
    """
    Matches request_path to a VirtualFolderTreeItem pootle_path

    If a match is found, the associated VirtualFolder and Directory.pootle_path
    are returned. Otherwise the original request_path is returned.

    :param request_path: a path that may contain a virtual folder
    :return: (`VirtualFolder`, path)
    """
    all_dir_paths = [
        dir_path
        for dir_path in get_all_pootle_paths(request_path)
        if dir_path.count("/") > 3 and dir_path.endswith("/")
    ]
    vftis = VirtualFolderTreeItem.objects.filter(pootle_path__in=all_dir_paths)
    if vftis.exists():
        # There may be more than one vfti with matching pootle_path, so we get
        # the one with the shortest path or highest priority.
        vfti = sorted(
            vftis.select_related("vfolder", "directory"), key=lambda obj: (-len(obj.pootle_path), obj.vfolder.priority)
        )[0]
        return vfti.vfolder, vfti.directory.pootle_path
    return None, request_path
Exemple #7
0
 def all_pootle_paths(self):
     """Get cache_key for all parents (to the Language and Project)
     of current TreeItem
     """
     return get_all_pootle_paths(self.get_cachekey())
Exemple #8
0
def extract_vfolder_from_path(pootle_path):
    """Return a valid virtual folder and an adjusted pootle path.

    This accepts a pootle path and extracts the virtual folder from it (if
    present) returning the virtual folder and the clean path.

    If it can't be determined the virtual folder, then the provided path is
    returned unchanged along as a None value.

    The path /gl/firefox/browser/vfolder/chrome/file.po with the vfolder
    virtual folder on it will be converted to
    /gl/firefox/browser/chrome/file.po if the virtual folder exists and is
    public.

    Have in mind that several virtual folders with the same name might apply in
    the same path (as long as they have different locations this is possible)
    and in such cases the one with higher priority is returned.
    """
    lang, proj, dir_path, filename = split_pootle_path(pootle_path)

    if ((filename and Store.objects.filter(pootle_path=pootle_path).exists())
        or Directory.objects.filter(pootle_path=pootle_path).exists()):
        # If there is no vfolder then return the provided path.
        return None, pootle_path

    # Get the pootle paths for all the parents except the one for the file and
    # those for the translation project and above.
    all_dir_paths = [dir_path for dir_path in get_all_pootle_paths(pootle_path)
                     if dir_path.count('/') > 3 and dir_path.endswith('/')]
    all_dir_paths = sorted(all_dir_paths)

    for dir_path in all_dir_paths:
        if Directory.objects.filter(pootle_path=dir_path).exists():
            continue

        # There is no directory with such path, and that might mean that it
        # includes a virtual folder.
        valid_starting_path, vfolder_name = dir_path.rstrip('/').rsplit('/', 1)

        vfolders = VirtualFolder.objects.filter(
            name=vfolder_name,
            is_public=True
        ).order_by('-priority')

        vfolder = None

        for vf in vfolders:
            # There might be several virtual folders with the same name, so get
            # the first higher priority one that applies to the adjusted path.
            try:
                # Ensure that the virtual folder applies in the path.
                vf.get_adjusted_location(valid_starting_path + '/')
            except Exception:
                continue

            vfolder = vf
            break

        if vfolder is None:
            # The virtual folder does not exist or is not public or doesn't
            # apply in this location, so this is an invalid path.
            break

        valid_ending_path = pootle_path.replace(dir_path, '')
        adjusted_path = '/'.join([valid_starting_path, valid_ending_path])

        return vfolder, adjusted_path

    # There is no virtual folder (or is not public) and the provided path
    # doesn't exist, so let the calling code to deal with this.
    return None, pootle_path
Exemple #9
0
 def all_pootle_paths(self):
     """Get cache_key for all parents (to the Language and Project)
     of current TreeItem
     """
     return get_all_pootle_paths(self.get_cachekey())
Exemple #10
0
def extract_vfolder_from_path(pootle_path):
    """Return a valid virtual folder and an adjusted pootle path.

    This accepts a pootle path and extracts the virtual folder from it (if
    present) returning the virtual folder and the clean path.

    If it can't be determined the virtual folder, then the provided path is
    returned unchanged along as a None value.

    The path /gl/firefox/browser/vfolder/chrome/file.po with the vfolder
    virtual folder on it will be converted to
    /gl/firefox/browser/chrome/file.po if the virtual folder exists and is
    public.

    Have in mind that several virtual folders with the same name might apply in
    the same path (as long as they have different locations this is possible)
    and in such cases the one with higher priority is returned.
    """
    lang, proj, dir_path, filename = split_pootle_path(pootle_path)

    if ((filename and Store.objects.filter(pootle_path=pootle_path).exists())
            or Directory.objects.filter(pootle_path=pootle_path).exists()):
        # If there is no vfolder then return the provided path.
        return None, pootle_path

    # Get the pootle paths for all the parents except the one for the file and
    # those for the translation project and above.
    all_dir_paths = [
        dir_path for dir_path in get_all_pootle_paths(pootle_path)
        if dir_path.count('/') > 3 and dir_path.endswith('/')
    ]
    all_dir_paths = sorted(all_dir_paths)

    for dir_path in all_dir_paths:
        if Directory.objects.filter(pootle_path=dir_path).exists():
            continue

        # There is no directory with such path, and that might mean that it
        # includes a virtual folder.
        valid_starting_path, vfolder_name = dir_path.rstrip('/').rsplit('/', 1)

        vfolders = VirtualFolder.objects.filter(
            name=vfolder_name, is_public=True).order_by('-priority')

        vfolder = None

        for vf in vfolders:
            # There might be several virtual folders with the same name, so get
            # the first higher priority one that applies to the adjusted path.
            try:
                # Ensure that the virtual folder applies in the path.
                vf.get_adjusted_location(valid_starting_path + '/')
            except Exception:
                continue

            vfolder = vf
            break

        if vfolder is None:
            # The virtual folder does not exist or is not public or doesn't
            # apply in this location, so this is an invalid path.
            break

        valid_ending_path = pootle_path.replace(dir_path, '')
        adjusted_path = '/'.join([valid_starting_path, valid_ending_path])

        return vfolder, adjusted_path

    # There is no virtual folder (or is not public) and the provided path
    # doesn't exist, so let the calling code to deal with this.
    return None, pootle_path