#! /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")))









[((162, 179), 'platform.system', 'platform.system', ([], {}), '()\n', (177, 179), False, 'import platform\n'), ((319, 351), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', ([], {}), '()\n', (349, 351), True, 'import rspub.util.resourcefilter as rf\n'), ((711, 748), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc"""'], {}), "('abc')\n", (741, 748), True, 'import rspub.util.resourcefilter as rf\n'), ((1000, 1039), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""^/abc"""'], {}), "('^/abc')\n", (1030, 1039), True, 'import rspub.util.resourcefilter as rf\n'), ((1177, 1215), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc$"""'], {}), "('abc$')\n", (1207, 1215), True, 'import rspub.util.resourcefilter as rf\n'), ((1520, 1557), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc"""'], {}), "('abc')\n", (1550, 1557), True, 'import rspub.util.resourcefilter as rf\n'), ((2043, 2082), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""^/abc"""'], {}), "('^/abc')\n", (2073, 2082), True, 'import rspub.util.resourcefilter as rf\n'), ((2219, 2261), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""^c:\\\\abc"""'], {}), "('^c:\\\\abc')\n", (2249, 2261), True, 'import rspub.util.resourcefilter as rf\n'), ((2394, 2432), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc$"""'], {}), "('abc$')\n", (2424, 2432), True, 'import rspub.util.resourcefilter as rf\n'), ((2834, 2860), 'os.path.realpath', 'os.path.realpath', (['__file__'], {}), '(__file__)\n', (2850, 2860), False, 'import os\n'), ((2877, 2911), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', ([], {}), '()\n', (2909, 2911), True, 'import rspub.util.resourcefilter as rf\n'), ((2969, 3013), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', (['(3000000000)'], {}), '(3000000000)\n', (3001, 3013), True, 'import rspub.util.resourcefilter as rf\n'), ((3114, 3160), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', (['"""2016-08-01"""'], {}), "('2016-08-01')\n", (3146, 3160), True, 'import rspub.util.resourcefilter as rf\n'), ((3307, 3345), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc$"""'], {}), "('abc$')\n", (3337, 3345), True, 'import rspub.util.resourcefilter as rf\n'), ((3514, 3552), 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', (['""".xml$"""'], {}), "('.xml$')\n", (3543, 3552), True, 'import rspub.util.resourcefilter as rf\n'), ((3710, 3746), 'rspub.util.gates.and_', 'lf.and_', (['dir_ends_with_abc', 'xml_file'], {}), '(dir_ends_with_abc, xml_file)\n', (3717, 3746), True, 'import rspub.util.gates as lf\n'), ((3986, 4032), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', (['"""2016-08-01"""'], {}), "('2016-08-01')\n", (4018, 4032), True, 'import rspub.util.resourcefilter as rf\n'), ((4124, 4151), 'rspub.util.gates.gate', 'lf.gate', (['includes', 'excludes'], {}), '(includes, excludes)\n', (4131, 4151), True, 'import rspub.util.gates as lf\n'), ((4368, 4406), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""abc$"""'], {}), "('abc$')\n", (4398, 4406), True, 'import rspub.util.resourcefilter as rf\n'), ((4575, 4613), 'rspub.util.resourcefilter.filename_pattern_predicate', 'rf.filename_pattern_predicate', (['""".xml$"""'], {}), "('.xml$')\n", (4604, 4613), True, 'import rspub.util.resourcefilter as rf\n'), ((4771, 4807), 'rspub.util.gates.and_', 'lf.and_', (['dir_ends_with_abc', 'xml_file'], {}), '(dir_ends_with_abc, xml_file)\n', (4778, 4807), True, 'import rspub.util.gates as lf\n'), ((5287, 5333), 'rspub.util.resourcefilter.last_modified_after_predicate', 'rf.last_modified_after_predicate', (['"""2016-08-01"""'], {}), "('2016-08-01')\n", (5319, 5333), True, 'import rspub.util.resourcefilter as rf\n'), ((5425, 5452), 'rspub.util.gates.gate', 'lf.gate', (['includes', 'excludes'], {}), '(includes, excludes)\n', (5432, 5452), True, 'import rspub.util.gates as lf\n'), ((5608, 5631), 'os.path.expanduser', 'os.path.expanduser', (['"""~"""'], {}), "('~')\n", (5626, 5631), False, 'import os\n'), ((5646, 5688), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (["('^' + path)"], {}), "('^' + path)\n", (5676, 5688), True, 'import rspub.util.resourcefilter as rf\n'), ((5760, 5804), 'rspub.util.resourcefilter.directory_pattern_predicate', 'rf.directory_pattern_predicate', (['"""^C:\\\\Users"""'], {}), "('^C:\\\\Users')\n", (5790, 5804), True, 'import rspub.util.resourcefilter as rf\n'), ((5717, 5742), 'os.path.join', 'os.path.join', (['path', '"""bla"""'], {}), "(path, 'bla')\n", (5729, 5742), False, 'import os\n'), ((5833, 5858), 'os.path.join', 'os.path.join', (['path', '"""bla"""'], {}), "(path, 'bla')\n", (5845, 5858), False, 'import os\n')]