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: pyblish-win   Author: pyblish   File: test_gdb.py    GNU Lesser General Public License v3.0 8 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 2
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 7 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 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_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 13
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 14
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 15
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 16
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 17
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 18
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 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 vsftpd@localhost
            # 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 
Example 51
Project: pyblish-win   Author: pyblish   File: cgi.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def valid_boundary(s, _vb_pattern="^[ -~]{0,200}[!-~]$"):
    import re
    return re.match(_vb_pattern, s)

# Invoke mainline
# ===============

# Call test() when this file is run as a script (not imported as a module) 
Example 52
Project: pyblish-win   Author: pyblish   File: ccompiler.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def mkpath(self, name, mode=0777):
        mkpath(name, mode, dry_run=self.dry_run)


# class CCompiler


# Map a sys.platform/os.name ('posix', 'nt') to the default compiler
# type for that platform. Keys are interpreted as re match
# patterns. Order is important; platform mappings are preferred over
# OS names. 
Example 53
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def set_defaults(self, **kwargs):
        self._defaults.update(kwargs)

        # if these defaults match any existing arguments, replace
        # the previous default on the object with the new one
        for action in self._actions:
            if action.dest in kwargs:
                action.default = kwargs[action.dest] 
Example 54
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _match_arguments_partial(self, actions, arg_strings_pattern):
        # progressively shorten the actions list by slicing off the
        # final actions until we find a match
        result = []
        for i in range(len(actions), 0, -1):
            actions_slice = actions[:i]
            pattern = ''.join([self._get_nargs_pattern(action)
                               for action in actions_slice])
            match = _re.match(pattern, arg_strings_pattern)
            if match is not None:
                result.extend([len(string) for string in match.groups()])
                break

        # return the list of arg string counts
        return result 
Example 55
Project: pyblish-win   Author: pyblish   File: __init__.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def make_id(str):
    identifier_chars = string.ascii_letters + string.digits + "._"
    str = "".join([c if c in identifier_chars else "_" for c in str])
    if str[0] in (string.digits + "."):
        str = "_" + str
    assert re.match("^[A-Za-z_][A-Za-z0-9_.]*$", str), "FILE"+str
    return str 
Example 56
Project: pyblish-win   Author: pyblish   File: ElementTree.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def register_namespace(prefix, uri):
    if re.match("ns\d+$", prefix):
        raise ValueError("Prefix format reserved for internal use")
    for k, v in _namespace_map.items():
        if k == uri or v == prefix:
            del _namespace_map[k]
    _namespace_map[uri] = prefix 
Example 57
Project: pyblish-win   Author: pyblish   File: warnings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def filterwarnings(action, message="", category=Warning, module="", lineno=0,
                   append=0):
    """Insert an entry into the list of warnings filters (at the front).

    'action' -- one of "error", "ignore", "always", "default", "module",
                or "once"
    'message' -- a regex that the warning message must match
    'category' -- a class that the warning must be a subclass of
    'module' -- a regex that the module name must match
    'lineno' -- an integer line number, 0 matches all warnings
    'append' -- if true, append to the list of filters
    """
    import re
    assert action in ("error", "ignore", "always", "default", "module",
                      "once"), "invalid action: %r" % (action,)
    assert isinstance(message, basestring), "message must be a string"
    assert isinstance(category, (type, types.ClassType)), \
           "category must be a class"
    assert issubclass(category, Warning), "category must be a Warning subclass"
    assert isinstance(module, basestring), "module must be a string"
    assert isinstance(lineno, int) and lineno >= 0, \
           "lineno must be an int >= 0"
    item = (action, re.compile(message, re.I), category,
            re.compile(module), lineno)
    if append:
        filters.append(item)
    else:
        filters.insert(0, item) 
Example 58
Project: pyblish-win   Author: pyblish   File: warnings.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _getcategory(category):
    import re
    if not category:
        return Warning
    if re.match("^[a-zA-Z0-9_]+$", category):
        try:
            cat = eval(category)
        except NameError:
            raise _OptionError("unknown warning category: %r" % (category,))
    else:
        i = category.rfind(".")
        module = category[:i]
        klass = category[i+1:]
        try:
            m = __import__(module, None, None, [klass])
        except ImportError:
            raise _OptionError("invalid module name: %r" % (module,))
        try:
            cat = getattr(m, klass)
        except AttributeError:
            raise _OptionError("unknown warning category: %r" % (category,))
    if not issubclass(cat, Warning):
        raise _OptionError("invalid warning category: %r" % (category,))
    return cat


