#! /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, 12, 11, 29), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n'), ((18, 14, 18, 46), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({}, {}), '()', True, 'import rspub.util.resourcefilter as rf\n'), ((29, 14, 29, 51), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(29, 45, 29, 50): '"""abc"""'}, {}), "('abc')", True, 'import rspub.util.resourcefilter as rf\n'), ((37, 14, 37, 53), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(37, 45, 37, 52): '"""^/abc"""'}, {}), "('^/abc')", True, 'import rspub.util.resourcefilter as rf\n'), ((43, 14, 43, 52), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(43, 45, 43, 51): '"""abc$"""'}, {}), "('abc$')", True, 'import rspub.util.resourcefilter as rf\n'), ((51, 14, 51, 51), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(51, 45, 51, 50): '"""abc"""'}, {}), "('abc')", True, 'import rspub.util.resourcefilter as rf\n'), ((64, 14, 64, 53), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(64, 45, 64, 52): '"""^/abc"""'}, {}), "('^/abc')", True, 'import rspub.util.resourcefilter as rf\n'), ((69, 14, 69, 56), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(69, 45, 69, 55): '"""^c:\\\\abc"""'}, {}), "('^c:\\\\abc')", True, 'import rspub.util.resourcefilter as rf\n'), ((74, 14, 74, 52), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(74, 45, 74, 51): '"""abc$"""'}, {}), "('abc$')", True, 'import rspub.util.resourcefilter as rf\n'), ((84, 20, 84, 46), 'os.path.realpath', 'os.path.realpath', ({(84, 37, 84, 45): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((86, 15, 86, 49), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ({}, {}), '()', True, 'import rspub.util.resourcefilter as rf\n'), ((89, 15, 89, 59), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ({(89, 48, 89, 58): '3000000000'}, {}), '(3000000000)', True, 'import rspub.util.resourcefilter as rf\n'), ((93, 15, 93, 61), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ({(93, 48, 93, 60): '"""2016-08-01"""'}, {}), "('2016-08-01')", True, 'import rspub.util.resourcefilter as rf\n'), ((99, 28, 99, 66), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(99, 59, 99, 65): '"""abc$"""'}, {}), "('abc$')", True, 'import rspub.util.resourcefilter as rf\n'), ((103, 19, 103, 57), 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', ({(103, 49, 103, 56): '""".xml$"""'}, {}), "('.xml$')", True, 'import rspub.util.resourcefilter as rf\n'), ((109, 27, 109, 63), 'rspub.util.gates.and_', 'lf.and_', ({(109, 35, 109, 52): 'dir_ends_with_abc', (109, 54, 109, 62): 'xml_file'}, {}), '(dir_ends_with_abc, xml_file)', True, 'import rspub.util.gates as lf\n'), ((114, 17, 114, 63), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ({(114, 50, 114, 62): '"""2016-08-01"""'}, {}), "('2016-08-01')", True, 'import rspub.util.resourcefilter as rf\n'), ((118, 24, 118, 51), 'rspub.util.gates.gate', 'lf.gate', ({(118, 32, 118, 40): 'includes', (118, 42, 118, 50): 'excludes'}, {}), '(includes, excludes)', True, 'import rspub.util.gates as lf\n'), ((125, 28, 125, 66), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(125, 59, 125, 65): '"""abc$"""'}, {}), "('abc$')", True, 'import rspub.util.resourcefilter as rf\n'), ((129, 19, 129, 57), 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', ({(129, 49, 129, 56): '""".xml$"""'}, {}), "('.xml$')", True, 'import rspub.util.resourcefilter as rf\n'), ((135, 27, 135, 63), 'rspub.util.gates.and_', 'lf.and_', ({(135, 35, 135, 52): 'dir_ends_with_abc', (135, 54, 135, 62): 'xml_file'}, {}), '(dir_ends_with_abc, xml_file)', True, 'import rspub.util.gates as lf\n'), ((144, 17, 144, 63), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ({(144, 50, 144, 62): '"""2016-08-01"""'}, {}), "('2016-08-01')", True, 'import rspub.util.resourcefilter as rf\n'), ((148, 24, 148, 51), 'rspub.util.gates.gate', 'lf.gate', ({(148, 32, 148, 40): 'includes', (148, 42, 148, 50): 'excludes'}, {}), '(includes, excludes)', True, 'import rspub.util.gates as lf\n'), ((153, 15, 153, 38), 'os.path.expanduser', 'os.path.expanduser', ({(153, 34, 153, 37): '"""~"""'}, {}), "('~')", False, 'import os\n'), ((154, 14, 154, 56), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(154, 45, 154, 55): "'^' + path"}, {}), "('^' + path)", True, 'import rspub.util.resourcefilter as rf\n'), ((157, 14, 157, 58), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ({(157, 45, 157, 57): '"""^C:\\\\Users"""'}, {}), "('^C:\\\\Users')", True, 'import rspub.util.resourcefilter as rf\n'), ((155, 28, 155, 53), 'os.path.join', 'os.path.join', ({(155, 41, 155, 45): 'path', (155, 47, 155, 52): '"""bla"""'}, {}), "(path, 'bla')", False, 'import os\n'), ((158, 28, 158, 53), 'os.path.join', 'os.path.join', ({(158, 41, 158, 45): 'path', (158, 47, 158, 52): '"""bla"""'}, {}), "(path, 'bla')", False, 'import os\n')]