Python re.search() Examples

The following are 60 code examples for showing how to use re.search(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

You may also 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: wafw00f   Author: EnableSecurity   File: main.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def matchHeader(self, headermatch, attack=False):
        if attack:
            r = self.attackres
        else: r = rq
        if r is None:
            return
        header, match = headermatch
        headerval = r.headers.get(header)
        if headerval:
            # set-cookie can have multiple headers, python gives it to us
            # concatinated with a comma
            if header == 'Set-Cookie':
                headervals = headerval.split(', ')
            else:
                headervals = [headerval]
            for headerval in headervals:
                if re.search(match, headerval, re.I):
                    return True
        return False 
Example 3
Project: backtrader-cn   Author: pandalibin   File: sina.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_stock_price(self, symbol):
        """
        获取股票当前价格
        :param symbol:
        :return:
        """
        url = "http://hq.sinajs.cn/list=s_%s" % symbol
        r = self.session.get(url)
        m = re.search(r'"(.*)"', r.text)
        if m and m.groups()[0]:
            name, price, _, _, _, _ = m.groups()[0].split(',')
            logger.info("股票[%s](%s)当前价格为: %s" % (name, symbol, price))
            return price
        else:
            logger.error("获取股票%s当前价格失败" % symbol)
            raise StockMatchError()

    # fixme: 该方法暂时只支持买入A股,不支持操作美股和港股 
Example 4
Project: aegea   Author: kislyuk   File: test.py    License: Apache License 2.0 6 votes vote down vote up
def call(self, cmd, **kwargs):
        print('Running "{}"'.format(cmd), file=sys.stderr)
        expect = kwargs.pop("expect", [dict(return_codes=[os.EX_OK], stdout=None, stderr=None)])
        process = subprocess.Popen(cmd, stdin=kwargs.get("stdin", subprocess.PIPE), stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, **kwargs)
        out, err = process.communicate()
        return_code = process.poll()
        out = out.decode(sys.stdin.encoding)
        err = err.decode(sys.stdin.encoding)

        def match(return_code, out, err, expected):
            exit_ok = return_code in expected["return_codes"]
            stdout_ok = re.search(expected.get("stdout") or "", out)
            stderr_ok = re.search(expected.get("stderr") or "", err)
            return exit_ok and stdout_ok and stderr_ok
        if not any(match(return_code, out, err, exp) for exp in expect):
            print(err)
            e = subprocess.CalledProcessError(return_code, cmd, output=out)
            e.stdout, e.stderr = out, err
            raise e
        return self.SubprocessResult(out, err, return_code) 
Example 5
Project: cherrypy   Author: cherrypy   File: _cpmodpy.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_process(cmd, args=''):
    fullcmd = '%s %s' % (cmd, args)
    pipeout = popen(fullcmd)
    try:
        firstline = pipeout.readline()
        cmd_not_found = re.search(
            b'(not recognized|No such file|not found)',
            firstline,
            re.IGNORECASE
        )
        if cmd_not_found:
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output 
Example 6
Project: cWMI   Author: fireeye   File: i_to_m.py    License: Apache License 2.0 6 votes vote down vote up
def convert_param(method, param):
    # remove notation, split by upper, convert to lowercase
    param_sanitized = param.replace('*', '')
    substr = param_sanitized
    try:
        substr = re.search('([A-Z]\w+)', param_sanitized).group(1)
    except:
        pass
    case_re = re.compile(r'((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))')
    converted_param = case_re.sub(r'_\1', substr).lower()
    if converted_param in keyword.kwlist or converted_param in dir(__builtins__):
        converted_param += '_param'
    # check for duplicates. if seen, append number to end
    if 'params' in method and len([param for param in method['params'] if param['name'] == converted_param]):
        param_names = [param['name'] for param in method['params']]
        for x in range(2, 10):
            count_name = '{:s}{:d}'.format(converted_param, x)
            if count_name not in param_names:
                converted_param = count_name
                break
    return converted_param 
Example 7
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: setup.py    License: Apache License 2.0 6 votes vote down vote up
def find_version(*file_paths):
    # Open in Latin-1 so that we avoid encoding errors.
    # Use codecs.open for Python 2 compatibility
    try:
        f = codecs.open(os.path.join(here, *file_paths), 'r', 'latin1')
        version_file = f.read()
        f.close()
    except:
        raise RuntimeError("Unable to find version string.")

    # The version line must have the form
    # __version__ = 'ver'
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError("Unable to find version string.")


# Get the long description from the relevant file 
Example 8
Project: multibootusb   Author: mbusb   File: isodump3.py    License: GNU General Public License v2.0 6 votes vote down vote up
def writeDir_r(self, det_dir, dire, pp, r, all_type):
        #gen.log "writeDir_r:(%s)"%(det_dir)
        dirs = self.readDirItems(dire.locExtent, dire.lenData)
        for d in dirs:
            if not d.fIdentifier in [".", ".."]:
                if (pp != None) and (pp.search(d.fIdentifier) == None):
                    match = False
                else:
                    match = True
                #gen.log "mathing %s, %s, (%x)"%(match, d.fIdentifier, d.fFlag)
                p = det_dir + "/" + d.fIdentifier
                if d.fFlag & 0x02 == 0x02:
                    if not os.path.exists(p):
                        os.makedirs(p, 0o777)
                    if r:
                        if match:
                            self.writeDir_r(p, d, None, r, all_type) # Don't need to match subdirectory.
                        else:
                            self.writeDir_r(p, d, pp, r, all_type)
                elif match:
                    self.writeFile(d, p, all_type)
            # if not d.fIdentifier end #
        # for d in dirs end # 
Example 9
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_symbol.py    License: Apache License 2.0 6 votes vote down vote up
def test_zero_prop():
    data = mx.symbol.Variable('data')
    for i in range(10):
        data = data * data

    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256))
    big = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256), grad_req='null')
    small1 = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    data = mx.sym.stop_gradient(data)
    exe = data.simple_bind(ctx=mx.cpu(), data=(10, 3, 256, 256))
    small2 = int(re.search('Total (\d+) MB allocated', exe.debug_str()).group(1))

    assert big > small2
    assert small1 == small2 
