Python re.MULTILINE Examples

The following are 30 code examples for showing how to use re.MULTILINE(). 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: recipes-py   Author: luci   File: magic_check_fn.py    License: Apache License 2.0 9 votes vote down vote up
def render_re(regex):
  """Renders a repr()-style value for a compiled regular expression."""
  actual_flags = []
  if regex.flags:
    flags = [
      (re.IGNORECASE, 'IGNORECASE'),
      (re.LOCALE, 'LOCALE'),
      (re.UNICODE, 'UNICODE'),
      (re.MULTILINE, 'MULTILINE'),
      (re.DOTALL, 'DOTALL'),
      (re.VERBOSE, 'VERBOSE'),
    ]
    for val, name in flags:
      if regex.flags & val:
        actual_flags.append(name)
  if actual_flags:
    return 're.compile(%r, %s)' % (regex.pattern, '|'.join(actual_flags))
  else:
    return 're.compile(%r)' % regex.pattern 
Example 2
Project: JetPack   Author: dsp-jetpack   File: ssh.py    License: Apache License 2.0 8 votes vote down vote up
def ssh_edit_file(adress, user, passw, remotefile, regex, replace):
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        trans = paramiko.Transport((adress, 22))
        trans.connect(username=user, password=passw)
        sftp = paramiko.SFTPClient.from_transport(trans)
        f_in = sftp.file(remotefile, "r")
        c_in = f_in.read()
        pattern = re.compile(regex, re.MULTILINE | re.DOTALL)
        c_out = pattern.sub(replace, c_in)
        f_out = sftp.file(remotefile, "w")
        f_out.write(c_out)
        f_in.close()
        f_out.close()
        sftp.close()
        trans.close() 
Example 3
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 6 votes vote down vote up
def tweak(self, content):
        tweaked = self.legacy_tweak(content)
        if tweaked:
            return tweaked
        apply_persistence_to_all_lines = \
            0 < self.setup_params.persistence_size and \
            not self.config_is_persistence_aware(content)
        matching_re = r'^(\s*(%s)\s*)(.*)$' % self.BOOT_PARAMS_STARTER
        kernel_parameter_line_pattern = re.compile(
            matching_re,
            flags = re.I | re.MULTILINE)
        out = self.tweak_first_match(
            content,
            kernel_parameter_line_pattern,
            apply_persistence_to_all_lines,
            self.param_operations(),
            self.param_operations_for_persistence())
        
        return self.post_process(out) 
Example 4
Project: razzy-spinner   Author: rafasashi   File: nemo_app.py    License: GNU General Public License v3.0 6 votes vote down vote up
def substitute(self,*args):
        for color in colors:
            self.txt.tag_remove(color,"1.0","end")
            self.txt.tag_remove("emph"+color,"1.0","end")
        self.rex = re.compile("") # default value in case of misformed regexp
        self.rex = re.compile(self.fld.get("1.0","end")[:-1],re.MULTILINE)
        try:
            re.compile("(?P<emph>%s)" % self.fld.get(tk.SEL_FIRST,
                                                      tk.SEL_LAST))
            self.rexSel = re.compile("%s(?P<emph>%s)%s" % (
                self.fld.get("1.0",tk.SEL_FIRST),
                self.fld.get(tk.SEL_FIRST,tk.SEL_LAST),
                self.fld.get(tk.SEL_LAST,"end")[:-1],
            ),re.MULTILINE)
        except:
            self.rexSel = self.rex
        self.rexSel.sub(self.addTags,self.txt.get("1.0","end")) 