# Code typically replaced by _warnings 
Example 59
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def parse(self, string, name='<string>'):
        """
        Divide the given string into examples and intervening text,
        and return them as a list of alternating Examples and strings.
        Line numbers for the Examples are 0-based.  The optional
        argument `name` is a name identifying this string, and is only
        used for error messages.
        """
        string = string.expandtabs()
        # If all lines begin with the same indentation, then strip it.
        min_indent = self._min_indent(string)
        if min_indent > 0:
            string = '\n'.join([l[min_indent:] for l in string.split('\n')])

        output = []
        charno, lineno = 0, 0
        # Find all doctest examples in the string:
        for m in self._EXAMPLE_RE.finditer(string):
            # Add the pre-example text to `output`.
            output.append(string[charno:m.start()])
            # Update lineno (lines before this example)
            lineno += string.count('\n', charno, m.start())
            # Extract info from the regexp match.
            (source, options, want, exc_msg) = \
                     self._parse_example(m, name, lineno)
            # Create an Example, and add it to the list.
            if not self._IS_BLANK_OR_COMMENT(source):
                output.append( Example(source, want, exc_msg,
                                    lineno=lineno,
                                    indent=min_indent+len(m.group('indent')),
                                    options=options) )
            # Update lineno (lines inside this example)
            lineno += string.count('\n', m.start(), m.end())
            # Update charno.
            charno = m.end()
        # Add any remaining post-example text to `output`.
        output.append(string[charno:])
        return output 
Example 60
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _find_options(self, source, name, lineno):
        """
        Return a dictionary containing option overrides extracted from
        option directives in the given source string.

        `name` is the string's name, and `lineno` is the line number
        where the example starts; both are used for error messages.
        """
        options = {}
        # (note: with the current regexp, this will match at most once:)
        for m in self._OPTION_DIRECTIVE_RE.finditer(source):
            option_strings = m.group(1).replace(',', ' ').split()
            for option in option_strings:
                if (option[0] not in '+-' or
                    option[1:] not in OPTIONFLAGS_BY_NAME):
                    raise ValueError('line %r of the doctest for %s '
                                     'has an invalid option: %r' %
                                     (lineno+1, name, option))
                flag = OPTIONFLAGS_BY_NAME[option[1:]]
                options[flag] = (option[0] == '+')
        if options and self._IS_BLANK_OR_COMMENT(source):
            raise ValueError('line %r of the doctest for %s has an option '
                             'directive on a line with no example: %r' %
                             (lineno, name, source))
        return options

    # This regular expression finds the indentation of every non-blank
    # line in a string. 
Example 61
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __patched_linecache_getlines(self, filename, module_globals=None):
        m = self.__LINECACHE_FILENAME_RE.match(filename)
        if m and m.group('name') == self.test.name:
            example = self.test.examples[int(m.group('examplenum'))]
            source = example.source
            if isinstance(source, unicode):
                source = source.encode('ascii', 'backslashreplace')
            return source.splitlines(True)
        else:
            return self.save_linecache_getlines(filename, module_globals) 
Example 62
Project: pyblish-win   Author: pyblish   File: test_gdb.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def gdb_has_frame_select():
    # Does this build of gdb have gdb.Frame.select ?
    stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
    m = re.match(r'.*\[(.*)\].*', stdout)
    if not m:
        raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
    gdb_frame_dir = m.group(1).split(', ')
    return "'select'" in gdb_frame_dir 
Example 63
Project: pyblish-win   Author: pyblish   File: test_gdb.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_classic_class(self):
        'Verify the pretty-printing of classic class instances'
        gdb_repr, gdb_output = self.get_gdb_repr('''
class Foo:
    pass
foo = Foo()
foo.an_int = 42
print foo''')
        m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
        self.assertTrue(m,
                        msg='Unexpected classic-class rendering %r' % gdb_repr) 
