def test_default(self):
     template = TemplatePath("{Shot}/{Step}/file.ex", self.keys,
                             self.project_root)
     expected = os.path.join(self.project_root, "s1", "Anm", "file.ex")
     fields = {"Step": "Anm"}
     result = template.apply_fields(fields)
     self.assertEquals(expected, result)
    def test_multi_key_optional(self):
        """
        Test optional section containing multiple keys.
        """
        definition = "shots/{Shot}[.{branch}.v{version}][.{snapshot}.ma]"
        template_path = TemplatePath(definition, self.keys, self.project_root)

        relative_path = os.path.join("shots",
                                     "s1.mmm.v003.002.ma")

        expected = os.path.join(self.project_root, relative_path)

        fields = { "Shot": "s1",
                   "branch":"mmm",
                   "version": 3,
                   "snapshot": 2}
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)

        # if one key from optional section missing, whole section skipped.
        relative_path = os.path.join("shots",
                                     "s1.002.ma")

        expected = os.path.join(self.project_root, relative_path)

        fields = { "Shot": "s1",
                   "branch":"mmm",
                   "snapshot": 2}

        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)
    def setUp(self):
        super(TestTemplatePath, self).setUp()

        # Make various types of keys(fields)
        self.keys = {
            "Sequence": StringKey("Sequence"),
            "Shot": StringKey("Shot",
                              default="s1",
                              choices=["s1", "s2", "shot_1"]),
            "Step": StringKey("Step"),
            "branch": StringKey("branch", filter_by="alphanumeric"),
            "name": StringKey("name"),
            "version": IntegerKey("version", format_spec="03"),
            "snapshot": IntegerKey("snapshot", format_spec="03"),
            "ext": StringKey("ext"),
            "seq_num": SequenceKey("seq_num"),
            "frame": SequenceKey("frame", format_spec="04")
        }
        # Make a template
        self.definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}.{branch}.v{version}.{snapshot}.ma"
        self.template_path = TemplatePath(self.definition, self.keys,
                                          self.project_root)

        # make template with sequence key
        self.sequence = TemplatePath("/path/to/seq.{frame}.ext", self.keys, "",
                                     "frame")
    def test_optional_values(self):
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template_path = TemplatePath(definition, self.keys, self.project_root)

        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1.mmm.v003.002.ma")
        expected = os.path.join(self.project_root, relative_path)

        fields = {
            "Sequence": "seq_1",
            "Shot": "s1",
            "Step": "Anm",
            "branch": "mmm",
            "version": 3,
            "snapshot": 2
        }
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)

        # remove optional value
        del (fields["snapshot"])
        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1.mmm.v003")
        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)

        # remove optional value
        del (fields["branch"])
        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1.v003")
        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)