Example 10
Project: DOTA_models   Author: ringringyi   File: wiki_data.py    License: Apache License 2.0 6 votes vote down vote up
def remove_consecutive(self, ner_tags, ner_values):
    for i in range(len(ner_tags)):
      if ((ner_tags[i] == "NUMBER" or ner_tags[i] == "MONEY" or
           ner_tags[i] == "PERCENT" or ner_tags[i] == "DATE") and
          i + 1 < len(ner_tags) and ner_tags[i] == ner_tags[i + 1] and
          ner_values[i] == ner_values[i + 1] and ner_values[i] != ""):
        word = ner_values[i]
        word = word.replace(">", "").replace("<", "").replace("=", "").replace(
            "%", "").replace("~", "").replace("$", "").replace("£", "").replace(
                "€", "")
        if (re.search("[A-Z]", word) and not (is_date(word)) and not (
            self.is_money(word))):
          ner_values[i] = "A"
        else:
          ner_values[i] = ","
    return ner_tags, ner_values 
Example 11
Project: git2net   Author: gotec   File: extraction.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def is_binary_file(filename, file_content):
    if filename is None:
        return False
    else:
        try:
            extension = re.search(r'.*\.([^\.]+)$', filename).groups()[0]
        except AttributeError:
            extension = None

        if extension in binary_extensions:
            return True
        else:
            try:
                file_content.encode('utf-8', errors='strict')
            except UnicodeEncodeError:
                return True
            else:
                return False 
Example 12
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: optimization.py    License: Apache License 2.0 5 votes vote down vote up
def _do_use_weight_decay(self, param_name):
        """Whether to use L2 weight decay for `param_name`."""
        if not self.weight_decay_rate:
            return False
        if self.exclude_from_weight_decay:
            for r in self.exclude_from_weight_decay:
                if re.search(r, param_name) is not None:
                    return False
        return True 
Example 13
Project: svviz   Author: svviz   File: setup.py    License: MIT License 5 votes vote down vote up
def get_version(string):
    """ Parse the version number variable __version__ from a script. """
    import re
    version_re = r"^__version__ = ['\"]([^'\"]*)['\"]"
    version_str = re.search(version_re, string, re.M).group(1)
    return version_str 
Example 14
Project: mutatest   Author: EvanKepner   File: conf.py    License: MIT License 5 votes vote down vote up
def find_meta(meta):
    """
    Extract __*meta*__ from META_FILE.
    """
    meta_match = re.search(r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta), META_FILE, re.M)
    if meta_match:
        return meta_match.group(1)
    raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta))


# -- Project information ----------------------------------------------------- 
Example 15
Project: mutatest   Author: EvanKepner   File: setup.py    License: MIT License 5 votes vote down vote up
def find_meta(meta):
    """
    Extract __*meta*__ from META_FILE.
    """
    meta_match = re.search(r"^__{meta}__ = ['\"]([^'\"]*)['\"]".format(meta=meta), META_FILE, re.M)
    if meta_match:
        return meta_match.group(1)
    raise RuntimeError("Unable to find __{meta}__ string.".format(meta=meta)) 
Example 16
Project: wafw00f   Author: EnableSecurity   File: main.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def matchContent(self, regex, attack=True):
        if attack:
            r = self.attackres
        else: r = rq
        if r is None:
            return
        # We may need to match multiline context in response body
        if re.search(regex, r.text, re.I):
            return True
        return False 
Example 17
Project: WazeRouteCalculator   Author: kovacsbalu   File: WazeRouteCalculator.py    License: GNU General Public License v3.0 5 votes vote down vote up
def already_coords(self, address):
        """test used to see if we have coordinates or address"""

        m = re.search(self.COORD_MATCH, address)
        return (m is not None) 
