Python re.match() Examples

The following are code examples for showing how to use re.match(). 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: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: modeling.py    Apache License 2.0 6 votes vote down vote up
def get_assignment_map_from_checkpoint(tvars, init_checkpoint):
    """Compute the union of the current variables and checkpoint variables."""
    assignment_map = {}
    initialized_variable_names = {}

    name_to_variable = collections.OrderedDict()
    for var in tvars:
        name = var.name
        m = re.match("^(.*):\\d+$", name)
        if m is not None:
            name = m.group(1)
        name_to_variable[name] = var

    init_vars = tf.train.list_variables(init_checkpoint)

    assignment_map = collections.OrderedDict()
    for x in init_vars:
        (name, var) = (x[0], x[1])
        if name not in name_to_variable:
            continue
        assignment_map[name] = name
        initialized_variable_names[name] = 1
        initialized_variable_names[name + ":0"] = 1

    return (assignment_map, initialized_variable_names) 
Example 2
Project: flexnet   Author: ressy   File: client.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def query_vendor_licenses(self):
        """Query a vendor daemon for full license details"""
        msg = self._stub(data='\x01\x00\x00\x00\x00', reqtype=TYPE_LICSET)
        # Sometimes this will have a garbage whitespace entry.
        # Remove those before continuing.
        msg["text"] = filter(lambda x: not re.match('^\s*$', x), msg["text"])
        num = len(msg["text"])/8
        license_sets =[{} for x in range(num)]
        keys = ["fid", "sig", "names", "date1", "date2", "fid", "url", "license_text"]
        for i in range(len(msg["text"])):
            lic = license_sets[i/8]
            lic[keys[i%8]] = msg["text"][i]
        self.license_sets.extend([flexnet.licenses.LicenseSet(lic) for lic in license_sets])
        return license_sets

    # TODO this is broken on Cadence. 
Example 3
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 6 votes vote down vote up
def list_selinux_modules():
    '''
    Produce list of SELinux policy modules

    Returns list of tuples (name,priority)
    '''
    try:
        semodule = run(['semodule', '-lfull'], split=True)
    except CalledProcessError:
        api.current_logger().warning('Cannot get list of selinux modules')
        return []

    modules = []
    for module in semodule.get("stdout", []):
        # Matching line such as "100 zebra             pp "
        # "<priority> <module name>    <module type - pp/cil> "
        m = re.match(r'([0-9]+)\s+([\w-]+)\s+([\w-]+)\s*\Z', module)
        if not m:
            # invalid output of "semodule -lfull"
            api.current_logger().warning('Invalid output of "semodule -lfull": %s', module)
            continue
        modules.append((m.group(2), m.group(1)))

    return modules 
Example 4
Project: clikit   Author: sdispater   File: abstract_option.py    MIT License 6 votes vote down vote up
def _validate_short_name(
        self, short_name, flags
    ):  # type: (Optional[str], int) -> None
        if short_name is None:
            if flags & self.PREFER_SHORT_NAME:
                raise ValueError(
                    "The short option name must be given if the option flag PREFER_SHORT_NAME is selected."
                )

            return

        if not isinstance(short_name, basestring):
            raise ValueError(
                "The short option name must be a string. Got: {}".format(
                    type(short_name)
                )
            )

        if not short_name:
            raise ValueError("The short option name must not be empty.")

        if not re.match(r"^[a-zA-Z]$", short_name):
            raise ValueError("The short option name must be exactly one letter.") 
Example 5
Project: BlueLightMeter   Author: chripell   File: as726x.py    Apache License 2.0 6 votes vote down vote up
def chat(self, tx: str, match: str):
        if tx is not None:
            if self.DEBUG:
                print("TX:", tx)
            tx += "\n"
            self.ser.write(tx.encode("utf-8"))
        rx = self.ser.read_until()
        rx = rx.decode("utf-8")
        if self.DEBUG:
            print("RX:", rx, end='')
        m = re.match(match, rx)
        if m is None:
            if re.match(r".*OK", rx):
                return None
            raise IOError("Unexpected answer: %s" % rx)
        return m 