Beispiel #5
0
    def test_translate_abstract_fields(self):
        # We should get back what we gave since there won't be a matching
        # template for this path.
        self.assertEqual(
            "/jbee/is/awesome.0001.jpg",
            tank.util.shotgun.publish_creation._translate_abstract_fields(
                self.tk,
                "/jbee/is/awesome.0001.jpg",
            ),
        )

        # Build a set of matching templates.
        keys = dict(
            seq=tank.templatekey.SequenceKey(
                "seq",
                format_spec="03",
            ),
            frame=SequenceKey(
                "frame",
                format_spec="04",
            ),
        )
        template = TemplatePath(
            "folder/name_{seq}.{frame}.ext",
            keys,
            self.project_root,
        )
        dup_template = TemplatePath(
            "folder/name_{seq}.{frame}.ext",
            keys,
            self.project_root,
        )

        self.tk.templates["translate_fields_test"] = template

        # We should get back a transformed path since there's a single
        # matching template.
        path = os.path.join(self.project_root, "folder", "name_001.9999.ext")
        t_path = os.path.join(self.project_root, "folder",
                              "name_%03d.%04d.ext")

        self.assertEqual(
            t_path,
            tank.util.shotgun.publish_creation._translate_abstract_fields(
                self.tk,
                path,
            ),
        )

        self.tk.templates["translate_fields_test_dup"] = dup_template

        # We should get back what we gave due to multiple matching templates.
        self.assertEqual(
            path,
            tank.util.shotgun.publish_creation._translate_abstract_fields(
                self.tk,
                path,
            ),
        )
 def test_skip_enum(self):
     expected = os.path.join(self.project_root, "*")
     key = StringKey("Shot", choices=["s1", "s2"])
     template = TemplatePath("{Shot}", {"Shot":key}, self.project_root)
     fields = {"Shot":"*"}
     skip_fields = ["Shot"]
     result = template._apply_fields(fields, ignore_types=skip_fields)
     self.assertEquals(expected, result)
 def test_one_key(self):
     definition = "{Shot}/boo.wtf"
     template = TemplatePath(definition, self.keys, root_path=self.project_root)
     relative_path = os.path.join("shot_1", "boo.wtf")
     file_path = os.path.join(self.project_root, relative_path)
     expected = {"Shot": "shot_1"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_valid_alphanumeric(self):
     """Bug reported in Ticket #17090"""
     template = TemplatePath("{branch}.v{version}.nk", self.keys, self.project_root)
     input_path = os.path.join(self.project_root, "comp.v001.nk")
     expected = {"branch": "comp",
                 "version": 1}
     result = template.get_fields(input_path)
     self.assertEquals(expected, result)
 def test_valid_alphanumeric(self):
     """Bug reported in Ticket #17090"""
     template = TemplatePath("{branch}.v{version}.nk", self.keys,
                             self.project_root)
     input_path = os.path.join(self.project_root, "comp.v001.nk")
     expected = {"branch": "comp", "version": 1}
     result = template.get_fields(input_path)
     self.assertEquals(expected, result)
 def test_matching_enum(self):
     template = TemplatePath("{Shot}",
                             self.keys,
                             root_path=self.project_root)
     file_path = os.path.join(self.project_root, "s2")
     expected = {"Shot": "s2"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_skip_enum(self):
     expected = os.path.join(self.project_root, "*")
     key = StringKey("Shot", choices=["s1", "s2"])
     template = TemplatePath("{Shot}", {"Shot": key}, self.project_root)
     fields = {"Shot": "*"}
     skip_fields = ["Shot"]
     result = template._apply_fields(fields, ignore_types=skip_fields)
     self.assertEquals(expected, result)
Beispiel #12
0
    def setUp(self):
        super(TestTemplatePath, self).setUp(
            parameters={"primary_root_name": "primary_with_a_different_name"})
        # Make various types of keys(fields)
        self.keys = {
            "Sequence": StringKey("Sequence"),
            "Shot": StringKey("Shot",
                              default="s1",
                              choices=["s1", "s2", "shot_1"]),
            "Step": StringKey("Step"),
            "branch": StringKey("branch", filter_by="alphanumeric"),
            "name": StringKey("name"),
            "name_alpha": StringKey("name_alpha", filter_by="alphanumeric"),
            "version": IntegerKey("version", format_spec="03"),
            "snapshot": IntegerKey("snapshot", format_spec="03"),
            "ext": StringKey("ext"),
            "seq_num": SequenceKey("seq_num"),
            "frame": SequenceKey("frame", format_spec="04"),
        }
        # Make a template
        self.definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}.{branch}.v{version}.{snapshot}.ma"

        # legacy style template object which only knows about the currently running operating system
        self.template_path_current_os_only = TemplatePath(
            self.definition, self.keys, self.project_root)

        project_root = os.path.join(self.tank_temp, "project_code")
        self._project_roots = {self.primary_root_name: {}}
        # Create the roots.yml like structure. Double down on the key names so it can be used in all scenarios
        # where we require the roots.
        for os_name in [
                "windows_path",
                "linux_path",
                "mac_path",
                "win32",
                "linux2",
                "darwin",
        ]:
            self._project_roots[self.primary_root_name][os_name] = project_root
        self._primary_project_root = project_root

        # new style template object which supports all recognized platforms
        # get all OS roots for primary storage
        all_roots = self._project_roots[self.primary_root_name]

        self.template_path = TemplatePath(self.definition,
                                          self.keys,
                                          self.project_root,
                                          per_platform_roots=all_roots)

        self.project_root_template = TemplatePath("/",
                                                  self.keys,
                                                  self.project_root,
                                                  per_platform_roots=all_roots)

        # make template with sequence key
        self.sequence = TemplatePath("/path/to/seq.{frame}.ext", self.keys, "",
                                     "frame")
    def test_optional_sections(self):
        """
        Test that definition using optional sections resolves for missing optional values.
        """
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template = TemplatePath(definition, self.keys, self.project_root)

        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1.mmm.v003.002.ma")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {"Sequence": "seq_1",
                    "Shot": "s1",
                    "Step": "Anm",
                    "branch":"mmm",
                    "version": 3,
                    "snapshot": 2}

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)

        # remove version value
        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1.mmm.002.ma")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {"Sequence": "seq_1",
                    "Shot": "s1",
                    "Step": "Anm",
                    "branch":"mmm",
                    "snapshot": 2}

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)

        # remove all optional values
        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {"Sequence": "seq_1",
                    "Shot": "s1",
                    "Step": "Anm"}

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)
 def test_aliased_key(self):
     key = IntegerKey("aliased_name")
     keys = {"initial_name": key}
     definition = "{initial_name}"
     template = TemplatePath(definition, keys, self.project_root)
     input_path = os.path.join(self.project_root, "3")
     expected = {"aliased_name": 3}
     result = template.get_fields(input_path)
     self.assertEquals(expected, result)
 def test_aliased_key(self):
     key = IntegerKey("aliased_name")
     keys = {"initial_name": key}
     definition = "{initial_name}"
     template = TemplatePath(definition, keys, self.project_root)
     input_path = os.path.join(self.project_root, "3")
     expected = {"aliased_name": 3}
     result = template.get_fields(input_path)
     self.assertEquals(expected, result)
 def test_key_first_short(self):
     definition =  "{Step}/{Shot}.png"
     template = TemplatePath(definition, self.keys, root_path=self.project_root)
     relative_path = os.path.join("Anm", "shot_1.png")
     file_path = os.path.join(self.project_root, relative_path)
     expected = {"Step": "Anm",
                 "Shot": "shot_1"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_one_key(self):
     definition = "{Shot}/boo.wtf"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     relative_path = os.path.join("shot_1", "boo.wtf")
     file_path = os.path.join(self.project_root, relative_path)
     expected = {"Shot": "shot_1"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_double_int_key(self):
     """Test case that key of type int appears twice in definition."""
     int_key = IntegerKey("int_key")
     definition = "{int_key}/something/{int_key}.else"
     template = TemplatePath(definition, {"int_key": int_key}, root_path=self.project_root)
     expected = {"int_key": 4}
     relative_path = "4/something/4.else"
     input_path = os.path.join(self.project_root, relative_path)
     actual = template.get_fields(input_path)
     self.assertEquals(expected, actual)
 def test_key_first_short(self):
     definition = "{Step}/{Shot}.png"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     relative_path = os.path.join("Anm", "shot_1.png")
     file_path = os.path.join(self.project_root, relative_path)
     expected = {"Step": "Anm", "Shot": "shot_1"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_key_at_end(self):
     definition = "sequences/{Sequence}/{Shot}/{Step}"
     template = TemplatePath(definition, self.keys, root_path=self.project_root)
     relative_path = "sequences/Seq1/Shot_1/Anm"
     input_path = os.path.join(self.project_root, relative_path)
     expected = {"Sequence": "Seq1",
                 "Shot": "Shot_1",
                 "Step": "Anm"}
     actual = template.get_fields(input_path)
     self.assertEquals(expected, actual)
 def test_key_at_end(self):
     definition = "sequences/{Sequence}/{Shot}/{Step}"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     relative_path = "sequences/Seq1/Shot_1/Anm"
     input_path = os.path.join(self.project_root, relative_path)
     expected = {"Sequence": "Seq1", "Shot": "Shot_1", "Step": "Anm"}
     actual = template.get_fields(input_path)
     self.assertEquals(expected, actual)
    def test_optional_none_value(self):
        definition = "shots/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template_path = TemplatePath(definition, self.keys, self.project_root)

        fields = {"Shot": "s1", "branch": None, "version": 3, "snapshot": 2}

        relative_path = os.path.join("shots", "s1.v003.002.ma")

        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)
 def test_double_int_key(self):
     """Test case that key of type int appears twice in definition."""
     int_key = IntegerKey("int_key")
     definition = "{int_key}/something/{int_key}.else"
     template = TemplatePath(definition, {"int_key": int_key},
                             root_path=self.project_root)
     expected = {"int_key": 4}
     relative_path = "4/something/4.else"
     input_path = os.path.join(self.project_root, relative_path)
     actual = template.get_fields(input_path)
     self.assertEquals(expected, actual)
 def test_diff_seperators(self):
     definition = "shots/{Sequence}/{Shot}/{Step}/work"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     relative_path = os.path.join("shots", "seq_1", "shot_1", "Anm", "work")
     file_path = os.path.join(self.project_root, relative_path)
     # adding extra seperator to end
     file_path = file_path + os.path.sep
     expected = {"Sequence": "seq_1", "Shot": "shot_1", "Step": "Anm"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_diff_seperators(self):
     definition = "shots/{Sequence}/{Shot}/{Step}/work"
     template = TemplatePath(definition, self.keys, root_path=self.project_root)
     relative_path = os.path.join("shots", "seq_1", "shot_1", "Anm", "work")
     file_path = os.path.join(self.project_root, relative_path)
     # adding extra seperator to end
     file_path = file_path + os.path.sep
     expected = {"Sequence": "seq_1",
                 "Shot": "shot_1",
                 "Step": "Anm"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
    def test_good_alphanumeric(self):
        """
        Tests applying valid values for alphanumeric key.
        """
        key = StringKey("alpha_num", filter_by="alphanumeric")
        template = TemplatePath("{alpha_num}", {"alpha_num":key}, self.project_root)

        valid_values = ["allChars", "123454", "mixed09", "29mixed", "mi2344xEd", "CAPS"]
        for valid_value in valid_values:
            result = template.apply_fields({"alpha_num": valid_value})
            expected = os.path.join(self.project_root, valid_value)
            self.assertEquals(expected, result)
    def test_format_default(self):
        definition = "shots/{Shot}.{branch}.{frame}.ext"
        template = TemplatePath(definition, self.keys, self.project_root)
        fields = {"Shot": "s1", "branch": "loon"}

        # default format
        expected = os.path.join(self.project_root, "shots", "s1.loon.%04d.ext")
        self.assertEquals(expected, template.apply_fields(fields))

        # specify default format
        expected = os.path.join(self.project_root, "shots", "s1.loon.####.ext")
        fields["frame"] = "FORMAT:#"
        self.assertEquals(expected, template.apply_fields(fields))
Beispiel #28
0
    def test_optional_values(self):
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template = TemplatePath(definition, self.keys, self.project_root)

        # test all combinations of optional values
        # branch T   version T   snapshot T
        test_path = os.path.join(
            self.project_root,
            "shots",
            "seq_1",
            "s1",
            "Anm",
            "work",
            "s1.mmm.v003.002.ma",
        )
        self.assertTrue(template.validate(test_path))

        # branch T   version T   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.mmm.v003")
        self.assertTrue(template.validate(test_path))

        # branch T   version F   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.mmm.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch T   version F   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.mmm")
        self.assertTrue(template.validate(test_path))

        # branch F   version T   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.v003.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch F   version T   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.v003")
        self.assertTrue(template.validate(test_path))

        # branch F   version F   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch F   version F   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1",
                                 "Anm", "work", "s1")
        self.assertTrue(template.validate(test_path))
    def test_format_default(self):
        definition = "shots/{Shot}.{branch}.{frame}.ext"
        template = TemplatePath(definition, self.keys, self.project_root)
        fields = { "Shot": "s1",
                   "branch": "loon"}
                   
        # default format
        expected = os.path.join(self.project_root, "shots", "s1.loon.%04d.ext")
        self.assertEquals(expected, template.apply_fields(fields))

        # specify default format
        expected = os.path.join(self.project_root, "shots", "s1.loon.####.ext")
        fields["frame"] = "FORMAT:#"
        self.assertEquals(expected, template.apply_fields(fields))
    def test_optional_none_value(self):
        definition = "shots/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template_path = TemplatePath(definition, self.keys, self.project_root)

        fields = { "Shot": "s1",
                   "branch": None,
                   "version": 3,
                   "snapshot": 2}

        relative_path = os.path.join("shots",
                                     "s1.v003.002.ma")

        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)
    def test_aliased_key(self):
        """
        Apply values to a template which has an aliased key.
        """
        key = IntegerKey("aliased_name")
        keys = {"initial_name": key}
        definition = "{initial_name}"
        template = TemplatePath(definition, keys, self.project_root)

        expected = os.path.join(self.project_root, "2")
        fields = {"aliased_name": 2}
        self.assertEquals(expected, template.apply_fields(fields))

        fields = {"initial_name": 2}
        self.assertRaises(TankError, template.apply_fields, fields)
    def test_aliased_key(self):
        """
        Apply values to a template which has an aliased key.
        """
        key = IntegerKey("aliased_name")
        keys = {"initial_name": key}
        definition = "{initial_name}"
        template = TemplatePath(definition, keys, self.project_root)

        expected = os.path.join(self.project_root, "2")
        fields = {"aliased_name": 2}
        self.assertEquals(expected, template.apply_fields(fields))

        fields = {"initial_name": 2}
        self.assertRaises(TankError, template.apply_fields, fields)
    def test_good_alphanumeric(self):
        """
        Tests applying valid values for alphanumeric key.
        """
        key = StringKey("alpha_num", filter_by="alphanumeric")
        template = TemplatePath("{alpha_num}", {"alpha_num": key},
                                self.project_root)

        valid_values = [
            "allChars", "123454", "mixed09", "29mixed", "mi2344xEd", "CAPS"
        ]
        for valid_value in valid_values:
            result = template.apply_fields({"alpha_num": valid_value})
            expected = os.path.join(self.project_root, valid_value)
            self.assertEquals(expected, result)
    def test_optional_sections(self):
        """
        Test that definition using optional sections resolves for missing optional values.
        """
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template = TemplatePath(definition, self.keys, self.project_root)

        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1.mmm.v003.002.ma")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {
            "Sequence": "seq_1",
            "Shot": "s1",
            "Step": "Anm",
            "branch": "mmm",
            "version": 3,
            "snapshot": 2
        }

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)

        # remove version value
        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1.mmm.002.ma")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {
            "Sequence": "seq_1",
            "Shot": "s1",
            "Step": "Anm",
            "branch": "mmm",
            "snapshot": 2
        }

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)

        # remove all optional values
        relative_path = os.path.join("shots", "seq_1", "s1", "Anm", "work",
                                     "s1")
        input_path = os.path.join(self.project_root, relative_path)

        expected = {"Sequence": "seq_1", "Shot": "s1", "Step": "Anm"}

        result = template.get_fields(input_path)
        self.assertEqual(expected, result)