Example 64
Project: fs_image   Author: facebookincubator   File: test_repo_downloader.py    MIT License 4 votes vote down vote up
def _check_download_error(self, url_regex, corrupt_file_fn, error_cls):
        original_open_url = repo_downloader.open_url

        def my_open_url(url):
            if re.match(url_regex, url):
                with original_open_url(url) as f:
                    return BytesIO(corrupt_file_fn(f.read()))
            return original_open_url(url)

        with tempfile.TemporaryDirectory() as storage_dir:
            downloader = self._make_downloader(storage_dir, '0/good_dog')
            with mock.patch.object(repo_downloader, 'open_url') as mock_fn:
                mock_fn.side_effect = my_open_url
                bad_snapshot = downloader.download()
                self._check_snapshot(
                    bad_snapshot, _GOOD_DOG_LOCATIONS, has_errors=True,
                )
                # Exactly one of RPMs & repodatas will have an error.
                storage_id_to_obj, = [
                    sito for sito in [
                        bad_snapshot.storage_id_to_rpm,
                        bad_snapshot.storage_id_to_repodata,
                    ] if any(not isinstance(sid, str) for sid in sito)
                ]
                yield self._check_storage_id_error(storage_id_to_obj, error_cls)

            # Re-downloading outside of the mock results in the same
            # snapshot, but with the error corrected.
            good_snapshot = downloader.download()
            self._check_snapshot(good_snapshot, _GOOD_DOG_LOCATIONS)
            self._check_repomd_equal(good_snapshot.repomd, bad_snapshot.repomd)
            for good_sito, bad_sito in [
                (
                    good_snapshot.storage_id_to_rpm,
                    bad_snapshot.storage_id_to_rpm,
                ),
                (
                    good_snapshot.storage_id_to_repodata,
                    bad_snapshot.storage_id_to_repodata,
                ),
            ]:
                # Compare the bad snapshot with the good.
                self.assertEqual(len(good_sito), len(bad_sito))
                for bad_sid, bad_obj in bad_sito.items():
                    if isinstance(bad_sid, str):
                        # By reusing the bad snapshot's storage ID, we
                        # implicitly check that the DB prevented double-
                        # storage of the objects.
                        self.assertEqual(bad_obj, good_sito[bad_sid])
                    # else:
                    #     We can't compare much since it's annoying to find
                    #     the corresponding object in `good_sido`. 
Example 65
Project: fs_image   Author: facebookincubator   File: test_repo_downloader.py    MIT License 4 votes vote down vote up
def test_lose_rpm_commit_race(self):
        'We downloaded & stored an RPM, but in the meantime some other '
        'writer committed the same RPM.'
        original_get_canonical = RepoDBContext.get_rpm_canonical_checksums

        # When we download the RPM, the mock `my_maybe_store` writes a
        # single `Checksum` here, the canonical one for the `mice` RPM.
        # Then, during mutable RPM detection, the mock `my_get_canonical`
        # grabs this checksum (since it's not actually in the DB).
        mice_canonical_checksums = []
        # The mice object which was "previously stored" via the mocks
        mice_rpms = []

        def my_get_canonical(self, table, filename):
            if filename == 'rpm-test-mice-0.1-a.x86_64.rpm':
                return {mice_canonical_checksums[0]}
            return original_get_canonical(self, table, filename)

        original_maybe_store = RepoDBContext.maybe_store

        def my_maybe_store(self, table, obj, storage_id):
            if re.match(MICE_01_RPM_REGEX, obj.location):
                mice_rpms.append(obj)
                assert not mice_canonical_checksums, mice_canonical_checksums
                mice_canonical_checksums.append(obj.canonical_checksum)
                return f'fake_already_stored_{obj.location}'
            return original_maybe_store(self, table, obj, storage_id)

        with mock.patch.object(
            RepoDBContext, 'get_rpm_canonical_checksums', new=my_get_canonical,
        ), 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()
            mice_rpm, = mice_rpms
            # We should be using the winning writer's storage ID.
            self.assertEqual(
                mice_rpm,
                snapshot.storage_id_to_rpm[
                    f'fake_already_stored_{mice_rpm.location}'
                ],
            ) 
Example 66
Project: clikit   Author: sdispater   File: argument.py    MIT License 4 votes vote down vote up
def __init__(
        self, name, flags=0, description=None, default=None
    ):  # type: (str, int, Optional[str], Any) -> None
        if not isinstance(name, basestring):
            raise ValueError(
                "The argument name must be a string. Got: {}".format(type(name))
            )

        if not name:
            raise ValueError("The argument name must not be empty.")

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

        if not re.match(r"^[a-zA-Z0-9\-]+$", name):
            raise ValueError(
                "The argument name must contain letters, digits and hyphens only."
            )

        if description is not None:
            if not isinstance(description, basestring):
                raise ValueError(
                    "The argument description must be a string. Got: {}".format(
                        type(description)
                    )
                )

            if not description:
                raise ValueError("The argument description must not be empty.")

        self._validate_flags(flags)

        flags = self._add_default_flags(flags)

        self._name = name
        self._flags = flags
        self._description = description

        if self.is_multi_valued():
            self._default = []
        else:
            self._default = None

        if self.is_optional() or default is not None:
            self.set_default(default) 
