Python re.escape() Examples

The following are 30 code examples for showing how to use re.escape(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module re , or try the search function .

Example 1
Project: clikit   Author: sdispater   File: test_exception_trace.py    License: MIT License 6 votes vote down vote up
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 
Example 2
Project: multibootusb   Author: mbusb   File: uninstall_distro.py    License: GNU General Public License v2.0 6 votes vote down vote up
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() 
Example 3
Project: multibootusb   Author: mbusb   File: uninstall_distro.py    License: GNU General Public License v2.0 6 votes vote down vote up
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() 
Example 4
Project: video2commons   Author: toolforge   File: urlextract.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 5
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 6 votes vote down vote up
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) 
Example 6
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 6 votes vote down vote up
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) 
Example 7
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 6 votes vote down vote up
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) 
Example 8
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 6 votes vote down vote up
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) 
Example 9
Project: smbprotocol   Author: jborean93   File: test_smbclient_os.py    License: MIT License 6 votes vote down vote up
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 
Example 10
Project: smbprotocol   Author: jborean93   File: test_smbclient_os.py    License: MIT License 6 votes vote down vote up
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 
Example 11
Project: smbprotocol   Author: jborean93   File: test_smbclient_os.py    License: MIT License 6 votes vote down vote up
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') 
Example 12
Project: smbprotocol   Author: jborean93   File: test_smbclient_os.py    License: MIT License 6 votes vote down vote up
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" 
Example 13
Project: smbprotocol   Author: jborean93   File: test_exceptions.py    License: MIT License 6 votes vote down vote up
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) 
Example 14
Project: smbprotocol   Author: jborean93   File: test_exceptions.py    License: MIT License 6 votes vote down vote up
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) 
Example 15
Project: smbprotocol   Author: jborean93   File: test_exceptions.py    License: MIT License 6 votes vote down vote up
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) 
Example 16
Project: clikit   Author: sdispater   File: test_exception_trace.py    License: MIT License 5 votes vote down vote up
def test_render_debug_better_error_message():
    io = BufferedIO()
    io.set_verbosity(DEBUG)

    try:
        fail()
    except Exception as e:  # Exception
        trace = ExceptionTrace(e)

    trace.render(io)

    expected = r"""^
  Stack trace:

  1  {}:112 in test_render_debug_better_error_message
      110\│ 
      111\│     try:
    → 112\│         fail\(\)
      113\│     except Exception as e:  # Exception
      114\│         trace = ExceptionTrace\(e\)

  Exception

  Failed

  at {}:14 in fail
       10\│ from clikit.utils._compat import PY38
       11\│ 
       12\│ 
       13\│ def fail\(\):
    →  14\│     raise Exception\("Failed"\)
       15\│ 
       16\│ 
       17\│ @pytest.mark.skipif\(PY36, reason="Legacy error messages are Python <3.6 only"\)
       18\│ def test_render_legacy_error_message\(\):
""".format(
        re.escape(trace._get_relative_file_path(__file__)),
        re.escape(trace._get_relative_file_path(__file__)),
    )

    assert re.match(expected, io.fetch_output()) is not None 
Example 17
Project: clikit   Author: sdispater   File: test_exception_trace.py    License: MIT License 5 votes vote down vote up
def test_render_verbose_better_error_message():
    io = BufferedIO()
    io.set_verbosity(VERBOSE)

    try:
        fail()
    except Exception as e:  # Exception
        trace = ExceptionTrace(e)

    trace.render(io)

    expected = r"""^
  Stack trace:

  1  {}:218 in test_render_verbose_better_error_message
     fail\(\)

  Exception

  Failed

  at {}:14 in fail
       10\│ from clikit.utils._compat import PY38
       11\│ 
       12\│ 
       13\│ def fail\(\):
    →  14\│     raise Exception\("Failed"\)
       15\│ 
       16\│ 
       17\│ @pytest.mark.skipif\(PY36, reason="Legacy error messages are Python <3.6 only"\)
       18\│ def test_render_legacy_error_message\(\):
""".format(
        re.escape(trace._get_relative_file_path(__file__)),
        re.escape(trace._get_relative_file_path(__file__)),
    )

    assert re.match(expected, io.fetch_output()) is not None 