Beispiel #35
0
    def test_static_ambiguous(self):
        """
        Tests that in case that static values are amiguous, no value is returned for that key.
        """
        # Set up a shot with different static values in paths
        shot = {"type": "Shot", "id": 3, "name": "shot_3"}
        shot_path_1 = os.path.join(self.project_root, "static_1", "shot_3")
        shot_path_2 = os.path.join(self.project_root, "static_2", "shot_3")
        self.add_production_path(shot_path_1, shot)
        self.add_production_path(shot_path_2, shot)

        template_def =  "/{static_key}/{Shot}"
        template = TemplatePath(template_def, self.keys, self.project_root)

        # Create context for this shot
        kws = {}
        kws["tk"] = self.tk
        kws["project"] = self.project
        kws["entity"]  = shot
        kws["step"]    = self.step
        ctx = context.Context(**kws)
        result = ctx.as_template_fields(template)

        # Check for non-entity value
        self.assertIsNone(result["static_key"])
Beispiel #36
0
    def test_user_ctx(self):
        """Check other_user is set when contained in the path."""
        
        # get a context containing a user
        ctx = self.tk.context_from_path(self.other_user_path)

        # check context's attributes
        self.assertEquals(self.shot["id"], ctx.entity["id"])
        self.assertEquals(self.shot["type"], ctx.entity["type"])
        self.assertEquals(self.project["id"], ctx.project["id"])
        self.assertEquals(self.project["type"], ctx.project["type"])
        self.assertEquals(self.step["id"], ctx.step["id"])
        self.assertEquals(self.step["type"], ctx.step["type"])
        self.assertEquals(self.other_user["id"], ctx.user["id"])
        self.assertEquals(self.other_user["type"], ctx.user["type"])
        self.assertIsNone(ctx.task)
        
        # create a template that uses user
        self.keys["HumanUser"] = StringKey("HumanUser")
        template_def = "/sequence/{Sequence}/{Shot}/{Step}/{HumanUser}"
        template = TemplatePath(template_def, self.keys, self.project_root)

        # pull out fields and test that we have everythign we expect
        fields = ctx.as_template_fields(template)

        self.assertEquals(fields["HumanUser"], "user_login")
        self.assertEquals(fields["Shot"], "shot_code")
        self.assertEquals(fields["Sequence"], "Seq")
        self.assertEquals(fields["Step"], "step_short_name")
        self.assertEquals(len(fields), 4)
