#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import platform
import unittest

import rspub.util.resourcefilter as rf


def on_windows():
    opsys = platform.system()
    return opsys == "Windows"


class TestPredicates(unittest.TestCase):

    def test_directory_pattern_filter_empty(self):
        dpf = rf.directory_pattern_predicate() # should pass all strings
        self.assertTrue(dpf(""))
        self.assertTrue(dpf("."))
        self.assertTrue(dpf("\n"))
        self.assertTrue(dpf("foo"))
        # rejects not string
        self.assertFalse(dpf(None))
        self.assertFalse(dpf(42))
        self.assertFalse(dpf(self))

    def test_directory_pattern_filter(self):
        dpf = rf.directory_pattern_predicate("abc")
        self.assertTrue(dpf("foo/babcd/bar/some.txt"))
        self.assertTrue(dpf("/abc/bar/some.txt"))
        self.assertTrue(dpf("/foo/bar/abc/some.txt"))
        #
        self.assertFalse(dpf("/foo/bar/baz/abc.txt"))

        # ##
        dpf = rf.directory_pattern_predicate("^/abc")
        self.assertTrue(dpf("/abc/bar/some.txt"))
        #
        self.assertFalse(dpf("abc/bar/some.txt"))
        # #

        dpf = rf.directory_pattern_predicate("abc$")
        self.assertTrue(dpf("foo/bar/abc/some.txt"))
        #
        self.assertFalse(dpf("abc/abc/bar/some.txt"))
        self.assertFalse(dpf("abc/abc/bar/abc.abc"))

    @unittest.skipUnless(on_windows(), "Only tested on Windows.")
    def test_directory_pattern_filter_windows(self):
        dpf = rf.directory_pattern_predicate("abc")
        self.assertTrue(dpf("foo/babcd/bar/some.txt"))
        self.assertTrue(dpf("/abc/bar/some.txt"))
        self.assertTrue(dpf("/foo/bar/abc/some.txt"))

        self.assertTrue(dpf("foo\\babcd\\bar\\some.txt"))
        self.assertTrue(dpf("c:\\abc\\bar\\some.txt"))
        self.assertTrue(dpf("c:\\foo\\bar\\abc\\some.txt"))
        #
        self.assertFalse(dpf("/foo/bar/baz/abc.txt"))
        self.assertFalse(dpf("c:\\foo\\bar\\baz\\abc.txt"))

        # ##
        dpf = rf.directory_pattern_predicate("^/abc")
        self.assertTrue(dpf("/abc/bar/some.txt"))
        #
        self.assertFalse(dpf("abc/bar/some.txt"))
        # #
        dpf = rf.directory_pattern_predicate("^c:\\abc")
        self.assertTrue(dpf("c:\\abc\\bar\\some.txt"))
        #
        self.assertFalse(dpf("abc\\bar\\some.txt"))

        dpf = rf.directory_pattern_predicate("abc$")
        self.assertTrue(dpf("foo/bar/abc/some.txt"))
        self.assertTrue(dpf("foo\\bar\\abc\\some.txt"))
        #
        self.assertFalse(dpf("abc/abc/bar/some.txt"))
        self.assertFalse(dpf("abc\\abc\\bar\\some.txt"))
        self.assertFalse(dpf("abc/abc/bar/abc.abc"))
        self.assertFalse(dpf("abc\\abc\\bar\\abc.abc"))

    def test_last_modified_filter(self):
        file_name = os.path.realpath(__file__)

        lmaf = rf.last_modified_after_predicate()
        self.assertTrue(lmaf(file_name))

        lmaf = rf.last_modified_after_predicate(3000000000)
        # valid until 2065-01-24 06:20:00
        self.assertFalse(lmaf(file_name))

        lmaf = rf.last_modified_after_predicate("2016-08-01")
        self.assertTrue(lmaf(file_name))

    def test_example(self):
        import rspub.util.resourcefilter as rf

        dir_ends_with_abc = rf.directory_pattern_predicate("abc$")
        assert dir_ends_with_abc("/foo/bar/folder_abc/my_resource.txt")
        assert not dir_ends_with_abc("/foo/bar/folder_def/my_resource.txt")

        xml_file = rf.filename_pattern_predicate(".xml$")
        assert xml_file("my_resource.xml")
        assert not xml_file("my_resource.txt")

        import rspub.util.gates as lf

        xml_files_in_abc = lf.and_(dir_ends_with_abc, xml_file)
        assert xml_files_in_abc("/foo/bar/folder_abc/my_resource.xml")
        assert not xml_files_in_abc("/foo/bar/folder_abc/my_resource.txt")
        assert not xml_files_in_abc("/foo/bar/folder_def/my_resource.xml")

        recent = rf.last_modified_after_predicate("2016-08-01")

        includes = [xml_files_in_abc]
        excludes = [recent]
        resource_gate = lf.gate(includes, excludes)
        # print(type(resource_gate))

    @unittest.skipUnless(on_windows(), "Only tested on Windows.")
    def test_example_windows(self):
        import rspub.util.resourcefilter as rf

        dir_ends_with_abc = rf.directory_pattern_predicate("abc$")
        assert dir_ends_with_abc("/foo/bar/folder_abc/my_resource.txt")
        assert not dir_ends_with_abc("/foo/bar/folder_def/my_resource.txt")

        xml_file = rf.filename_pattern_predicate(".xml$")
        assert xml_file("my_resource.xml")
        assert not xml_file("my_resource.txt")

        import rspub.util.gates as lf

        xml_files_in_abc = lf.and_(dir_ends_with_abc, xml_file)
        assert xml_files_in_abc("/foo/bar/folder_abc/my_resource.xml")
        assert not xml_files_in_abc("/foo/bar/folder_abc/my_resource.txt")
        assert not xml_files_in_abc("/foo/bar/folder_def/my_resource.xml")

        assert xml_files_in_abc("c:\\foo\\bar\\folder_abc\\my_resource.xml")
        assert not xml_files_in_abc("c:\\foo\\bar\\folder_abc\\my_resource.txt")
        assert not xml_files_in_abc("c:\\foo\\bar\\folder_def\\my_resource.xml")

        recent = rf.last_modified_after_predicate("2016-08-01")

        includes = [xml_files_in_abc]
        excludes = [recent]
        resource_gate = lf.gate(includes, excludes)
        # print(type(resource_gate))

    @unittest.skipUnless(on_windows(), "Only tested on Windows.")
    def test_windows_to_unix(self):
        path = os.path.expanduser("~")
        dpf = rf.directory_pattern_predicate("^" + path)
        self.assertTrue(dpf(os.path.join(path, "bla")))

        dpf = rf.directory_pattern_predicate("^C:\\Users")
        self.assertTrue(dpf(os.path.join(path, "bla")))









[(11, 'platform.system', 'platform.system', False, 'import platform\n'), (18, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (29, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (37, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (43, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (51, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (64, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (69, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (74, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (84, 'os.path.realpath', 'os.path.realpath', False, 'import os\n'), (86, 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (89, 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (93, 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (99, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (103, 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (109, 'rspub.util.gates.and_', 'lf.and_', True, 'import rspub.util.gates as lf\n'), (114, 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (118, 'rspub.util.gates.gate', 'lf.gate', True, 'import rspub.util.gates as lf\n'), (125, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (129, 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (135, 'rspub.util.gates.and_', 'lf.and_', True, 'import rspub.util.gates as lf\n'), (144, 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (148, 'rspub.util.gates.gate', 'lf.gate', True, 'import rspub.util.gates as lf\n'), (153, 'os.path.expanduser', 'os.path.expanduser', False, 'import os\n'), (154, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (157, 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', True, 'import rspub.util.resourcefilter as rf\n'), (155, 'os.path.join', 'os.path.join', False, 'import os\n'), (158, 'os.path.join', 'os.path.join', False, 'import os\n')]