Example 18
Project: backtrader-cn   Author: pandalibin   File: xq_client.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_portfolio_info(self, portfolio_code):
        """
        获取组合信息
        :return: 字典
        """
        url = self.config['portfolio_url'] + portfolio_code
        html = self.__get_html(url)
        match_info = re.search(r'(?<=SNB.cubeInfo = ).*(?=;\n)', html)
        if match_info is None:
            raise Exception('cant get portfolio info, portfolio html : {}'.format(html))
        try:
            portfolio_info = json.loads(match_info.group())
        except Exception as e:
            raise Exception('get portfolio info error: {}'.format(e))
        return portfolio_info 
Example 19
Project: backtrader-cn   Author: pandalibin   File: sina.py    License: GNU General Public License v3.0 5 votes vote down vote up
def extract_stock_info(stock_str):
    """解析股票信息
    :param stock_str:
    :return:
    >>> empty_stock_str="var suggestdata_1511245999245="";"
    >>> extract_stock_info(empty_stock_str)
    []
    >>> cn_stock_str='var suggestdata_1511246914696="格力电器,111,000651,sz000651,格力电器,gldq,格力电器,0;格力地产,111,600185,sh600185,格力地产,gldc,格力地产,0";'
    >>> extract_stock_info(cn_stock_str)
    [{'name': '格力电器', 'type': '111', 'stock_code': '000651', 'symbol': 'sz000651', 'first_letters': 'gldq'}, {'name': '格力地产', 'type': '111', 'stock_code': '600185', 'symbol': 'sh600185', 'first_letters': 'gldc'}]

    >>> us_stock_str='var suggestdata_1511246560937="阿里巴巴,41,baba,alibaba group,阿里巴巴,albb,阿里巴巴,10;阿里那,41,arna,arena pharmaceuticals,阿里那,aln,阿里那,0;阿里阿德,41,aria,ariad pharmaceuticals,阿里阿德,alad,阿里阿德,0";'
    >>> extract_stock_info(us_stock_str)
    [{'name': '阿里巴巴', 'type': '41', 'stock_code': 'baba', 'symbol': 'alibaba group', 'first_letters': 'albb'}, {'name': '阿里那', 'type': '41', 'stock_code': 'arna', 'symbol': 'arena pharmaceuticals', 'first_letters': 'aln'}, {'name': '阿里阿德', 'type': '41', 'stock_code': 'aria', 'symbol': 'ariad pharmaceuticals', 'first_letters': 'alad'}]
    """
    stocks = []
    match = re.search(r'"(.*)"', stock_str)
    if not match:
        return stocks
    stock_info = match.groups()[0]
    str_stocks = stock_info.split(";")
    for stock in str_stocks:
        search_key, market_type, stock_code, symbol, name, first_letters, _, _ = stock.split(
            ',')
        stocks.append(
            {
                "name": name,
                "type": market_type,
                "stock_code": stock_code,
                "symbol": symbol,
                "first_letters": first_letters,
            }
        )
    return stocks 
Example 20
Project: aegea   Author: kislyuk   File: ebs.py    License: Apache License 2.0 5 votes vote down vote up
def find_volume_id(mountpoint):
    with open("/proc/mounts") as fh:
        for line in fh:
            devnode, mount, _ = line.split(" ", 2)
            if mountpoint == mount:
                break
        else:
            raise Exception("Mountpoint {} not found in /proc/mounts".format(mountpoint))
    for devnode_link in os.listdir("/dev/disk/by-id"):
        if "Elastic_Block_Store" in devnode_link and os.path.realpath("/dev/disk/by-id/" + devnode_link) == devnode:
            break
    else:
        raise Exception("EBS volume ID not found for mountpoint {} (devnode {})".format(mountpoint, devnode))
    return re.search(r"Elastic_Block_Store_(vol[\w]+)", devnode_link).group(1).replace("vol", "vol-") 
