Python re._pattern_type() Examples

The following are code examples for showing how to use re._pattern_type(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: beavy   Author: beavyHQ   File: fetching.py    Mozilla Public License 2.0 6 votes vote down vote up
def _matches(data, **kwargs):
    for key, match in kwargs.items():
        try:
            given_value = data[key]
        except KeyError:
            return False
        else:
            if isinstance(match, re._pattern_type):
                if not match.match(given_value):
                    return False
            elif callable(match):
                if not match(given_value):
                    return False
            else:
                if match != given_value:
                    return False
    return True 
Example 2
Project: python-aptly   Author: tcpcloud   File: __init__.py    GNU General Public License v2.0 6 votes vote down vote up
def _publish_match(self, publish, names=False, name_only=False):
        """
        Check if publish name matches list of names or regex patterns
        """
        if names:
            for name in names:
                if not name_only and isinstance(name, re._pattern_type):
                    if re.match(name, publish.name):
                        return True
                else:
                    operand = name if name_only else [name, './%s' % name]
                    if publish in operand:
                        return True
            return False
        else:
            return True 
Example 3
Project: tcf   Author: intel   File: testing.py    Apache License 2.0 6 votes vote down vote up
def _check_log_line_for_issues(self, cnt, line):
        line = line.strip()
        if self.error_regex.search(line):
            for exclude in self.errors_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
            raise tcfl.tc.failed_e(
                "ttbd[%s]: errors found #%d: %s" % (self.port, cnt, line),
                { 'stdout': open(self.stdout), 'stderr': open(self.stderr) })
        if self.warning_regex.search(line):
            for exclude in self.warnings_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
                raise tcfl.tc.failed_e(
                    "ttbd[%s]: error found #%d: %s" % (self.port, cnt, line),
                    { 'stdout': self.stdout, 'stderr': self.stderr }) 
Example 4
Project: tcf   Author: intel   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def property_is_user(self, name):
        """
        Return *True* if a property is considered a user property (no
        admin rights are needed to set it or read it).

        :returns: bool
        """
        for prop in self.properties_user:
            if isinstance(prop, basestring):
                if prop == name:
                    return True
                continue
            if isinstance(prop, re._pattern_type):
                if prop.match(name):
                    return True
                continue
            raise AssertionError, \
                "user property %s: not a string or regex, but %s" \
                % (prop, type(prop).__name__)
        return False 
Example 5
Project: tcf   Author: intel   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def property_keep_value(self, name):
        """
        Return *True* if a user property's value needs to be kept.
        """
        for prop in self.properties_keep_on_release:
            if isinstance(prop, basestring):
                if prop == name:
                    return True
                continue
            if isinstance(prop, re._pattern_type):
                if prop.match(name):
                    return True
                continue
            raise AssertionError, \
                "user property %s: not a string or regex, but %s" \
                % (prop, type(prop).__name__)
        return False 
Example 6
Project: tcf   Author: intel   File: fastboot.py    Apache License 2.0 6 votes vote down vote up
def get_list(self, _target, _who, _args, _user_path):
        data = dict()
        for command, param_list in self.allowed_commands.iteritems():
            _param_list = []
            count = 0
            for param in param_list:
                if isinstance(param, tuple):
                    value = param[0]
                    if isinstance(value, basestring):
                        _param_list.append(value)
                    elif isinstance(value, re._pattern_type):
                        _param_list.append(value.pattern)
                    else:
                        assert(
                            "BUG: bad type %s in item #%d, expected "
                            "str or re._pattern_type"
                            % (type(value).__name__, count))
                else:
                    _param_list.append(param)
                count += 1
            params = " ".join(_param_list)
            data[command] = params
        return { 'commands': data } 
Example 7
Project: tcf   Author: intel   File: testing.py    Apache License 2.0 6 votes vote down vote up
def _check_log_line_for_issues(self, cnt, line):
        line = line.strip()
        if self.error_regex.search(line):
            for exclude in self.errors_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
            raise tcfl.tc.failed_e(
                "ttbd[%s]: errors found #%d: %s" % (self.port, cnt, line),
                { 'stdout': open(self.stdout), 'stderr': open(self.stderr) })
        if self.warning_regex.search(line):
            for exclude in self.warnings_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
                raise tcfl.tc.failed_e(
                    "ttbd[%s]: error found #%d: %s" % (self.port, cnt, line),
                    { 'stdout': self.stdout, 'stderr': self.stderr }) 
Example 8
Project: tcf   Author: intel   File: testing.py    Apache License 2.0 6 votes vote down vote up
def _check_log_line_for_issues(self, cnt, line):
        line = line.strip()
        if self.error_regex.search(line):
            for exclude in self.errors_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
            raise tcfl.tc.failed_e(
                "ttbd[%s]: errors found #%d: %s" % (self.port, cnt, line),
                { 'stdout': open(self.stdout), 'stderr': open(self.stderr) })
        if self.warning_regex.search(line):
            for exclude in self.warnings_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
                raise tcfl.tc.failed_e(
                    "ttbd[%s]: error found #%d: %s" % (self.port, cnt, line),
                    { 'stdout': self.stdout, 'stderr': self.stderr }) 
Example 9
Project: tcf   Author: intel   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def property_is_user(self, name):
        """
        Return *True* if a property is considered a user property (no
        admin rights are needed to set it or read it).

        :returns: bool
        """
        for prop in self.properties_user:
            if isinstance(prop, basestring):
                if prop == name:
                    return True
                continue
            if isinstance(prop, re._pattern_type):
                if prop.match(name):
                    return True
                continue
            raise AssertionError, \
                "user property %s: not a string or regex, but %s" \
                % (prop, type(prop).__name__)
        return False 
Example 10
Project: tcf   Author: intel   File: fastboot.py    Apache License 2.0 6 votes vote down vote up
def get_list(self, _target, _who, _args, _user_path):
        data = dict()
        for command, param_list in self.allowed_commands.iteritems():
            _param_list = []
            count = 0
            for param in param_list:
                if isinstance(param, tuple):
                    value = param[0]
                    if isinstance(value, basestring):
                        _param_list.append(value)
                    elif isinstance(value, re._pattern_type):
                        _param_list.append(value.pattern)
                    else:
                        assert(
                            "BUG: bad type %s in item #%d, expected "
                            "str or re._pattern_type"
                            % (type(value).__name__, count))
                else:
                    _param_list.append(param)
                count += 1
            params = " ".join(_param_list)
            data[command] = params
        return { 'commands': data } 
Example 11
Project: tcf   Author: intel   File: testing.py    Apache License 2.0 6 votes vote down vote up
def _check_log_line_for_issues(self, cnt, line):
        line = line.strip()
        if self.error_regex.search(line):
            for exclude in self.errors_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
            raise tcfl.tc.failed_e(
                "ttbd[%s]: errors found #%d: %s" % (self.port, cnt, line),
                { 'stdout': open(self.stdout), 'stderr': open(self.stderr) })
        if self.warning_regex.search(line):
            for exclude in self.warnings_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
                raise tcfl.tc.failed_e(
                    "ttbd[%s]: error found #%d: %s" % (self.port, cnt, line),
                    { 'stdout': self.stdout, 'stderr': self.stderr }) 
Example 12
Project: tcf   Author: intel   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def property_is_user(self, name):
        """
        Return *True* if a property is considered a user property (no
        admin rights are needed to set it or read it).

        :returns: bool
        """
        for prop in self.properties_user:
            if isinstance(prop, basestring):
                if prop == name:
                    return True
                continue
            if isinstance(prop, re._pattern_type):
                if prop.match(name):
                    return True
                continue
            raise AssertionError, \
                "user property %s: not a string or regex, but %s" \
                % (prop, type(prop).__name__)
        return False 
Example 13
Project: tcf   Author: intel   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def property_keep_value(self, name):
        """
        Return *True* if a user property's value needs to be kept.
        """
        for prop in self.properties_keep_on_release:
            if isinstance(prop, basestring):
                if prop == name:
                    return True
                continue
            if isinstance(prop, re._pattern_type):
                if prop.match(name):
                    return True
                continue
            raise AssertionError, \
                "user property %s: not a string or regex, but %s" \
                % (prop, type(prop).__name__)
        return False 
Example 14
Project: tcf   Author: intel   File: fastboot.py    Apache License 2.0 6 votes vote down vote up
def get_list(self, _target, _who, _args, _user_path):
        data = dict()
        for command, param_list in self.allowed_commands.iteritems():
            _param_list = []
            count = 0
            for param in param_list:
                if isinstance(param, tuple):
                    value = param[0]
                    if isinstance(value, basestring):
                        _param_list.append(value)
                    elif isinstance(value, re._pattern_type):
                        _param_list.append(value.pattern)
                    else:
                        assert(
                            "BUG: bad type %s in item #%d, expected "
                            "str or re._pattern_type"
                            % (type(value).__name__, count))
                else:
                    _param_list.append(param)
                count += 1
            params = " ".join(_param_list)
            data[command] = params
        return { 'commands': data } 
Example 15
Project: tcf   Author: intel   File: testing.py    Apache License 2.0 6 votes vote down vote up
def _check_log_line_for_issues(self, cnt, line):
        line = line.strip()
        if self.error_regex.search(line):
            for exclude in self.errors_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
            raise tcfl.tc.failed_e(
                "ttbd[%s]: errors found #%d: %s" % (self.port, cnt, line),
                { 'stdout': open(self.stdout), 'stderr': open(self.stderr) })
        if self.warning_regex.search(line):
            for exclude in self.warnings_ignore:
                if isinstance(exclude, re._pattern_type) \
                   and exclude.search(line):
                    return
                elif exclude in line:
                    return
                raise tcfl.tc.failed_e(
                    "ttbd[%s]: error found #%d: %s" % (self.port, cnt, line),
                    { 'stdout': self.stdout, 'stderr': self.stderr }) 
Example 16
Project: filters   Author: eflglobal   File: string.py    MIT License 6 votes vote down vote up
def __init__(self, pattern):
        # type: (Union[Text, regex._pattern_type, re._pattern_type]) -> None
        """
        :param pattern:
            String pattern, or pre-compiled regex.

            IMPORTANT:  If you specify your own compiled regex, be sure to
            add the ``UNICODE`` flag for Unicode support!
        """
        super(Regex, self).__init__()

        self.regex = (
            pattern
                if isinstance(pattern, (regex._pattern_type, re._pattern_type))
                else regex.compile(pattern, regex.UNICODE)
        ) 
Example 17
Project: filters   Author: eflglobal   File: string.py    MIT License 6 votes vote down vote up
def __init__(self, pattern, keys=None):
        # type: (Union[Text, regex._pattern_type, re._pattern_type], Optional[Sequence[Text]]) -> None
        """
        :param pattern:
            Regex used to split incoming string values.

            IMPORTANT:  If you specify your own compiled regex, be sure
            to add the ``UNICODE`` flag for Unicode support!

        :param keys:
            If set, the resulting list will be converted into an
            OrderedDict, using the specified keys.

            IMPORTANT:  If ``keys`` is set, the split value's length
            must be less than or equal to ``len(keys)``.
        """
        super(Split, self).__init__()

        self.regex = (
            pattern
                if isinstance(pattern, (regex._pattern_type, re._pattern_type))
                else regex.compile(pattern, regex.UNICODE)
        )

        self.keys = keys 
Example 18
Project: simple_mud   Author: tobynance   File: telnet.py    MIT License 6 votes vote down vote up
def check_predicate(self, predicate, data):
        if isinstance(predicate, basestring):
            return predicate == data
        elif isinstance(predicate, list) or isinstance(predicate, tuple):
            return data in predicate
        elif isinstance(predicate, bool):
            return predicate
        elif isinstance(predicate, re._pattern_type):
            if predicate.match(data):
                return True
            else:
                return False
        else:
            raise ValueError("I don't know how to use the predicate '%s' of type '%s'" % predicate, type(predicate))


######################################################################## 
Example 19
Project: Weiss   Author: WangWenjun559   File: utils.py    Apache License 2.0 6 votes vote down vote up
def preprocess_string(self, html_string):
        """Here we can modify the text, before it's parsed."""
        if not html_string:
            return html_string
        # Remove silly  » and – chars.
        html_string = html_string.replace(u' \xbb', u'')
        html_string = html_string.replace(u'–', u'-')
        try:
            preprocessors = self.preprocessors
        except AttributeError:
            return html_string
        for src, sub in preprocessors:
            # re._pattern_type is present only since Python 2.5.
            if callable(getattr(src, 'sub', None)):
                html_string = src.sub(sub, html_string)
            elif isinstance(src, str):
                html_string = html_string.replace(src, sub)
            elif callable(src):
                try:
                    html_string = src(html_string)
                except Exception, e:
                    _msg = '%s: caught exception preprocessing html'
                    self._logger.error(_msg, self._cname, exc_info=True)
                    continue
        ##print html_string.encode('utf8') 
Example 20
Project: cloudera-cassandra   Author: elisska   File: run_cqlsh.py    Apache License 2.0 6 votes vote down vote up
def read_until(self, until, blksize=4096, timeout=None,
                   flags=0, ptty_timeout=None):
        if not isinstance(until, re._pattern_type):
            until = re.compile(until, flags)
        got = self.readbuf
        self.readbuf = ''
        with timing_out(timeout):
            while True:
                val = self.read(blksize, ptty_timeout)
                cqlshlog.debug("read %r from subproc" % (val,))
                if val == '':
                    raise EOFError("'until' pattern %r not found" % (until.pattern,))
                got += val
                m = until.search(got)
                if m is not None:
                    self.readbuf = got[m.end():]
                    got = got[:m.end()]
                    return got 
Example 21
Project: tfutils   Author: neuroailab   File: db_interface.py    MIT License 6 votes vote down vote up
def filter_var_list(self, var_list):
        """Filter checkpoint vars for those to be restored.

        Args:
            checkpoint_vars (list): Vars that can be restored from checkpoint.
            to_restore (list[str] or regex, optional): Selects vars to restore.

        Returns:
            list: Variables to be restored from checkpoint.

        """
        if not self.to_restore:
            return var_list
        elif isinstance(self.to_restore, re._pattern_type):
            return {name: var for name, var in var_list.items()
                    if self.to_restore.match(name)}
        elif isinstance(self.to_restore, list):
            return {name: var for name, var in var_list.items()
                    if name in self.to_restore}
        raise TypeError('to_restore ({}) unsupported.'.format(type(self.to_restore))) 
Example 22
Project: wine-ml-on-aws-lambda   Author: pierreant   File: inference.py    Apache License 2.0 6 votes vote down vote up
def is_re(obj):
    """
    Check if the object is a regex pattern instance.

    Parameters
    ----------
    obj : The object to check.

    Returns
    -------
    is_regex : bool
        Whether `obj` is a regex pattern.

    Examples
    --------
    >>> is_re(re.compile(".*"))
    True
    >>> is_re("foo")
    False
    """

    return isinstance(obj, re._pattern_type) 
Example 23
Project: ATX   Author: NetEaseGame   File: android_layout.py    Apache License 2.0 6 votes vote down vote up
def _filter_nodes(self, cond, nodes=None):
        if nodes is None:
            nodes = self.nodes
        res = []
        for n in nodes:
            match = True
            for k, v in cond.iteritems():
                attr = getattr(n, k)
                if isinstance(v, re._pattern_type) and \
                    isinstance(attr, basestring) and v.match(attr) is None:
                    match = False
                    break
                elif attr != v:
                    match = False
                    break
            if match:
                res.append(n)
        return res 
Example 24
Project: Splunking-Crime   Author: nccgroup   File: inference.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def is_re(obj):
    """
    Check if the object is a regex pattern instance.

    Parameters
    ----------
    obj : The object to check.

    Returns
    -------
    is_regex : bool
        Whether `obj` is a regex pattern.

    Examples
    --------
    >>> is_re(re.compile(".*"))
    True
    >>> is_re("foo")
    False
    """

    return isinstance(obj, re._pattern_type) 
Example 25
Project: elasticintel   Author: securityclippy   File: inference.py    GNU General Public License v3.0 6 votes vote down vote up
def is_re(obj):
    """
    Check if the object is a regex pattern instance.

    Parameters
    ----------
    obj : The object to check.

    Returns
    -------
    is_regex : bool
        Whether `obj` is a regex pattern.

    Examples
    --------
    >>> is_re(re.compile(".*"))
    True
    >>> is_re("foo")
    False
    """

    return isinstance(obj, re._pattern_type) 
Example 26
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 27
Project: restful-ben   Author: CityOfPhiladelphia   File: test_utils.py    MIT License 5 votes vote down vote up
def dict_contains(dict1, dict2):
    for key in dict2:
        if key not in dict1:
            return False
        if isinstance(dict2[key], re._pattern_type):
            if re.match(dict2[key], dict1[key]) == None:
                return False
        elif type(dict2[key]) == type:
            if type(dict1[key]) != dict2[key]:
                return False
        elif dict1[key] != dict2[key]:
            return False
    return True 
Example 28
Project: payment-card-identifier   Author: Ousret   File: card.py    MIT License 5 votes vote down vote up
def json(self):
        return json.dumps(self, default=lambda o: o.__dict__ if not isinstance(o, re._pattern_type) else o.pattern,
                          sort_keys=True, indent=4) 
Example 29
Project: payment-card-identifier   Author: Ousret   File: test_card.py    MIT License 5 votes vote down vote up
def test_card_json(self):

        my_card = VISA('4532040524589053')

        self.assertIsNotNone(my_card.json)
        self.assertIsInstance(my_card.json, str)
        self.assertIsInstance(my_card.regex, re._pattern_type)
        self.assertEqual(my_card.numbers, '4532040524589053')
        self.assertEqual(my_card.masked_numbers(), 'XXXXXXXXXXXX9053') 
Example 30
Project: jawfish   Author: war-and-code   File: test_re.py    MIT License 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 31
Project: recordlinkage   Author: J535D165   File: types.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_re(obj):
    return isinstance(obj, re._pattern_type) 
Example 32
Project: recipes-py   Author: luci   File: post_process_inputs.py    Apache License 2.0 5 votes vote down vote up
def __contains__(self, item):
    # Get a function that can be used for matching against an element
    # Command's elements will always be strings, so we'll only try to match
    # against strings or regexes
    def get_matcher(obj):
      if isinstance(obj, basestring):
        return lambda other: obj == other
      if isinstance(obj, re._pattern_type):
        return obj.search
      return None

    if isinstance(item, Iterable) and not isinstance(item, basestring):
      matchers = [get_matcher(e) for e in item]
    else:
      matchers = [get_matcher(item)]

    # If None is present in matchers, then that means item is/contains an object
    # of a type that we won't use for matching
    if any(m is None for m in matchers):
      return False

    # At this point, matchers is a list of functions that we can apply against
    # the elements of each subsequence in the list; if each matcher matches the
    # corresponding element of the subsequence then we say that the sequence of
    # strings/regexes is contained in the command
    for i in xrange(len(self) - len(matchers) + 1):
      for j, matcher in enumerate(matchers):
        if not matcher(self[i + j]):
          break
      else:
        return True
    return False 
Example 33
Project: recipes-py   Author: luci   File: magic_check_fn.py    Apache License 2.0 5 votes vote down vote up
def render_user_value(val):
  """Takes a subexpression user value, and attempts to render it in the most
  useful way possible.

  Currently this will use render_re for compiled regular expressions, and will
  fall back to repr() for everything else.

  It should be the goal of this function to return an `eval`able string that
  would yield the equivalent value in a python interpreter.
  """
  if isinstance(val, re._pattern_type):
    return render_re(val)
  return repr(val) 
Example 34
Project: package_coverage   Author: codexns   File: package_coverage.py    MIT License 5 votes vote down vote up
def run_tests(tests_module, queue, name_pattern, on_done):
    """
    Executes the tests within a module and sends the output through the queue
    for display via another thread

    :param tests_module:
        The module that contains unittest.TestCase classes to execute

    :param queue:
        A StringQueue object to send the results to

    :param name_pattern:
        None or a re._pattern_type object for matching test names against

    :param on_done:
        A callback to execute when the tests are done being run
    """

    test_classes = []
    for name, obj in inspect.getmembers(tests_module):
        if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
            test_classes.append(obj)

    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for test_class in test_classes:
        if name_pattern:
            for name in loader.getTestCaseNames(test_class):
                if name_pattern.search(name):
                    suite.addTest(test_class(name))
        else:
            suite.addTest(loader.loadTestsFromTestCase(test_class))
    verbosity = 2 if name_pattern else 1
    unittest.TextTestRunner(stream=queue, verbosity=verbosity).run(suite)

    on_done() 
Example 35
Project: PrivacyScore   Author: PrivacyScore   File: serverleak.py    GNU General Public License v3.0 5 votes vote down vote up
def process_test_data(raw_data: list, previous_results: dict) -> Dict[str, Dict[str, object]]:
    leaks = []
    result = {}
    
    url = None
    if 'url' in raw_data:
        url = raw_data['url']['data'].decode()

    for trial, pattern in TRIALS:
        if url:
            if callable(trial):
                trial = trial(url)
                if trial is None:
                    continue
        if trial not in raw_data:
            # Test raw data too old or particular request failed.
            continue
        response = json.loads(raw_data[trial]['data'].decode())
        if response['status_code'] == 200:
            # The pattern can have three different types.
            # - If it is a simple string, we only check if it is contained in the response
            if isinstance(pattern, str):
                if pattern in response['text']:
                    leaks.append(trial)
            # - If it is a RegEx object, we perform a pattern match
            elif isinstance(pattern, re._pattern_type):
                if re.match(response['text']):
                    leaks.append(trial)
            # - If it is callable, we call it with the response text and check the return value
            elif callable(pattern):
                if pattern(response['text']):
                    leaks.append(trial)

    result['leaks'] = leaks
    return result 
Example 36
Project: paramz   Author: sods   File: parameterized.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def grep_param_names(self, regexp):
        """
        create a list of parameters, matching regular expression regexp
        """
        if not isinstance(regexp, _pattern_type): regexp = compile(regexp)
        found_params = []
        def visit(innerself, regexp):
            if (innerself is not self) and regexp.match(innerself.hierarchy_name().partition('.')[2]):
                found_params.append(innerself)
        self.traverse(visit, regexp)
        return found_params 
Example 37
Project: ironpython2   Author: IronLanguages   File: test_re.py    Apache License 2.0 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 38
Project: FIR   Author: certsocietegenerale   File: links.py    GNU General Public License v3.0 5 votes vote down vote up
def register_reverse_link(self, parser_regex, url_name, model=None, reverse=None):
        """
        Registers a new parser for links using Django urlconf
        :param parser_regex: string or regex object
        :param url_name: urlconf name
        :param model: model label (app_label.model_name) or model class (used by fir_relations)
        :param reverse: string template to render object id to text (used by fir_relations)
        """
        if not isinstance(parser_regex, re._pattern_type):
            parser_regex = re.compile(parser_regex)
        self.reverse_links.append((parser_regex, url_name))
        if model is not None:
            if not isinstance(model, six.string_types) and issubclass(models.Model, model):
                model = model._meta.label
            self.model_links[model] = (parser_regex, url_name, reverse) 
Example 39
Project: FIR   Author: certsocietegenerale   File: links.py    GNU General Public License v3.0 5 votes vote down vote up
def register_regex_link(self, parser_regex, template):
        """
        Registers a new parser for links using regex replacement
        :param parser_regex: string or regex object
        :param template: template to pass to regex expand
        """
        if not isinstance(parser_regex, re._pattern_type):
            parser_regex = re.compile(parser_regex)
        self.regex_links.append((parser_regex, template)) 
Example 40
Project: tcf   Author: intel   File: fastboot.py    Apache License 2.0 5 votes vote down vote up
def _regex_make(arg):
        if isinstance(arg, basestring):
            return re.compile(re.escape(arg))
        elif isinstance(arg, re._pattern_type):
            return arg
        raise ValueError("Bad type given (%s); str or re.compile() expected"
                         % type(arg).__name__) 
Example 41
Project: tcf   Author: intel   File: fastboot.py    Apache License 2.0 5 votes vote down vote up
def _regex_make(arg):
        if isinstance(arg, basestring):
            return re.compile(re.escape(arg))
        elif isinstance(arg, re._pattern_type):
            return arg
        raise ValueError("Bad type given (%s); str or re.compile() expected"
                         % type(arg).__name__) 
Example 42
Project: pisa   Author: IceCubeOpenSource   File: convert_config_format.py    Apache License 2.0 5 votes vote down vote up
def replace_substitution(match):
    """Replace old substitution syntax ``<!section|key!>`` with new syntax
    ``${section:key}``. Also, convert any colons in `section` or `key` to
    ``OTHER_SECTION_NAME_SEPARATOR``.

    Parameters
    ----------
    match : re._pattern_type

    Returns
    -------
    repl : string
        Replacement text

    """
    substrs = match.groups()
    loc = substrs[0].find('stage:')
    if loc >= 0:
        postloc = loc + len('stage:')
        s0 = (
            substrs[0][:loc]
            + 'stage:'
            + substrs[0][postloc:].replace(':', OTHER_SECTION_NAME_SEPARATOR)
        )
    else:
        s0 = substrs[0].replace(':', OTHER_SECTION_NAME_SEPARATOR)

    s1 = substrs[1].replace(':', OTHER_SECTION_NAME_SEPARATOR)

    return '${%s:%s}' % (s0, s1) 
Example 43
Project: pisa   Author: IceCubeOpenSource   File: convert_config_format.py    Apache License 2.0 5 votes vote down vote up
def replace_order(match):
    """Replace e.g.
        ``  order = flux:honda , osc : prob3cpu,aeff :hist, reco : hist``
    with
        ``  order = flux.honda, osc.prob3cpu, aeff.hist, reco.hist``

    Parameters
    ----------
    match : re._pattern_type

    Returns
    -------
    repl : string
        Replacement text, starting with ``order`` (i.e., retain whitespace
        preceding the word "order").

    """
    new_substrings = []
    for old_substring in match.groups()[0].split(','):
        new_substrings.append(
            ORDER_SUBSTRING_RE.sub(
                repl=lambda m: '%s.%s' % m.groups(),
                string=old_substring.strip()
            )
        )
    return 'order = %s' % ', '.join(new_substrings) 
Example 44
Project: pisa   Author: IceCubeOpenSource   File: convert_config_format.py    Apache License 2.0 5 votes vote down vote up
def append_include_as(include_match):
    """Convert ``#include x`` to ``#include x as y``, where appropriate; also,
    convert incorrect "as" statements. See INCLUDE_AS dict for mapping from
    resource to its "as" target.

    Parameters
    ----------
    include_match : re._pattern_type
        Match produced by INCLUDE_RE.match(string)

    Returns
    -------
    repl : string
        Replacement text for whatever comes after the "#include "

    """
    include_text = include_match.groups()[0]
    include_as_match = PISAConfigParser.INCLUDE_AS_RE.match(include_text)

    as_section = None
    if include_as_match:
        gd = include_as_match.groupdict()
        resource = gd['file']
        as_section = gd['as']
    else:
        resource = include_text
        if resource in INCLUDE_AS.keys():
            as_section = INCLUDE_AS[resource]

    if as_section is None:
        repl = '#include ' + resource
    else:
        repl = '#include %s as %s' % (resource, as_section)

    return repl 
Example 45
Project: 2015cdb_g4   Author: 40223208   File: test_re.py    GNU General Public License v3.0 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 46
Project: python-ami   Author: ettoreleandrotognoli   File: event.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, on_event=None, white_list=None, black_list=[], **kwargs):
        self.white_list = [white_list] if isinstance(white_list, (basestring, re._pattern_type)) else white_list
        self.black_list = [black_list] if isinstance(black_list, (basestring, re._pattern_type)) else black_list
        for k in list(kwargs.keys()):
            if k.startswith('on_'):
                setattr(self, k, kwargs.pop(k))
        self.assert_attrs = kwargs
        if on_event is None:
            self.on_event = getattr(self, 'on_event', self._on_event)
        else:
            self.on_event = on_event 