Example 5
Project: verge3d-blender-addon   Author: Soft8Soft   File: generator.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _make_boundary(cls, text=None):
        # Craft a random boundary.  If text is given, ensure that the chosen
        # boundary doesn't appear in the text.
        token = random.randrange(sys.maxsize)
        boundary = ('=' * 15) + (_fmt % token) + '=='
        if text is None:
            return boundary
        b = boundary
        counter = 0
        while True:
            cre = cls._compile_re('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
            if not cre.search(text):
                break
            b = boundary + '.' + str(counter)
            counter += 1
        return b 
Example 6
Project: rift-python   Author: brunorijsman   File: interop.py    License: Apache License 2.0 6 votes vote down vote up
def check_juniper_rift_in_path():
    if shutil.which("rift-environ") is None:
        fatal_error("Cannot find Juniper RIFT (rift-environ) in PATH")

    # run it and check version
    output = subprocess.check_output(["rift-environ",
                                      "--version"], universal_newlines=True)
    # print (output)
    regex = re.compile(r"^.hrift encoding schema: *(\d+)\.(\d+).*",
                       re.IGNORECASE  | re.MULTILINE)
    major = re.search(regex, output)

    if not major or not major.group(1):
        fatal_error("Cannot detect major version of Juniper RIFT")

    minor = major.group(2)
    major = major.group(1)

    expected_minor = protocol_minor_version
    expected_major = protocol_major_version

    if int(major) != expected_major or int(minor) != expected_minor:
        fatal_error("Wrong Major/Minor version of Juniper RIFT: (expected {}.{}, got {}.{})"
                    .format(expected_major, expected_minor, major, minor)) 
Example 7
Project: misp42splunk   Author: remg427   File: generator.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _make_boundary(cls, text=None):
        # Craft a random boundary.  If text is given, ensure that the chosen
        # boundary doesn't appear in the text.
        token = random.randrange(sys.maxsize)
        boundary = ('=' * 15) + (_fmt % token) + '=='
        if text is None:
            return boundary
        b = boundary
        counter = 0
        while True:
            cre = cls._compile_re('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
            if not cre.search(text):
                break
            b = boundary + '.' + str(counter)
            counter += 1
        return b 
Example 8
Project: misp42splunk   Author: remg427   File: generator.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _make_boundary(cls, text=None):
        # Craft a random boundary.  If text is given, ensure that the chosen
        # boundary doesn't appear in the text.
        token = random.randrange(sys.maxsize)
        boundary = ('=' * 15) + (_fmt % token) + '=='
        if text is None:
            return boundary
        b = boundary
        counter = 0
        while True:
            cre = cls._compile_re('^--' + re.escape(b) + '(--)?$', re.MULTILINE)
            if not cre.search(text):
                break
            b = boundary + '.' + str(counter)
            counter += 1
        return b 
Example 9
Project: qutebrowser   Author: qutebrowser   File: version.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _pdfjs_version() -> str:
    """Get the pdf.js version.

    Return:
        A string with the version number.
    """
    try:
        pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path('build/pdf.js')
    except pdfjs.PDFJSNotFound:
        return 'no'
    else:
        pdfjs_file = pdfjs_file.decode('utf-8')
        version_re = re.compile(
            r"^ *(PDFJS\.version|var pdfjsVersion) = '([^']+)';$",
            re.MULTILINE)

        match = version_re.search(pdfjs_file)
        if not match:
            pdfjs_version = 'unknown'
        else:
            pdfjs_version = match.group(2)
        if file_path is None:
            file_path = 'bundled'
        return '{} ({})'.format(pdfjs_version, file_path) 
Example 10
Project: conan-center-index   Author: conan-io   File: conanfile.py    License: MIT License 6 votes vote down vote up
def package(self):
        self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
        if self.settings.compiler == "Visual Studio":
            cmake = self._configure_cmake()
            cmake.install()
        else:
            autotools = self._configure_autotools()
            autotools.install()

            os.unlink(os.path.join(self.package_folder, "lib", "libapr-1.la"))
            tools.rmdir(os.path.join(self.package_folder, "build-1"))
            tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))

            apr_rules_mk = os.path.join(self.package_folder, "bin", "build-1", "apr_rules.mk")
            apr_rules_cnt = open(apr_rules_mk).read()
            for key in ("apr_builddir", "apr_builders", "top_builddir"):
                apr_rules_cnt, nb = re.subn("^{}=[^\n]*\n".format(key), "{}=$(_APR_BUILDDIR)\n".format(key), apr_rules_cnt, flags=re.MULTILINE)
                if nb == 0:
                    raise ConanException("Could not find/replace {} in {}".format(key, apr_rules_mk))
            open(apr_rules_mk, "w").write(apr_rules_cnt) 