Example 21
Project: aegea   Author: kislyuk   File: ebs.py    License: Apache License 2.0 5 votes vote down vote up
def attach(args):
    if args.instance is None:
        args.instance = get_metadata("instance-id")
    devices = args.device if args.device else ["xvd" + chr(i + 1) for i in reversed(range(ord("a"), ord("z")))]
    for i, device in enumerate(devices):
        try:
            args.device = devices[i]
            res = attach_volume(args)
            break
        except ClientError as e:
            if re.search("VolumeInUse.+already attached to an instance", str(e)):
                if resources.ec2.Volume(args.volume_id).attachments[0]["InstanceId"] == args.instance:
                    logger.warn("Volume %s is already attached to instance %s", args.volume_id, args.instance)
                    break
            if i + 1 < len(devices) and re.search("InvalidParameterValue.+Attachment point.+is already in use", str(e)):
                logger.warn("BDM node %s is already in use, looking for next available node", devices[i])
                continue
            raise
    res = clients.ec2.get_waiter("volume_in_use").wait(VolumeIds=[args.volume_id])
    if args.format or args.mount:
        for i in range(30):
            try:
                find_devnode(args.volume_id)
                break
            except Exception:
                logger.debug("Waiting for device node to appear for %s", args.volume_id)
                time.sleep(1)
    if args.format:
        logger.info("Formatting %s (%s)", args.volume_id, find_devnode(args.volume_id))
        label = get_fs_label(args.volume_id)
        command = get_mkfs_command(fs_type=args.format, label=label) + find_devnode(args.volume_id)
        subprocess.check_call(command, shell=True, stdout=sys.stderr.buffer)
    if args.mount:
        logger.info("Mounting %s at %s", args.volume_id, args.mount)
        subprocess.check_call(["mount", find_devnode(args.volume_id), args.mount], stdout=sys.stderr.buffer)
    return res 