Example 47
Project: python-ami   Author: ettoreleandrotognoli   File: event.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_white_list(self, event_name):
        if self.white_list is None:
            return True
        for rule in self.white_list:
            if isinstance(rule, basestring) and event_name == rule:
                return True
            if isinstance(rule, re._pattern_type) and rule.search(event_name) is not None:
                return True
        return False 
Example 48
Project: python-ami   Author: ettoreleandrotognoli   File: event.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_black_list(self, event_name):
        for rule in self.black_list:
            if isinstance(rule, basestring) and event_name == rule:
                return False
            if isinstance(rule, re._pattern_type) and rule.match(event_name) is not None:
                return False
        return True 
Example 49
Project: python-ami   Author: ettoreleandrotognoli   File: event.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_attribute(self, rules, value):
        if isinstance(rules, (re._pattern_type, basestring)):
            rules = [rules]
        for rule in rules:
            if isinstance(rule, basestring) and rule == value:
                return True
            if isinstance(rule, re._pattern_type) and rule.search(value):
                return True
        return False 
Example 50
Project: Computable   Author: ktraunmueller   File: common.py    MIT License 5 votes vote down vote up
def is_re(obj):
    return isinstance(obj, re._pattern_type) 
Example 51
Project: wolf-lord   Author: lojikil   File: wolflord.py    ISC License 5 votes vote down vote up
def _filter(self, item, prefix, contains=False):
        """ Helper function to filter path prefixes.

            Arguments:
            item: the item to be tested
            prefix: the (string | compiled regex) filter
        """
        if isinstance(prefix, re._pattern_type):
            return prefix.search(item)
        elif contains:
            return prefix in item
        else:
            return item.startswith(prefix) 