Example 11
Project: gransk   Author: pcbje   File: find_names_brute.py    License: Apache License 2.0 6 votes vote down vote up
def setup(self, config):
    """
    Load name model (word list) and compile regexes for stop characters.

    :param config: Configuration object.
    :type config: ``dict``
    """
    reference_model = os.path.join(
        config[helper.CODE_ROOT], config[helper.NAME_MODEL])

    self.stopper = regex.compile(('(%s)' % '|'.join([
        'and', 'or', 'og', 'eller', r'\?', '&', '<', '>', '@', ':', ';', '/',
        r'\(', r'\)', 'i', 'of', 'from', 'to', r'\n', '!'])),
        regex.I | regex.MULTILINE)

    self.semistop = regex.compile(
        ('(%s)' % '|'.join([','])), regex.I | regex.MULTILINE)
    self.size_probability = [0.000, 0.000, 0.435, 0.489, 0.472, 0.004, 0.000]
    self.threshold = 0.25
    self.candidates = defaultdict(int)

    with gzip.open(reference_model, 'rb') as inp:
      self.model = json.loads(inp.read().decode('utf-8'))

    self.tokenizer = regex.compile(r'\w{2,20}') 
Example 12
Project: honeybee   Author: ladybug-tools   File: radparser.py    License: GNU General Public License v3.0 6 votes vote down vote up
def parse_from_string(full_string):
    """
    separate a Radiance file string into multiple strings for each object.

    Args:
        rad_fileString: Radiance data as a single string. The string can be multiline.

    Returns:
        A list of strings. Each string represents a different Radiance Object
    """
    raw_rad_objects = re.findall(
        r'^\s*([^0-9].*(\s*[\d.-]+.*)*)',
        full_string,
        re.MULTILINE)

    rad_objects = (' '.join(radiance_object[0].split())
                   for radiance_object in raw_rad_objects)

    filtered_objects = tuple(rad_object for rad_object in rad_objects
                             if rad_object and rad_object[0] not in ['#', '!'])

    return filtered_objects 
Example 13
Project: vehicle_counting_tensorflow   Author: ahmetozlu   File: coco_tools_test.py    License: MIT License 6 votes vote down vote up
def testExportGroundtruthToCOCO(self):
    image_ids = ['first', 'second']
    groundtruth_boxes = [np.array([[100, 100, 200, 200]], np.float),
                         np.array([[50, 50, 100, 100]], np.float)]
    groundtruth_classes = [np.array([1], np.int32), np.array([1], np.int32)]
    categories = [{'id': 0, 'name': 'person'},
                  {'id': 1, 'name': 'cat'},
                  {'id': 2, 'name': 'dog'}]
    output_path = os.path.join(tf.test.get_temp_dir(), 'groundtruth.json')
    result = coco_tools.ExportGroundtruthToCOCO(
        image_ids,
        groundtruth_boxes,
        groundtruth_classes,
        categories,
        output_path=output_path)
    self.assertDictEqual(result, self._groundtruth_dict)
    with tf.gfile.GFile(output_path, 'r') as f:
      written_result = f.read()
      # The json output should have floats written to 4 digits of precision.
      matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE)
      self.assertTrue(matcher.findall(written_result))
      written_result = json.loads(written_result)
      self.assertAlmostEqual(result, written_result) 
Example 14
Project: vehicle_counting_tensorflow   Author: ahmetozlu   File: coco_tools_test.py    License: MIT License 6 votes vote down vote up
def testExportDetectionsToCOCO(self):
    image_ids = ['first', 'second']
    detections_boxes = [np.array([[100, 100, 200, 200]], np.float),
                        np.array([[50, 50, 100, 100]], np.float)]
    detections_scores = [np.array([.8], np.float), np.array([.7], np.float)]
    detections_classes = [np.array([1], np.int32), np.array([1], np.int32)]
    categories = [{'id': 0, 'name': 'person'},
                  {'id': 1, 'name': 'cat'},
                  {'id': 2, 'name': 'dog'}]
    output_path = os.path.join(tf.test.get_temp_dir(), 'detections.json')
    result = coco_tools.ExportDetectionsToCOCO(
        image_ids,
        detections_boxes,
        detections_scores,
        detections_classes,
        categories,
        output_path=output_path)
    self.assertListEqual(result, self._detections_list)
    with tf.gfile.GFile(output_path, 'r') as f:
      written_result = f.read()
      # The json output should have floats written to 4 digits of precision.
      matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE)
      self.assertTrue(matcher.findall(written_result))
      written_result = json.loads(written_result)
      self.assertAlmostEqual(result, written_result) 