Example 22
Project: keras_mixnets   Author: titu1994   File: setup.py    License: MIT License 5 votes vote down vote up
def get_version():
    """Return package version as listed in `__version__` in `init.py`."""
    init_py = open(os.path.join(PACKAGE_NAME, '__init__.py')).read()
    return re.search("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1) 
Example 23
Project: aospy   Author: spencerahill   File: versioneer.py    License: Apache License 2.0 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 24
Project: aospy   Author: spencerahill   File: versioneer.py    License: Apache License 2.0 5 votes vote down vote up
def versions_from_file(filename):
    """Try to determine the version from _version.py if present."""
    try:
        with open(filename) as f:
            contents = f.read()
    except EnvironmentError:
        raise NotThisMethod("unable to read _version.py")
    mo = re.search(r"version_json = '''\n(.*)'''  # END VERSION_JSON",
                   contents, re.M | re.S)
    if not mo:
        mo = re.search(r"version_json = '''\r\n(.*)'''  # END VERSION_JSON",
                       contents, re.M | re.S)
    if not mo:
        raise NotThisMethod("no version_json in _version.py")
    return json.loads(mo.group(1)) 
Example 25
Project: aospy   Author: spencerahill   File: _version.py    License: Apache License 2.0 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 26
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 27
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: workflow.py    License: MIT License 5 votes vote down vote up
def get_password(self, account, service=None):
        """Retrieve the password saved at ``service/account``.

        Raise :class:`PasswordNotFound` exception if password doesn't exist.

        :param account: name of the account the password is for, e.g.
            "Pinboard"
        :type account: ``unicode``
        :param service: Name of the service. By default, this is the workflow's
                        bundle ID
        :type service: ``unicode``
        :returns: account password
        :rtype: ``unicode``

        """
        if not service:
            service = self.bundleid

        output = self._call_security('find-generic-password', service,
                                     account, '-g')

        # Parsing of `security` output is adapted from python-keyring
        # by Jason R. Coombs
        # https://pypi.python.org/pypi/keyring
        m = re.search(
            r'password:\s*(?:0x(?P<hex>[0-9A-F]+)\s*)?(?:"(?P<pw>.*)")?',
            output)

        if m:
            groups = m.groupdict()
            h = groups.get('hex')
            password = groups.get('pw')
            if h:
                password = unicode(binascii.unhexlify(h), 'utf-8')

        self.logger.debug('got password : %s:%s', service, account)

        return password 
Example 28
Project: xrft   Author: xgcm   File: versioneer.py    License: MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 29
Project: xrft   Author: xgcm   File: versioneer.py    License: MIT License 5 votes vote down vote up
def versions_from_file(filename):
    """Try to determine the version from _version.py if present."""
    try:
        with open(filename) as f:
            contents = f.read()
    except EnvironmentError:
        raise NotThisMethod("unable to read _version.py")
    mo = re.search(r"version_json = '''\n(.*)'''  # END VERSION_JSON",
                   contents, re.M | re.S)
    if not mo:
        mo = re.search(r"version_json = '''\r\n(.*)'''  # END VERSION_JSON",
                       contents, re.M | re.S)
    if not mo:
        raise NotThisMethod("no version_json in _version.py")
    return json.loads(mo.group(1)) 
Example 30
Project: xrft   Author: xgcm   File: _version.py    License: MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 31
Project: cherrypy   Author: cherrypy   File: covercp.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _skip_file(path, exclude):
    if exclude:
        return bool(re.search(exclude, path)) 
Example 32
Project: cherrypy   Author: cherrypy   File: static.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def staticfile(filename, root=None, match='', content_types=None, debug=False):
    """Serve a static resource from the given (root +) filename.

    match
        If given, request.path_info will be searched for the given
        regular expression before attempting to serve static content.

    content_types
        If given, it should be a Python dictionary of
        {file-extension: content-type} pairs, where 'file-extension' is
        a string (e.g. "gif") and 'content-type' is the value to write
        out in the Content-Type response header (e.g. "image/gif").

    """
    request = cherrypy.serving.request
    if request.method not in ('GET', 'HEAD'):
        if debug:
            cherrypy.log('request.method not GET or HEAD', 'TOOLS.STATICFILE')
        return False

    if match and not re.search(match, request.path_info):
        if debug:
            cherrypy.log('request.path_info %r does not match pattern %r' %
                         (request.path_info, match), 'TOOLS.STATICFILE')
        return False

    # If filename is relative, make absolute using "root".
    if not os.path.isabs(filename):
        if not root:
            msg = "Static tool requires an absolute filename (got '%s')." % (
                filename,)
            if debug:
                cherrypy.log(msg, 'TOOLS.STATICFILE')
            raise ValueError(msg)
        filename = os.path.join(root, filename)

    return _attempt(filename, content_types, debug=debug) 
Example 33
Project: cherrypy   Author: cherrypy   File: modfastcgi.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read_process(cmd, args=''):
    pipein, pipeout = os.popen4('%s %s' % (cmd, args))
    try:
        firstline = pipeout.readline()
        if (re.search(r'(not recognized|No such file|not found)', firstline,
                      re.IGNORECASE)):
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output 
Example 34
Project: cherrypy   Author: cherrypy   File: benchmark.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(self):
        # Parse output of ab, setting attributes on self
        try:
            self.output = _cpmodpy.read_process(AB_PATH or 'ab', self.args())
        except Exception:
            print(_cperror.format_exc())
            raise

        for attr, name, pattern in self.parse_patterns:
            val = re.search(pattern, self.output, re.MULTILINE)
            if val:
                val = val.group(1)
                setattr(self, attr, val)
            else:
                setattr(self, attr, None) 
Example 35
Project: cherrypy   Author: cherrypy   File: modwsgi.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read_process(cmd, args=''):
    pipein, pipeout = os.popen4('%s %s' % (cmd, args))
    try:
        firstline = pipeout.readline()
        if (re.search(r'(not recognized|No such file|not found)', firstline,
                      re.IGNORECASE)):
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output 
Example 36
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 37
Project: cherrypy   Author: cherrypy   File: modpy.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def read_process(cmd, args=''):
    pipein, pipeout = os.popen4('%s %s' % (cmd, args))
    try:
        firstline = pipeout.readline()
        if (re.search(r'(not recognized|No such file|not found)', firstline,
                      re.IGNORECASE)):
            raise IOError('%s must be on your system path.' % cmd)
        output = firstline + pipeout.read()
    finally:
        pipeout.close()
    return output 
Example 38
Project: django-click   Author: GaretJax   File: setup.py    License: MIT License 5 votes vote down vote up
def get_metavar(name):
        data = Setup.read(os.path.join(PACKAGE, "__init__.py"))
        value = (
            re.search(u"__{}__\s*=\s*u?'([^']+)'".format(name), data).group(1).strip()
        )
        return value 
Example 39
Project: django-template   Author: liip   File: post_gen_project.py    License: MIT License 5 votes vote down vote up
def patch_playbook(path):
    patched_lines = []
    roles_to_enable = set(['django', 'postgresql', 'webpack', 'gitlabci'])

    with open(path) as f:
        lines = f.readlines()

    for line in lines:
        role_match = re.search(r'role: (\w+)', line)

        if role_match and role_match.group(1) in roles_to_enable:
            line = line.replace('# -', '-')

        patched_lines.append(line)
    patched_lines.append(
        """
  tasks:
  - name: Make sure rsync is installed
    apt:
      state: present
      pkg: rsync
    become: yes
"""
    )

    with open(path, 'w') as f:
        f.write(''.join(patched_lines)) 
Example 40
Project: django-json-widget   Author: jmrivas86   File: setup.py    License: MIT License 5 votes vote down vote up
def get_version(*file_paths):
    """Retrieves the version from django_json_widget/__init__.py"""
    filename = os.path.join(os.path.dirname(__file__), *file_paths)
    version_file = open(filename).read()
    version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                              version_file, re.M)
    if version_match:
        return version_match.group(1)
    raise RuntimeError('Unable to find version string.')


# version = get_version("django_json_widget", "__init__.py") 
Example 41
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def extract_distroinfo_from_file(self, subpath, regex, distro_group,
                                    version_group):
        content = self.file_content(subpath)
        if not content:
            return None
        m = re.compile(regex, re.I).search(content)
        if not m:
            return None
        return (m.group(distro_group),
                [int(x) for x in m.group(version_group).split('.')]) 
Example 42
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def config_is_persistence_aware(self, content):
        """ Used to restrict update of boot parameters to persistent-aware
        menu entries if the distribution provides any.
        """
        return self.persistence_awareness_checking_re.search(content) \
            is not None 