Example 67
Project: clikit   Author: sdispater   File: choice_question.py    MIT License 4 votes vote down vote up
def validate(self, selected):
        """
        Validate a choice.
        """
        # Collapse all spaces.
        if isinstance(selected, int):
            selected = str(selected)

        selected_choices = selected.replace(" ", "")

        if self._question.supports_multiple_choices():
            # Check for a separated comma values
            if not re.match("^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$", selected_choices):
                raise ValueError(self._question.error_message.format(selected))

            selected_choices = selected_choices.split(",")
        else:
            selected_choices = [selected]

        multiselect_choices = []
        for value in selected_choices:
            results = []

            for key, choice in enumerate(self._values):
                if choice == value:
                    results.append(key)

            if len(results) > 1:
                raise ValueError(
                    "The provided answer is ambiguous. Value should be one of {}.".format(
                        " or ".join(str(r) for r in results)
                    )
                )

            try:
                result = self._values.index(value)
                result = self._values[result]
            except ValueError:
                try:
                    value = int(value)

                    if value < len(self._values):
                        result = self._values[value]
                    else:
                        result = False
                except ValueError:
                    result = False

            if result is False:
                raise ValueError(self._question.error_message.format(value))

            multiselect_choices.append(result)

        if self._question.supports_multiple_choices():
            return multiselect_choices

        return multiselect_choices[0] 
Example 68
Project: f5go   Author: f5devcentral   File: go.py    MIT License 4 votes vote down vote up
def default(self, *rest, **kwargs):
        self.redirectIfNotFullHostname()

        keyword = rest[0]
        rest = rest[1:]

        forceListDisplay = False

        if keyword[0] == ".":  # force list page instead of redirect
            if keyword == ".me":
                username = getSSOUsername()
                self.redirect("." + username)
            forceListDisplay = True
            keyword = keyword[1:]

        if rest:
            keyword += "/"
        elif forceListDisplay and cherrypy.request.path_info[-1] == "/":
            # allow go/keyword/ to redirect to go/keyword but go/.keyword/
            #  to go to the keyword/ index
            keyword += "/"

        # try it as a list
        try:
            ll = g_db.getList(keyword, create=False)
        except InvalidKeyword as e:
            return self.notfound(str(e))

        if not ll:  # nonexistent list
            # check against all special cases
            matches = []
            for R in list(g_db.regexes.values()):
                matches.extend([(R, L, genL) for L, genL in R.matches(keyword)])

            if not matches:
                kw = sanitary(keyword)
                if not kw:
                    return self.notfound("No match found for '%s'" % keyword)

                # serve up empty fake list
                return env.get_template('list.html').render(L=ListOfLinks(0), keyword=kw)
            elif len(matches) == 1:
                R, L, genL = matches[0]  # actual regex, generated link
                R.clicked()
                L.clicked()
                return self.redirect(deampify(genL.url()))
            else:  # len(matches) > 1
                LL = ListOfLinks(-1)  # -1 means non-editable
                LL.links = [genL for R, L, genL in matches]
                return env.get_template('list.html').render(L=LL, keyword=keyword)

        listtarget = ll.getDefaultLink()

        if listtarget and not forceListDisplay:
            ll.clicked()
            listtarget.clicked()
            return self.redirect(deampify(listtarget.url()))

        tmplList = env.get_template('list.html')
        return tmplList.render(L=ll, keyword=keyword) 
