当前位置: 首页>>代码示例>>Python>>正文


Python re.escape方法代码示例

本文整理汇总了Python中re.escape方法的典型用法代码示例。如果您正苦于以下问题:Python re.escape方法的具体用法?Python re.escape怎么用?Python re.escape使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在re的用法示例。


在下文中一共展示了re.escape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_render_debug_better_error_message_recursion_error_with_multiple_duplicated_frames():
    io = BufferedIO()
    io.set_verbosity(VERBOSE)

    with pytest.raises(RecursionError) as e:
        first()

    trace = ExceptionTrace(e.value)

    trace.render(io)

    expected = r"...  Previous 2 frames repeated \d+ times".format(
        filename=re.escape(trace._get_relative_file_path(__file__)),
    )

    assert re.search(expected, io.fetch_output()) is not None 
开发者ID:sdispater,项目名称:clikit,代码行数:18,代码来源:test_exception_trace.py

示例2: update_sys_cfg_file

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def update_sys_cfg_file(uninstall_distro_dir_name):
    """
    Main function to remove uninstall distro specific operations.
    :return:
    """

    sys_cfg_file = os.path.join(config.usb_mount, "multibootusb", "syslinux.cfg")
    if not os.path.exists(sys_cfg_file):
        gen.log("syslinux.cfg file not found for updating changes.")
    else:
        gen.log("Updating syslinux.cfg file...")
        string = open(sys_cfg_file).read()
        string = re.sub(r'#start ' + re.escape(uninstall_distro_dir_name)
                        + '.*?' + '#end '
                        + re.escape(uninstall_distro_dir_name)
                        + r'\s*', '', string, flags=re.DOTALL)
        config_file = open(sys_cfg_file, "w")
        config_file.write(string)
        config_file.close() 
开发者ID:mbusb,项目名称:multibootusb,代码行数:21,代码来源:uninstall_distro.py

示例3: update_grub_cfg_file

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def update_grub_cfg_file(uninstall_distro_dir_name):
    """
    Main function to remove uninstall distro name from the grub.cfg file.
    :return:
    """

    grub_cfg_file = os.path.join(config.usb_mount, "multibootusb",
                                 "grub", "grub.cfg")
    if not os.path.exists(grub_cfg_file):
        gen.log("grub.cfg file not found for updating changes.")
    else:
        gen.log("Updating grub.cfg file...")
        string = open(grub_cfg_file).read()
        string = re.sub(r'#start ' + re.escape(uninstall_distro_dir_name)
                        + '.*?' + '#end '
                        + re.escape(uninstall_distro_dir_name)
                        + r'\s*', '', string, flags=re.DOTALL)
        config_file = open(grub_cfg_file, "w")
        config_file.write(string)
        config_file.close() 
开发者ID:mbusb,项目名称:multibootusb,代码行数:22,代码来源:uninstall_distro.py

示例4: escape_wikitext

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def escape_wikitext(wikitext):
    """Escape wikitext for use in file description."""
    rep = OrderedDict([
        ('{|', '{{(}}|'),
        ('|}', '|{{)}}'),
        ('||', '||'),
        ('|', '|'),
        ('[[', '{{!((}}'),
        (']]', '{{))!}}'),
        ('{{', '{{((}}'),
        ('}}', '{{))}}'),
        ('{', '{{(}}'),
        ('}', '{{)}}'),
    ])
    rep = dict((re.escape(k), v) for k, v in rep.iteritems())
    pattern = re.compile("|".join(rep.keys()))
    return pattern.sub(lambda m: rep[re.escape(m.group(0))], wikitext)


# Source: mediawiki.Title.js@9df363d 
开发者ID:toolforge,项目名称:video2commons,代码行数:22,代码来源:urlextract.py

示例5: test_copymode_local_to_local_symlink_dont_follow

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_copymode_local_to_local_symlink_dont_follow(tmpdir):
    test_dir = tmpdir.mkdir('test')
    src_filename = "%s\\source.txt" % test_dir
    dst_filename = "%s\\target.txt" % test_dir

    with open(src_filename, mode='w') as fd:
        fd.write(u"content")
    os.chmod(src_filename, stat.S_IREAD)

    with open(dst_filename, mode='w') as fd:
        fd.write(u"content")

    src_link = "%s\\source-link.txt" % test_dir
    dst_link = "%s\\target-link.txt" % test_dir

    os.symlink(src_filename, src_link)
    os.symlink(dst_filename, dst_link)

    expected = "chmod: follow_symlinks unavailable on this platform"
    with pytest.raises(NotImplementedError, match=re.escape(expected)):
        copymode(src_link, dst_link, follow_symlinks=False) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:23,代码来源:test_smbclient_shutil.py