Example 18
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    License: MIT License 5 votes vote down vote up
def _search_for_query(self, query):
        if query in self._search_pattern_cache:
            return self._search_pattern_cache[query]

        # Build pattern: include all characters
        pattern = []
        for c in query:
            # pattern.append('[^{0}]*{0}'.format(re.escape(c)))
            pattern.append('.*?{0}'.format(re.escape(c)))
        pattern = ''.join(pattern)
        search = re.compile(pattern, re.IGNORECASE).search

        self._search_pattern_cache[query] = search
        return search 
Example 19
Project: cherrypy   Author: cherrypy   File: helper.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def assertErrorPage(self, status, message=None, pattern=''):
        """Compare the response body with a built in error page.

        The function will optionally look for the regexp pattern,
        within the exception embedded in the error page."""

        # This will never contain a traceback
        page = cherrypy._cperror.get_error_page(status, message=message)

        # First, test the response body without checking the traceback.
        # Stick a match-all group (.*) in to grab the traceback.
        def esc(text):
            return re.escape(ntob(text))
        epage = re.escape(page)
        epage = epage.replace(
            esc('<pre id="traceback"></pre>'),
            esc('<pre id="traceback">') + b'(.*)' + esc('</pre>'))
        m = re.match(epage, self.body, re.DOTALL)
        if not m:
            self._handlewebError(
                'Error page does not match; expected:\n' + page)
            return

        # Now test the pattern against the traceback
        if pattern is None:
            # Special-case None to mean that there should be *no* traceback.
            if m and m.group(1):
                self._handlewebError('Error page contains traceback')
        else:
            if (m is None) or (
                not re.search(ntob(re.escape(pattern), self.encoding),
                              m.group(1))):
                msg = 'Error page does not contain %s in traceback'
                self._handlewebError(msg % repr(pattern)) 
Example 20
Project: zmirror   Author: aploium   File: utils.py    License: MIT License 5 votes vote down vote up
def calc_domain_replace_prefix(_domain):
    """生成各种形式的scheme变体
    :type _domain: str
    :rtype: bool
    """
    return dict(
        # normal
        slash='//' + _domain,
        http='http://' + _domain,
        https='https://' + _domain,
        double_quoted='"%s"' % _domain,
        single_quoted="'%s'" % _domain,
        # hex
        hex_lower=('//' + _domain).replace('/', r'\x2f'),
        hex_upper=('//' + _domain).replace('/', r'\x2F'),
        # escape slash
        slash_esc=s_esc('//' + _domain),
        http_esc=s_esc('http://' + _domain),
        https_esc=s_esc('https://' + _domain),
        double_quoted_esc=r'\"%s\"' % _domain,
        single_quoted_esc=r"\'%s\'" % _domain,
        # double escape slash
        slash_double_esc=('//' + _domain).replace('/', r'\\/'),
        http_double_esc=('http://' + _domain).replace('/', r'\\/'),
        https_double_esc=('https://' + _domain).replace('/', r'\\/'),
        # triple escape slash
        slash_triple_esc=('//' + _domain).replace('/', r'\\\/'),
        http_triple_esc=('http://' + _domain).replace('/', r'\\\/'),
        https_triple_esc=('https://' + _domain).replace('/', r'\\\/'),
        # urlencoded
        slash_ue=quote_plus('//' + _domain),
        http_ue=quote_plus('http://' + _domain),
        https_ue=quote_plus('https://' + _domain),
        double_quoted_ue=quote_plus('"%s"' % _domain),
        single_quoted_ue=quote_plus("'%s'" % _domain),
        # escaped and urlencoded
        slash_esc_ue=quote_plus(s_esc('//' + _domain)),
        http_esc_ue=quote_plus(s_esc('http://' + _domain)),
        https_esc_ue=quote_plus(s_esc('https://' + _domain)),
    ) 
