コード例 #1
0
    def test_matcher_match_preconditions(self):
        mtcher = datetime_glob.Matcher('/some/./path/*%Y-%m-%dT%H-%M-%SZ.jpg')

        # yapf: disable
        table = [
            ('/', ValueError),
            ('', ValueError),
            ('/some/../path', ValueError),
            ('some/relative/path', ValueError),
            ('/some//path', None),
            ('/some/./path', None)
        ]
        # yapf: enable

        for path, expected_exception in table:
            if expected_exception is not None:
                with self.assertRaises(expected_exception):
                    _ = mtcher.match(path=path)

                with self.assertRaises(expected_exception):
                    _ = mtcher.match(path=pathlib.Path(path))

            else:
                _ = mtcher.match(path=path)
                _ = mtcher.match(path=pathlib.Path(path))

        relative_mtcher = datetime_glob.Matcher(
            'some/./path/*%Y-%m-%dT%H-%M-%SZ.jpg')
        with self.assertRaises(ValueError):
            relative_mtcher.match('/some/absolute/path')
コード例 #2
0
    def test_matcher(self):
        relative_pattern = 'some/./path/*%Y-%m-%dT%H-%M-%SZ.jpg'

        # yapf: disable
        table = [
            ('some/path/ooo2016-07-03T21-22-23Z.jpg', datetime_glob.Match(2016, 7, 3, 21, 22, 23)),
            ('some/path/2016-07-03T21-22-23Z.jpg', datetime_glob.Match(2016, 7, 3, 21, 22, 23)),
            ('some/./path/2016-07-03T21-22-23Z.jpg', datetime_glob.Match(2016, 7, 3, 21, 22, 23)),
            ('some//path/2016-07-03T21-22-23Z.jpg', datetime_glob.Match(2016, 7, 3, 21, 22, 23)),
            ('some//path/2016-07-03T21-22-23Z.jpg', datetime_glob.Match(2016, 7, 3, 21, 22, 23)),
            ('some/other/path/2016-07-03T21-22-23Z.jpg', None),
            ('some//path/2016-07-03', None)
        ]
        # yapf: enable

        for relative_path, expected_match in table:
            # test both absolute and relative paths
            abs_matcher = datetime_glob.Matcher(
                pattern='/{}'.format(relative_pattern))
            abs_pth = '/{}'.format(relative_path)

            self.assertTrue(
                match_equal(match=abs_matcher.match(path=abs_pth),
                            other=expected_match))

            rel_matcher = datetime_glob.Matcher(pattern=relative_pattern)
            self.assertTrue(
                match_equal(match=rel_matcher.match(path=relative_path),
                            other=expected_match))
コード例 #3
0
    def test_match_conversion(self) -> None:
        mtcher = datetime_glob.Matcher(
            pattern='/some/path/%Y-%m-%dT%H-%M-%S.%fZ.jpg')
        mtch = mtcher.match(path='/some/path/2016-12-02T03-04-05.123456Z.jpg')

        self.assertIsNotNone(mtch)
        assert mtch is not None  # needed for mypy

        self.assertEqual(mtch.as_datetime(),
                         datetime.datetime(2016, 12, 2, 3, 4, 5, 123456))
        self.assertEqual(mtch.as_maybe_datetime(),
                         datetime.datetime(2016, 12, 2, 3, 4, 5, 123456))
        self.assertEqual(mtch.as_date(), datetime.date(2016, 12, 2))
        self.assertEqual(mtch.as_maybe_date(), datetime.date(2016, 12, 2))
        self.assertEqual(mtch.as_time(), datetime.time(3, 4, 5, 123456))

        # match time without date
        mtcher = datetime_glob.Matcher(pattern='/some/path/%H-%M-%S.jpg')
        mtch = mtcher.match(path='/some/path/03-04-05.jpg')

        self.assertIsNotNone(mtch)
        assert mtch is not None  # needed for mypy

        with self.assertRaises(ValueError):
            _ = mtch.as_datetime()
            _ = mtch.as_date()

        self.assertIsNone(mtch.as_maybe_datetime())
        self.assertIsNone(mtch.as_maybe_date())

        self.assertEqual(mtch.as_time(), datetime.time(3, 4, 5, 0))