Example 69
Project: hydrus   Author: HTTP-APIs   File: crud.py    MIT License 4 votes vote down vote up
def insert_iii_with_link(instance_id: str, property_: BaseProperty,
                         property_value: str, session: scoped_session):
    """
    Inserts GraphIII triple to store a relation defined with hydra:Link.
    :param instance_id:  Id of the instance being inserted
    :param property_: Property being used as predicate in the new triple.
    :param property_value: Value of the property being inserted.
    :param session: sqlalchemy session
    :return:
    """
    if property_.type_ == "PROPERTY" or property_.type_ == "INSTANCE":
        property_.type_ = "INSTANCE"
        # If value matches with the regex then value is an id and link is to an
        # instance of a collection class otherwise value is a class_type and link
        # is to a non collection class.
        regex = r'[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}'
        matchObj = re.match(regex, property_value)
        # Link is to an instance of a collection class
        if matchObj:
            try:
                nested_instance = session.query(Instance).filter(
                    Instance.id == property_value).one()
            except NoResultFound:
                raise InstanceNotFound(id_=property_value, type_="")
            triple = GraphIII(
                subject=instance_id,
                predicate=property_.id,
                object_=nested_instance.id)
            session.add(triple)
            return triple
        # Link is to a non collection, single instance class
        else:
            try:
                nested_rdf_class = session.query(RDFClass).filter(
                    RDFClass.name == property_value).one()
            except NoResultFound:
                raise ClassNotFound(type_=property_value)
            try:
                nested_instance = session.query(Instance).filter(
                    Instance.type_ == nested_rdf_class.id).all()[-1]
            except (NoResultFound, IndexError, ValueError):
                raise InstanceNotFound(type_=nested_rdf_class.name)
            triple = GraphIII(
                subject=instance_id,
                predicate=property_.id,
                object_=nested_instance.id)
            session.add(triple)
            return triple
    else:
        session.close()
        raise NotInstanceProperty(type_=property_.name) 
Example 70
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 4 votes vote down vote up
def _safe_cast(out_type, val):
    """Try to covert val to out_type but never raise an exception. If
    the value can't be converted, then a sensible default value is
    returned. out_type should be bool, int, or unicode; otherwise, the
    value is just passed through.
    """
    if val is None:
        return None

    if out_type == int:
        if isinstance(val, int) or isinstance(val, float):
            # Just a number.
            return int(val)
        else:
            # Process any other type as a string.
            if isinstance(val, bytes):
                val = val.decode('utf-8', 'ignore')
            elif not isinstance(val, six.string_types):
                val = six.text_type(val)
            # Get a number from the front of the string.
            match = re.match(r'[\+-]?[0-9]+', val.strip())
            return int(match.group(0)) if match else 0

    elif out_type == bool:
        try:
            # Should work for strings, bools, ints:
            return bool(int(val))
        except ValueError:
            return False

    elif out_type == six.text_type:
        if isinstance(val, bytes):
            return val.decode('utf-8', 'ignore')
        elif isinstance(val, six.text_type):
            return val
        else:
            return six.text_type(val)

    elif out_type == float:
        if isinstance(val, int) or isinstance(val, float):
            return float(val)
        else:
            if isinstance(val, bytes):
                val = val.decode('utf-8', 'ignore')
            else:
                val = six.text_type(val)
            match = re.match(r'[\+-]?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)',
                             val.strip())
            if match:
                val = match.group(0)
                if val:
                    return float(val)
            return 0.0

    else:
        return val


# Image coding for ASF/WMA. 
Example 71
Project: pyblish-win   Author: pyblish   File: gprof2html.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def main():
    filename = "gprof.out"
    if sys.argv[1:]:
        filename = sys.argv[1]
    outputfilename = filename + ".html"
    input = add_escapes(file(filename))
    output = file(outputfilename, "w")
    output.write(header % filename)
    for line in input:
        output.write(line)
        if line.startswith(" time"):
            break
    labels = {}
    for line in input:
        m = re.match(r"(.*  )(\w+)\n", line)
        if not m:
            output.write(line)
            break
        stuff, fname = m.group(1, 2)
        labels[fname] = fname
        output.write('%s<a name="flat:%s" href="#call:%s">%s</a>\n' %
                     (stuff, fname, fname, fname))
    for line in input:
        output.write(line)
        if line.startswith("index % time"):
            break
    for line in input:
        m = re.match(r"(.*  )(\w+)(( &lt;cycle.*&gt;)? \[\d+\])\n", line)
        if not m:
            output.write(line)
            if line.startswith("Index by function name"):
                break
            continue
        prefix, fname, suffix = m.group(1, 2, 3)
        if fname not in labels:
            output.write(line)
            continue
        if line.startswith("["):
            output.write('%s<a name="call:%s" href="#flat:%s">%s</a>%s\n' %
                         (prefix, fname, fname, fname, suffix))
        else:
            output.write('%s<a href="#call:%s">%s</a>%s\n' %
                         (prefix, fname, fname, suffix))
    for line in input:
        for part in re.findall(r"(\w+(?:\.c)?|\W+)", line):
            if part in labels:
                part = '<a href="#call:%s">%s</a>' % (part, part)
            output.write(part)
    output.write(trailer)
    output.close()
    webbrowser.open("file:" + os.path.abspath(outputfilename)) 