Beispiel #37
0
    def test_bad_alphanumeric(self):
        """
        Tests applying non-alphanumeric values to keys of type alphanumeric.
        """
        # single key template
        key = StringKey("alpha_num", filter_by="alphanumeric")
        template = TemplatePath("{alpha_num}", {"alpha_num": key},
                                self.project_root)

        invalid_values = [
            "_underscore",
            "white space",
            "@mpersand",
            "par(enthes)",
            "###",
        ]
        for invalid_value in invalid_values:
            expected_msg = (
                "%s Illegal value '%s' does not fit filter_by 'alphanumeric'" %
                (str(key), invalid_value))
            self.check_error_message(
                TankError,
                expected_msg,
                template.apply_fields,
                {"alpha_num": invalid_value},
            )
Beispiel #38
0
    def setUp(self):
        super(TestPathsFromTemplateGlob, self).setUp()
        keys = {"Shot": StringKey("Shot"),
                "version": IntegerKey("version", format_spec="03"),
                "seq_num": SequenceKey("seq_num", format_spec="05")}

        self.template = TemplatePath("{Shot}/{version}/filename.{seq_num}", keys, root_path=self.project_root)
Beispiel #39
0
    def setUp(self):
        super(TestAsTemplateFields, self).setUp()
        # create a context obj using predefined data
        kws = {}
        kws["tk"] = self.tk
        kws["project"] = self.project
        kws["entity"] = self.shot
        kws["step"] = self.step
        self.ctx = context.Context(**kws)

        # create a template with which to filter
        self.keys = {
            "Sequence":
            StringKey("Sequence"),
            "Shot":
            StringKey("Shot"),
            "Step":
            StringKey("Step"),
            "static_key":
            StringKey("static_key"),
            "shotgun_field":
            StringKey("shotgun_field",
                      shotgun_entity_type="Shot",
                      shotgun_field_name="shotgun_field")
        }

        template_def = "/sequence/{Sequence}/{Shot}/{Step}/work"
        self.template = TemplatePath(template_def, self.keys,
                                     self.project_root)