Example 52
Project: oss-ftp   Author: aliyun   File: test_re.py    MIT License 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 53
Project: r2-d7   Author: FreakyDug   File: core.py    MIT License 5 votes vote down vote up
def is_pattern_type(obj):
    if hasattr(re, 'Pattern'):
        return isinstance(obj, re.Pattern)
    else:
        return isinstance(obj, re._pattern_type) 
Example 54
Project: 2015wcm   Author: coursemdetw   File: test_re.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 55
Project: ReGraph   Author: Kappa-Dev   File: attribute_sets.py    MIT License 5 votes vote down vote up
def _regex_to_string(a):
    if isinstance(a, str):
        return a
    elif isinstance(a, re._pattern_type):
        return a.pattern
    elif isinstance(a, RegexSet):
        if a.pattern is not None:
            return a.pattern
        else:
            return None
    else:
        raise AttributeSetError("Cannot convert regex to string!") 
Example 56
Project: napalm-dellos10   Author: napalm-automation-community   File: config_diff_util.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, indent=1, contents=None, ignore_lines=None):
        self._indent = indent
        self._items = list()
        self._config_text = None

        if ignore_lines:
            for item in ignore_lines:
                if not isinstance(item, re._pattern_type):
                    item = re.compile(item)
                DEFAULT_IGNORE_LINES_RE.add(item)

        if contents:
            self.load(contents) 