Example 72
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def splitquery(url):
    """splitquery('/path?query') --> '/path', 'query'."""
    global _queryprog
    if _queryprog is None:
        import re
        _queryprog = re.compile('^(.*)\?([^?]*)$')

    match = _queryprog.match(url)
    if match: return match.group(1, 2)
    return url, None 
Example 73
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def proxy_bypass_macosx_sysconf(host):
        """
        Return True iff this host shouldn't be accessed using a proxy

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        """
        import re
        import socket
        from fnmatch import fnmatch

        hostonly, port = splitport(host)

        def ip2num(ipAddr):
            parts = ipAddr.split('.')
            parts = map(int, parts)
            if len(parts) != 4:
                parts = (parts + [0, 0, 0, 0])[:4]
            return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]

        proxy_settings = _get_proxy_settings()

        # Check for simple host names:
        if '.' not in host:
            if proxy_settings['exclude_simple']:
                return True

        hostIP = None

        for value in proxy_settings.get('exceptions', ()):
            # Items in the list are strings like these: *.local, 169.254/16
            if not value: continue

            m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
            if m is not None:
                if hostIP is None:
                    try:
                        hostIP = socket.gethostbyname(hostonly)
                        hostIP = ip2num(hostIP)
                    except socket.error:
                        continue

                base = ip2num(m.group(1))
                mask = m.group(2)
                if mask is None:
                    mask = 8 * (m.group(1).count('.') + 1)

                else:
                    mask = int(mask[1:])
                mask = 32 - mask

                if (hostIP >> mask) == (base >> mask):
                    return True

            elif fnmatch(host, value):
                return True

        return False 
Example 74
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def getproxies_registry():
        """Return a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        """
        proxies = {}
        try:
            import _winreg
        except ImportError:
            # Std module, so should be around - but you never know!
            return proxies
        try:
            internetSettings = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
                r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
            proxyEnable = _winreg.QueryValueEx(internetSettings,
                                               'ProxyEnable')[0]
            if proxyEnable:
                # Returned as Unicode but problems if not converted to ASCII
                proxyServer = str(_winreg.QueryValueEx(internetSettings,
                                                       'ProxyServer')[0])
                if '=' in proxyServer:
                    # Per-protocol settings
                    for p in proxyServer.split(';'):
                        protocol, address = p.split('=', 1)
                        # See if address has a type:// prefix
                        import re
                        if not re.match('^([^/:]+)://', address):
                            address = '%s://%s' % (protocol, address)
                        proxies[protocol] = address
                else:
                    # Use one setting for all protocols
                    if proxyServer[:5] == 'http:':
                        proxies['http'] = proxyServer
                    else:
                        proxies['http'] = 'http://%s' % proxyServer
                        proxies['https'] = 'https://%s' % proxyServer
                        proxies['ftp'] = 'ftp://%s' % proxyServer
            internetSettings.Close()
        except (WindowsError, ValueError, TypeError):
            # Either registry key not found etc, or the value in an
            # unexpected format.
            # proxies already set up to be empty so nothing to do
            pass
        return proxies 
Example 75
Project: pyblish-win   Author: pyblish   File: urllib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def proxy_bypass_registry(host):
        try:
            import _winreg
            import re
        except ImportError:
            # Std modules, so should be around - but you never know!
            return 0
        try:
            internetSettings = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
                r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
            proxyEnable = _winreg.QueryValueEx(internetSettings,
                                               'ProxyEnable')[0]
            proxyOverride = str(_winreg.QueryValueEx(internetSettings,
                                                     'ProxyOverride')[0])
            # ^^^^ Returned as Unicode but problems if not converted to ASCII
        except WindowsError:
            return 0
        if not proxyEnable or not proxyOverride:
            return 0
        # try to make a host list from name and IP address.
        rawHost, port = splitport(host)
        host = [rawHost]
        try:
            addr = socket.gethostbyname(rawHost)
            if addr != rawHost:
                host.append(addr)
        except socket.error:
            pass
        try:
            fqdn = socket.getfqdn(rawHost)
            if fqdn != rawHost:
                host.append(fqdn)
        except socket.error:
            pass
        # make a check value list from the registry entry: replace the
        # '<local>' string by the localhost entry and the corresponding
        # canonical entry.
        proxyOverride = proxyOverride.split(';')
        # now check if we match one of the registry values.
        for test in proxyOverride:
            if test == '<local>':
                if '.' not in rawHost:
                    return 1
            test = test.replace(".", r"\.")     # mask dots
            test = test.replace("*", r".*")     # change glob sequence
            test = test.replace("?", r".")      # change glob char
            for val in host:
                # print "%s <--> %s" %( test, val )
                if re.match(test, val, re.I):
                    return 1
        return 0 