Example 43
Project: multibootusb   Author: mbusb   File: gen.py    License: GNU General Public License v2.0 5 votes vote down vote up
def grub_efi_exist(grub_efi_path):
    """
    Detect efi present in USB disk is copied by multibootusb.
    :param isolinux_path: Path to "grub efi image"
    :return: True if yes else False
    """
    from . import iso
    if grub_efi_path is not None:
        sl = list(iso.strings(grub_efi_path))
        for strin in sl:
            if re.search(r'multibootusb', strin, re.I):
                return True
        return False 
Example 44
Project: multibootusb   Author: mbusb   File: grub.py    License: GNU General Public License v2.0 5 votes vote down vote up
def grub_raw_iso(mbus_grub_cfg_path):
    """
    Generic menu entry for booting ISO files directly using memdisk. Should have enough memory to load to RAM
    :return:
    """
    menu_entry = '    search --set -f /multibootusb/' + iso.iso_basename(config.image_path) + '/' + iso.iso_name(config.image_path) + '\n' \
                 '    menuentry ' + iso.iso_basename(config.image_path) + ' {\n' \
                 '    linux16 /multibootusb/memdisk iso raw vmalloc=750M\n' \
                 '    initrd16 /multibootusb/' + iso.iso_basename(config.image_path) + '/' + iso.iso_name(config.image_path) + '\n' \
                 '}\n'
    with open(mbus_grub_cfg_path, 'a') as f:
        f.write("#start " + iso.iso_basename(config.image_path) + "\n")
        f.write(menu_entry)
        f.write("#end " + iso.iso_basename(config.image_path) + "\n")
    return menu_entry 
Example 45
Project: multibootusb   Author: mbusb   File: isodump3.py    License: GNU General Public License v2.0 5 votes vote down vote up
def checkIntegrity(self):
        if self.priVol == None: # no primary volume
            return False

        if self.priVol.ptSize == 0: # empty ?
            return True

        path_table = self.readPathtableL()
        if path_table == []: # pathtable record is broken.
            return False

        # find last file item to check
        for dr in reversed(path_table):
            #gen.log dr.fIdentifier
            dirs = self.readDirItems(dr.locExtent, BLOCK_SIZE)
            if len(dirs) > 2:
                dot = dirs[0]
                dirs2 = self.readDirItems(dot.locExtent, dot.lenData) # get the whole items.
                for dr2 in reversed(dirs2): # search last file item.
                    if dr2.fFlag == 0:
                        #gen.log "get last file(%s)"%(dr2.fIdentifier)
                        try:
                            #self.isoFile.seek(BLOCK_SIZE * dr2.locExtent+dr2.lenData)
                            lastfile_end = BLOCK_SIZE * dr2.locExtent + dr2.lenData
                            self.isoFile.seek(0, os.SEEK_END)
                            iso_end = self.isoFile.tell()
                            #gen.log("%d-->%d")%(lastfile_end, iso_end)
                            if iso_end >= lastfile_end:
                                return True
                            else:
                                return False
                        except(IOError):
                            #gen.log "exception when seek. iso is broken"
                            return False
            elif len(dirs) < 2: # Dir record is broken. At least, should have two entries.
               return False
        return True

########################################################################### 
Example 46
Project: multibootusb   Author: mbusb   File: isodump3.py    License: GNU General Public License v2.0 5 votes vote down vote up
def usage():
    """ Prompt user how to use   """
    gen.log("""
Usage: isodump  dump-what [options]  iso-file
       [dump-what]
       -----------
       boot               - Dump boot record.
       primary-volume     - Dump primary volume.
       pathtable          - Dump path table.
       dir-record [block number] [length] - Dump a raw data of a Directory Record

       iso:/dir [-r]  [-o output] [-p pattern]  - Dump a dirctory or file to [output]
           -r    recursively visit directory.
           -p    spcify a Regular expression pattern for re.search(pattern,).

isodump xx.iso              - Dump the root directory
isodump pathtable xx.iso    - Dump the path table record.

isodump iso:/ -r     xx.iso
    -- Dump the root directory of xx.iso recursively.

isodump iso:/ -r -o /tmp/iso    xx.iso
    -- Extract the iso to /tmp/iso/.

isodump iso:/boot -o /tmp/iso/boot    xx.iso
    -- Extract the /boot directory of xx.iso to /tmp/iso/boot.

isodump iso:/boot/grup.cfg -o /tmp/grub.cfg  xx.iso
    -- Extract the file "grup.cfg" to "/tmp/grub.cfg"

isodump iso:/boot -r -o /tmp/iso -p "*.cfg"  xx.iso
    -- Extract any files or directories under /boot maching "*.cfg" to /tmp/iso/.
""")
    sys.exit(-1) 