Example 15
Project: cassandra-migrate   Author: Cobliteam   File: cql.py    License: MIT License 6 votes vote down vote up
def scanner(cls):
        if not getattr(cls, '_scanner', None):
            def h(tpe):
                return lambda sc, tk: cls.Token(tpe, tk)

            cls._scanner = re.Scanner([
                (r"(--|//).*?$",               h(cls.LINE_COMMENT)),
                (r"\/\*.+?\*\/",               h(cls.BLOCK_COMMENT)),
                (r'"(?:[^"\\]|\\.)*"',         h(cls.STRING)),
                (r"'(?:[^'\\]|\\.)*'",         h(cls.STRING)),
                (r"\$\$(?:[^\$\\]|\\.)*\$\$",  h(cls.STRING)),
                (r";",                         h(cls.SEMICOLON)),
                (r"\s+",                       h(cls.WHITESPACE)),
                (r".",                         h(cls.OTHER))
            ], re.MULTILINE | re.DOTALL)
        return cls._scanner 
Example 16
Project: moler   Author: nokia   File: terminal.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def data_received(self, data, recv_time):
        """
        Await initial prompt of started shell command.

        After that - do real forward
        """
        if not self._shell_operable.done():
            decoded_data = self.moler_connection.decode(data)
            self.logger.debug("<|{}".format(data))
            assert isinstance(decoded_data, str)
            self.read_buffer += decoded_data
            if re.search(self.prompt, self.read_buffer, re.MULTILINE):
                self._notify_on_connect()
                self._shell_operable.set_result(True)  # makes Future done
                # TODO: should we remove initial prompt as it is inside raw.terminal?
                # TODO: that way (maybe) we won't see it in logs
                data_str = re.sub(self.prompt, '', self.read_buffer, re.MULTILINE)
                data = self.moler_connection.encode(data_str)
            else:
                return
        self.logger.debug("<|{}".format(data))
        super(AsyncioTerminal, self).data_received(data) 
Example 17
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def package_version(self, project, package):
        """
        Return the version of a package, None in case the package does not exist
        The first non-commented Version: tag found is used.
        :param project: the project the package resides in
        :param package: the package to check
        :param product: if passed, the package to be checked is considered to be part of _product
        """
        if not self.item_exists(project, package):
            return None

        version = None

        specfile = source_file_load(self.apiurl, project, package, '{}.spec'.format(package))
        if specfile:
            try:
                version = re.findall('^Version:(.*)', specfile, re.MULTILINE)[0].strip()
            except IndexError:
                pass
        return version 
Example 18
Project: openSUSE-release-tools   Author: openSUSE   File: stagingapi.py    License: GNU General Public License v2.0 6 votes vote down vote up
def package_version(self, project, package):
        """
        Return the version of a package, None in case the package does not exist
        The first non-commented Version: tag found is used.
        :param project: the project the package resides in
        :param package: the package to check
        :param product: if passed, the package to be checked is considered to be part of _product
        """
        if not self.item_exists(project, package):
            return None

        version = None

        specfile = source_file_load(self.apiurl, project, package, '{}.spec'.format(package))
        if specfile:
            try:
                version = re.findall('^Version:(.*)', specfile, re.MULTILINE)[0].strip()
            except IndexError:
                pass
        return version 
Example 19
Project: recruit   Author: Frank-qlu   File: regex.py    License: Apache License 2.0 6 votes vote down vote up
def str_flags_to_int(str_flags):
    flags = 0
    if "i" in str_flags:
        flags |= re.IGNORECASE
    if "l" in str_flags:
        flags |= re.LOCALE
    if "m" in str_flags:
        flags |= re.MULTILINE
    if "s" in str_flags:
        flags |= re.DOTALL
    if "u" in str_flags:
        flags |= re.UNICODE
    if "x" in str_flags:
        flags |= re.VERBOSE

    return flags 
Example 20
Project: recruit   Author: Frank-qlu   File: regex.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, pattern, flags=0):
        """BSON regular expression data.

        This class is useful to store and retrieve regular expressions that are
        incompatible with Python's regular expression dialect.

        :Parameters:
          - `pattern`: string
          - `flags`: (optional) an integer bitmask, or a string of flag
            characters like "im" for IGNORECASE and MULTILINE
        """
        if not isinstance(pattern, string_types):
            raise TypeError("pattern must be a string, not %s" % type(pattern))
        self.pattern = pattern

        if isinstance(flags, string_types):
            self.flags = str_flags_to_int(flags)
        elif isinstance(flags, int):
            self.flags = flags
        else:
            raise TypeError(
                "flags must be a string or int, not %s" % type(flags)) 