Beispiel #40
0
    def test_skip_invalid(self):
        """Test that files not valid for an template are not returned.

        This refers to bug reported in Ticket #17090
        """
        keys = {
            "Shot": StringKey("Shot"),
            "Sequence": StringKey("Sequence"),
            "Step": StringKey("Step"),
            "name": StringKey("name"),
            "version": IntegerKey("version", format_spec="03")
        }

        definition = "sequences/{Sequence}/{Shot}/{Step}/work/{name}.v{version}.nk"
        template = TemplatePath(definition, keys, self.project_root,
                                "my_template")
        tk = tank.Tank(self.project_root)
        tk._templates = {template.name: template}
        bad_file_path = os.path.join(self.project_root, "sequences",
                                     "Sequence1", "Shot1", "Foot", "work",
                                     "name1.va.nk")
        good_file_path = os.path.join(self.project_root, "sequences",
                                      "Sequence1", "Shot1", "Foot", "work",
                                      "name.v001.nk")
        self.create_file(bad_file_path)
        self.create_file(good_file_path)
        ctx_fields = {"Sequence": "Sequence1", "Shot": "Shot1", "Step": "Foot"}
        result = tk.paths_from_template(template, ctx_fields)
        self.assertIn(good_file_path, result)
        self.assertNotIn(bad_file_path, result)
 def test_no_parent_exists(self):
     definition = "{Shot}"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     parent_def = template.parent
     self.assertTrue(parent_def is None)
 def test_aliased_key(self):
     """Test template which uses aliased key in it's definition."""
     keys = {}
     keys["old_name"] = StringKey("new_name")
     definition = "{old_name}/something"
     template = TemplatePath(definition, keys, root_path=self.project_root)
     result = template.parent
     self.assertEquals("{new_name}", result.definition)
 def test_key_first(self):
     definition = "{Sequence}/{Shot}/{Step}/work/{Shot}.{branch}.v{version}.{snapshot}.ma"
     template = TemplatePath(definition, self.keys, root_path=self.project_root)
     relative_path = os.path.join("seq_1",
                                  "shot_1",
                                  "Anm",
                                  "work",
                                  "shot_1.mmm.v003.002.ma")
     file_path = os.path.join(self.project_root, relative_path)
     expected = {"Sequence": "seq_1",
                 "Shot": "shot_1",
                 "Step": "Anm",
                 "branch":"mmm",
                 "version": 3,
                 "snapshot": 2}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def test_static_tokens(self):
     definition = "{Sequence}/{Shot}/3d/maya/scenes/{branch}-v{version}.{ext}"
     if sys.platform.lower() == "win32":
         expected = [["\\", "\\3d\\maya\\scenes\\", "-v", "."]]
     else:
         expected = [["/", "/3d/maya/scenes/", "-v", "."]]
     template = TemplatePath(definition, self.keys, root_path="")
     self.assertEquals(expected, template._static_tokens)
 def test_one_key_no_match(self):
     definition = "comp/{Shot}s/boo.wtf"
     template = TemplatePath(definition,
                             self.keys,
                             root_path=self.project_root)
     relative_path = os.path.join("anim", "shot_1", "boo.wtf")
     file_path = os.path.join(self.project_root, relative_path)
     self.assertRaises(TankError, template.get_fields, file_path)
    def test_optional_values(self):
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template_path = TemplatePath(definition, self.keys, self.project_root)

        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1.mmm.v003.002.ma")
        expected = os.path.join(self.project_root, relative_path)

        fields = {"Sequence": "seq_1",
                   "Shot": "s1",
                   "Step": "Anm",
                   "branch":"mmm",
                   "version": 3,
                   "snapshot": 2}
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)

        # remove optional value
        del(fields["snapshot"])
        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1.mmm.v003")
        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)


        # remove optional value
        del(fields["branch"])
        relative_path = os.path.join("shots",
                                     "seq_1",
                                     "s1",
                                     "Anm",
                                     "work",
                                     "s1.v003")
        expected = os.path.join(self.project_root, relative_path)
        result = template_path.apply_fields(fields)
        self.assertEquals(expected, result)
    def test_optional_values(self):
        definition = "shots/{Sequence}/{Shot}/{Step}/work/{Shot}[.{branch}][.v{version}][.{snapshot}.ma]"
        template = TemplatePath(definition, self.keys, self.project_root)

        # test all combinations of optional values
        # branch T   version T   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.mmm.v003.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch T   version T   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.mmm.v003")
        self.assertTrue(template.validate(test_path))

        # branch T   version F   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.mmm.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch T   version F   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.mmm")
        self.assertTrue(template.validate(test_path))

        # branch F   version T   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.v003.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch F   version T   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.v003")
        self.assertTrue(template.validate(test_path))

        # branch F   version F   snapshot T
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1.002.ma")
        self.assertTrue(template.validate(test_path))

        # branch F   version F   snapshot F
        test_path = os.path.join(self.project_root, "shots", "seq_1", "s1", "Anm", "work", "s1")
        self.assertTrue(template.validate(test_path))
 def test_default(self):
     template = TemplatePath("{Shot}/{Step}/file.ex", self.keys, self.project_root)
     expected = os.path.join(self.project_root, "s1", "Anm", "file.ex")
     fields = {"Step": "Anm"}
     result = template.apply_fields(fields)
     self.assertEquals(expected, result)
 def test_matching_enum(self):
     template = TemplatePath("{Shot}", self.keys, root_path=self.project_root)
     file_path = os.path.join(self.project_root, "s2")
     expected = {"Shot": "s2"}
     result = template.get_fields(file_path)
     self.assertEquals(result, expected)
 def assert_path_matches(self, definition, input_path, expected):
     template = TemplatePath(definition, self.keys, "")
     result = template.get_fields(input_path)
     self.assertEquals(expected, result)