class AbsPosixPathTest(unittest.TestCase):
    def setUp(self):
        self.p = Path("/a/b/c")

    def test_fslash_out(self):
        self.assertEqual(self.p.fslash(), "/a/b/c")

    def test_win_path_out(self):
        self.assertEqual(self.p.bslash(), "\\a\\b\\c")
class SpecifyDriveLetterUse(unittest.TestCase):
    def test_remove_from_path(self):
        self.p = Path("C:\\a\\b\\c")
        self.assertEqual(self.p.fslash(with_drive=False), "/a/b/c")
        self.assertEqual(self.p.bslash(with_drive=False), "\\a\\b\\c")

    def test_remove_from_root_path(self):
        self.p = Path("C:\\")
        self.assertEqual(self.p.fslash(with_drive=False), "/")
        self.assertEqual(self.p.bslash(with_drive=False), "\\")
class RootPath(unittest.TestCase):
    def test_root_path(self):
        self.p = Path("/")
        self.assertEqual(self.p.fslash(), "/")
        self.assertEqual(self.p.bslash(), "\\")

    def test_drive_letter_root_path(self):
        self.p = Path("C:\\")
        self.assertEqual(self.p.fslash(), "C:/")
        self.assertEqual(self.p.bslash(), "C:\\")
Example #4
0
    def glob(self):
        """Glob expansion for entries containing globbable characters.

        We don't simply glob every entry since that would remove entries
        that don't yet exist. And we can't just rely on zero glob
        results because it may have been a legitimate zero result if it
        was globbable but matched nothing. So we test for glob
        characters (*|?|[) to determine whether to attempt a glob.

        However, if it looks like a glob, but the glob library can't
        handle it, then we have to assume it really is a filename with 
        glob-like characters, and then we just add the literal path 
        unchanged. See the test: test_ignore_invalid_glob().
        """
        self._deduplicate()
        result = []
        for entry in self._entries:
            pp = entry.fslash()
            if GLOBBABLE_REGEX.search(pp):
                try:
                    globs = glob.glob(entry.fslash())
                    result += globs
                except re.error:
                    result.append(pp)
            else:
                result.append(pp)
        self._entries = [Path(g) for g in result]
        self._clean = False
        self._current = 0
class AbsWindowsPathTest(unittest.TestCase):
    def setUp(self):
        self.p = Path("C:\\a\\b\\c")

    def test_fslash_out(self):
        self.assertEqual(self.p.fslash(), "C:/a/b/c")

    def test_win_path_out(self):
        self.assertEqual(self.p.bslash(), "C:\\a\\b\\c")

    # consider just testing on both platforms
    def test_os_path_out(self):
        with mock.patch("os.name", "posix"):
            self.assertEqual(self.p.os_path(), "C:/a/b/c")
        with mock.patch("os.name", "nt"):
            self.assertEqual(self.p.os_path(), "C:\\a\\b\\c")
 def test_common_path_when_duplicate_entries_of_single_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foo.txt", "/users/joebloggs/tmp/foo.txt"
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp/foo.txt"))
 def test_next_reset_after_add(self):
     d = PathList()
     d.add("/file1", "/file2", "/file3")
     next(d)
     next(d)
     d.add("/file4")
     self.assertEqual(next(d), Path("/file1"))