Example 21
Project: zmirror   Author: aploium   File: zmirror.py    License: MIT License 5 votes vote down vote up
def _regex_generate__basic_mirrorlization():
    """产生 regex_basic_mirrorlization
    用一个函数包裹起来是因为在 try_match_and_add_domain_to_rewrite_white_list()
    中需要动态修改 external_domains, 修改以后可能需要随之生成新的正则, 包裹一下比较容易调用
    """
    from collections import Counter

    # 统计各个后缀出现的频率, 并且按照出现频率降序排列, 有助于提升正则效率
    c = Counter(re.escape(x.split(".")[-1]) for x in allowed_domains_set)
    regex_all_remote_tld = sorted(list(c.keys()), key=lambda x: c[x], reverse=True)

    regex_all_remote_tld = "(?:" + "|".join(regex_all_remote_tld) + ")"
    return re.compile(
        r"""(?:""" +
        (  # [[http(s):]//] or [\?["']] or %27 %22 or &quot;
            r"""(?P<scheme>""" +
            (  # [[http(s):]//]
                (  # [http(s):]
                    r"""(?:https?(?P<colon>{REGEX_COLON}))?""".format(REGEX_COLON=REGEX_COLON)  # https?:
                ) +
                r"""(?P<scheme_slash>%s)(?P=scheme_slash)""" % REGEX_SLASH  # //
            ) +
            r""")""" +
            r"""|""" +
            # [\?["']] or %27 %22 or &quot
            r"""(?P<quote>{REGEX_QUOTE})""".format(REGEX_QUOTE=REGEX_QUOTE)
        ) +
        r""")""" +
        # End prefix.
        # Begin domain
        r"""(?P<domain>([a-zA-Z0-9-]+\.){1,5}%s)\b""" % regex_all_remote_tld +
        # Optional suffix slash
        r"""(?P<suffix_slash>(?(scheme_slash)(?P=scheme_slash)|{SLASH}))?""".format(SLASH=REGEX_SLASH) +

        # right quote (if we have left quote)
        r"""(?(quote)(?P=quote))"""
    ) 
Example 22
Project: zmirror   Author: aploium   File: custom_func_youtube.py    License: MIT License 5 votes vote down vote up
def custom_response_text_rewriter(raw_text, content_mime, remote_url):
    # if 'html' in content_mime or 'x-www-form-urlencoded' in content_mime:
    raw_text = regex_youtube_video_videoplayback_resolve.sub(
        ('http\g<percent>3A\g<percent>2F\g<percent>2F' if my_host_scheme == 'http://' else '') +
        video_cdn_domain + '\g<percent>2Fvideoplayback\g<percent>3Fewmytbserver\g<percent>3D\g<prefix>\g<percent>26', raw_text)
    raw_text = regex_youtube_video_url_resolve.sub(
        ('http:\g<escape_slash>/\g<escape_slash>/' if my_host_scheme == 'http://' else '') + video_cdn_domain, raw_text)

    raw_text = regex_youtube_video_c_videoplayback_resolve.sub(
        ('http://' if my_host_scheme == 'http://' else '') +
        video_cdn_domain + '/videoplayback?ewmytbserver=\g<prefix>&', raw_text)

    if 'javascript' in content_mime:
        raw_text = raw_text.replace(r'\\.googlevideo\\.com$', r".*?\\."
                                    # + my_host_name_root.replace('.',r'\\.')
                                    + videocdn_video_root_domain.replace('.', r'\\.')
                                    + '$')

        _buff = re.escape(videocdn_video_root_domain) + '|' + re.escape(my_host_name_root)
        raw_text = raw_text.replace(r'-nocookie)?\.com\/|(m\.)?[a-z0-9\-]',
                                    r'-nocookie)?\.com\/|' + _buff + r'|(m\.)?[a-z0-9\-]')  # xp

        raw_text = raw_text.replace(r'googlevideo\.com|play\.google\.com|',
                                    r'googlevideo\.com|' + _buff + r'|play\.google\.com|')  # hr

        raw_text = raw_text.replace(r'prod\.google\.com|sandbox\.google\.com',
                                    r'prod\.google\.com|' + _buff + r'|sandbox\.google\.com')  # gx

        raw_text = raw_text.replace(r'corp\.google\.com|borg\.google\.com',
                                    r'corp\.google\.com|' + _buff + r'|borg\.google\.com')  # Saa

    return raw_text 