Example 6
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_hostname(sitename):
    parsed_url = urllib.parse.urlparse(sitename)
    hostname = parsed_url.netloc
    if hostname == '':
        # If you pass in 'facebook.com/foo' urlparse treats it all as the path
        match = re.match(r'[^/]+', parsed_url.path)
        if match == None:
            print('ERROR: Unable to make the provided sitename into a hostname: ' + parsed_url.path)
            sys.exit(1)
        hostname = match.group(0)

    www_matcher = r'^www\.(.+)'
    if re.search(www_matcher, hostname) != None:
        hostname = re.sub(www_matcher, r'\1', hostname)

    return hostname 
Example 7
Project: autofff   Author: ChiefGokhlayeh   File: scanner.py    MIT License 6 votes vote down vote up
def _parse_error_context(self, text: str, error: pycparser.c_parser.ParseError) -> str:
        match = re.match(
            r"\s*(.*?)\s*:\s*([0-9]*?)\s*:\s*([0-9]*?)\s*:\s*(.*)", str(error))
        filename = match.group(1)
        row = int(match.group(2)) - 1
        column = int(match.group(3)) - 1

        context = f">Caused in file: {filename}\n"
        prevRowStart = max(
            row - CONFIG[c.AUTOFFF_SECTION][c.GCC_SCANNER_SECTION][c.GCC_SCANNER_ERROR_CONTEXT_PREV_LINES], 0)
        postRowStart = row + \
            CONFIG[c.AUTOFFF_SECTION][c.GCC_SCANNER_SECTION][c.GCC_SCANNER_ERROR_CONTEXT_POST_LINES]
        with open(filename) as fp:
            for i, line in enumerate(fp):
                if i == row:
                    context += f">{line[:-1]}\n"
                    context += f"{'-' * (column + 1)}^{'-' * (len(line) - column - 2)}\n"
                elif i >= prevRowStart and i <= postRowStart:
                    context += f"{line[:-1]}\n"
        return context 
Example 8
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 6 votes vote down vote up
def image_mime_type(data):
    """Return the MIME type of the image data (a bytestring).
    """
    # This checks for a jpeg file with only the magic bytes (unrecognized by
    # imghdr.what). imghdr.what returns none for that type of file, so
    # _wider_test_jpeg is run in that case. It still returns None if it didn't
    # match such a jpeg file.
    kind = _imghdr_what_wrapper(data)
    if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']:
        return 'image/{0}'.format(kind)
    elif kind == 'pgm':
        return 'image/x-portable-graymap'
    elif kind == 'pbm':
        return 'image/x-portable-bitmap'
    elif kind == 'ppm':
        return 'image/x-portable-pixmap'
    elif kind == 'xbm':
        return 'image/x-xbitmap'
    else:
        return 'image/x-{0}'.format(kind) 
Example 9
Project: pyblish-win   Author: pyblish   File: fixps.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def main():
    for filename in sys.argv[1:]:
        try:
            f = open(filename, 'r')
        except IOError, msg:
            print filename, ': can\'t open :', msg
            continue
        line = f.readline()
        if not re.match('^#! */usr/local/bin/python', line):
            print filename, ': not a /usr/local/bin/python script'
            f.close()
            continue
        rest = f.read()
        f.close()
        line = re.sub('/usr/local/bin/python',
                      '/usr/bin/env python', line)
        print filename, ':', repr(line)
        f = open(filename, "w")
        f.write(line)
        f.write(rest)
        f.close() 
Example 10
Project: pyblish-win   Author: pyblish   File: fixcid.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def fix(filename):
##  dbg('fix(%r)\n' % (filename,))
    if filename == '-':
        # Filter mode
        f = sys.stdin
        g = sys.stdout
    else:
        # File replacement mode
        try:
            f = open(filename, 'r')
        except IOError, msg:
            err(filename + ': cannot open: ' + str(msg) + '\n')
            return 1
        head, tail = os.path.split(filename)
        tempname = os.path.join(head, '@' + tail)
        g = None
    # If we find a match, we rewind the file and start over but
    # now copy everything to a temp file. 
Example 11
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
        """Error 401 -- authentication required.
        This function supports Basic authentication only."""
        if not 'www-authenticate' in headers:
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        stuff = headers['www-authenticate']
        import re
        match = re.match('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"', stuff)
        if not match:
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        scheme, realm = match.groups()
        if scheme.lower() != 'basic':
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        name = 'retry_' + self.type + '_basic_auth'
        if data is None:
            return getattr(self,name)(url, realm)
        else:
            return getattr(self,name)(url, realm, data) 