Example 47
Project: python-clean-architecture   Author: pcah   File: predicate.py    License: MIT License 5 votes vote down vote up
def search(self, regex: str) -> Predicate:
        """
        Run a regex test against the value (only substring string has to
        match).
        >>> var('f1').search(r'^\\w+$')

        :param regex: The regular expression to use for matching
        """
        return self._build_predicate(
            lambda lhs, value: bool(re.search(regex, lhs)),
            Operation.SEARCH,
            (self._path, regex)
        ) 
Example 48
Project: NiBetaSeries   Author: HBClab   File: versioneer.py    License: MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 49
Project: NiBetaSeries   Author: HBClab   File: versioneer.py    License: MIT License 5 votes vote down vote up
def versions_from_file(filename):
    """Try to determine the version from _version.py if present."""
    try:
        with open(filename) as f:
            contents = f.read()
    except EnvironmentError:
        raise NotThisMethod("unable to read _version.py")
    mo = re.search(r"version_json = '''\n(.*)'''  # END VERSION_JSON",
                   contents, re.M | re.S)
    if not mo:
        mo = re.search(r"version_json = '''\r\n(.*)'''  # END VERSION_JSON",
                       contents, re.M | re.S)
    if not mo:
        raise NotThisMethod("no version_json in _version.py")
    return json.loads(mo.group(1)) 