Example 23
Project: grlc   Author: CLARIAH   File: gquery.py    License: MIT License 5 votes vote down vote up
def get_enumeration_sparql(rq, v, endpoint, auth=None):
    """
    Returns a list of enumerated values for variable 'v' in query 'rq'
    """
    glogger.info('Retrieving enumeration for variable {}'.format(v))
    vcodes = []
    # tpattern_matcher = re.compile(".*(FROM\s+)?(?P<gnames>.*)\s+WHERE.*[\.\{][\n\t\s]*(?P<tpattern>.*\?" + re.escape(v) + ".*?\.).*", flags=re.DOTALL)
    # tpattern_matcher = re.compile(".*?((FROM\s*)(?P<gnames>(\<.*\>)+))?\s*WHERE\s*\{(?P<tpattern>.*)\}.*", flags=re.DOTALL)

    # WHERE is optional too!!
    tpattern_matcher = re.compile(".*?(FROM\s*(?P<gnames>\<.*\>+))?\s*(WHERE\s*)?\{(?P<tpattern>.*)\}.*",
                                  flags=re.DOTALL)

    glogger.debug(rq)
    tp_match = tpattern_matcher.match(rq)
    if tp_match:
        vtpattern = tp_match.group('tpattern')
        gnames = tp_match.group('gnames')
        glogger.debug("Detected graph names: {}".format(gnames))
        glogger.debug("Detected BGP: {}".format(vtpattern))
        glogger.debug("Matched triple pattern with parameter")
        if gnames:
            codes_subquery = re.sub("SELECT.*\{.*\}.*",
                                    "SELECT DISTINCT ?" + v + " FROM " + gnames + " WHERE { " + vtpattern + " }", rq,
                                    flags=re.DOTALL)
        else:
            codes_subquery = re.sub("SELECT.*\{.*\}.*",
                                    "SELECT DISTINCT ?" + v + " WHERE { " + vtpattern + " }", rq,
                                    flags=re.DOTALL)
        glogger.debug("Codes subquery: {}".format(codes_subquery))
        glogger.debug(endpoint)
        codes_json = requests.get(endpoint, params={'query': codes_subquery},
                                  headers={'Accept': static.mimetypes['json'],
                                           'Authorization': 'token {}'.format(static.ACCESS_TOKEN)}, auth=auth).json()
        for code in codes_json['results']['bindings']:
            vcodes.append(list(code.values())[0]["value"])
    else:
        glogger.debug("No match between variable name and query.")

    return vcodes 