Example 76
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def add_argument(self, *args, **kwargs):
        """
        add_argument(dest, ..., name=value, ...)
        add_argument(option_string, option_string, ..., name=value, ...)
        """

        # if no positional args are supplied or only one is supplied and
        # it doesn't look like an option string, parse a positional
        # argument
        chars = self.prefix_chars
        if not args or len(args) == 1 and args[0][0] not in chars:
            if args and 'dest' in kwargs:
                raise ValueError('dest supplied twice for positional argument')
            kwargs = self._get_positional_kwargs(*args, **kwargs)

        # otherwise, we're adding an optional argument
        else:
            kwargs = self._get_optional_kwargs(*args, **kwargs)

        # if no default was supplied, use the parser-level default
        if 'default' not in kwargs:
            dest = kwargs['dest']
            if dest in self._defaults:
                kwargs['default'] = self._defaults[dest]
            elif self.argument_default is not None:
                kwargs['default'] = self.argument_default

        # create the action object, and add it to the parser
        action_class = self._pop_action_class(kwargs)
        if not _callable(action_class):
            raise ValueError('unknown action "%s"' % (action_class,))
        action = action_class(**kwargs)

        # raise an error if the action type is not callable
        type_func = self._registry_get('type', action.type, action.type)
        if not _callable(type_func):
            raise ValueError('%r is not callable' % (type_func,))

        # raise an error if the metavar does not match the type
        if hasattr(self, "_get_formatter"):
            try:
                self._get_formatter()._format_args(action, None)
            except TypeError:
                raise ValueError("length of metavar tuple does not match nargs")

        return self._add_action(action) 
Example 77
Project: pyblish-win   Author: pyblish   File: smtplib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def ehlo(self, name=''):
        """ SMTP 'ehlo' command.
        Hostname to send for this command defaults to the FQDN of the local
        host.
        """
        self.esmtp_features = {}
        self.putcmd(self.ehlo_msg, name or self.local_hostname)
        (code, msg) = self.getreply()
        # According to RFC1869 some (badly written)
        # MTA's will disconnect on an ehlo. Toss an exception if
        # that happens -ddm
        if code == -1 and len(msg) == 0:
            self.close()
            raise SMTPServerDisconnected("Server not connected")
        self.ehlo_resp = msg
        if code != 250:
            return (code, msg)
        self.does_esmtp = 1
        #parse the ehlo response -ddm
        resp = self.ehlo_resp.split('\n')
        del resp[0]
        for each in resp:
            # To be able to communicate with as many SMTP servers as possible,
            # we have to take the old-style auth advertisement into account,
            # because:
            # 1) Else our SMTP feature parser gets confused.
            # 2) There are some servers that only advertise the auth methods we
            #    support using the old style.
            auth_match = OLDSTYLE_AUTH.match(each)
            if auth_match:
                # This doesn't remove duplicates, but that's no problem
                self.esmtp_features["auth"] = self.esmtp_features.get("auth", "") \
                        + " " + auth_match.groups(0)[0]
                continue

            # RFC 1869 requires a space between ehlo keyword and parameters.
            # It's actually stricter, in that only spaces are allowed between
            # parameters, but were not going to check for that here.  Note
            # that the space isn't present if there are no parameters.
            m = re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*) ?', each)
            if m:
                feature = m.group("feature").lower()
                params = m.string[m.end("feature"):].strip()
                if feature == "auth":
                    self.esmtp_features[feature] = self.esmtp_features.get(feature, "") \
                            + " " + params
                else:
                    self.esmtp_features[feature] = params
        return (code, msg) 