Example 57
Project: python-ia-markov   Author: accraze   File: markov.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _clean_sentences(self, sentences):
        """
        Removes excluded regex patterns
        """
        if isinstance(self.exclude, re._pattern_type):
            regex = self.exclude
            return [s for s in sentences if not regex.search(s)]
        if isinstance(self.exclude, list):
            filtered = sentences
            for regex in self.exclude:
                filtered = [s for s in filtered if not regex.search(s)]
            return filtered 
Example 58
Project: summer_hoilday2019   Author: Edgaraaa   File: roputils.py    MIT License 5 votes vote down vote up
def search(self, s, xonly=False):
        if isinstance(s, int):
            s = self.p(s)

        for virtaddr, blob, is_executable in self._load_blobs:
            if xonly and not is_executable:
                continue

            if isinstance(s, re._pattern_type):
                for m in re.finditer(s, blob):
                    addr = self.offset(virtaddr + m.start())
                    if self.arch == 'arm' and xonly and addr % 2 != 0:
                        continue
                    return addr
            else:
                i = -1
                while True:
                    i = blob.find(s, i+1)
                    if i == -1:
                        break
                    addr = self.offset(virtaddr + i)
                    if self.arch == 'arm' and xonly and addr % 2 != 0:
                        continue
                    return addr
        else:
            raise ValueError() 