Example 12
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def http_error_407(self, url, fp, errcode, errmsg, headers, data=None):
        """Error 407 -- proxy authentication required.
        This function supports Basic authentication only."""
        if not 'proxy-authenticate' in headers:
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        stuff = headers['proxy-authenticate']
        import re
        match = re.match('[ \t]*([^ \t]+)[ \t]+realm="([^"]*)"', stuff)
        if not match:
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        scheme, realm = match.groups()
        if scheme.lower() != 'basic':
            URLopener.http_error_default(self, url, fp,
                                         errcode, errmsg, headers)
        name = 'retry_proxy_' + self.type + '_basic_auth'
        if data is None:
            return getattr(self,name)(url, realm)
        else:
            return getattr(self,name)(url, realm, data) 
Example 13
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def splitnport(host, defport=-1):
    """Split host and port, returning numeric port.
    Return given default port if no ':' found; defaults to -1.
    Return numerical port if a valid number are found after ':'.
    Return None if ':' but not a valid number."""
    global _nportprog
    if _nportprog is None:
        import re
        _nportprog = re.compile('^(.*):(.*)$')

    match = _nportprog.match(host)
    if match:
        host, port = match.group(1, 2)
        if port:
            try:
                nport = int(port)
            except ValueError:
                nport = None
            return host, nport
    return host, defport 
Example 14
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _add_action(self, action):
        # resolve any conflicts
        self._check_conflict(action)

        # add to actions list
        self._actions.append(action)
        action.container = self

        # index the action by any option strings it has
        for option_string in action.option_strings:
            self._option_string_actions[option_string] = action

        # set the flag if any option strings look like negative numbers
        for option_string in action.option_strings:
            if self._negative_number_matcher.match(option_string):
                if not self._has_negative_number_optionals:
                    self._has_negative_number_optionals.append(True)

        # return the created action
        return action 
Example 15
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _match_argument(self, action, arg_strings_pattern):
        # match the pattern for this action to the arg strings
        nargs_pattern = self._get_nargs_pattern(action)
        match = _re.match(nargs_pattern, arg_strings_pattern)

        # raise an exception if we weren't able to find a match
        if match is None:
            nargs_errors = {
                None: _('expected one argument'),
                OPTIONAL: _('expected at most one argument'),
                ONE_OR_MORE: _('expected at least one argument'),
            }
            default = _('expected %s argument(s)') % action.nargs
            msg = nargs_errors.get(action.nargs, default)
            raise ArgumentError(action, msg)

        # return the number of arguments matched
        return len(match.group(1)) 
Example 16
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, verbose=False, parser=DocTestParser(),
                 recurse=True, exclude_empty=True):
        """
        Create a new doctest finder.

        The optional argument `parser` specifies a class or
        function that should be used to create new DocTest objects (or
        objects that implement the same interface as DocTest).  The
        signature for this factory function should match the signature
        of the DocTest constructor.

        If the optional argument `recurse` is false, then `find` will
        only examine the given object, and not any contained objects.

        If the optional argument `exclude_empty` is false, then `find`
        will include tests for objects with empty docstrings.
        """
        self._parser = parser
        self._verbose = verbose
        self._recurse = recurse
        self._exclude_empty = exclude_empty 
Example 17
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _do_a_fancy_diff(self, want, got, optionflags):
        # Not unless they asked for a fancy diff.
        if not optionflags & (REPORT_UDIFF |
                              REPORT_CDIFF |
                              REPORT_NDIFF):
            return False

        # If expected output uses ellipsis, a meaningful fancy diff is
        # too hard ... or maybe not.  In two real-life failures Tim saw,
        # a diff was a major help anyway, so this is commented out.
        # [todo] _ellipsis_match() knows which pieces do and don't match,
        # and could be the basis for a kick-ass diff in this case.
        ##if optionflags & ELLIPSIS and ELLIPSIS_MARKER in want:
        ##    return False

        # ndiff does intraline difference marking, so can be useful even
        # for 1-line differences.
        if optionflags & REPORT_NDIFF:
            return True

        # The other diff types need at least a few lines to be helpful.
        return want.count('\n') > 2 and got.count('\n') > 2 