示例6: test_rmtree_non_existing

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_rmtree_non_existing(smb_share):
    dir_name = "%s\\dir2" % smb_share

    expected = "[NtStatus 0xc0000034] No such file or directory: "
    with pytest.raises(OSError, match=re.escape(expected)):
        rmtree(dir_name)

    rmtree(dir_name, ignore_errors=True)

    callback_args = []

    def callback(*args):
        callback_args.append(args)

    rmtree(dir_name, onerror=callback)
    assert len(callback_args) == 2
    assert callback_args[0][0].__name__ == 'scandir'
    assert callback_args[0][1] == dir_name
    assert isinstance(callback_args[0][2][1], SMBOSError)
    assert callback_args[1][0].__name__ == 'rmdir'
    assert callback_args[1][1] == dir_name
    assert isinstance(callback_args[1][2][1], SMBOSError) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:24,代码来源:test_smbclient_shutil.py

示例7: test_rmtree_as_file

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_rmtree_as_file(smb_share):
    filename = "%s\\file.txt" % smb_share
    with open_file(filename, mode='w') as fd:
        fd.write(u"content")

    expected = "[NtStatus 0xc0000103] Not a directory: "
    with pytest.raises(OSError, match=re.escape(expected)):
        rmtree(filename)

    rmtree(filename, ignore_errors=True)

    callback_args = []

    def callback(*args):
        callback_args.append(args)

    rmtree(filename, onerror=callback)
    assert len(callback_args) == 2
    assert callback_args[0][0].__name__ == 'scandir'
    assert callback_args[0][1] == filename
    assert isinstance(callback_args[0][2][1], SMBOSError)
    assert callback_args[1][0].__name__ == 'rmdir'
    assert callback_args[1][1] == filename
    assert isinstance(callback_args[1][2][1], SMBOSError) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:26,代码来源:test_smbclient_shutil.py

示例8: test_rmtree_symlink_as_dir

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_rmtree_symlink_as_dir(smb_share):
    src_dirname = "%s\\dir" % smb_share
    dst_dirname = "%s\\target" % smb_share
    mkdir(src_dirname)
    symlink("dir", dst_dirname)

    expected = "Cannot call rmtree on a symbolic link"
    with pytest.raises(OSError, match=re.escape(expected)):
        rmtree(dst_dirname)

    assert exists(src_dirname)
    assert exists(dst_dirname)

    rmtree(dst_dirname, ignore_errors=True)

    callback_args = []

    def callback(*args):
        callback_args.append(args)

    rmtree(dst_dirname, onerror=callback)
    assert len(callback_args) == 1
    assert callback_args[0][0].__name__ == 'islink'
    assert callback_args[0][1] == dst_dirname
    assert isinstance(callback_args[0][2][1], OSError) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:27,代码来源:test_smbclient_shutil.py

示例9: test_write_exclusive_text_file

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_write_exclusive_text_file(smb_share):
    file_path = "%s\\%s" % (smb_share, "file.txt")
    file_contents = u"File Contents\nNewline"

    with smbclient.open_file(file_path, mode='x') as fd:
        assert isinstance(fd, io.TextIOWrapper)
        assert fd.closed is False

        with pytest.raises(IOError):
            fd.read()

        assert fd.tell() == 0
        fd.write(file_contents)
        assert int(fd.tell()) == (len(file_contents) - 1 + len(os.linesep))

    assert fd.closed is True

    with smbclient.open_file(file_path, mode='r') as fd:
        assert fd.read() == file_contents

    with pytest.raises(OSError, match=re.escape("[NtStatus 0xc0000035] File exists: ")):
        smbclient.open_file(file_path, mode='x')

    assert fd.closed is True 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:26,代码来源:test_smbclient_os.py

示例10: test_write_exclusive_byte_file

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_write_exclusive_byte_file(smb_share):
    file_path = "%s\\%s" % (smb_share, "file.txt")
    file_contents = b"File Contents\nNewline"

    with smbclient.open_file(file_path, mode='xb') as fd:
        assert isinstance(fd, io.BufferedWriter)
        assert fd.closed is False

        with pytest.raises(IOError):
            fd.read()

        assert fd.tell() == 0
        fd.write(file_contents)
        assert fd.tell() == len(file_contents)

    assert fd.closed is True

    with smbclient.open_file(file_path, mode='rb') as fd:
        assert fd.read() == file_contents

    with pytest.raises(OSError, match=re.escape("[NtStatus 0xc0000035] File exists: ")):
        smbclient.open_file(file_path, mode='xb')

    assert fd.closed is True 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:26,代码来源:test_smbclient_os.py

示例11: test_open_file_with_read_share_access

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_open_file_with_read_share_access(smb_share):
    file_path = "%s\\%s" % (smb_share, "file.txt")

    with smbclient.open_file(file_path, mode='w') as fd:
        fd.write(u"contents")

    with smbclient.open_file(file_path):
        expected = "[NtStatus 0xc0000043] The process cannot access the file because it is being used by " \
                   "another process"
        with pytest.raises(OSError, match=re.escape(expected)):
            smbclient.open_file(file_path)

    with smbclient.open_file(file_path, share_access='r') as fd:
        assert fd.read() == u"contents"
        with smbclient.open_file(file_path, share_access='r') as fd_child:
            assert fd_child.read() == u"contents"

        with pytest.raises(OSError):
            smbclient.open_file(file_path, mode='a') 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:21,代码来源:test_smbclient_os.py