Example 78
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _ellipsis_match(want, got):
    """
    Essentially the only subtle case:
    >>> _ellipsis_match('aa...aa', 'aaa')
    False
    """
    if ELLIPSIS_MARKER not in want:
        return want == got

    # Find "the real" strings.
    ws = want.split(ELLIPSIS_MARKER)
    assert len(ws) >= 2

    # Deal with exact matches possibly needed at one or both ends.
    startpos, endpos = 0, len(got)
    w = ws[0]
    if w:   # starts with exact match
        if got.startswith(w):
            startpos = len(w)
            del ws[0]
        else:
            return False
    w = ws[-1]
    if w:   # ends with exact match
        if got.endswith(w):
            endpos -= len(w)
            del ws[-1]
        else:
            return False

    if startpos > endpos:
        # Exact end matches required more characters than we have, as in
        # _ellipsis_match('aa...aa', 'aaa')
        return False

    # For the rest, we only need to find the leftmost non-overlapping
    # match for each piece.  If there's no overall match that way alone,
    # there's no overall match period.
    for w in ws:
        # w may be '' at times, if there are consecutive ellipses, or
        # due to an ellipsis at the start or end of `want`.  That's OK.
        # Search for an empty string succeeds, and doesn't change startpos.
        startpos = got.find(w, startpos, endpos)
        if startpos < 0:
            return False
        startpos += len(w)

    return True 
Example 79
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _parse_example(self, m, name, lineno):
        """
        Given a regular expression match from `_EXAMPLE_RE` (`m`),
        return a pair `(source, want)`, where `source` is the matched
        example's source code (with prompts and indentation stripped);
        and `want` is the example's expected output (with indentation
        stripped).

        `name` is the string's name, and `lineno` is the line number
        where the example starts; both are used for error messages.
        """
        # Get the example's indentation level.
        indent = len(m.group('indent'))

        # Divide source into lines; check that they're properly
        # indented; and then strip their indentation & prompts.
        source_lines = m.group('source').split('\n')
        self._check_prompt_blank(source_lines, indent, name, lineno)
        self._check_prefix(source_lines[1:], ' '*indent + '.', name, lineno)
        source = '\n'.join([sl[indent+4:] for sl in source_lines])

        # Divide want into lines; check that it's properly indented; and
        # then strip the indentation.  Spaces before the last newline should
        # be preserved, so plain rstrip() isn't good enough.
        want = m.group('want')
        want_lines = want.split('\n')
        if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
            del want_lines[-1]  # forget final newline & spaces after it
        self._check_prefix(want_lines, ' '*indent, name,
                           lineno + len(source_lines))
        want = '\n'.join([wl[indent:] for wl in want_lines])

        # If `want` contains a traceback message, then extract it.
        m = self._EXCEPTION_RE.match(want)
        if m:
            exc_msg = m.group('msg')
        else:
            exc_msg = None

        # Extract options from the source.
        options = self._find_options(source, name, lineno)

        return source, options, want, exc_msg

    # This regular expression looks for option directives in the
    # source code of an example.  Option directives are comments
    # starting with "doctest:".  Warning: this may give false
    # positives for string-literals that contain the string
    # "#doctest:".  Eliminating these false positives would require
    # actually parsing the string; but we limit them by ignoring any
    # line containing "#doctest:" that is *followed* by a quote mark. 
Example 80
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def check_output(self, want, got, optionflags):
        """
        Return True iff the actual output from an example (`got`)
        matches the expected output (`want`).  These strings are
        always considered to match if they are identical; but
        depending on what option flags the test runner is using,
        several non-exact match types are also possible.  See the
        documentation for `TestRunner` for more information about
        option flags.
        """
        # Handle the common case first, for efficiency:
        # if they're string-identical, always return true.
        if got == want:
            return True

        # The values True and False replaced 1 and 0 as the return
        # value for boolean comparisons in Python 2.3.
        if not (optionflags & DONT_ACCEPT_TRUE_FOR_1):
            if (got,want) == ("True\n", "1\n"):
                return True
            if (got,want) == ("False\n", "0\n"):
                return True

        # <BLANKLINE> can be used as a special sequence to signify a
        # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used.
        if not (optionflags & DONT_ACCEPT_BLANKLINE):
            # Replace <BLANKLINE> in want with a blank line.
            want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
                          '', want)
            # If a line in got contains only spaces, then remove the
            # spaces.
            got = re.sub('(?m)^\s*?$', '', got)
            if got == want:
                return True

        # This flag causes doctest to ignore any differences in the
        # contents of whitespace strings.  Note that this can be used
        # in conjunction with the ELLIPSIS flag.
        if optionflags & NORMALIZE_WHITESPACE:
            got = ' '.join(got.split())
            want = ' '.join(want.split())
            if got == want:
                return True

        # The ELLIPSIS flag says to let the sequence "..." in `want`
        # match any substring in `got`.
        if optionflags & ELLIPSIS:
            if _ellipsis_match(want, got):
                return True

        # We didn't find any match; return false.
        return False

    # Should we do a fancy diff?