Example 18
Project: pyblish-win   Author: pyblish   File: test_gdb.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_gdb_repr(self, source,
                     cmds_after_breakpoint=None,
                     import_site=False):
        # Given an input python source representation of data,
        # run "python -c'print DATA'" under gdb with a breakpoint on
        # PyObject_Print and scrape out gdb's representation of the "op"
        # parameter, and verify that the gdb displays the same string
        #
        # For a nested structure, the first time we hit the breakpoint will
        # give us the top-level structure
        gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print',
                                          cmds_after_breakpoint=cmds_after_breakpoint,
                                          import_site=import_site)
        # gdb can insert additional '\n' and space characters in various places
        # in its output, depending on the width of the terminal it's connected
        # to (using its "wrap_here" function)
        m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*',
                     gdb_output, re.DOTALL)
        if not m:
            self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
        return m.group(1), gdb_output 
Example 19
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: optimization.py    Apache License 2.0 5 votes vote down vote up
def _get_variable_name(self, param_name):
        """Get the variable name from the tensor name."""
        m = re.match("^(.*):\\d+$", param_name)
        if m is not None:
            param_name = m.group(1)
        return param_name 
Example 20
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: modeling.py    Apache License 2.0 5 votes vote down vote up
def assert_rank(tensor, expected_rank, name=None):
    """Raises an exception if the tensor rank is not of the expected rank.

    Args:
      tensor: A tf.Tensor to check the rank of.
      expected_rank: Python integer or list of integers, expected rank.
      name: Optional name of the tensor for the error message.

    Raises:
      ValueError: If the expected shape doesn't match the actual shape.
    """
    if name is None:
        name = tensor.name

    expected_rank_dict = {}
    if isinstance(expected_rank, six.integer_types):
        expected_rank_dict[expected_rank] = True
    else:
        for x in expected_rank:
            expected_rank_dict[x] = True

    actual_rank = tensor.shape.ndims
    if actual_rank not in expected_rank_dict:
        scope_name = tf.get_variable_scope().name
        raise ValueError(
            "For the tensor `%s` in scope `%s`, the actual rank "
            "`%d` (shape = %s) is not equal to the expected rank `%s`" %
            (name, scope_name, actual_rank, str(tensor.shape), str(expected_rank))) 
Example 21
Project: flexnet   Author: ressy   File: client.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def query_vendor_details(self):
        """Connect to each vendor daemon for full details"""
        p = self.server_params
        vendors = {}
        # Gather vendor hostnames and ports
        for vendor_name in p["vendors"]:
            self.vendor = vendor_name
            msg = self.hello()
            vendors[vendor_name] = {}
            vendors[vendor_name]["hostname"] = msg["vendor_hostname"]
            vendors[vendor_name]["port"] = msg["vendor_port"]
        # Connect to each vendor
        for vendor_name in vendors.keys():
            v = vendors[vendor_name]
            client = VendorClient(v["hostname"], v["port"])
            self.vendors.append(client)
            client.vendor = vendor_name
            client.hello()
            client.query_vendor_features()
            # These are licenses reported directly by the vendor daemon,
            # and not mentioned in the license file text from the license
            # manager daemon.
            # TODO make this work for oldproto
            if not self.oldproto:
                client.query_vendor_licenses()
            # Alternatively, some licenses may only be listed in the license
            # file text returned by the license manager.  So, also query
            # licenses from the file that match this vendor.
            license_file_entries = filter(lambda lic: lic["vendor"] == vendor_name, p["licenses_in_file"])
            client.licenses = [flexnet.licenses.License(data) for data in license_file_entries]
            # Now that all licenses for this vendor have been accounted for,
            # actually request license status/usage from the vendor daemon
            for lic_set in client.license_sets:
                for lic in lic_set.licenses:
                    client.query_vendor_license_status(lic)
            for lic in client.licenses:
                client.query_vendor_license_status(lic)
        return vendors 