Example 50
Project: NiBetaSeries   Author: HBClab   File: _version.py    License: MIT License 5 votes vote down vote up
def git_get_keywords(versionfile_abs):
    """Extract version information from the given file."""
    # the code embedded in _version.py can just fetch the value of these
    # keywords. When used from setup.py, we don't want to import _version.py,
    # so we do it with a regexp instead. This function is not used from
    # _version.py.
    keywords = {}
    try:
        f = open(versionfile_abs, "r")
        for line in f.readlines():
            if line.strip().startswith("git_refnames ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["refnames"] = mo.group(1)
            if line.strip().startswith("git_full ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["full"] = mo.group(1)
            if line.strip().startswith("git_date ="):
                mo = re.search(r'=\s*"(.*)"', line)
                if mo:
                    keywords["date"] = mo.group(1)
        f.close()
    except EnvironmentError:
        pass
    return keywords 
Example 51
Project: hearst_patterns_python   Author: mmichelsonIF   File: hearstPatterns.py    License: Apache License 2.0 5 votes vote down vote up
def find_hyponyms(self, rawtext):

        hyponyms = []
        np_tagged_sentences = self.chunk(rawtext)

        for sentence in np_tagged_sentences:
            # two or more NPs next to each other should be merged
            # into a single NP, it's a chunk error

            for (hearst_pattern, parser) in self.__hearst_patterns:
                matches = re.search(hearst_pattern, sentence)
                if matches:
                    match_str = matches.group(0)

                    nps = [a for a in match_str.split() if a.startswith("NP_")]

                    if parser == "first":
                        general = nps[0]
                        specifics = nps[1:]
                    else:
                        general = nps[-1]
                        specifics = nps[:-1]

                    for i in range(len(specifics)):
                        pair = (
                            self.clean_hyponym_term(specifics[i]),
                            self.clean_hyponym_term(general)
                        )
                        # reduce duplicates
                        if pair not in hyponyms:
                            hyponyms.append(pair)

        return hyponyms 
Example 52
Project: sqliv   Author: the-robot   File: crawler.py    License: GNU General Public License v3.0 5 votes vote down vote up
def requestfinish(self, queue, queue_item, new_queue_items):
        # Called after the crawler finishes a request. Default is a null route.
        url = queue_item.request.url
        if re.search('(.*?)(.php\?|.asp\?|.apsx\?|.jsp\?)(.*?)=(.*?)', url):
            if not url in self.links:
                self.links.append(url)
        return CrawlerActions.DO_CONTINUE_CRAWLING 
Example 53
Project: DOTA_models   Author: ringringyi   File: wiki_data.py    License: Apache License 2.0 5 votes vote down vote up
def load_qa(self):
    data_source = os.path.join(self.data_folder, self.data_name)
    f = tf.gfile.GFile(data_source, "r")
    id_regex = re.compile("\(id ([^\)]*)\)")
    for line in f:
      id_match = id_regex.search(line)
      id = id_match.group(1)
      self.examples.append(id) 
Example 54
Project: DOTA_models   Author: ringringyi   File: wiki_data.py    License: Apache License 2.0 5 votes vote down vote up
def is_date(word):
  if (not (bool(re.search("[a-z0-9]", word, re.IGNORECASE)))):
    return False
  if (len(word) != 10):
    return False
  if (word[4] != "-"):
    return False
  if (word[7] != "-"):
    return False
  for i in range(len(word)):
    if (not (word[i] == "X" or word[i] == "x" or word[i] == "-" or re.search(
        "[0-9]", word[i]))):
      return False
  return True 
Example 55
Project: DOTA_models   Author: ringringyi   File: wiki_data.py    License: Apache License 2.0 5 votes vote down vote up
def is_money(self, word):
    if (not (bool(re.search("[a-z0-9]", word, re.IGNORECASE)))):
      return False
    for i in range(len(word)):
      if (not (word[i] == "E" or word[i] == "." or re.search("[0-9]",
                                                             word[i]))):
        return False
    return True 
Example 56
Project: DOTA_models   Author: ringringyi   File: wiki_data.py    License: Apache License 2.0 5 votes vote down vote up
def pre_process_sentence(self, tokens, ner_tags, ner_values):
    sentence = []
    tokens = tokens.split("|")
    ner_tags = ner_tags.split("|")
    ner_values = ner_values.split("|")
    ner_tags, ner_values = self.remove_consecutive(ner_tags, ner_values)
    #print "old: ", tokens
    for i in range(len(tokens)):
      word = tokens[i]
      if (ner_values[i] != "" and
          (ner_tags[i] == "NUMBER" or ner_tags[i] == "MONEY" or
           ner_tags[i] == "PERCENT" or ner_tags[i] == "DATE")):
        word = ner_values[i]
        word = word.replace(">", "").replace("<", "").replace("=", "").replace(
            "%", "").replace("~", "").replace("$", "").replace("£", "").replace(
                "€", "")
        if (re.search("[A-Z]", word) and not (is_date(word)) and not (
            self.is_money(word))):
          word = tokens[i]
        if (is_number(ner_values[i])):
          word = float(ner_values[i])
        elif (is_number(word)):
          word = float(word)
        if (tokens[i] == "score"):
          word = "score"
      if (is_number(word)):
        word = float(word)
      if (not (self.annotated_word_reject.has_key(word))):
        if (is_number(word) or is_date(word) or self.is_money(word)):
          sentence.append(word)
        else:
          word = full_normalize(word)
          if (not (self.annotated_word_reject.has_key(word)) and
              bool(re.search("[a-z0-9]", word, re.IGNORECASE))):
            m = re.search(",", word)
            sentence.append(word.replace(",", ""))
    if (len(sentence) == 0):
      sentence.append("UNK")
    return sentence 
Example 57
Project: OpenFermion-Cirq   Author: quantumlib   File: incremental_coverage.py    License: Apache License 2.0 5 votes vote down vote up
def line_counts_as_uncovered(line: str,
                             is_from_cover_annotation_file: bool) -> bool:
    """
    Args:
        line: The line of code (including coverage annotation).
        is_from_cover_annotation_file: Whether this line has been annotated.
    Returns:
        Does the line count as uncovered?
    """

    # Ignore this line?
    if is_from_cover_annotation_file:
        # Already covered, or the tool deemed it not relevant for coverage.
        if not line.startswith('! '):
            return False

        content = line[2:]
    else:
        content = line

    # Ignore surrounding whitespace.
    content = content.strip()

    # Ignore end-of-line comments.
    # TODO: avoid # in strings, etc.
    if '#' in content:
        content = content[:content.index('#')].strip()

    # Ignored line pattern?
    if any(re.search(pat, content) for pat in IGNORED_LINE_PATTERNS):
        return False

    return (is_from_cover_annotation_file or
            line_content_counts_as_uncovered_manual(content)) 
Example 58
Project: OpenFermion-Cirq   Author: quantumlib   File: incremental_coverage.py    License: Apache License 2.0 5 votes vote down vote up
def is_applicable_python_file(rel_path: str) -> bool:
    """
    Determines if a file should be included in incremental coverage analysis.

    Args:
        rel_path: The repo-relative file path being considered.
    Returns:
        Whether to include the file.
    """
    return (rel_path.endswith('.py') and
            not any(re.search(pat, rel_path) for pat in IGNORED_FILE_PATTERNS)) 
Example 59
Project: aws-waf-security-automations   Author: awslabs   File: partition_s3_logs.py    License: Apache License 2.0 5 votes vote down vote up
def parse_cloudfront_logs(key, filename):
    # Get year, month, day and hour
    time_stamp = re.search('(\\d{4})-(\\d{2})-(\\d{2})-(\\d{2})', key)
    year, month, day, hour = time_stamp.group(0).split('-')

    # Create destination path
    dest = 'AWSLogs-Partitioned/year={}/month={}/day={}/hour={}/{}' \
           .format(year, month, day, hour, filename)

    return dest 
Example 60
Project: aws-waf-security-automations   Author: awslabs   File: partition_s3_logs.py    License: Apache License 2.0 5 votes vote down vote up
def parse_alb_logs(key, filename):
    # Get year, month and day
    time_stamp = re.search('(\\d{4})/(\\d{2})/(\\d{2})', key)
    year, month, day = time_stamp.group(0).split('/')

    # Get hour
    time_stamp = re.search('(\\d{8})T(\\d{2})', filename)
    hour = time_stamp.group(0).split('T')[1]

    # Create destination path
    dest = 'AWSLogs-Partitioned/year={}/month={}/day={}/hour={}/{}' \
           .format(year, month, day, hour, filename)

    return dest