Example 21
Project: Penny-Dreadful-Tools   Author: PennyDreadfulMTG   File: update.py    License: GNU General Public License v3.0 6 votes vote down vote up
def update_issue_body(issue: Issue, cards: List[str], see_also: Optional[str]) -> None:
    expected = '<!-- Images --> '
    images = re.search(IMAGES_REGEX, issue.body, re.MULTILINE)
    for row in strings.grouper(4, cards):
        expected = expected + '<img src="https://pennydreadfulmagic.com/image/{0}/" height="300px">'.format('|'.join([urllib.parse.quote(c) for c in row if c is not None]))
    if see_also is not None:
        for row in strings.grouper(5, re.findall(REGEX_CARDREF, see_also)):
            expected = expected + '<img src="https://pennydreadfulmagic.com/image/{0}/" height="250px">'.format('|'.join([urllib.parse.quote(c) for c in row if c is not None]))

    if not images:
        print('Adding Images...')
        body = issue.body + '\n' + expected
        issue.edit(body=body)
    elif images.group(0) != expected:
        print('Updating images...')
        body = issue.body.replace(images.group(0), expected)
        issue.edit(body=body) 
Example 22
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 23
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def commentout_gfxboot(input_text):
    return re.sub(r'(ui\s+.*?gfxboot\.c32.*)$', r'# \1', input_text,
                  flags=re.I | re.MULTILINE) 
Example 24
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kw):
        persistence_awareness_checking_re = re.compile(
            r'^\s*(%s).*?\s%s(\s.*|)$' % \
            (self.BOOT_PARAMS_STARTER, self.PERSISTENCY_TOKEN),
            flags=re.I|re.MULTILINE)
        super(ConfigTweakerWithDebianStylePersistenceParam,
              self).__init__(persistence_awareness_checking_re, *args, **kw) 
Example 25
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kw):
        persistence_awareness_checking_re = re.compile(
            r'^\s*(%s).*?\s(rd.live.overlay|overlay)=.+?' %
            self.BOOT_PARAMS_STARTER,  flags=re.I|re.MULTILINE)
        super(FedoraConfigTweaker, self).__init__(
            persistence_awareness_checking_re, *args, **kw) 
Example 26
Project: multibootusb   Author: mbusb   File: update_cfg_file.py    License: GNU General Public License v2.0 5 votes vote down vote up
def post_process(self, s):
        s = re.sub(r'^(\s*UI\s+(.*?gfxboot(\.c32|)))\s+(.*?)\s+(.*)$',
                   r'# \1 \4.renamed-to-avoid-lockup \5', s,
                   flags=re.I + re.MULTILINE)
        return s 
Example 27
Project: Servo   Author: fpsw   File: note.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def unquote(self):
        return re.sub(r'^>.*', '', self.body, flags=re.MULTILINE).strip() 
Example 28
Project: puput   Author: APSL   File: setup.py    License: MIT License 5 votes vote down vote up
def get_version(package):
    """
    Return package version as listed in `__version__` in `init.py`.
    """
    init_py = codecs.open(os.path.join(package, '__init__.py'), encoding='utf-8').read()
    return re.search("^__version__ = ['\"]([^'\"]+)['\"]", init_py, re.MULTILINE).group(1) 
Example 29
Project: puput   Author: APSL   File: setup.py    License: MIT License 5 votes vote down vote up
def get_author(package):
    """
    Return package author as listed in `__author__` in `init.py`.
    """
    init_py = codecs.open(os.path.join(package, '__init__.py'), encoding='utf-8').read()
    return re.search("^__author__ = ['\"]([^'\"]+)['\"]", init_py, re.MULTILINE).group(1) 
Example 30
Project: puput   Author: APSL   File: setup.py    License: MIT License 5 votes vote down vote up
def get_email(package):
    """
    Return package email as listed in `__email__` in `init.py`.
    """
    init_py = codecs.open(os.path.join(package, '__init__.py'), encoding='utf-8').read()
    return re.search("^__email__ = ['\"]([^'\"]+)['\"]", init_py, re.MULTILINE).group(1)