示例12: test_open_file_with_write_share_access

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_open_file_with_write_share_access(smb_share):
    file_path = "%s\\%s" % (smb_share, "file.txt")

    with smbclient.open_file(file_path, mode='w') as fd:
        expected = "[NtStatus 0xc0000043] The process cannot access the file because it is being used by " \
                   "another process: "
        with pytest.raises(OSError, match=re.escape(expected)):
            smbclient.open_file(file_path, mode='a')

    with smbclient.open_file(file_path, mode='w', share_access='w') as fd:
        fd.write(u"contents")
        fd.flush()

        with pytest.raises(OSError):
            smbclient.open_file(file_path, mode='r')

        with smbclient.open_file(file_path, mode='a', share_access='w') as fd_child:
            fd_child.write(u"\nnewline")

    with smbclient.open_file(file_path, mode='r') as fd:
        assert fd.read() == u"contents\nnewline" 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:23,代码来源:test_smbclient_os.py

示例13: test_resolve_path_local_fail

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_resolve_path_local_fail(self):
        b_sub_name = to_bytes(u'\\??\\C:\\foldér', encoding='utf-16-le')
        b_print_name = to_bytes(u'C:\\foldér', encoding='utf-16-le')
        resp = SMB2SymbolicLinkErrorResponse()
        resp['unparsed_path_length'] = 0
        resp['substitute_name_offset'] = 0
        resp['substitute_name_length'] = len(b_sub_name)
        resp['print_name_offset'] = len(b_sub_name)
        resp['print_name_length'] = len(b_print_name)
        resp['flags'] = SymbolicLinkErrorFlags.SYMLINK_FLAG_ABSOLUTE
        resp['path_buffer'] = b_sub_name + b_print_name

        link_path = u'\\\\sérver\\sharé\\foldér'
        expected = u"Encountered symlink at '%s' that points to 'C:\\foldér' which cannot be redirected: Cannot " \
                   u"resolve link targets that point to a local path" % link_path
        with pytest.raises(SMBLinkRedirectionError, match=re.escape(to_native(expected))):
            resp.resolve_path(link_path) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:19,代码来源:test_exceptions.py

示例14: test_resolve_path_different_share

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_resolve_path_different_share(self):
        b_sub_name = to_bytes(u'\\??\\UNC\\other-sérver\\sharé\\foldér', encoding='utf-16-le')
        b_print_name = to_bytes(u'\\\\other-sérver\\sharé\\foldér', encoding='utf-16-le')
        resp = SMB2SymbolicLinkErrorResponse()
        resp['unparsed_path_length'] = 0
        resp['substitute_name_offset'] = 0
        resp['substitute_name_length'] = len(b_sub_name)
        resp['print_name_offset'] = len(b_sub_name)
        resp['print_name_length'] = len(b_print_name)
        resp['flags'] = SymbolicLinkErrorFlags.SYMLINK_FLAG_ABSOLUTE
        resp['path_buffer'] = b_sub_name + b_print_name

        link_path = u'\\\\sérver\\sharé\\foldér'
        expected = u"Encountered symlink at '%s' that points to '\\\\other-sérver\\sharé\\foldér' which cannot be " \
                   u"redirected: Cannot resolve link targets that point to a different host/share" % link_path
        with pytest.raises(SMBLinkRedirectionError, match=re.escape(to_native(expected))):
            resp.resolve_path(link_path) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:19,代码来源:test_exceptions.py

示例15: test_resolve_path_different_host

# 需要导入模块: import re [as 别名]
# 或者: from re import escape [as 别名]
def test_resolve_path_different_host(self):
        b_sub_name = to_bytes(u'\\??\\UNC\\sérver\\sharé2\\foldér', encoding='utf-16-le')
        b_print_name = to_bytes(u'\\\\sérver\\sharé2\\foldér', encoding='utf-16-le')
        resp = SMB2SymbolicLinkErrorResponse()
        resp['unparsed_path_length'] = 0
        resp['substitute_name_offset'] = 0
        resp['substitute_name_length'] = len(b_sub_name)
        resp['print_name_offset'] = len(b_sub_name)
        resp['print_name_length'] = len(b_print_name)
        resp['flags'] = SymbolicLinkErrorFlags.SYMLINK_FLAG_ABSOLUTE
        resp['path_buffer'] = b_sub_name + b_print_name

        link_path = u'\\\\sérver\\sharé\\foldér'
        expected = u"Encountered symlink at '%s' that points to '\\\\sérver\\sharé2\\foldér' which cannot be " \
                   u"redirected: Cannot resolve link targets that point to a different host/share" % link_path
        with pytest.raises(SMBLinkRedirectionError, match=re.escape(to_native(expected))):
            resp.resolve_path(link_path) 
开发者ID:jborean93,项目名称:smbprotocol,代码行数:19,代码来源:test_exceptions.py


注:本文中的re.escape方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。