Example 59
Project: medicare_utils   Author: kylebarron   File: medicare_df.py    MIT License 5 votes vote down vote up
def _str_in_keep_vars(
            instr: str, keep_vars: List[Union[str, Pattern]]) -> bool:
        """Return True if string is in keep_vars
        """

        match = False
        for keep_var in keep_vars:
            if isinstance(keep_var, str):
                if instr == keep_var:
                    match = True
            elif isinstance(keep_var, re._pattern_type):
                if keep_var.search(instr):
                    match = True
        return match 
Example 60
Project: medicare_utils   Author: kylebarron   File: medicare_df.py    MIT License 5 votes vote down vote up
def _get_pattern(obj: Union[str, Pattern]) -> str:
        """
        If str, returns str. If compiled regex, returns string representation of
        that pattern
        """
        if isinstance(obj, str):
            return obj
        elif isinstance(obj, re._pattern_type):
            return obj.pattern
        else:
            raise TypeError('Provided non string or regex to _get_pattern()') 
Example 61
Project: pybrake   Author: airbrake   File: blacklist_filter.py    MIT License 5 votes vote down vote up
def _is_regexp(v):
    return isinstance(v, _pattern_type) 
Example 62
Project: mockingjay   Author: kevinjqiu   File: matcher.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __eq__(self, other_str):
        if isinstance(self.subject, re._pattern_type):
            return self.subject.search(other_str) is not None
        else:
            return self.subject == other_str 