class WindowsMixedPathTest(unittest.TestCase):
    def test_abs_in_fslash_out(self):
        self.p = Path("\\a\\b\\c/d/e")
        self.assertEqual(self.p.fslash(), "/a/b/c/d/e")

    def test_abs_in_bslash_out(self):
        self.p = Path("\\a\\b\\c/d/e")
        self.assertEqual(self.p.bslash(), "\\a\\b\\c\\d\\e")

    def test_letter_abs_in_fslash_out(self):
        self.p = Path("C:\\a\\b\\c/d/e")
        self.assertEqual(self.p.fslash(), "C:/a/b/c/d/e")

    def test_letter_abs_in_bslash_out(self):
        self.p = Path("C:\\a\\b\\c/d/e")
        self.assertEqual(self.p.bslash(), "C:\\a\\b\\c\\d\\e")
 def test_common_path_when_common_prefix_in_filename(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/dissention/perfect",
         "/users/joebloggs/tmp/disagreement/crimson",
         "/users/joebloggs/tmp/diatribe/belew",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foobar/test",
         "/users/joebloggs/tmp/baz/fripp",
         "/users/joebloggs/tmp/elephant/corner",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path_when_one_path_is_the_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp",
         "/users/joebloggs/tmp/bolly/operation",
         "/users/joebloggs/tmp/stay/go",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_path_when_lowest_path_is_the_common_path(self):
     d = PathList()
     files = [
         "/users/joebloggs/tmp/foo.txt",
         "/users/joebloggs/tmp/modelman.jpg",
         "/users/joebloggs/tmp/ration.cpp",
         "/users/joebloggs/tmp/bill.project",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/users/joebloggs/tmp"))
 def test_common_different_drive_letter(self):
     d = PathList()
     files = [
         "D:/users/joebloggs/tmp/foo.txt",
         "D:/users/joebloggs/tmp/modelman.jpg",
         "C:/users/joebloggs/tmp/ration.cpp",
         "C:/users/joebloggs/tmp/bill.project",
     ]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/"))
Example #14
0
    def _add_one(self, path):
        """Add a single file.

        Note that when an element is added, it may cause the list to
        change next time it is deduplicated, which includes getting
        shorter. This could happen if a containing directory is added.
        Therefore we have to set the peg position to zero.
        """

        if not type(path).__name__ == "Path":
            path = Path(path)
        self._entries.append(path)
        self._clean = False
        self._current = 0
class PathContextExpansionTest(unittest.TestCase):
    def setUp(self):

        self.env = {
            "HOME": "/users/joebloggs",
            "SHOT": "/metropolis/shot01",
            "DEPT": "texturing",
        }

        self.context = {
            "HOME": "/users/janedoe",
            "FOO": "fooval",
            "BAR_FLY1_": "bar_fly1_val",
            "ROOT_DIR": "/some/root",
        }

    def test_path_replaces_context(self):
        self.p = Path("$ROOT_DIR/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/thefile.jpg")

    def test_path_replaces_multiple_context(self):
        self.p = Path("$ROOT_DIR/$BAR_FLY1_/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/bar_fly1_val/thefile.jpg")

    def test_path_context_overrides_env(self):
        self.p = Path("$HOME/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/users/janedoe/thefile.jpg")

    def test_path_leave_unknown_variable_in_tact(self):
        self.p = Path("$ROOT_DIR/$BAR_FLY1_/$FOO/thefile.$F.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/bar_fly1_val/fooval/thefile.$F.jpg")

    def test_path_replaces_context_braces(self):
        self.p = Path("${ROOT_DIR}/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/thefile.jpg")

    def test_path_replaces_multiple_context_braces(self):
        self.p = Path("${ROOT_DIR}/${BAR_FLY1_}/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/bar_fly1_val/thefile.jpg")

    def test_path_context_overrides_env_braces(self):
        self.p = Path("${HOME}/thefile.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/users/janedoe/thefile.jpg")

    def test_path_leave_unknown_variable_in_tact_braces(self):
        self.p = Path("${ROOT_DIR}/${BAR_FLY1_}/${FOO}/thefile.$F.jpg", context=self.context)
        self.assertEqual(self.p.fslash(), "/some/root/bar_fly1_val/fooval/thefile.$F.jpg")
Example #16
0
    def common_path(self):
        """Find the common path among entries.

        This is useful for determining output directory when many renders are
        rendering to different places.

        In the case where only single path exists, it is not possible to tell
        from its name whether it is a file or directory. We don't want this
        method to touch the filesystem, that should be someone else's problem. A
        trailing slash would be a hint, but the absence of a trailing slash does
        not mean its a regular file. Therefore, in the case of a single file we
        return it AS-IS and the caller can then stat to find out for sure.

        If no files exist return None.

        If the filesystem root is the common path, return root path, which is
        not entirely correct on windows with drive letters.
        """
        if not self._entries:
            return None

        absolute = list(self._entries)[0].absolute

        def _all_the_same(rhs):
            return all(n == rhs[0] for n in rhs[1:])

        levels = zip(*[p.all_components for p in self._entries])

        common = [x[0] for x in takewhile(_all_the_same, levels)]

        if not len(common):
            return Path("/")
        elif common[0].endswith(":"):
            return Path("/".join(common))
        elif absolute:
            return Path("/" + "/".join(common))
        return Path("/".join(common))
class PathExpansionTest(unittest.TestCase):
    def setUp(self):
        self.env = {
            "HOME": "/users/joebloggs",
            "SHOT": "/metropolis/shot01",
            "DEPT": "texturing",
        }

    def test_posix_tilde_input(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("~/a/b/c")
            self.assertEqual(self.p.fslash(), "/users/joebloggs/a/b/c")

    def test_posix_var_input(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$SHOT/a/b/c")
            self.assertEqual(self.p.fslash(), "/metropolis/shot01/a/b/c")

    def test_posix_two_var_input(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$SHOT/a/b/$DEPT/c")
            self.assertEqual(self.p.fslash(), "/metropolis/shot01/a/b/texturing/c")

    def test_windows_var_input(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$HOME\\a\\b\\c")
            self.assertEqual(self.p.bslash(), "\\users\\joebloggs\\a\\b\\c")
            self.assertEqual(self.p.fslash(), "/users/joebloggs/a/b/c")

    def test_tilde_no_expand(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("~/a/b/c", no_expand=True)
            self.assertEqual(self.p.fslash(), "~/a/b/c")

    def test_posix_var_no_expand(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$SHOT/a/b/c", no_expand=True)
            self.assertEqual(self.p.fslash(), "$SHOT/a/b/c")

    def no_expand_variable_considered_relative(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$SHOT/a/b/c", no_expand=True)
            self.assertTrue(self.p.relative)
            self.assertFalse(self.p.absolute)

    def expanded_variable_considered_absolute(self):
        with mock.patch.dict("os.environ", self.env):
            self.p = Path("$SHOT/a/b/c", no_expand=False)
            self.assertFalse(self.p.relative)
            self.assertTrue(self.p.absolute)
 def test_removes_path(self):
     d = PathList()
     d.add("/a/file1", "/a/file2", "/a/file3")
     d.remove(Path("/a/file2"))
     self.assertEqual(len(d), 2)
     self.assertEqual(list(d)[1], Path("/a/file3"))
 def test_adds_mix(self):
     d = PathList()
     d.add("/a/file1", "/a/file2", Path("/a/file3"))
     self.assertEqual(len(d), 3)
 def test_adds_paths(self):
     d = PathList()
     d.add(Path("/a/file1"), Path("/a/file2"))
     self.assertEqual(len(d), 2)
 def test_unpacking(self):
     d = PathList()
     d.add(Path("/a/file1"), Path("/a/file2"))
     a, b = d
     self.assertEqual(type(a), Path)
 def test_common_path_is_slash_when_root(self):
     d = PathList()
     files = ["/users/joebloggs/tmp/foo.txt", "/dev/joebloggs/tmp/foo.txt"]
     d.add(*files)
     self.assertEqual(d.common_path(), Path("/"))
 def test_remove_from_root_path(self):
     self.p = Path("C:\\")
     self.assertEqual(self.p.fslash(with_drive=False), "/")
     self.assertEqual(self.p.bslash(with_drive=False), "\\")
 def setUp(self):
     self.p = Path("/a/b/c")
 def setUp(self):
     self.p = Path("C:\\a\\b\\c")
 def test_path_emits_string_posix(self):
     input_file = "/path/to/thefile.jpg"
     p = Path(input_file)
     self.assertEqual(str(p), input_file)
 def test_path_emits_string_relative(self):
     input_file = "path/to/thefile.jpg"
     p = Path(input_file)
     self.assertEqual(str(p), input_file)
Example #28
0
 def __contains__(self, key):
     if not isinstance(key, Path):
         key = Path(key)
     return key in self._entries
 def test_next(self):
     d = PathList()
     d.add("/file1", "/file2", "/file3")
     self.assertEqual(next(d), Path("/file1"))
     self.assertEqual(next(d), Path("/file2"))
 def test_dedup_same_paths(self):
     d = PathList()
     d.add(Path("/file1"), Path("/file2"), Path("/file2"))
     self.assertEqual(len(d), 2)
     self.assertIn(Path("/file1"), d)
     self.assertIn(Path("/file2"), d)