Example 22
Project: fs_image   Author: facebookincubator   File: test_extents_to_chunks.py    MIT License 5 votes vote down vote up
def _gen_ranges_from_figure(figure: str):
    for s in textwrap.dedent(figure.strip('\n')).split('\n'):
        s = s.rstrip()
        # Number lines should aid reading off positions. Check they're right.
        if re.match('[0-9]*$', s):
            assert ('0123456789' * math.ceil(len(s) / 10))[:len(s)] == s, \
                f'Bad number line {s} in {figure}'
            continue
        offset = 0
        for m in re.finditer(r'(.)\1*', s):
            v = m.group(0)
            if v[0] != ' ':
                yield v[0], offset, len(v)
            offset += len(v) 
Example 23
Project: fs_image   Author: facebookincubator   File: test_repo_downloader.py    MIT License 5 votes vote down vote up
def test_lose_repodata_commit_race(self):
        'We downloaded & stored a repodata, but in the meantime some other '
        'writer committed the same repodata.'

        original_maybe_store = RepoDBContext.maybe_store
        faked_objs = []

        def my_maybe_store(self, table, obj, storage_id):
            if re.match(FILELISTS_REPODATA_REGEX, obj.location):
                faked_objs.append(obj)
                return f'fake_already_stored_{obj.location}'
            return original_maybe_store(self, table, obj, storage_id)

        with mock.patch.object(
            RepoDBContext, 'maybe_store', new=my_maybe_store
        ), tempfile.TemporaryDirectory() as storage_dir:
            snapshot = self._make_downloader(
                storage_dir, '0/good_dog',
            ).download()
            faked_obj, = faked_objs
            # We should be using the winning writer's storage ID.
            self.assertEqual(
                faked_obj,
                snapshot.storage_id_to_repodata[
                    f'fake_already_stored_{faked_obj.location}'
                ],
            ) 
Example 24
Project: fs_image   Author: facebookincubator   File: test_unshare.py    MIT License 5 votes vote down vote up
def _kill_keepalive(self, unshare: Unshare):
        # We can kill the inner keepalive `cat` since it runs w/ our UID
        # Since it's an `init` of a PID namespace, we must use SIGKILL.
        cat_pid = int(re.match(
            '^/proc/([0-9]+)/ns/',
            next(iter(unshare._namespace_to_file.values())).name,
        ).group(1))
        print('Sending SIGKILL to', cat_pid)
        os.kill(cat_pid, signal.SIGKILL) 
Example 25
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def is_udp_disabled():
    """Check if UDP port is disabled in the sysconfig file."""
    with open(sysconfig_path) as f:
        for line in f:
            if re.match(r'^\s*OPTIONS=.*-U\s*0[^0-9]', line):
                return True
    return False 
Example 26
Project: leapp-repository   Author: oamg   File: ntp2chrony.py    Apache License 2.0 5 votes vote down vote up
def parse_trustedkey(self, words):
        key_ranges = []
        for word in words:
            if word.isdigit():
                key_ranges.append((int(word), int(word)))
            elif re.match("^[0-9]+-[0-9]+$", word):
                first, last = word.split("-")
                key_ranges.append((int(first), int(last)))
            else:
                return False

        self.trusted_keys = key_ranges
        return True 
Example 27
Project: leapp-repository   Author: oamg   File: ntp2chrony.py    Apache License 2.0 5 votes vote down vote up
def get_processed_time_sources(self):
        # Convert {0,1,2,3}.*pool.ntp.org servers to 2.*pool.ntp.org pools

        # Make shallow copies of all sources (only type will be modified)
        time_sources = [s.copy() for s in self.time_sources]

        pools = {}
        for source in time_sources:
            if source["type"] != "server":
                continue
            m = re.match("^([0123])(\\.\\w+)?\\.pool\\.ntp\\.org$", source["address"])
            if m is None:
                continue
            number = m.group(1)
            zone = m.group(2)
            if zone not in pools:
                pools[zone] = []
            pools[zone].append((int(number), source))

        remove_servers = set()
        for zone, pool in pools.items():
            # sort and skip all pools not in [0, 3] range
            pool.sort()
            if [num for num, source in pool] != [0, 1, 2, 3]:
                # only exact group of 4 servers can be converted, nothing to do here
                continue
            # verify that parameters are the same for all servers in the pool
            if not all([p[1]["options"] == pool[0][1]["options"] for p in pool]):
                break
            remove_servers.update([pool[i][1]["address"] for i in [0, 1, 3]])
            pool[2][1]["type"] = "pool"

        processed_sources = []
        for source in time_sources:
            if source["type"] == "server" and source["address"] in remove_servers:
                continue
            processed_sources.append(source)
        return processed_sources 