Example 24
Project: hearst_patterns_python   Author: mmichelsonIF   File: hearstPatterns.py    License: Apache License 2.0 5 votes vote down vote up
def chunk(self, rawtext):
        doc = self.__spacy_nlp(rawtext)
        chunks = []
        for sentence in doc.sents:
            sentence_text = sentence.lemma_
            for chunk in sentence.noun_chunks:
                if chunk.lemma_.lower() == "example":
                    start = chunk.start
                    pre_token = sentence[start - 1].lemma_.lower()
                    post_token = sentence[start + 1].lemma_.lower()
                    if start > 0 and\
                            (pre_token == "for" or post_token == "of"):
                        continue
                if chunk.lemma_.lower() == "type":
                    continue
                chunk_arr = []
                replace_arr = []
                # print("chunk:", chunk)
                for token in chunk:
                    if token.lemma_ in self.__adj_stopwords + ["i.e.", "e.g."]:
                        continue
                    chunk_arr.append(token.lemma_)
                    # Remove punctuation and stopword adjectives
                    # (generally quantifiers of plurals)
                    if token.lemma_.isalnum():
                        replace_arr.append(token.lemma_)
                    else:
                        replace_arr.append(''.join(
                            char for char in token.lemma_ if char.isalnum()
                        ))
                if len(chunk_arr) == 0:
                    chunk_arr.append(chunk[-1].lemma_)
                chunk_lemma = ' '.join(chunk_arr)
                # print(chunk_lemma)
                replacement_value = 'NP_' + '_'.join(replace_arr)
                if chunk_lemma:
                    sentence_text = re.sub(r'\b%s\b' % re.escape(chunk_lemma),
                                           r'%s' % replacement_value,
                                           sentence_text)
            chunks.append(sentence_text)
        return chunks 
Example 25
Project: Dumb-Cogs   Author: irdumbs   File: repl.py    License: MIT License 5 votes vote down vote up
def repl_format_source(self, thing):
        """returns get_source formatted to be used in repl

        rtfs originated as this alias:
        debug (lambda cmd, bot=bot: (lambda f, out: out[0] if len(out) == 1 else (f(f,out[1:5] + (['{} more pages remaining..\njust tell them to read the actual source file man.'.format(len(out)-5)] if len(out) > 5 else [])) or out[0]))((lambda self, more: None if not more else bot.loop.create_task(bot.say('``'+'`py\n'+more.pop(0)+'``'+'`')).add_done_callback(self(self, more))), list(pagify((lambda ic, fc, pg: (lambda fcs: ic.getsourcefile(fc).split('/')[-1]+'\nline: {}'.format(fcs[1])+'``'+'`'+'\n'+'``'+'`py\n'+''.join(fcs[0]))(ic.getsourcelines(fc)))(__import__('inspect'), (cmd if not isinstance(cmd, str) else (lambda f, ms: f(f, __import__(ms.pop(0)), ms))((lambda f, prev, ms: getattr(prev, 'callback') if hasattr(prev, 'callback') else prev if not ms else f(f, getattr(prev, ms.pop(0)), ms)), cmd.split('.')) if '.' in cmd else (lambda end, cmds: end(end, cmds, bot.commands[cmds.pop(0)]).callback)((lambda end, names, cmd: cmd if not names else end(end, names, cmd.commands[names.pop(0)])), cmd.split()) ), __import__('cogs').utils.chat_formatting.pagify), delims=['\n', ' '], escape=False, shorten_by=12)) ))
        """
        source = self.get_source(thing)
        msg =  source.filename.split('/')[-1] + '\n'
        msg += 'line: {}'.format(source.line_number)
        msg += '``'+'`\n`'+'``py\n'  # codeblock break
        msg += source.source
        return msg 
Example 26
Project: Dumb-Cogs   Author: irdumbs   File: repl.py    License: MIT License 5 votes vote down vote up
def dir(self, ctx, thing: str,
                  re_search: str=".*", re_exclude: str="^_"):
        """displays the attributes of a thing

        provide a second argument as a regex pattern to search for within the list
        provide a third exclude pattern to exclude those matches from the list
        defaults to excluding items starting with an underscore "_"

        Note: be careful with double quotes since discord.py parses those as strings"""
        # re_search = re.escape(re_search)
        # re_exclude = re.escape(re_exclude)
        msg = ctx.message

        variables = {
            'ctx': ctx,
            'bot': self.bot,
            'message': msg,
            'server': msg.server,
            'channel': msg.channel,
            'author': msg.author
        }

        def call(thing, variables):
            return repr([a for a in dir(eval(thing, variables))
                         if not re.search(re_exclude, a)
                         and re.search(re_search, a)])

        closure = _close(call, thing, variables)
        await self.print_results(ctx, _call_catch_fmt(closure, 0)) 