コード例 #4
0
    def test_sort_listdir(self) -> None:
        with tempfile.TemporaryDirectory() as tempdir:
            pth = pathlib.Path(tempdir)
            (pth / 'some-description-20.3.2016.txt').write_text('tested')
            (pth / 'other-description-7.4.2016.txt').write_text('tested')
            (pth / 'yet-another-description-1.1.2016.txt').write_text('tested')

            matcher = datetime_glob.Matcher(pattern='*%-d.%-m.%Y.txt')
            subpths_matches = [(subpth, matcher.match(subpth.name))
                               for subpth in pth.iterdir()]

            dtimes_subpths = [
            ]  # type: List[Tuple[datetime.datetime, pathlib.Path]]
            for subpth, mtch in subpths_matches:
                self.assertIsNotNone(subpth)
                assert subpth is not None  # needed for mypy

                self.assertIsNotNone(mtch)
                assert mtch is not None  # needed for mypy

                dtimes_subpths.append((mtch.as_datetime(), subpth))

            subpths = [subpth for _, subpth in sorted(dtimes_subpths)]

            # yapf: disable
            expected = [
                pth / 'yet-another-description-1.1.2016.txt',
                pth / 'some-description-20.3.2016.txt',
                pth / 'other-description-7.4.2016.txt'
            ]
            # yapf: enable

            self.assertListEqual(subpths, expected)
コード例 #5
0
    def test_matcher_preconditions(self):
        # yapf: disable
        table = [
            ('/', ValueError),
            ('', ValueError),
            ('/some/../path', ValueError),
            ('/some//path/*', None),
            ('/some/./path/*', None)
        ]
        # yapf: enable

        for pattern, expected_exception in table:
            if expected_exception is not None:
                with self.assertRaises(expected_exception):
                    _ = datetime_glob.Matcher(pattern=pattern)
            else:
                _ = datetime_glob.Matcher(pattern=pattern)
コード例 #6
0
    def test_match_multiple_definition(self):
        mtcher = datetime_glob.Matcher(
            pattern='/some/path/%Y-%m-%d/%Y-%m-%dT%H-%M-%S.%fZ.jpg')
        mtch = mtcher.match(
            path='/some/path/2016-12-02/2016-12-02T03-04-05.123456Z.jpg')
        self.assertEqual(mtch.as_datetime(),
                         datetime.datetime(2016, 12, 2, 3, 4, 5, 123456))

        mtch = mtcher.match(
            path='/some/path/2017-04-12/2016-12-02T03-04-05.123456Z.jpg')
        self.assertIsNone(mtch)
コード例 #7
0
    def test_sort_listdir(self):
        with tempfile.TemporaryDirectory() as tempdir:
            pth = pathlib.Path(tempdir)
            (pth / 'some-description-20.3.2016.txt').write_text('tested')
            (pth / 'other-description-7.4.2016.txt').write_text('tested')
            (pth / 'yet-another-description-1.1.2016.txt').write_text('tested')

            matcher = datetime_glob.Matcher(pattern='*%-d.%-m.%Y.txt')
            subpths_matches = [(subpth, matcher.match(subpth.name))
                               for subpth in pth.iterdir()]
            dtimes_subpths = [(mtch.as_datetime(), subpth)
                              for subpth, mtch in subpths_matches]

            subpths = [subpth for _, subpth in sorted(dtimes_subpths)]

            # yapf: disable
            expected = [
                pth / 'yet-another-description-1.1.2016.txt',
                pth / 'some-description-20.3.2016.txt',
                pth / 'other-description-7.4.2016.txt'
            ]
            # yapf: enable

            self.assertListEqual(subpths, expected)
コード例 #8
0
 def __init__(self, db):
     self.date_matcher = datetime_glob.Matcher(
         pattern='%Y-%m-%dT%H:%M:%S-*')
     self.db = db
     self.helper = ScraperUtil()