Example 28
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def _replace_in_config(config_lines, option, value):
    res = []
    for line in config_lines:
        if re.match(r'^\s*' + option, line) is None:
            res.append(line)
        else:
            res.append('# Commented out by Leapp:')
            res.append('#' + line)
    if value is not None:
        res.append('# Added by Leapp:')
        res.append('%s=%s' % (option, value))
    return res 
Example 29
Project: leapp-repository   Author: oamg   File: tcpwrappersutils.py    Apache License 2.0 5 votes vote down vote up
def _pattern_matches(pattern, string):
    if pattern.lower() == 'all':
        return True
    regex = _build_regex(pattern)
    return re.match(regex, string, re.IGNORECASE) is not None 
Example 30
Project: leapp-repository   Author: oamg   File: tcpwrappersutils.py    Apache License 2.0 5 votes vote down vote up
def _daemon_list_matches_daemon(daemon_list, daemon, recursion_depth):
    try:
        cur_list_end = daemon_list.index('except')
    except ValueError:
        cur_list_end = len(daemon_list)
    cur_list = daemon_list[:cur_list_end]
    matches_cur_list = False
    for item in cur_list:
        try:
            ix = item.index('@')
            # For simplicity, we ignore the host part. So we must make sure
            # that a daemon list containing a host-based pattern will always match
            # the daemon part of that host-based pattern (e.g. 'all except [email protected]
            # matches 'vsftpd'). See test_config_applies_to_daemon_with_host_except().
            if recursion_depth % 2 == 1:
                continue
            pattern = item[:ix]
        except ValueError:
            pattern = item
        if _pattern_matches(pattern, daemon):
            matches_cur_list = True
            break

    next_list = daemon_list[cur_list_end + 1:]
    if not next_list:
        matches_next_list = False
    else:
        matches_next_list = _daemon_list_matches_daemon(next_list, daemon, recursion_depth + 1)

    return matches_cur_list and not matches_next_list 
Example 31
Project: clikit   Author: sdispater   File: command_option.py    MIT License 5 votes vote down vote up
def _validate_long_alias(self, alias):  # type: (str) -> None
        if not alias[:1].isalpha():
            raise ValueError("A long option alias must start with a letter.")

        if not re.match("^[a-zA-Z0-9\-]+$", alias):
            raise ValueError(
                "A long option alias must contain letters, digits and hyphens only."
            ) 
Example 32
Project: clikit   Author: sdispater   File: command_option.py    MIT License 5 votes vote down vote up
def _validate_short_alias(self, alias):  # type: (str) -> None
        if not re.match("^[a-zA-Z]$", alias):
            raise ValueError(
                'A short option alias must be exactly one letter. Got: "{}"'.format(
                    alias
                )
            ) 
Example 33
Project: clikit   Author: sdispater   File: abstract_option.py    MIT License 5 votes vote down vote up
def _validate_long_name(self, long_name):  # type: (Optional[str]) -> None
        if long_name is None:
            raise ValueError("The long option name must not be null.")

        if not isinstance(long_name, basestring):
            raise ValueError(
                "The long option name must be a string. Got: {}".format(type(long_name))
            )

        if not long_name:
            raise ValueError("The long option name must not be empty.")

        if len(long_name) < 2:
            raise ValueError(
                'The long option name must contain more than one character. Got: "{}"'.format(
                    len(long_name)
                )
            )

        if not long_name[:1].isalpha():
            raise ValueError("The long option name must start with a letter")

        if not re.match(r"^[a-zA-Z0-9\-]+$", long_name):
            raise ValueError(
                "The long option name must contain letters, digits and hyphens only."
            ) 