Example 27
Project: smbprotocol   Author: jborean93   File: test_change_notify.py    License: MIT License 5 votes vote down vote up
def test_change_notify_underlying_close(self, smb_real):
        connection = Connection(uuid.uuid4(), smb_real[2], smb_real[3])
        connection.connect()
        session = Session(connection, smb_real[0], smb_real[1])
        tree = TreeConnect(session, smb_real[4])
        open = Open(tree, "directory-watch")
        try:
            session.connect()
            tree.connect()

            open.create(ImpersonationLevel.Impersonation,
                        DirectoryAccessMask.MAXIMUM_ALLOWED,
                        FileAttributes.FILE_ATTRIBUTE_DIRECTORY,
                        ShareAccess.FILE_SHARE_READ |
                        ShareAccess.FILE_SHARE_WRITE |
                        ShareAccess.FILE_SHARE_DELETE,
                        CreateDisposition.FILE_OPEN_IF,
                        CreateOptions.FILE_DIRECTORY_FILE)

            watcher = FileSystemWatcher(open)
            watcher.start(CompletionFilter.FILE_NOTIFY_CHANGE_FILE_NAME)
            assert watcher.result is None
            assert watcher.response_event.is_set() is False

            open.close()

            expected = "Received unexpected status from the server: (267) STATUS_NOTIFY_CLEANUP"
            with pytest.raises(SMBResponseException, match=re.escape(expected)):
                watcher.wait()
        finally:
            connection.disconnect(True) 
Example 28
Project: smbprotocol   Author: jborean93   File: test_change_notify.py    License: MIT License 5 votes vote down vote up
def test_change_notify_on_a_file(self, smb_real):
        connection = Connection(uuid.uuid4(), smb_real[2], smb_real[3])
        connection.connect()
        session = Session(connection, smb_real[0], smb_real[1])
        tree = TreeConnect(session, smb_real[4])
        open = Open(tree, "file-watch.txt")
        try:
            session.connect()
            tree.connect()

            open.create(ImpersonationLevel.Impersonation,
                        FilePipePrinterAccessMask.MAXIMUM_ALLOWED,
                        FileAttributes.FILE_ATTRIBUTE_NORMAL,
                        ShareAccess.FILE_SHARE_READ |
                        ShareAccess.FILE_SHARE_WRITE |
                        ShareAccess.FILE_SHARE_DELETE,
                        CreateDisposition.FILE_OPEN_IF,
                        CreateOptions.FILE_NON_DIRECTORY_FILE)

            watcher = FileSystemWatcher(open)
            watcher.start(CompletionFilter.FILE_NOTIFY_CHANGE_FILE_NAME)
            expected = "Received unexpected status from the server: (3221225485) STATUS_INVALID_PARAMETER"
            with pytest.raises(SMBResponseException, match=re.escape(expected)):
                watcher.wait()
        finally:
            connection.disconnect(True) 
Example 29
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 5 votes vote down vote up
def test_copy_raises_when_source_and_target_identical_remote(smb_share):
    filename = "%s\\file.txt" % smb_share
    with open_file(filename, mode='w') as fd:
        fd.write(u"content")

    expected = "are the same file"
    with pytest.raises(Exception, match=re.escape(expected)):
        copy(filename, filename) 
Example 30
Project: smbprotocol   Author: jborean93   File: test_smbclient_shutil.py    License: MIT License 5 votes vote down vote up
def test_copy_raises_when_source_and_target_identical_local(tmpdir):
    test_dir = tmpdir.mkdir('test').strpath
    filename = os.path.join(test_dir, 'file.txt')
    with open(filename, mode='w') as fd:
        fd.write(u"content")

    expected = "are the same file"
    with pytest.raises(Exception, match=re.escape(expected)):
        copy(filename, filename)