Example 63
Project: aresponses   Author: CircleUp   File: utils.py    MIT License 5 votes vote down vote up
def _text_matches_pattern(pattern, text):
    # This is needed for compatibility with old Python versions
    try:
        pattern_class = re.Pattern
    except AttributeError:
        pattern_class = re._pattern_type
    if isinstance(pattern, str):
        if pattern == text:
            return True
    elif isinstance(pattern, pattern_class):
        if pattern.search(text):
            return True
    return False 
Example 64
Project: Light_control   Author: laurent-colas   File: unittest_config.py    The Unlicense 5 votes vote down vote up
def test__regexp_validator_valid():
    result = config._regexp_validator(None, None, "test_.*")
    assert isinstance(result, re._pattern_type)
    assert result.pattern == "test_.*" 
Example 65
Project: Light_control   Author: laurent-colas   File: unittest_config.py    The Unlicense 5 votes vote down vote up
def test__regexp_csv_validator_valid():
    pattern_strings = ["test_.*", "foo\\.bar", "^baz$"]
    result = config._regexp_csv_validator(None, None, ",".join(pattern_strings))
    for i, regex in enumerate(result):
        assert isinstance(regex, re._pattern_type)
        assert regex.pattern == pattern_strings[i] 
Example 66
Project: bellybutton   Author: hchasestevens   File: linting.py    MIT License 5 votes vote down vote up
def lint_file(filepath, file_contents, rules):
    """Run rules against file, yielding any failures."""
    matching_rules = [
        rule
        for rule in rules
        if rule_settings_match(rule, filepath)
    ]
    if not matching_rules:
        return

    ignored_lines = get_ignored_lines(file_contents)
    xml_ast = file_contents_to_xml_ast(file_contents)  # todo - use caching module?

    for rule in sorted(matching_rules, key=attrgetter('name')):
        # TODO - hacky - need to find better way to do this (while keeping chain)
        # TODO - possibly having both filepath and contents/input supplied?
        if isinstance(rule.expr, XPath):
            matching_lines = set(find_in_ast(
                xml_ast,
                rule.expr.path,
                return_lines=True
            ))
        elif isinstance(rule.expr, re._pattern_type):
            matching_lines = {
                file_contents[:match.start()].count('\n') + 1  # TODO - slow
                for match in re.finditer(rule.expr, file_contents)
            }
        elif callable(rule.expr):
            matching_lines = set(rule.expr(file_contents))
        else:
            continue  # todo - maybe throw here?

        if rule.settings.allow_ignore:
            matching_lines -= ignored_lines

        if not matching_lines:
            yield LintingResult(rule, filepath, succeeded=True, lineno=None)

        for line in matching_lines:
            yield LintingResult(rule, filepath, succeeded=False, lineno=line) 
Example 67
Project: Blackjack-Tracker   Author: martinabeleda   File: unittest_config.py    MIT License 5 votes vote down vote up
def test__regexp_validator_valid():
    result = config._regexp_validator(None, None, "test_.*")
    assert isinstance(result, re._pattern_type)
    assert result.pattern == "test_.*" 
Example 68
Project: Blackjack-Tracker   Author: martinabeleda   File: unittest_config.py    MIT License 5 votes vote down vote up
def test__regexp_csv_validator_valid():
    pattern_strings = ["test_.*", "foo\\.bar", "^baz$"]
    result = config._regexp_csv_validator(None, None, ",".join(pattern_strings))
    for i, regex in enumerate(result):
        assert isinstance(regex, re._pattern_type)
        assert regex.pattern == pattern_strings[i] 
Example 69
Project: pheweb   Author: statgen   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def extract_phenocode_from_filepath(phenolist, regex):
    print("NOTE: working with {} phenos".format(len(phenolist)))
    re_pattern_type = re._pattern_type if hasattr(re, '_pattern_type') else re.Pattern # changed in py3.7
    if not isinstance(regex, re_pattern_type):
        regex = re.compile(regex)
    for pheno in phenolist:
        if 'assoc_files' not in pheno:
            raise PheWebError("ERROR: At least one phenotype doesn't have the key 'assoc_files'.")
        if not pheno['assoc_files']:
            raise PheWebError("ERROR: At least one phenotype has an empty 'assoc_files' list.")
        phenocodes = []
        for assoc_filepath in pheno['assoc_files']:
            match = re.search(regex, assoc_filepath)
            if match is None:
                raise PheWebError("ERROR: The regex {!r} doesn't match the filepath {!r}".format(regex.pattern, assoc_filepath))
            groups = match.groups()
            if len(groups) != 1:
                raise PheWebError("ERROR: The regex {!r} doesn't capture any groups on the filepath {!r}!  You're using parentheses without backslashes, right?".format(regex.pattern, assoc_filepath))
            phenocodes.append(groups[0])
        if len(set(phenocodes)) != 1:
            raise PheWebError("ERROR: At least one phenotype gets multiple different phenocodes from its several association filepaths.  Here they are: {!r}".format(list(set(phenocodes))))
        if 'phenocode' in pheno:
            if pheno['phenocode'] != phenocodes[0]:
                raise PheWebError("""\
ERROR: The regex {!r} matched the filepaths {!r} to produce the phenocode {!r}.  But that phenotype already had a phenocode, {!r}.
""".format(regex.pattern, pheno['assoc_files'], phenocodes[0], pheno['phenocode']))
        pheno['phenocode'] = phenocodes[0]
    return phenolist 
Example 70
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_re.py    GNU General Public License v3.0 5 votes vote down vote up
def test_compile(self):
        # Test return value when given string and pattern as parameter
        pattern = re.compile('random pattern')
        self.assertIsInstance(pattern, re._pattern_type)
        same_pattern = re.compile(pattern)
        self.assertIsInstance(same_pattern, re._pattern_type)
        self.assertIs(same_pattern, pattern)
        # Test behaviour when not given a string or pattern as parameter
        self.assertRaises(TypeError, re.compile, 0) 
