本文整理匯總了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
示例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()
示例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()
示例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
示例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)
示例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)
示例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)
示例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)
示例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
示例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
示例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')
示例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"
示例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)
示例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)
示例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)