import os
import glob
import unittest
from ...BaseTestCase import BaseTestCase
from centipede.Crawler import Crawler
from centipede.Crawler.Fs import FsPath
from centipede.Crawler.Fs import File
from centipede.PathHolder import PathHolder
from centipede.Crawler.Fs.Render import ExrRender
from centipede.Crawler.Fs.Image import Exr
from centipede.Crawler.Crawler import InvalidVarError
from centipede.Crawler.Crawler import InvalidTagError

class FsPathTest(BaseTestCase):
    """Test Directory crawler."""

    __dir = os.path.join(BaseTestCase.dataDirectory(), "glob")
    __turntableFile = os.path.join(__dir, "images", "RND_ass_lookdev_default_beauty_tt.1001.exr")
    __shotRenderFile = os.path.join(__dir, "images", "RND-TST-SHT_lighting_beauty_sr.1001.exr")

    def testFsCrawler(self):
        """
        Test that the fs crawler test works.
        """
        pathHolder = PathHolder(self.__dir)
        self.assertTrue(FsPath.test(pathHolder, None))

        notAPathHolder = {}
        self.assertFalse(FsPath.test(notAPathHolder, None))

    def testFsPathGlob(self):
        """
        Test the glob functionality.
        """
        crawler = Crawler.create(PathHolder(self.__dir))
        crawlers = crawler.glob()
        result = glob.glob("{}/**".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        self.assertCountEqual(result, crawlerPaths)

        crawlers = crawler.glob(filterTypes=["turntable", "shotRender"])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        self.assertCountEqual(crawlerPaths, [self.__turntableFile, self.__shotRenderFile])

        crawlers = crawler.glob(filterTypes=[ExrRender])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        result = glob.glob("{}/**/RND**.exr".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        self.assertCountEqual(result, crawlerPaths)

        crawlers = crawler.glob(filterTypes=['exr'])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        result = glob.glob("{}/**/**.exr".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        self.assertCountEqual(result, crawlerPaths)

        crawler = Crawler.create(PathHolder(self.__turntableFile))
        otherCrawlers = crawler.globFromParent(filterTypes=[ExrRender])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        otherCrawlerPaths = list(map(lambda x: x.var("filePath"), otherCrawlers))
        self.assertCountEqual(crawlerPaths, otherCrawlerPaths)

    def testPathVariables(self):
        """
        Test that the crawler variables are set properly.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        name, ext = os.path.splitext(self.__turntableFile)
        self.assertEqual(crawler.var('filePath'), self.__turntableFile)
        self.assertEqual(crawler.var('ext'), ext.lstrip("."))
        self.assertEqual(crawler.var('baseName'), os.path.basename(self.__turntableFile))
        self.assertEqual(crawler.var('name'), os.path.basename(name).split(".")[0])
        self.assertEqual(crawler.var('sourceDirectory'), os.path.dirname(name))
        self.assertRaises(InvalidVarError, crawler.var, "dummyVar")

    def testCrawlerTags(self):
        """
        Test that the Crawler tags are set properly.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        self.assertRaises(InvalidTagError, crawler.tag, "dummyTag")

    def testCrawlerRegistration(self):
        """
        Test that you can register a new crawler.
        """
        class DummyCrawler(File):
            @classmethod
            def test(cls, pathHolder, parentCrawler):
                return False

        Crawler.register("dummy", DummyCrawler)
        self.assertIn("dummy", Crawler.registeredNames())
        self.assertIn(DummyCrawler, Crawler.registeredSubclasses("generic"))
        self.assertIn(DummyCrawler, Crawler.registeredSubclasses(FsPath))

    def testCrawlerClone(self):
        """
        Test that cloning crawlers works.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        clone = crawler.clone()
        self.assertCountEqual(crawler.varNames(), clone.varNames())
        self.assertCountEqual(crawler.contextVarNames(), clone.contextVarNames())
        self.assertCountEqual(crawler.tagNames(), clone.tagNames())

    def testCrawlerJson(self):
        """
        Test that you can convert a crawler to json and back.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        jsonResult = crawler.toJson()
        crawlerResult = Crawler.createFromJson(jsonResult)
        self.assertCountEqual(crawler.varNames(), crawlerResult.varNames())
        self.assertCountEqual(crawler.contextVarNames(), crawlerResult.contextVarNames())
        self.assertCountEqual(crawler.tagNames(), crawlerResult.tagNames())

    def testCrawlerCreate(self):
        """
        Test that you can create a crawler with a specific type.
        """
        crawler = FsPath.createFromPath(self.__turntableFile, "exr")
        self.assertIsInstance(crawler, Exr)

    def testPathHolder(self):
        """
        Test PathHolder functions.
        """
        pathHolder = PathHolder(self.__turntableFile)
        self.assertEqual(pathHolder.size(), 5430903)
        self.assertEqual(pathHolder.name(), "RND_ass_lookdev_default_beauty_tt.1001")
        self.assertTrue(pathHolder.exists())
        pathHolder = PathHolder("/")
        self.assertEqual(pathHolder.baseName(), os.sep)

if __name__ == "__main__":
    unittest.main()

import os
import glob
import unittest
from ...BaseTestCase import BaseTestCase
from centipede.Crawler import Crawler
from centipede.Crawler.Fs import FsPath
from centipede.Crawler.Fs import File
from centipede.PathHolder import PathHolder
from centipede.Crawler.Fs.Render import ExrRender
from centipede.Crawler.Fs.Image import Exr
from centipede.Crawler.Crawler import InvalidVarError
from centipede.Crawler.Crawler import InvalidTagError

class FsPathTest(BaseTestCase):
    """Test Directory crawler."""

    __dir = os.path.join(BaseTestCase.dataDirectory(), "glob")
    __turntableFile = os.path.join(__dir, "images", "RND_ass_lookdev_default_beauty_tt.1001.exr")
    __shotRenderFile = os.path.join(__dir, "images", "RND-TST-SHT_lighting_beauty_sr.1001.exr")

    def testFsCrawler(self):
        """
        Test that the fs crawler test works.
        """
        pathHolder = PathHolder(self.__dir)
        self.assertTrue(FsPath.test(pathHolder, None))

        notAPathHolder = {}
        self.assertFalse(FsPath.test(notAPathHolder, None))

    def testFsPathGlob(self):
        """
        Test the glob functionality.
        """
        crawler = Crawler.create(PathHolder(self.__dir))
        crawlers = crawler.glob()
        result = glob.glob("{}/**".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        self.assertCountEqual(result, crawlerPaths)

        crawlers = crawler.glob(filterTypes=["turntable", "shotRender"])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        self.assertCountEqual(crawlerPaths, [self.__turntableFile, self.__shotRenderFile])

        crawlers = crawler.glob(filterTypes=[ExrRender])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        result = glob.glob("{}/**/RND**.exr".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        self.assertCountEqual(result, crawlerPaths)

        crawlers = crawler.glob(filterTypes=['exr'])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        result = glob.glob("{}/**/**.exr".format(self.__dir), recursive=True)
        result = list(map(lambda x: x.rstrip("/"), result))
        self.assertCountEqual(result, crawlerPaths)

        crawler = Crawler.create(PathHolder(self.__turntableFile))
        otherCrawlers = crawler.globFromParent(filterTypes=[ExrRender])
        crawlerPaths = list(map(lambda x: x.var("filePath"), crawlers))
        otherCrawlerPaths = list(map(lambda x: x.var("filePath"), otherCrawlers))
        self.assertCountEqual(crawlerPaths, otherCrawlerPaths)

    def testPathVariables(self):
        """
        Test that the crawler variables are set properly.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        name, ext = os.path.splitext(self.__turntableFile)
        self.assertEqual(crawler.var('filePath'), self.__turntableFile)
        self.assertEqual(crawler.var('ext'), ext.lstrip("."))
        self.assertEqual(crawler.var('baseName'), os.path.basename(self.__turntableFile))
        self.assertEqual(crawler.var('name'), os.path.basename(name).split(".")[0])
        self.assertEqual(crawler.var('sourceDirectory'), os.path.dirname(name))
        self.assertRaises(InvalidVarError, crawler.var, "dummyVar")

    def testCrawlerTags(self):
        """
        Test that the Crawler tags are set properly.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        self.assertRaises(InvalidTagError, crawler.tag, "dummyTag")

    def testCrawlerRegistration(self):
        """
        Test that you can register a new crawler.
        """
        class DummyCrawler(File):
            @classmethod
            def test(cls, pathHolder, parentCrawler):
                return False

        Crawler.register("dummy", DummyCrawler)
        self.assertIn("dummy", Crawler.registeredNames())
        self.assertIn(DummyCrawler, Crawler.registeredSubclasses("generic"))
        self.assertIn(DummyCrawler, Crawler.registeredSubclasses(FsPath))

    def testCrawlerClone(self):
        """
        Test that cloning crawlers works.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        clone = crawler.clone()
        self.assertCountEqual(crawler.varNames(), clone.varNames())
        self.assertCountEqual(crawler.contextVarNames(), clone.contextVarNames())
        self.assertCountEqual(crawler.tagNames(), clone.tagNames())

    def testCrawlerJson(self):
        """
        Test that you can convert a crawler to json and back.
        """
        crawler = Crawler.create(PathHolder(self.__turntableFile))
        jsonResult = crawler.toJson()
        crawlerResult = Crawler.createFromJson(jsonResult)
        self.assertCountEqual(crawler.varNames(), crawlerResult.varNames())
        self.assertCountEqual(crawler.contextVarNames(), crawlerResult.contextVarNames())
        self.assertCountEqual(crawler.tagNames(), crawlerResult.tagNames())

    def testCrawlerCreate(self):
        """
        Test that you can create a crawler with a specific type.
        """
        crawler = FsPath.createFromPath(self.__turntableFile, "exr")
        self.assertIsInstance(crawler, Exr)

    def testPathHolder(self):
        """
        Test PathHolder functions.
        """
        pathHolder = PathHolder(self.__turntableFile)
        self.assertEqual(pathHolder.size(), 5430903)
        self.assertEqual(pathHolder.name(), "RND_ass_lookdev_default_beauty_tt.1001")
        self.assertTrue(pathHolder.exists())
        pathHolder = PathHolder("/")
        self.assertEqual(pathHolder.baseName(), os.sep)

if __name__ == "__main__":
    unittest.main()

Test Directory crawler. Test that the fs crawler test works. Test the glob functionality. Test that the crawler variables are set properly. Test that the Crawler tags are set properly. Test that you can register a new crawler. Test that cloning crawlers works. Test that you can convert a crawler to json and back. Test that you can create a crawler with a specific type. Test PathHolder functions.