Example 71
Project: cnidaria   Author: sauloal   File: common.py    MIT License 5 votes vote down vote up
def is_re(obj):
    return isinstance(obj, re._pattern_type) 
Example 72
Project: tcf   Author: intel   File: target_ext_shell.py    Apache License 2.0 4 votes vote down vote up
def _run(self, cmd = None, expect = None, prompt_regex = None,
             output = False, output_filter_crlf = True, trim = False):
        if cmd:
            assert isinstance(cmd, basestring)
        assert expect == None \
            or isinstance(expect, basestring) \
            or isinstance(expect, re._pattern_type) \
            or isinstance(expect, list)
        assert prompt_regex == None \
            or isinstance(prompt_regex, basestring) \
            or isinstance(prompt_regex, re._pattern_type)

        if output:
            offset = self.target.console.size()

        if cmd:
            self.target.send(cmd)
        if expect:
            if isinstance(expect, list):
                for expectation in expect:
                    assert isinstance(expectation, basestring) \
                        or isinstance(expectation, re._pattern_type)
                    self.target.expect(expectation)
            else:
                self.target.expect(expect)
        if prompt_regex == None:
            self.target.expect(self.shell_prompt_regex)
        else:
            self.target.expect(prompt_regex)
        if output:
            output = self.target.console.read(offset = offset)
            if output_filter_crlf:
                # replace \r\n, \r\r\n, \r\r\r\r\n... it happens
                output = re.sub(self.crnl_regex, self.target.crlf, output)
            if trim:
                # When we can run(), it usually prints in the console:
                ## <command-echo from our typing>
                ## <command output>
                ## <prompt>
                #
                # So to trim we just remove the first and last
                # lines--won't work well without output_filter_crlf
                # and it is quite a hack.
                first_nl = output.find(self.target.crlf)
                last_nl = output.rfind(self.target.crlf)
                output = output[first_nl+1:last_nl+1]
            return output
        return None 
Example 73
Project: tcf   Author: intel   File: target_ext_shell.py    Apache License 2.0 4 votes vote down vote up
def _run(self, cmd = None, expect = None, prompt_regex = None,
             output = False, output_filter_crlf = True, trim = False):
        if cmd:
            assert isinstance(cmd, basestring)
        assert expect == None \
            or isinstance(expect, basestring) \
            or isinstance(expect, re._pattern_type) \
            or isinstance(expect, list)
        assert prompt_regex == None \
            or isinstance(prompt_regex, basestring) \
            or isinstance(prompt_regex, re._pattern_type)

        if output:
            offset = self.target.console.size()

        if cmd:
            self.target.send(cmd)
        if expect:
            if isinstance(expect, list):
                for expectation in expect:
                    assert isinstance(expectation, basestring) \
                        or isinstance(expectation, re._pattern_type)
                    self.target.expect(expectation)
            else:
                self.target.expect(expect)
        if prompt_regex == None:
            self.target.expect(self.shell_prompt_regex)
        else:
            self.target.expect(prompt_regex)
        if output:
            output = self.target.console.read(offset = offset)
            if output_filter_crlf:
                # replace \r\n, \r\r\n, \r\r\r\r\n... it happens
                output = re.sub(self.crnl_regex, self.target.crlf, output)
            if trim:
                # When we can run(), it usually prints in the console:
                ## <command-echo from our typing>
                ## <command output>
                ## <prompt>
                #
                # So to trim we just remove the first and last
                # lines--won't work well without output_filter_crlf
                # and it is quite a hack.
                first_nl = output.find(self.target.crlf)
                last_nl = output.rfind(self.target.crlf)
                output = output[first_nl+1:last_nl+1]
            return output
        return None 
Example 74
Project: streamalert   Author: airbnb   File: helpers.py    Apache License 2.0 4 votes vote down vote up
def user_input(requested_info, mask, input_restrictions):
    """Prompt user for requested information

    Args:
        requested_info (str): Description of the information needed
        mask (bool): Decides whether to mask input or not

    Returns:
        str: response provided by the user
    """
    # pylint: disable=protected-access
    response = ''
    prompt = '\nPlease supply {}: '.format(requested_info)

    if not mask:
        while not response:
            response = raw_input(prompt)

        # Restrict having spaces or colons in items (applies to things like
        # descriptors, etc)
        if isinstance(input_restrictions, re._pattern_type):
            valid_response = input_restrictions.match(response)
            if not valid_response:
                LOGGER.error('The supplied input should match the following '
                             'regular expression: %s', input_restrictions.pattern)
        elif callable(input_restrictions):
            # Functions can be passed here to perform complex validation of input
            # Transform the response with the validating function
            response = input_restrictions(response)
            valid_response = response is not None and response is not False
            if not valid_response:
                LOGGER.error('The supplied input failed to pass the validation '
                             'function: %s', input_restrictions.__doc__)
        else:
            valid_response = not any(x in input_restrictions for x in response)
            if not valid_response:
                restrictions = ', '.join(
                    '\'{}\''.format(restriction) for restriction in input_restrictions)
                LOGGER.error('The supplied input should not contain any of the following: %s',
                             restrictions)

        if not valid_response:
            return user_input(requested_info, mask, input_restrictions)
    else:
        while not response:
            response = getpass(prompt=prompt)

    return response 
Example 75
Project: ReGraph   Author: Kappa-Dev   File: attribute_sets.py    MIT License 4 votes vote down vote up
def issubset(self, other):
        """Test regexp inclusion relation.

        Tests if a set defined by `self` is a included
        in a set defined by `other`.

        Parameters
        ----------
        other : set, str, re._pattern_type, RegexSet
            Another regex to test inclusion.

        Returns
        -------
        `True` is `self` defines a subset of `other`, `False` otherwise

        Raises
        ------
        AttributeSetError
            If the type `other` is not recognized.
        """
        if self.pattern is None:
            return True
        else:
            self_exp = parse(self.pattern)

            def included(a):
                if isinstance(a, str):
                    other_exp = parse(a)
                elif isinstance(a, re._pattern_type):
                    other_exp = parse(a.pattern)
                elif isinstance(a, RegexSet):
                    if a.pattern:
                        other_exp = parse(a.pattern)
                    else:
                        return False
                else:
                    raise AttributeSetError(
                        "Regexp object should be of type `str` or `re._pattern_type`!"
                    )
                return (self_exp & other_exp.everythingbut()).empty()

            if isinstance(other, set):
                res = True

                for element in other:
                    if element is not None and not included(element):
                        res = False
                        break
            else:
                res = included(other)
            return res 