Example 34
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def transform_body(self, search_re, replacement_string):
        managed = self.current_body()

        matched_hostnames = None
        if managed == '':
            print('Your hostsfile is not managed by webnull, we won\'t change anything')
            exit(1)
        else:
            lines = re.findall(search_re, managed, flags=re.MULTILINE)
            matched_hostnames = set(map(lambda line: re.match(r'^[^\t]+\t+([^\t]+)$', line).group(1), lines))
            if (len(matched_hostnames) != 0):
                new_managed = re.sub(search_re, replacement_string, managed, flags=re.MULTILINE)
                self.write_body(new_managed)

        return matched_hostnames 
Example 35
Project: f5go   Author: f5devcentral   File: go.py    MIT License 5 votes vote down vote up
def matches(self, kw=None):
        if kw is None:
            kw = cherrypy.request.path_info.split("/")[1]

        ret = []

        m = re.match(self.regex, kw, re.IGNORECASE)
        if m:
            deflink = self.getDefaultLink()
            for L in deflink and [deflink] or self.links:
                url = L.url(keyword=kw, args=(m.group(0), ) + m.groups())
                ret.append((L, Link(0, url, L.title)))

        return ret 
Example 36
Project: f5go   Author: f5devcentral   File: go.py    MIT License 5 votes vote down vote up
def url(self, kw=None):

        if kw is None:
            kw = cherrypy.request.path_info.split("/")[1]

        m = re.match(self.regex, kw, re.IGNORECASE)
        if not m:
            return None

        return ListOfLinks.url(self, keyword=kw, args=(m.group(0), ) + m.groups()) 
Example 37
Project: autofff   Author: ChiefGokhlayeh   File: scanner.py    MIT License 5 votes vote down vote up
def _read_symbols(self, pathToObj: str) -> SymbolTable:
        path_list = ['objdump', '-t']
        path_list += [pathToObj]

        try:
            pipe = subprocess.Popen(path_list,
                                    stdout=subprocess.PIPE,
                                    universal_newlines=True)
            text = pipe.communicate()[0]
            matches = re.finditer(
                r"(?P<object>.*):\s+file format.*\s+SYMBOL TABLE:\n(?P<symbols>(?:.+(\n|$))*)", text, re.MULTILINE)
            tables = list()
            for match in matches:
                print(match)
                objectFile = match.group('object') or pathToObj
                symbols = match.group('symbols')
                symMatch = re.search(
                    r"\*ABS\*\s+[0-9a-fA-F]*\s+(?P<source>.*)", symbols, re.MULTILINE)
                sourceFile = symMatch.group('source')
                fuNMatches = re.finditer(r"", symbols, re.MULTILINE)

                tables.append(SymbolTable(objectFile, None))
                print(objectFile)
                print(sourceFile)
                print(symbols)
        except OSError as e:
            raise RuntimeError("Unable to invoke 'readelf'.  " +
                               'Make sure its path was passed correctly\n' +
                               ('Original error: %s' % e)) 
Example 38
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_object_POST(self):
        """Test replace of a given object using ID."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "POST" in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    post_replace_response = self.client.post(
                        '{}/{}'.format(endpoints[endpoint], id_), data=json.dumps(dummy_object))
                    assert post_replace_response.status_code == 200 
Example 39
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_object_DELETE(self):
        """Test DELETE of a given object using ID."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "DELETE" in class_methods:
                    delete_response = self.client.delete(
                        '{}/{}'.format(endpoints[endpoint], id_))
                    assert delete_response.status_code == 200 
Example 40
Project: hydrus   Author: HTTP-APIs   File: test_app.py    MIT License 5 votes vote down vote up
def test_bad_requests(self):
        """Checks if bad requests are handled or not."""
        index = self.client.get("/{}".format(self.API_NAME))
        assert index.status_code == 200
        endpoints = json.loads(index.data.decode('utf-8'))
        for endpoint in endpoints:
            collection_name = "/".join(endpoints[endpoint].split(
                "/{}/".format(self.API_NAME))[1:])
            if collection_name in self.doc.collections:
                collection = self.doc.collections[collection_name]["collection"]
                class_ = self.doc.parsed_classes[collection.class_.title]["class"]
                class_methods = [x.method for x in class_.supportedOperation]
                dummy_object = gen_dummy_object(
                    collection.class_.title, self.doc)
                initial_put_response = self.client.put(
                    endpoints[endpoint], data=json.dumps(dummy_object))
                assert initial_put_response.status_code == 201
                response = json.loads(
                    initial_put_response.data.decode('utf-8'))
                regex = r'(.*)ID (.{36})* (.*)'
                matchObj = re.match(regex, response["description"])
                assert matchObj is not None
                id_ = matchObj.group(2)
                if "POST" not in class_methods:
                    dummy_object = gen_dummy_object(
                        collection.class_.title, self.doc)
                    post_replace_response = self.client.post(
                        '{}/{}'.format(endpoints[endpoint], id_), data=json.dumps(dummy_object))
                    assert post_replace_response.status_code == 405
                if "DELETE" not in class_methods:
                    delete_response = self.client.delete(
                        '{}/{}'.format(endpoints[endpoint], id_))
                    assert delete_response.status_code == 405 
