File: glob2regex.py

package info (click to toggle)
dfvfs 20201219-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 284,900 kB
  • sloc: python: 30,025; vhdl: 1,921; sh: 465; makefile: 16
file content (64 lines) | stat: -rw-r--r-- 1,964 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the Windows Registry searcher."""

from __future__ import unicode_literals

import fnmatch
import unittest

from dfvfs.lib import glob2regex

from tests import test_lib


class Glob2RegexTest(test_lib.BaseTestCase):
  """Tests for the glob to regular expression conversion function."""

  _TEST_PATTERNS = [
      'plain.txt', '*.txt', 'plain?.txt', 'plain[?].txt', 'plai[nN].txt',
      'plai[!nN].txt', 'plai[nN.txt', 'plain.(jpg|txt)', '.^$*+?{}\\[]|()',
      'plai[^nN].txt', '!%&`~:', '[.^$*+?{}\\|()]', '[\\]]', '[]]']

  def _Glob2Regex(self, glob_pattern):
    """Converts a glob pattern to a regular expression.

    Args:
      glob_pattern (str): glob pattern.

    Returns:
      str: regular expression of the glob pattern.

    Raises:
      ValueError: if the glob pattern cannot be converted.
    """
    # fnmatch.translate() is used to convert a glob into a regular
    # expression. For Python 3.5 and earlier, the resulting regular
    # expression has "\Z(?ms)" defined at its end, which needs to be
    # removed and escapes the forward slash "/", which needs to be
    # undone. Python 3.6 puts the options at the beginning of the
    # regular expression instead.

    fnmatch_regex = fnmatch.translate(glob_pattern)

    for suffix in (r'\Z(?ms)', r')\Z'):
      if fnmatch_regex.endswith(suffix):
        fnmatch_regex = fnmatch_regex[:-len(suffix)]
      if fnmatch_regex.startswith('(?s:'):
        fnmatch_regex = fnmatch_regex[len('(?s:'):]

    return fnmatch_regex.replace('\\/', '/')

  def testGlob2Regex(self):
    """Tests the Glob2Regex function."""
    for glob_pattern in self._TEST_PATTERNS:
      regex = glob2regex.Glob2Regex(glob_pattern)
      expected_regex = self._Glob2Regex(glob_pattern)
      self.assertEqual(regex, expected_regex)

    with self.assertRaises(ValueError):
      glob2regex.Glob2Regex(None)


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