Example 76
Project: ReGraph   Author: Kappa-Dev   File: attribute_sets.py    MIT License 4 votes vote down vote up
def union(self, other):
        """Find the union with another set.

        The union is found in the following ways:

        * If `other` is a string, a Python dict or a FiniteSet
          the result of the union is a simple concatenation of the
          string representations of the elements of `other`
          with the pattern of `self`.

        * If `other` is an instance of `UniversalSet`, the union
          is a `UniversalSet` object.

        * If `other` is an instance of `EmptySet`, the union is
          a copy of `self`.

        Parameters
        ----------
        other : set, str, re._pattern_type, RegexSet

        Returns
        -------
        result : RegexSet
            The union set
        """
        if self.pattern is None:
            other_str = _regex_to_string(other)
            if other_str is None:
                return RegexSet.empty()
            else:
                return RegexSet(other_str)
        if self.is_universal():
            return RegexSet.universal()

        patterns = []
        if isinstance(other, set) or isinstance(other, FiniteSet):
            for element in other:
                element_str = _regex_to_string(element)
                if element_str is not None:
                    patterns.append(element_str)
        elif isinstance(other, UniversalSet):
            return UniversalSet()
        elif isinstance(other, EmptySet):
            return copy.deepcopy(self)
        else:
            other_str = _regex_to_string(other)
            if other_str is None:
                return self.copy()
            else:
                patterns.append(other_str)

        new_pattern = self.pattern + "|" + "|".join(patterns)
        result = RegexSet(new_pattern)
        return result 
Example 77
Project: ReGraph   Author: Kappa-Dev   File: attribute_sets.py    MIT License 4 votes vote down vote up
def difference(self, other):
        """Find the difference of two regexps.

        This method uses greenery library to find and
        reduce the difference pattern between two regex's.

        * If `other` is a string, a Python dict or a FiniteSet,
          it is converted to a regex pattern, after which it
          is parsed by `greenery.lego.parse` method and its
          difference with the pattern of the `self` is found.
          See more details here:
          https://github.com/qntm/greenery

        * If `other` is an instance of `EmpySet`, the difference
          is a copy of `self`.

        * If `other` is an instance of `UniversalSet`, the difference
          is an instance of `EmptySet`.

        Parameters
        ----------
        other : set, str, re._pattern_type, RegexSet

        Returns
        -------
        result : RegexSet
            The union set
        """
        if self.pattern is None:
            return RegexSet.empty()

        other_exp = []

        if isinstance(other, set):
            for exp in other:
                exp_str = _regex_to_string(exp)
                if exp_str is not None:
                    other_exp.append(parse(exp_str))
        else:
            other_str = _regex_to_string(other)
            if other_str is not None:
                other_exp.append(parse(other_str))
            else:
                return self.copy()
        complement_exp = parse(self.pattern)
        for exp in other_exp:
            complement_exp = complement_exp.difference(exp)

        return RegexSet(str(complement_exp.reduce())) 
Example 78
Project: lexpredict-contraxsuite   Author: LexPredict   File: lease_doc_properties_locator.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def prepare_matcher(
            matcher: Union[str, Pattern, Callable[[str], str], List]) \
            -> Callable[[str], Generator[str, None, None]]:

        if type(matcher) is list:
            matchers = [FieldDetector.prepare_matcher(m) for m in list(matcher)]

            def matcher_func(text):
                for m in matchers:
                    if m is not None:
                        yield from m(text)

            return matcher_func

        elif callable(matcher):
            def matcher_func(text):
                res = matcher(text)
                if type(res) is list:
                    for e in list(res):
                        yield e
                elif isinstance(res, types.GeneratorType):
                    yield from res
                else:
                    yield res

            return matcher_func
        elif isinstance(matcher, str):
            matcher_re = re.compile(matcher, FieldDetector.DEF_RE_FLAGS)

            def matcher_func(str):
                for m in matcher_re.findall(str):
                    yield m

            return matcher_func
        elif isinstance(matcher, re._pattern_type):
            def matcher_func(str):
                for m in matcher.findall(str):
                    yield m

            return matcher_func
        else:
            return None 
Example 79
Project: lexpredict-contraxsuite   Author: LexPredict   File: document_fields.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def prepare_matcher(
            matcher: Union[str, Pattern, Callable[[str], str], List]) \
            -> Union[None, Callable[[str], Generator[str, None, None]]]:

        if type(matcher) is list:
            matchers = [FieldDetector.prepare_matcher(m) for m in list(matcher)]

            def matcher_func(text):
                for m in matchers:
                    if m is not None:
                        yield from m(text)

            return matcher_func

        elif callable(matcher):
            def matcher_func(text):
                res = matcher(text)
                if type(res) is list:
                    for e in list(res):
                        yield e
                elif isinstance(res, types.GeneratorType):
                    yield from res
                else:
                    yield res

            return matcher_func
        elif isinstance(matcher, str):
            matcher_re = re.compile(matcher, FieldDetector.DEF_RE_FLAGS)

            def matcher_func(str):
                for m in matcher_re.findall(str):
                    yield m

            return matcher_func
        elif isinstance(matcher, re._pattern_type):
            def matcher_func(str):
                for m in matcher.findall(str):
                    yield m

            return matcher_func
        else:
            return None 
Example 80
Project: montydb   Author: davidlatwe   File: queries.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _modify_regex_optins(sub_spec):
    """Merging $regex and $options values in query document

    Besides string type value, field $regex accept `bson.Regex` and
    `re._pattern_type` in pymongo. For re.flags and $options, based
    on the key order of dict, seconded will override the first, if
    they both exists in the query document.
    """
    new_sub_spec = None
    _re = None
    flags = ""

    for key, val in sub_spec.items():
        if key == "$options":
            flags = val
        if key == "$regex" and isinstance(val, (RE_PATTERN_TYPE, Regex)):
            flags = _FALG(val.flags)
            # We will deepcopy `sub_spec` later for merging "$regex" and
            # "$options" to query parser, but we can't deepcopy regex
            # object, so move it to somewhere else and retrieve it later.
            _re = sub_spec["$regex"]
            sub_spec["$regex"] = None

    new_sub_spec = deepcopy(sub_spec)
    new_sub_spec["$regex"] = {
        "pattern": _re.pattern if _re else sub_spec["$regex"],
        "flags": flags
    }

    if "#" in new_sub_spec["$regex"]["pattern"].rsplit("\n")[-1]:
        # (NOTE) davidlatwe:
        #   if pound(#) char exists in $regex string value and not ends with
        #   newline(\n), Mongo raise error. (but the message seems incomplete)
        raise OperationFailure("Regular expression is invalid: missing )")

    if _re:
        # Put `re._pattern_type` or `Regex` object back.
        sub_spec["$regex"] = _re

    if "$options" in new_sub_spec:
        # Remove $options, Monty can't digest it
        del new_sub_spec["$options"]

    return new_sub_spec