Example 41
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 5 votes vote down vote up
def _imghdr_what_wrapper(data):
    """A wrapper around imghdr.what to account for jpeg files that can only be
    identified as such using their magic bytes
    See #1545
    See https://github.com/file/file/blob/master/magic/Magdir/jpeg#L12
    """
    # imghdr.what returns none for jpegs with only the magic bytes, so
    # _wider_test_jpeg is run in that case. It still returns None if it didn't
    # match such a jpeg file.
    return imghdr.what(None, h=data) or _wider_test_jpeg(data) 
Example 42
Project: explirefit   Author: codogogo   File: data_shaper.py    Apache License 2.0 5 votes vote down vote up
def is_number(token):
	return re.match('^[\d]+[,]*.?\d*$', token) is not None 
Example 43
Project: pyblish-win   Author: pyblish   File: fixcid.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def wanted(name):
    return re.match(Wanted, name) >= 0 
Example 44
Project: pyblish-win   Author: pyblish   File: imaplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _match(self, cre, s):

        # Run compiled regular expression match method on 's'.
        # Save result, return success.

        self.mo = cre.match(s)
        if __debug__:
            if self.mo is not None and self.debug >= 5:
                self._mesg("\tmatched r'%s' => %r" % (cre.pattern, self.mo.groups()))
        return self.mo is not None 
Example 45
Project: pyblish-win   Author: pyblish   File: imaplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def ParseFlags(resp):

    """Convert IMAP4 flags response to python tuple."""

    mo = Flags.match(resp)
    if not mo:
        return ()

    return tuple(mo.group('flags').split()) 
Example 46
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def splittype(url):
    """splittype('type:opaquestring') --> 'type', 'opaquestring'."""
    global _typeprog
    if _typeprog is None:
        import re
        _typeprog = re.compile('^([^/:]+):')

    match = _typeprog.match(url)
    if match:
        scheme = match.group(1)
        return scheme.lower(), url[len(scheme) + 1:]
    return None, url 
Example 47
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def splithost(url):
    """splithost('//host[:port]/path') --> 'host[:port]', '/path'."""
    global _hostprog
    if _hostprog is None:
        import re
        _hostprog = re.compile('^//([^/?]*)(.*)$')

    match = _hostprog.match(url)
    if match:
        host_port = match.group(1)
        path = match.group(2)
        if path and not path.startswith('/'):
            path = '/' + path
        return host_port, path
    return None, url 
Example 48
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def splitpasswd(user):
    """splitpasswd('user:passwd') -> 'user', 'passwd'."""
    global _passwdprog
    if _passwdprog is None:
        import re
        _passwdprog = re.compile('^([^:]*):(.*)$',re.S)

    match = _passwdprog.match(user)
    if match: return match.group(1, 2)
    return user, None

# splittag('/path#tag') --> '/path', 'tag' 
Example 49
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def splitport(host):
    """splitport('host:port') --> 'host', 'port'."""
    global _portprog
    if _portprog is None:
        import re
        _portprog = re.compile('^(.*):([0-9]*)$')

    match = _portprog.match(host)
    if match:
        host, port = match.groups()
        if port:
            return host, port
    return host, None 
Example 50
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def splittag(url):
    """splittag('/path#tag') --> '/path', 'tag'."""
    global _tagprog
    if _tagprog is None:
        import re
        _tagprog = re.compile('^(.*)#([^#]*)$')

    match = _tagprog.match(url)
    if match: return match.group(1, 2)
    return url, None