Python re.sub() Examples

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

Example 1
Project: explirefit   Author: codogogo   File: data_helper.py    Apache License 2.0 10 votes vote down vote up
def clean_str(string):
    """
    Tokenization/string cleaning.
    Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
    """
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip().lower() 
Example 2
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def parse_hostname(sitename):
    parsed_url = urllib.parse.urlparse(sitename)
    hostname = parsed_url.netloc
    if hostname == '':
        # If you pass in 'facebook.com/foo' urlparse treats it all as the path
        match = re.match(r'[^/]+', parsed_url.path)
        if match == None:
            print('ERROR: Unable to make the provided sitename into a hostname: ' + parsed_url.path)
            sys.exit(1)
        hostname = match.group(0)

    www_matcher = r'^www\.(.+)'
    if re.search(www_matcher, hostname) != None:
        hostname = re.sub(www_matcher, r'\1', hostname)

    return hostname 
Example 3
Project: parsechain   Author: Suor   File: chains.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __call__(self, *args, **kwargs):
        # Rebuild the chain if waiting for args
        if self and self[-1].args is ...:
            *head, last = self
            return Chain(head + [Link(last.func, name=last.name, args=args, kwargs=kwargs)])
        else:
            assert len(args) == 1 and not kwargs, "Expecting single value to rpocess by chain"
            value, = args
            for link in self:
                if value is None:
                    return None
                try:
                    value = link(value)
                except Exception as e:
                    if isinstance(value, lxml.html.HtmlElement):
                        value = re.sub(r'\s+', ' ', Ops.outer_html(value))[:100]
                    raise ChainError(f'Link .{link} failed on {value} in {self}.') from e
            return value 
Example 4
Project: flexnet   Author: ressy   File: file.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def _flexnet_lex(text):
    text = text.replace('\r\n', '\n')
    text = text.replace('\\\n', '')
    lines = []
    for line in text.split('\n'):
        line = re.sub(r'\s+', ' ', line)
        lines.append([])
        str_file = StringIO.StringIO(line)
        lex = shlex.shlex(str_file)
        lex.wordchars += ".,-/:;+^"
        token = lex.get_token()
        while token != lex.eof:
            lines[-1].append(token)
            token = lex.get_token()
    lines = filter(len, lines)
    return lines 
Example 5
Project: leapp-repository   Author: oamg   File: lib_spamc.py    Apache License 2.0 6 votes vote down vote up
def _rewrite_spamc_config(content):
    res_lines = []
    read_ssl_option = False
    for line in content.split('\n'):
        if line.startswith('#'):
            res_lines.append(line)
            continue
        if read_ssl_option:
            line = re.sub(r'^\s*(sslv3|tlsv1)', '', line)
            read_ssl_option = False
        if re.search(r'(?<!\S)--ssl\s*$', line):
            read_ssl_option = True
        line = re.sub(r'(?<!\S)--ssl(\s+|=)(sslv3|tlsv1)(?!\S)', '--ssl', line)
        res_lines.append(line)
    res = '\n'.join(res_lines)
    return res 
Example 6
Project: clikit   Author: sdispater   File: progress_bar.py    MIT License 6 votes vote down vote up
def display(self):
        """
        Output the current progress string.
        """
        if self._io.is_quiet():
            return

        if self._format is None:
            self._set_real_format(
                self._internal_format or self._determine_best_format()
            )

        self._overwrite(
            re.sub(
                r"(?i)%([a-z\-_]+)(?::([^%]+))?%",
                self._overwrite_callback,
                self._format,
            )
        ) 
Example 7
Project: chainer-openai-transformer-lm   Author: soskek   File: text_utils.py    MIT License 6 votes vote down vote up
def text_standardize(text):
    """
    fixes some issues the spacy tokenizer had on books corpus
    also does some whitespace standardization
    """
    text = text.replace('—', '-')
    text = text.replace('–', '-')
    text = text.replace('―', '-')
    text = text.replace('…', '...')
    text = text.replace('´', "'")
    text = re.sub(
        r'''(-+|~+|!+|"+|;+|\?+|\++|,+|\)+|\(+|\\+|\/+|\*+|\[+|\]+|}+|{+|\|+|_+)''',
        r' \1 ',
        text)
    text = re.sub(r'\s*\n\s*', ' \n ', text)
    text = re.sub(r'[^\S\n]+', ' ', text)
    return text.strip() 
Example 8
Project: pyblish-win   Author: pyblish   File: fixps.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def main():
    for filename in sys.argv[1:]:
        try:
            f = open(filename, 'r')
        except IOError, msg:
            print filename, ': can\'t open :', msg
            continue
        line = f.readline()
        if not re.match('^#! */usr/local/bin/python', line):
            print filename, ': not a /usr/local/bin/python script'
            f.close()
            continue
        rest = f.read()
        f.close()
        line = re.sub('/usr/local/bin/python',
                      '/usr/bin/env python', line)
        print filename, ':', repr(line)
        f = open(filename, "w")
        f.write(line)
        f.write(rest)
        f.close() 
Example 9
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def formataddr(pair):
    """The inverse of parseaddr(), this takes a 2-tuple of the form
    (realname, email_address) and returns the string value suitable
    for an RFC 2822 From, To or Cc header.

    If the first element of pair is false, then the second element is
    returned unmodified.
    """
    name, address = pair
    if name:
        quotes = ''
        if specialsre.search(name):
            quotes = '"'
        name = escapesre.sub(r'\\\g<0>', name)
        return '%s%s%s <%s>' % (quotes, name, quotes, address)
    return address 
Example 10
Project: pyblish-win   Author: pyblish   File: _osx_support.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _check_for_unavailable_sdk(_config_vars):
    """Remove references to any SDKs not available"""
    # If we're on OSX 10.5 or later and the user tries to
    # compile an extension using an SDK that is not present
    # on the current machine it is better to not use an SDK
    # than to fail.  This is particularly important with
    # the standalone Command Line Tools alternative to a
    # full-blown Xcode install since the CLT packages do not
    # provide SDKs.  If the SDK is not present, it is assumed
    # that the header files and dev libs have been installed
    # to /usr and /System/Library by either a standalone CLT
    # package or the CLT component within Xcode.
    cflags = _config_vars.get('CFLAGS', '')
    m = re.search(r'-isysroot\s+(\S+)', cflags)
    if m is not None:
        sdk = m.group(1)
        if not os.path.exists(sdk):
            for cv in _UNIVERSAL_CONFIG_VARS:
                # Do not alter a config var explicitly overriden by env var
                if cv in _config_vars and cv not in os.environ:
                    flags = _config_vars[cv]
                    flags = re.sub(r'-isysroot\s+\S+(?:\s|$)', ' ', flags)
                    _save_modified_value(_config_vars, cv, flags)

    return _config_vars 
Example 11
Project: pyblish-win   Author: pyblish   File: filelist.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def glob_to_re(pattern):
    """Translate a shell-like glob pattern to a regular expression.

    Return a string containing the regex.  Differs from
    'fnmatch.translate()' in that '*' does not match "special characters"
    (which are platform-specific).
    """
    pattern_re = fnmatch.translate(pattern)

    # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
    # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
    # and by extension they shouldn't match such "special characters" under
    # any OS.  So change all non-escaped dots in the RE to match any
    # character except the special characters (currently: just os.sep).
    sep = os.sep
    if os.sep == '\\':
        # we're using a regex to manipulate a regex, so we need
        # to escape the backslash twice
        sep = r'\\\\'
    escaped = r'\1[^%s]' % sep
    pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
    return pattern_re 
Example 12
Project: pyblish-win   Author: pyblish   File: test_support.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def reap_children():
    """Use this function at the end of test_main() whenever sub-processes
    are started.  This will help ensure that no extra children (zombies)
    stick around to hog resources and create problems when looking
    for refleaks.
    """

    # Reap all our dead child processes so we don't leave zombies around.
    # These hog resources and might be causing some of the buildbots to die.
    if hasattr(os, 'waitpid'):
        any_process = -1
        while True:
            try:
                # This will raise an exception on Windows.  That's ok.
                pid, status = os.waitpid(any_process, os.WNOHANG)
                if pid == 0:
                    break
            except:
                break 
Example 13
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_bug_1140(self):
        # re.sub(x, y, u'') should return u'', not '', and
        # re.sub(x, y, '') should return '', not u''.
        # Also:
        # re.sub(x, y, unicode(x)) should return unicode(y), and
        # re.sub(x, y, str(x)) should return
        #     str(y) if isinstance(y, str) else unicode(y).
        for x in 'x', u'x':
            for y in 'y', u'y':
                z = re.sub(x, y, u'')
                self.assertEqual(z, u'')
                self.assertEqual(type(z), unicode)
                #
                z = re.sub(x, y, '')
                self.assertEqual(z, '')
                self.assertEqual(type(z), str)
                #
                z = re.sub(x, y, unicode(x))
                self.assertEqual(z, y)
                self.assertEqual(type(z), unicode)
                #
                z = re.sub(x, y, str(x))
                self.assertEqual(z, y)
                self.assertEqual(type(z), type(y)) 
Example 14
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_sub_template_numeric_escape(self):
        # bug 776311 and friends
        self.assertEqual(re.sub('x', r'\0', 'x'), '\0')
        self.assertEqual(re.sub('x', r'\000', 'x'), '\000')
        self.assertEqual(re.sub('x', r'\001', 'x'), '\001')
        self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8')
        self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9')
        self.assertEqual(re.sub('x', r'\111', 'x'), '\111')
        self.assertEqual(re.sub('x', r'\117', 'x'), '\117')

        self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111')
        self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1')

        self.assertEqual(re.sub('x', r'\00', 'x'), '\x00')
        self.assertEqual(re.sub('x', r'\07', 'x'), '\x07')
        self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8')
        self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
        self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')

        self.assertEqual(re.sub('x', r'\400', 'x'), '\0')
        self.assertEqual(re.sub('x', r'\777', 'x'), '\377')

        self.assertRaises(re.error, re.sub, 'x', r'\1', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\8', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\9', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\11', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\18', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\1a', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\90', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\99', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
        self.assertRaises(re.error, re.sub, 'x', r'\11a', 'x')
        self.assertRaises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
        self.assertRaises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0'

        # in python2.3 (etc), these loop endlessly in sre_parser.py
        self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
        self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'),
                         'xz8')
        self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'),
                         'xza') 
Example 15
Project: pyblish-win   Author: pyblish   File: EditorWindow.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_accelerator(keydefs, eventname):
    keylist = keydefs.get(eventname)
    # issue10940: temporary workaround to prevent hang with OS X Cocoa Tk 8.5
    # if not keylist:
    if (not keylist) or (macosxSupport.isCocoaTk() and eventname in {
                            "<<open-module>>",
                            "<<goto-line>>",
                            "<<change-indentwidth>>"}):
        return ""
    s = keylist[0]
    s = re.sub(r"-[a-z]\b", lambda m: m.group().upper(), s)
    s = re.sub(r"\b\w+\b", lambda m: keynames.get(m.group(), m.group()), s)
    s = re.sub("Key-", "", s)
    s = re.sub("Cancel","Ctrl-Break",s)   # dscherer@cmu.edu
    s = re.sub("Control-", "Ctrl-", s)
    s = re.sub("-", "+", s)
    s = re.sub("><", " ", s)
    s = re.sub("<", "", s)
    s = re.sub(">", "", s)
    return s 
Example 16
Project: pyblish-win   Author: pyblish   File: PyShell.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def recall(self, s, event):
        # remove leading and trailing empty or whitespace lines
        s = re.sub(r'^\s*\n', '' , s)
        s = re.sub(r'\n\s*$', '', s)
        lines = s.split('\n')
        self.text.undo_block_start()
        try:
            self.text.tag_remove("sel", "1.0", "end")
            self.text.mark_set("insert", "end-1c")
            prefix = self.text.get("insert linestart", "insert")
            if prefix.rstrip().endswith(':'):
                self.newline_and_indent_event(event)
                prefix = self.text.get("insert linestart", "insert")
            self.text.insert("insert", lines[0].strip())
            if len(lines) > 1:
                orig_base_indent = re.search(r'^([ \t]*)', lines[0]).group(0)
                new_base_indent  = re.search(r'^([ \t]*)', prefix).group(0)
                for line in lines[1:]:
                    if line.startswith(orig_base_indent):
                        # replace orig base indentation with new indentation
                        line = new_base_indent + line[len(orig_base_indent):]
                    self.text.insert('insert', '\n'+line.rstrip())
        finally:
            self.text.see("insert")
            self.text.undo_block_stop() 
Example 17
Project: parsechain   Author: Suor   File: chains.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def html_to_text(html):
        """Cleans html preserving newlines"""
        if isinstance(html, lxml.html.HtmlElement):
            html = Ops.inner_html(html)

        html = re.sub(r'\s+', ' ', html).strip()
        html = re.sub(r'<br[^>]*>|</li>', '\n', html, flags=re.I)
        html = re.sub(r'</p>', '\n\n', html, flags=re.I)
        if not html or html.isspace():
            return ''
        return lxml.html.tostring(lxml.html.fromstring(html), encoding='unicode', method='text')

    # Text utils
    # TODO: make these two work with bytes? 
Example 18
Project: parsechain   Author: Suor   File: chains.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def re_sub(pattern, repl, count=0, flags=0):
        return lambda text: re.sub(pattern, repl, text, count=count, flags=flags)

    # Data utils 
Example 19
Project: MercrediFiction   Author: bortzmeyer   File: mercredifiction2others.py    GNU General Public License v2.0 5 votes vote down vote up
def cleandate(str):
    # En dépit de ce que dit la documentation, les secondes
    # fractionnaires ne sont pas acceptées, il faut donc les retirer
    return(re.sub('\.[0-9]+Z$', 'Z', str)) 
Example 20
Project: fs_image   Author: facebookincubator   File: nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def _colon_quote_path(path):
    return re.sub('[\\\\:]', lambda m: '\\' + m.group(0), path)


# NB: This assumes the path is readable to unprivileged users. 
Example 21
Project: fs_image   Author: facebookincubator   File: test_repo_db.py    MIT License 5 votes vote down vote up
def _check_schema(self, conn):
        for (a_name, a_sql), (e_name, e_sql) in zip(_get_schema(conn), [
            ('rpm', (
                'CREATE TABLE `rpm` ('
                ' `filename` BLOB NOT NULL,'
                ' `checksum` BLOB NOT NULL,'
                ' `canonical_checksum` BLOB NOT NULL,'
                ' `size` INTEGER NOT NULL,'
                ' `build_timestamp` INTEGER NOT NULL,'
                ' `storage_id` BLOB NOT NULL,'
                ' PRIMARY KEY (`filename`, `checksum`)'
                ' )'
            )),
            ('repodata', (
                'CREATE TABLE `repodata` ('
                ' `checksum` BLOB NOT NULL,'
                ' `size` INTEGER NOT NULL,'
                ' `build_timestamp` INTEGER NOT NULL,'
                ' `storage_id` BLOB NOT NULL,'
                ' PRIMARY KEY (`checksum`)'
                ' )'
            )),
            ('repo_metadata', (
                'CREATE TABLE `repo_metadata` ('
                ' `repo` BLOB NOT NULL,'
                ' `fetch_timestamp` INTEGER NOT NULL,'
                ' `build_timestamp` INTEGER NOT NULL,'
                ' `checksum` BLOB NOT NULL,'
                ' `xml` BLOB NOT NULL,'
                ' PRIMARY KEY (`repo`, `fetch_timestamp`),'
                ' UNIQUE (`repo`, `checksum`)'
                ' )'
            )),
        ]):
            self.assertEqual(e_name, a_name)
            self.assertEqual(e_sql, re.sub(r'\s+', ' ', a_sql)) 
Example 22
Project: leapp-repository   Author: oamg   File: lib_spamd.py    Apache License 2.0 5 votes vote down vote up
def _drop_ssl_version(value):
    regex = r'(?<![\w-])--ssl-version(=|\s+)(sslv3|tlsv1)(?![\w-])'
    if not re.search(r'(?<![\w-])--ssl(?![\w-])', value):
        value = re.sub(regex, '--ssl', value, count=1)
    return re.sub(regex, '', value) 
Example 23
Project: leapp-repository   Author: oamg   File: lib_spamd.py    Apache License 2.0 5 votes vote down vote up
def _drop_daemonize_option(value):
    shortopts = utils.SPAMD_SHORTOPTS_NOARG
    value = re.sub(r'(?<![\w-])-d+(?![\w-])',
                   r'', value)
    while True:
        value, nsubs = re.subn(r'(?<![\w-])(-[' + shortopts + ']*)d',
                               r'\1', value)
        if nsubs == 0:
            break
    return re.sub(r'(?<![\w-])--daemonize(?![\w-])', '', value) 
Example 24
Project: leapp-repository   Author: oamg   File: lib_spamd.py    Apache License 2.0 5 votes vote down vote up
def _rewrite_spamd_option(content, ops):
    pre_assignment, assignment, post_assignment = \
        utils.parse_sysconfig_spamassassin(content)
    if assignment:
        value = re.sub(r'^\s*' + utils.SYSCONFIG_VARIABLE + r'\s*=(.*)$',
                       r'\1', assignment)
        for op in ops:
            value = op(value)
        assignment = utils.SYSCONFIG_VARIABLE + '=' + value
    res = '\n'.join(filter(None, (pre_assignment, assignment, post_assignment)))
    if res and not res.endswith('\n'):
        res += '\n'
    return res 
Example 25
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def uncompress_ipv6(ipv6):
    addr = text_type(ipv6.replace('IPv6:', ''))
    try:
        addr = 'IPv6:' + ipaddress.ip_address(addr).exploded
    except ValueError:
        addr = ipv6
    return re.sub(r':0([^:])', r':\1', re.sub(r'0+', r'0', addr)) 
Example 26
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def migrate_file(fn):
    # make backup
    shutil.copy2(fn, fn + BackupSuffix)
    with open(fn, 'w') as file_out:
        with open(fn + BackupSuffix) as file_in:
            for line in file_in:
                if rs.search(line) and not check_false_positives(os.path.basename(fn), line):
                    line = rs.sub(sub_ipv6, line)
                file_out.write(line) 
Example 27
Project: leapp-repository   Author: oamg   File: modscan.py    Apache License 2.0 5 votes vote down vote up
def _create_dracut_modules():
    dracut_base_path = api.get_actor_folder_path('dracut')
    if dracut_base_path:
        dracut_base_path = os.path.abspath(dracut_base_path)
        for module in os.listdir(dracut_base_path):
            yield UpgradeDracutModule(
                name=re.sub(r'^\d+', '', module),
                module_path=os.path.join(dracut_base_path, module)
            ) 
Example 28
Project: leapp-repository   Author: oamg   File: test_modscan.py    Apache License 2.0 5 votes vote down vote up
def test_created_modules(monkeypatch):
    monkeypatch.setattr(api, 'get_actor_folder_path', _files_get_folder_path)
    path = os.path.abspath(api.get_actor_folder_path('dracut'))
    required_modules = ['sys-upgrade', 'sys-upgrade-redhat']
    for mod in modscan._create_dracut_modules():
        index = required_modules.index(mod.name)

        # Ensures that this actor only includes known modules
        assert index != -1

        required_modules.pop(index)

        # Ensures that the path is valid
        assert mod.module_path
        assert mod.module_path == os.path.abspath(mod.module_path)

        # Ensure it's a directory
        assert os.path.isdir(mod.module_path)

        # Ensure it's located within the actors files path
        assert mod.module_path.startswith(path)

        # Ensure the directory name ends with the module name
        assert os.path.basename(mod.module_path).endswith(mod.name)
        assert not re.sub(r'^(85|90){}$'.format(mod.name), '', os.path.basename(mod.module_path))
    assert not required_modules 
Example 29
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def check_files_for_compressed_ipv6():
    conf_files = get_conf_files()
    migrate_files = []
    files = [os.path.join(SendmailConfDir, re.sub(r'\.db$', '', f)) for f in os.listdir(SendmailConfDir)
             if f.endswith('.db')] + conf_files
    regex = re.compile(r'IPv6:[0-9a-fA-F:]*::')
    for filename in files:
        if not os.path.exists(filename):
            continue
        with open(filename) as file_check:
            for line in file_check:
                if regex.search(line) and not check_false_positives(os.path.basename(filename), line):
                    migrate_files.append(filename)
                    break
    return migrate_files 
Example 30
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 5 votes vote down vote up
def _get_lines(content):
    content = re.sub(r'\\\n', '', content)
    return content.split('\n') 
Example 31
Project: clikit   Author: sdispater   File: application_config.py    MIT License 5 votes vote down vote up
def default_display_name(self):  # type: () -> Optional[str]
        if self._name is None:
            return

        return re.sub(r"[\s\-_]+", " ", self._name).title() 
Example 32
Project: clikit   Author: sdispater   File: progress_indicator.py    MIT License 5 votes vote down vote up
def _display(self):
        if self._io.is_quiet():
            return

        self._overwrite(
            re.sub(
                r"(?i){([a-z\-_]+)(?::([^}]+))?}", self._overwrite_callback, self._fmt
            )
        ) 
Example 33
Project: clikit   Author: sdispater   File: paragraph.py    MIT License 5 votes vote down vote up
def render(self, io, indentation=0):  # type: (IO, int) -> None
        line_prefix = " " * indentation
        text_width = io.terminal_dimensions.width - 1 - indentation

        text = re.sub(
            r"\n(?!\n)",
            "\n" + line_prefix,
            "\n".join(textwrap.wrap(self._text, text_width)),
        )

        io.write(line_prefix + text.rstrip() + "\n") 
Example 34
Project: clikit   Author: sdispater   File: labeled_paragraph.py    MIT License 5 votes vote down vote up
def render(self, io, indentation=0):  # type: (IO, int) -> None
        line_prefix = " " * indentation
        visible_label = io.remove_format(self._label)
        style_tag_length = len(self._label) - len(visible_label)

        if self._aligned and self._alignment:
            text_offset = self._alignment.text_offset - indentation
        else:
            text_offset = 0

        text_offset = max(text_offset, len(visible_label) + self._padding)
        text_prefix = " " * text_offset

        # 1 trailing space
        text_width = io.terminal_dimensions.width - 1 - text_offset - indentation
        text = re.sub(
            r"\n(?!\n)",
            "\n" + line_prefix + text_prefix,
            "\n".join(textwrap.wrap(self._text, text_width)),
        )

        # Add the total length of the style tags ("<b>", ...)
        label_width = text_offset + style_tag_length

        io.write(
            "{}{:<{}}{}".format(
                line_prefix, self._label, label_width, text.rstrip()
            ).rstrip()
            + "\n"
        ) 
Example 35
Project: webnull   Author: macrael   File: test_integration.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def check_test_file(self, test_case, work_in_progress=False):
        test_case_name = re.sub(r'^(?:tests\.)?test_integration\.(.+)',r'\1', unittest.TestCase.id(self))
        file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), './test_resources/', test_case_name + '.out')
        if work_in_progress:
            with open(file_path, 'w') as f:
                f.write(test_case)
                print 'Wrote Success for ' + test_case_name
                print test_case
                self.assertTrue(False, msg="Test is still a work in progress.")
        else:
            with open(file_path, 'r') as f:
                success_case = f.read()
                self.assertEqual(test_case, success_case) 
Example 36
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def transform_body(self, search_re, replacement_string):
        managed = self.current_body()

        matched_hostnames = None
        if managed == '':
            print('Your hostsfile is not managed by webnull, we won\'t change anything')
            exit(1)
        else:
            lines = re.findall(search_re, managed, flags=re.MULTILINE)
            matched_hostnames = set(map(lambda line: re.match(r'^[^\t]+\t+([^\t]+)$', line).group(1), lines))
            if (len(matched_hostnames) != 0):
                new_managed = re.sub(search_re, replacement_string, managed, flags=re.MULTILINE)
                self.write_body(new_managed)

        return matched_hostnames 
Example 37
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 5 votes vote down vote up
def _get_date_tuple(self, mediafile):
        """Get a 3-item sequence representing the date consisting of a
        year, month, and day number. Each number is either an integer or
        None.
        """
        # Get the underlying data and split on hyphens and slashes.
        datestring = super(DateField, self).__get__(mediafile, None)
        if isinstance(datestring, six.string_types):
            datestring = re.sub(r'[Tt ].*$', '', six.text_type(datestring))
            items = re.split('[-/]', six.text_type(datestring))
        else:
            items = []

        # Ensure that we have exactly 3 components, possibly by
        # truncating or padding.
        items = items[:3]
        if len(items) < 3:
            items += [None] * (3 - len(items))

        # Use year field if year is missing.
        if not items[0] and hasattr(self, '_year_field'):
            items[0] = self._year_field.__get__(mediafile)

        # Convert each component to an integer if possible.
        items_ = []
        for item in items:
            try:
                items_.append(int(item))
            except (TypeError, ValueError):
                items_.append(None)
        return items_ 
Example 38
Project: phrydy   Author: Josef-Friedrich   File: mediafile.py    MIT License 5 votes vote down vote up
def _field_sort_name(cls, name):
        """Get a sort key for a field name that determines the order
        fields should be written in.

        Fields names are kept unchanged, unless they are instances of
        :class:`DateItemField`, in which case `year`, `month`, and `day`
        are replaced by `date0`, `date1`, and `date2`, respectively, to
        make them appear in that order.
        """
        if isinstance(cls.__dict__[name], DateItemField):
            name = re.sub('year',  'date0', name)
            name = re.sub('month', 'date1', name)
            name = re.sub('day',   'date2', name)
        return name 
Example 39
Project: pyblish-win   Author: pyblish   File: lfcr.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def main():
    for filename in sys.argv[1:]:
        if os.path.isdir(filename):
            print filename, "Directory!"
            continue
        data = open(filename, "rb").read()
        if '\0' in data:
            print filename, "Binary!"
            continue
        newdata = re.sub("\r?\n", "\r\n", data)
        if newdata != data:
            print filename
            f = open(filename, "wb")
            f.write(newdata)
            f.close() 
Example 40
Project: pyblish-win   Author: pyblish   File: quoprimime.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def header_decode(s):
    """Decode a string encoded with RFC 2045 MIME header `Q' encoding.

    This function does not parse a full MIME header value encoded with
    quoted-printable (like =?iso-8895-1?q?Hello_World?=) -- please use
    the high level email.header class for that functionality.
    """
    s = s.replace('_', ' ')
    return re.sub(r'=[a-fA-F0-9]{2}', _unquote_match, s) 
Example 41
Project: pyblish-win   Author: pyblish   File: utils.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def fix_eols(s):
    """Replace all line-ending characters with \\r\\n."""
    # Fix newlines with no preceding carriage return
    s = re.sub(r'(?<!\r)\n', CRLF, s)
    # Fix carriage returns with no following newline
    s = re.sub(r'\r(?!\n)', CRLF, s)
    return s 
Example 42
Project: pyblish-win   Author: pyblish   File: _osx_support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _remove_universal_flags(_config_vars):
    """Remove all universal build arguments from config vars"""

    for cv in _UNIVERSAL_CONFIG_VARS:
        # Do not alter a config var explicitly overriden by env var
        if cv in _config_vars and cv not in os.environ:
            flags = _config_vars[cv]
            flags = re.sub('-arch\s+\w+\s', ' ', flags)
            flags = re.sub('-isysroot [^ \t]*', ' ', flags)
            _save_modified_value(_config_vars, cv, flags)

    return _config_vars 
Example 43
Project: pyblish-win   Author: pyblish   File: _osx_support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _remove_unsupported_archs(_config_vars):
    """Remove any unsupported archs from config vars"""
    # Different Xcode releases support different sets for '-arch'
    # flags. In particular, Xcode 4.x no longer supports the
    # PPC architectures.
    #
    # This code automatically removes '-arch ppc' and '-arch ppc64'
    # when these are not supported. That makes it possible to
    # build extensions on OSX 10.7 and later with the prebuilt
    # 32-bit installer on the python.org website.

    # skip checks if the compiler was overriden with a CC env variable
    if 'CC' in os.environ:
        return _config_vars

    if re.search('-arch\s+ppc', _config_vars['CFLAGS']) is not None:
        # NOTE: Cannot use subprocess here because of bootstrap
        # issues when building Python itself
        status = os.system(
            """echo 'int main{};' | """
            """'%s' -c -arch ppc -x c -o /dev/null /dev/null 2>/dev/null"""
            %(_config_vars['CC'].replace("'", "'\"'\"'"),))
        if status:
            # The compile failed for some reason.  Because of differences
            # across Xcode and compiler versions, there is no reliable way
            # to be sure why it failed.  Assume here it was due to lack of
            # PPC support and remove the related '-arch' flags from each
            # config variables not explicitly overriden by an environment
            # variable.  If the error was for some other reason, we hope the
            # failure will show up again when trying to compile an extension
            # module.
            for cv in _UNIVERSAL_CONFIG_VARS:
                if cv in _config_vars and cv not in os.environ:
                    flags = _config_vars[cv]
                    flags = re.sub('-arch\s+ppc\w*\s', ' ', flags)
                    _save_modified_value(_config_vars, cv, flags)

    return _config_vars 
Example 44
Project: pyblish-win   Author: pyblish   File: _osx_support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _override_all_archs(_config_vars):
    """Allow override of all archs with ARCHFLAGS env var"""
    # NOTE: This name was introduced by Apple in OSX 10.5 and
    # is used by several scripting languages distributed with
    # that OS release.
    if 'ARCHFLAGS' in os.environ:
        arch = os.environ['ARCHFLAGS']
        for cv in _UNIVERSAL_CONFIG_VARS:
            if cv in _config_vars and '-arch' in _config_vars[cv]:
                flags = _config_vars[cv]
                flags = re.sub('-arch\s+\w+\s', ' ', flags)
                flags = flags + ' ' + arch
                _save_modified_value(_config_vars, cv, flags)

    return _config_vars 
Example 45
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def request_host(request):
    """Return request-host, as defined by RFC 2965.

    Variation from RFC: returned value is lowercased, for convenient
    comparison.

    """
    url = request.get_full_url()
    host = urlparse.urlparse(url)[1]
    if host == "":
        host = request.get_header("Host", "")

    # remove port, if present
    host = cut_port_re.sub("", host, 1)
    return host.lower() 
Example 46
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def escape_path(path):
    """Escape any invalid characters in HTTP URL, and uppercase all escapes."""
    # There's no knowing what character encoding was used to create URLs
    # containing %-escapes, but since we have to pick one to escape invalid
    # path characters, we pick UTF-8, as recommended in the HTML 4.0
    # specification:
    # http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.1
    # And here, kind of: draft-fielding-uri-rfc2396bis-03
    # (And in draft IRI specification: draft-duerst-iri-05)
    # (And here, for new URI schemes: RFC 2718)
    if isinstance(path, unicode):
        path = path.encode("utf-8")
    path = urllib.quote(path, HTTP_PATH_SAFE)
    path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path)
    return path 
Example 47
Project: pyblish-win   Author: pyblish   File: test_array_in_pointer.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def dump(obj):
    # helper function to dump memory contents in hex, with a hyphen
    # between the bytes.
    h = hexlify(memoryview(obj))
    return re.sub(r"(..)", r"\1-", h)[:-1] 
Example 48
Project: pyblish-win   Author: pyblish   File: test_pep3118.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def normalize(format):
    # Remove current endian specifier and white space from a format
    # string
    if format is None:
        return ""
    format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
    return re.sub(r"\s", "", format) 
Example 49
Project: pyblish-win   Author: pyblish   File: install_egg_info.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def safe_name(name):
    """Convert an arbitrary string to a standard distribution name

    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
    """
    return re.sub('[^A-Za-z0-9.]+', '-', name) 
Example 50
Project: pyblish-win   Author: pyblish   File: install_egg_info.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def safe_version(version):
    """Convert an arbitrary string to a standard version string

    Spaces become dots, and all other non-alphanumeric characters become
    dashes, with runs of multiple dashes condensed to a single dash.
    """
    version = version.replace(' ','.')
    return re.sub('[^A-Za-z0-9.]+', '-', version) 
Example 51
Project: pyblish-win   Author: pyblish   File: msvc9compiler.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def sub(self, s):
        for k, v in self.macros.items():
            s = s.replace(k, v)
        return s 
Example 52
Project: pyblish-win   Author: pyblish   File: pydoc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def stripid(text):
    """Remove the hexadecimal id from a Python object representation."""
    # The behaviour of %p is implementation-dependent in terms of case.
    return _re_stripid.sub(r'\1', text) 
Example 53
Project: pyblish-win   Author: pyblish   File: pydoc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def repr_string(self, x, level):
        test = cram(x, self.maxstring)
        testrepr = repr(test)
        if '\\' in test and '\\' not in replace(testrepr, r'\\', ''):
            # Backslashes are only literal in the string and are never
            # needed to make any special characters, so show a raw string.
            return 'r' + testrepr[0] + self.escape(test) + testrepr[0]
        return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)',
                      r'<font color="#c040c0">\1</font>',
                      self.escape(testrepr)) 
Example 54
Project: pyblish-win   Author: pyblish   File: pydoc.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def plain(text):
    """Remove boldface formatting from text."""
    return re.sub('.\b', '', text) 
Example 55
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def format_help(self):
        help = self._root_section.format_help()
        if help:
            help = self._long_break_matcher.sub('\n\n', help)
            help = help.strip('\n') + '\n'
        return help 
Example 56
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _fill_text(self, text, width, indent):
        text = self._whitespace_matcher.sub(' ', text).strip()
        return _textwrap.fill(text, width, initial_indent=indent,
                                           subsequent_indent=indent) 
Example 57
Project: pyblish-win   Author: pyblish   File: smtplib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def quotedata(data):
    """Quote data for email.

    Double leading '.', and change Unix newline '\\n', or Mac '\\r' into
    Internet CRLF end-of-line.
    """
    return re.sub(r'(?m)^\.', '..',
        re.sub(r'(?:\r\n|\n|\r(?!\n))', CRLF, data)) 
Example 58
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def _indent(s, indent=4):
    """
    Add the given number of space characters to the beginning of
    every non-blank line in `s`, and return the result.
    If the string `s` is Unicode, it is encoded using the stdout
    encoding and the `backslashreplace` error handler.
    """
    if isinstance(s, unicode):
        s = s.encode(_encoding, 'backslashreplace')
    # This regexp matches the start of non-blank lines:
    return re.sub('(?m)^(?!$)', indent*' ', s) 
Example 59
Project: pyblish-win   Author: pyblish   File: test_support.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def strip_python_stderr(stderr):
    """Strip the stderr of a Python process from potential debug output
    emitted by the interpreter.

    This will typically be run on the result of the communicate() method
    of a subprocess.Popen object.
    """
    stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
    return stderr 
Example 60
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertStderrEqual(self, stderr, expected, msg=None):
        # In a debug build, stuff like "[6580 refs]" is printed to stderr at
        # shutdown time.  That frustrates tests trying to check stderr produced
        # from a spawned Python process.
        actual = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr)
        self.assertEqual(actual, expected, msg) 
Example 61
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_basic_re_sub(self):
        self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
        self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
                         '9.3 -3 24x100y')
        self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3),
                         '9.3 -3 23x99y')

        self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
        self.assertEqual(re.sub('.', r"\n", 'x'), '\n')

        s = r"\1\1"
        self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
        self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
        self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)

        self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
        self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
        self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
        self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')

        self.assertEqual(re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a'),
                         '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
        self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'), '\t\n\v\r\f\a')
        self.assertEqual(re.sub('a', '\t\n\v\r\f\a', 'a'),
                         (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))

        self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest') 
Example 62
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bug_449964(self):
        # fails for group followed by other escape
        self.assertEqual(re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx'),
                         'xx\bxx\b') 
Example 63
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_qualified_re_sub(self):
        self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
        self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa') 
Example 64
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bug_114660(self):
        self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello  there'),
                         'hello there') 
Example 65
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bug_462270(self):
        # Test for empty sub() behaviour, see SF bug #462270
        self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
        self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d') 
Example 66
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_dollar_matches_twice(self):
        "$ matches the end of string, and just before the terminating \n"
        pattern = re.compile('$')
        self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#')
        self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#')
        self.assertEqual(pattern.sub('#', '\n'), '#\n#')

        pattern = re.compile('$', re.MULTILINE)
        self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' )
        self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
        self.assertEqual(pattern.sub('#', '\n'), '#\n#') 
Example 67
Project: pyblish-win   Author: pyblish   File: test_re.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_large_search(self, size):
        # Issue #10182: indices were 32-bit-truncated.
        s = 'a' * size
        m = re.search('$', s)
        self.assertIsNotNone(m)
        self.assertEqual(m.start(), size)
        self.assertEqual(m.end(), size)

    # The huge memuse is because of re.sub() using a list and a join()
    # to create the replacement result. 
Example 68
Project: pyblish-win   Author: pyblish   File: test_cookielib.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_missing_value(self):
        from cookielib import MozillaCookieJar, lwp_cookie_str

        # missing = sign in Cookie: header is regarded by Mozilla as a missing
        # name, and by cookielib as a missing value
        filename = test_support.TESTFN
        c = MozillaCookieJar(filename)
        interact_netscape(c, "http://www.acme.com/", 'eggs')
        interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
        cookie = c._cookies["www.acme.com"]["/"]["eggs"]
        self.assertIsNone(cookie.value)
        self.assertEqual(cookie.name, "eggs")
        cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
        self.assertIsNone(cookie.value)
        self.assertEqual(cookie.name, '"spam"')
        self.assertEqual(lwp_cookie_str(cookie), (
            r'"spam"; path="/foo/"; domain="www.acme.com"; '
            'path_spec; discard; version=0'))
        old_str = repr(c)
        c.save(ignore_expires=True, ignore_discard=True)
        try:
            c = MozillaCookieJar(filename)
            c.revert(ignore_expires=True, ignore_discard=True)
        finally:
            os.unlink(c.filename)
        # cookies unchanged apart from lost info re. whether path was specified
        self.assertEqual(
            repr(c),
            re.sub("path_specified=%s" % True, "path_specified=%s" % False,
                   old_str)
            )
        self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
                         '"spam"; eggs') 
Example 69
Project: pyblish-win   Author: pyblish   File: test_syntax.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_global_err_then_warn(self):
        # Bug tickler:  The SyntaxError raised for one global statement
        # shouldn't be clobbered by a SyntaxWarning issued for a later one.
        source = re.sub('(?m)^ *:', '', """\
            :def error(a):
            :    global a  # SyntaxError
            :def warning():
            :    b = 1
            :    global b  # SyntaxWarning
            :""")
        warnings.filterwarnings(action='ignore', category=SyntaxWarning)
        self._check_error(source, "global")
        warnings.filters.pop(0) 
Example 70
Project: pyblish-win   Author: pyblish   File: test_syntax.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_delete_deref(self):
        source = re.sub('(?m)^ *:', '', """\
            :def foo(x):
            :  def bar():
            :    print x
            :  del x
            :""")
        self._check_error(source, "nested scope") 
Example 71
Project: pyblish-win   Author: pyblish   File: ScriptBinding.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def checksyntax(self, filename):
        self.shell = shell = self.flist.open_shell()
        saved_stream = shell.get_warning_stream()
        shell.set_warning_stream(shell.stderr)
        with open(filename, 'r') as f:
            source = f.read()
        if '\r' in source:
            source = re.sub(r"\r\n", "\n", source)
            source = re.sub(r"\r", "\n", source)
        if source and source[-1] != '\n':
            source = source + '\n'
        text = self.editwin.text
        text.tag_remove("ERROR", "1.0", "end")
        try:
            try:
                # If successful, return the compiled code
                return compile(source, filename, "exec")
            except (SyntaxError, OverflowError, ValueError) as err:
                try:
                    msg, (errorfilename, lineno, offset, line) = err
                    if not errorfilename:
                        err.args = msg, (filename, lineno, offset, line)
                        err.filename = filename
                    self.colorize_syntax_error(msg, lineno, offset)
                except:
                    msg = "*** " + str(err)
                self.errorbox("Syntax error",
                              "There's an error in your program:\n" + msg)
                return False
        finally:
            shell.set_warning_stream(saved_stream) 
Example 72
Project: leapp-repository   Author: oamg   File: systemfacts.py    Apache License 2.0 4 votes vote down vote up
def _get_active_kernel_modules(logger):
    lines = run(['lsmod'], split=True)['stdout']
    for l in lines[1:]:
        name = l.split(' ')[0]

        # Read parameters of the given module as exposed by the
        # `/sys` VFS, if there are no parameters exposed we just
        # take the name of the module
        base_path = '/sys/module/{module}'.format(module=name)
        parameters_path = os.path.join(base_path, 'parameters')
        if not os.path.exists(parameters_path):
            yield ActiveKernelModule(filename=name, parameters=[])
            continue

        # Use `modinfo` to probe for signature information
        parameter_dict = {}
        try:
            signature = run(['modinfo', '-F', 'signature', name], split=False)['stdout']
        except CalledProcessError:
            signature = None

        signature_string = None
        if signature:
            # Remove whitspace from the signature string
            signature_string = re.sub(r"\s+", "", signature, flags=re.UNICODE)

        # Since we're using the `/sys` VFS we need to use `os.listdir()` to get
        # all the property names and then just read from all the listed paths
        parameters = sorted(os.listdir(parameters_path))
        for param in parameters:
            try:
                with open(os.path.join(parameters_path, param), mode='r') as fp:
                    parameter_dict[param] = fp.read().strip()
            except IOError as exc:
                # Some parameters are write-only, in that case we just log the name of parameter
                # and the module and continue
                if exc.errno in (errno.EACCES, errno.EPERM):
                    msg = 'Unable to read parameter "{param}" of kernel module "{name}"'
                    logger.warning(msg.format(param=param, name=name))
                else:
                    raise exc

        # Project the dictionary as a list of key values
        items = [
            KernelModuleParameter(name=k, value=v)
            for (k, v) in six.iteritems(parameter_dict)
        ]

        yield ActiveKernelModule(
            filename=name,
            parameters=items,
            signature=signature_string
        ) 
Example 73
Project: autofff   Author: ChiefGokhlayeh   File: generator.py    MIT License 4 votes vote down vote up
def generate(self, result: scanner.ScannerResult, output: io.IOBase) -> None:
        if self.generateIncludeGuard:
            incGuard = os.path.splitext(
                os.path.basename(self.fakeName.upper()))[0]
            if incGuard[0].isdigit():
                incGuard = '_' + incGuard

            fffHeader = os.path.expandvars(
                CONFIG[c.AUTOFFF_SECTION][c.SIMPLE_GENERATOR_SECTION][c.SIMPLE_GENERATOR_FFF_PATH])
            incGuard = f"{re.sub('([^A-Z0-9_]*)', '', incGuard)}_H_"
            LOGGER.debug(f"Generated include guard macro: '{incGuard}'.")
            incGuardBeginning = [
                f'#ifndef {incGuard}\n',
                f'#define {incGuard}\n\n',
                f'#include "{fffHeader}"\n'
            ]
            if self.includeFiles is not None:
                incGuardBeginning += [
                    f'#include "{os.path.basename(f)}"\n' for f in self.includeFiles]
            incGuardBeginning += f'#include "{os.path.basename(self.originalHeader)}"\n\n'
            incGuardBeginning += f"#ifdef __cplusplus\nextern \"C\" {{ \n #endif\n"
            incGuardEnd = [
                f"#ifdef __cplusplus\n}}\n#endif\n",
                f"\n#endif /* {incGuard} */\n"
            ]
            output.writelines(incGuardBeginning)

        for decl in filter(
                lambda decl: decl.type.args is not None and any(map(
                    lambda param: (not isinstance(param, EllipsisParam) and
                                   utils.is_function_pointer_type(param.type)),
                    decl.type.args.params
                )),
                result.declarations):
            output.write(self._generateTypeDefForDecl(decl))
        for defin in filter(
                lambda defin: defin.decl.type.args is not None and any(map(
                    lambda param: (not isinstance(param, EllipsisParam) and
                                   utils.is_function_pointer_type(param.type)),
                    defin.decl.type.args.params
                )),
                result.definitions):
            output.write(self._generateTypeDefForDecl(defin.decl))

        output.write("\n")
        for decl in result.declarations:
            output.write(self._generateFakeForDecl(decl))

        output.write("\n")
        for definition in result.definitions:
            output.write(self._generateBypassForFuncDef(definition))
            output.write(self._generateFakeForDecl(definition.decl))

        if self.generateIncludeGuard:
            output.writelines(incGuardEnd) 
Example 74
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def http2time(text):
    """Returns time in seconds since epoch of time represented by a string.

    Return value is an integer.

    None is returned if the format of str is unrecognized, the time is outside
    the representable range, or the timezone string is not recognized.  If the
    string contains no timezone, UTC is assumed.

    The timezone in the string may be numerical (like "-0800" or "+0100") or a
    string timezone (like "UTC", "GMT", "BST" or "EST").  Currently, only the
    timezone strings equivalent to UTC (zero offset) are known to the function.

    The function loosely parses the following formats:

    Wed, 09 Feb 1994 22:23:32 GMT       -- HTTP format
    Tuesday, 08-Feb-94 14:15:29 GMT     -- old rfc850 HTTP format
    Tuesday, 08-Feb-1994 14:15:29 GMT   -- broken rfc850 HTTP format
    09 Feb 1994 22:23:32 GMT            -- HTTP format (no weekday)
    08-Feb-94 14:15:29 GMT              -- rfc850 format (no weekday)
    08-Feb-1994 14:15:29 GMT            -- broken rfc850 format (no weekday)

    The parser ignores leading and trailing whitespace.  The time may be
    absent.

    If the year is given with only 2 digits, the function will select the
    century that makes the year closest to the current date.

    """
    # fast exit for strictly conforming string
    m = STRICT_DATE_RE.search(text)
    if m:
        g = m.groups()
        mon = MONTHS_LOWER.index(g[1].lower()) + 1
        tt = (int(g[2]), mon, int(g[0]),
              int(g[3]), int(g[4]), float(g[5]))
        return _timegm(tt)

    # No, we need some messy parsing...

    # clean up
    text = text.lstrip()
    text = WEEKDAY_RE.sub("", text, 1)  # Useless weekday

    # tz is time zone specifier string
    day, mon, yr, hr, min, sec, tz = [None]*7

    # loose regexp parse
    m = LOOSE_HTTP_DATE_RE.search(text)
    if m is not None:
        day, mon, yr, hr, min, sec, tz = m.groups()
    else:
        return None  # bad format

    return _str2time(day, mon, yr, hr, min, sec, tz) 
Example 75
Project: pyblish-win   Author: pyblish   File: cookielib.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _cookie_attrs(self, cookies):
        """Return a list of cookie-attributes to be returned to server.

        like ['foo="bar"; $Path="/"', ...]

        The $Version attribute is also added when appropriate (currently only
        once per request).

        """
        # add cookies in order of most specific (ie. longest) path first
        cookies.sort(key=lambda arg: len(arg.path), reverse=True)

        version_set = False

        attrs = []
        for cookie in cookies:
            # set version of Cookie header
            # XXX
            # What should it be if multiple matching Set-Cookie headers have
            #  different versions themselves?
            # Answer: there is no answer; was supposed to be settled by
            #  RFC 2965 errata, but that may never appear...
            version = cookie.version
            if not version_set:
                version_set = True
                if version > 0:
                    attrs.append("$Version=%s" % version)

            # quote cookie value if necessary
            # (not for Netscape protocol, which already has any quotes
            #  intact, due to the poorly-specified Netscape Cookie: syntax)
            if ((cookie.value is not None) and
                self.non_word_re.search(cookie.value) and version > 0):
                value = self.quote_re.sub(r"\\\1", cookie.value)
            else:
                value = cookie.value

            # add cookie-attributes to be returned in Cookie header
            if cookie.value is None:
                attrs.append(cookie.name)
            else:
                attrs.append("%s=%s" % (cookie.name, value))
            if version > 0:
                if cookie.path_specified:
                    attrs.append('$Path="%s"' % cookie.path)
                if cookie.domain.startswith("."):
                    domain = cookie.domain
                    if (not cookie.domain_initial_dot and
                        domain.startswith(".")):
                        domain = domain[1:]
                    attrs.append('$Domain="%s"' % domain)
                if cookie.port is not None:
                    p = "$Port"
                    if cookie.port_specified:
                        p = p + ('="%s"' % cookie.port)
                    attrs.append(p)

        return attrs 
Example 76
Project: pyblish-win   Author: pyblish   File: msvc9compiler.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _remove_visual_c_ref(self, manifest_file):
        try:
            # Remove references to the Visual C runtime, so they will
            # fall through to the Visual C dependency of Python.exe.
            # This way, when installed for a restricted user (e.g.
            # runtimes are not in WinSxS folder, but in Python's own
            # folder), the runtimes do not need to be in every folder
            # with .pyd's.
            # Returns either the filename of the modified manifest or
            # None if no manifest should be embedded.
            manifest_f = open(manifest_file)
            try:
                manifest_buf = manifest_f.read()
            finally:
                manifest_f.close()
            pattern = re.compile(
                r"""<assemblyIdentity.*?name=("|')Microsoft\."""\
                r"""VC\d{2}\.CRT("|').*?(/>|</assemblyIdentity>)""",
                re.DOTALL)
            manifest_buf = re.sub(pattern, "", manifest_buf)
            pattern = "<dependentAssembly>\s*</dependentAssembly>"
            manifest_buf = re.sub(pattern, "", manifest_buf)
            # Now see if any other assemblies are referenced - if not, we
            # don't want a manifest embedded.
            pattern = re.compile(
                r"""<assemblyIdentity.*?name=(?:"|')(.+?)(?:"|')"""
                r""".*?(?:/>|</assemblyIdentity>)""", re.DOTALL)
            if re.search(pattern, manifest_buf) is None:
                return None

            manifest_f = open(manifest_file, 'w')
            try:
                manifest_f.write(manifest_buf)
                return manifest_file
            finally:
                manifest_f.close()
        except IOError:
            pass

    # -- Miscellaneous methods -----------------------------------------
    # These are all used by the 'gen_lib_options() function, in
    # ccompiler.py. 
Example 77
Project: pyblish-win   Author: pyblish   File: argparse.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def _format_action(self, action):
        # determine the required width and the entry label
        help_position = min(self._action_max_length + 2,
                            self._max_help_position)
        help_width = max(self._width - help_position, 11)
        action_width = help_position - self._current_indent - 2
        action_header = self._format_action_invocation(action)

        # ho nelp; start on same line and add a final newline
        if not action.help:
            tup = self._current_indent, '', action_header
            action_header = '%*s%s\n' % tup

        # short action name; start on the same line and pad two spaces
        elif len(action_header) <= action_width:
            tup = self._current_indent, '', action_width, action_header
            action_header = '%*s%-*s  ' % tup
            indent_first = 0

        # long action name; start on the next line
        else:
            tup = self._current_indent, '', action_header
            action_header = '%*s%s\n' % tup
            indent_first = help_position

        # collect the pieces of the action help
        parts = [action_header]

        # if there was help for the action, add lines of help text
        if action.help:
            help_text = self._expand_help(action)
            help_lines = self._split_lines(help_text, help_width)
            parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
            for line in help_lines[1:]:
                parts.append('%*s%s\n' % (help_position, '', line))

        # or add a newline if the description doesn't end with one
        elif not action_header.endswith('\n'):
            parts.append('\n')

        # if there are any sub-actions, add their help as well
        for subaction in self._iter_indented_subactions(action):
            parts.append(self._format_action(subaction))

        # return a single string
        return self._join_parts(parts) 
Example 78
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def check_output(self, want, got, optionflags):
        """
        Return True iff the actual output from an example (`got`)
        matches the expected output (`want`).  These strings are
        always considered to match if they are identical; but
        depending on what option flags the test runner is using,
        several non-exact match types are also possible.  See the
        documentation for `TestRunner` for more information about
        option flags.
        """
        # Handle the common case first, for efficiency:
        # if they're string-identical, always return true.
        if got == want:
            return True

        # The values True and False replaced 1 and 0 as the return
        # value for boolean comparisons in Python 2.3.
        if not (optionflags & DONT_ACCEPT_TRUE_FOR_1):
            if (got,want) == ("True\n", "1\n"):
                return True
            if (got,want) == ("False\n", "0\n"):
                return True

        # <BLANKLINE> can be used as a special sequence to signify a
        # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used.
        if not (optionflags & DONT_ACCEPT_BLANKLINE):
            # Replace <BLANKLINE> in want with a blank line.
            want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER),
                          '', want)
            # If a line in got contains only spaces, then remove the
            # spaces.
            got = re.sub('(?m)^\s*?$', '', got)
            if got == want:
                return True

        # This flag causes doctest to ignore any differences in the
        # contents of whitespace strings.  Note that this can be used
        # in conjunction with the ELLIPSIS flag.
        if optionflags & NORMALIZE_WHITESPACE:
            got = ' '.join(got.split())
            want = ' '.join(want.split())
            if got == want:
                return True

        # The ELLIPSIS flag says to let the sequence "..." in `want`
        # match any substring in `got`.
        if optionflags & ELLIPSIS:
            if _ellipsis_match(want, got):
                return True

        # We didn't find any match; return false.
        return False

    # Should we do a fancy diff? 
Example 79
Project: pyblish-win   Author: pyblish   File: doctest.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def output_difference(self, example, got, optionflags):
        """
        Return a string describing the differences between the
        expected output for a given example (`example`) and the actual
        output (`got`).  `optionflags` is the set of option flags used
        to compare `want` and `got`.
        """
        want = example.want
        # If <BLANKLINE>s are being used, then replace blank lines
        # with <BLANKLINE> in the actual output string.
        if not (optionflags & DONT_ACCEPT_BLANKLINE):
            got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)

        # Check if we should use diff.
        if self._do_a_fancy_diff(want, got, optionflags):
            # Split want & got into lines.
            want_lines = want.splitlines(True)  # True == keep line ends
            got_lines = got.splitlines(True)
            # Use difflib to find their differences.
            if optionflags & REPORT_UDIFF:
                diff = difflib.unified_diff(want_lines, got_lines, n=2)
                diff = list(diff)[2:] # strip the diff header
                kind = 'unified diff with -expected +actual'
            elif optionflags & REPORT_CDIFF:
                diff = difflib.context_diff(want_lines, got_lines, n=2)
                diff = list(diff)[2:] # strip the diff header
                kind = 'context diff with expected followed by actual'
            elif optionflags & REPORT_NDIFF:
                engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
                diff = list(engine.compare(want_lines, got_lines))
                kind = 'ndiff with -expected +actual'
            else:
                assert 0, 'Bad diff option'
            # Remove trailing whitespace on diff output.
            diff = [line.rstrip() + '\n' for line in diff]
            return 'Differences (%s):\n' % kind + _indent(''.join(diff))

        # If we're not using diff, then simply list the expected
        # output followed by the actual output.
        if want and got:
            return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
        elif want:
            return 'Expected:\n%sGot nothing\n' % _indent(want)
        elif got:
            return 'Expected nothing\nGot:\n%s' % _indent(got)
        else:
            return 'Expected nothing\nGot nothing\n' 
Example 80
Project: pyblish-win   Author: pyblish   File: textwrap.py    GNU Lesser General Public License v3.0 4 votes vote down vote up
def dedent(text):
    """Remove any common leading whitespace from every line in `text`.

    This can be used to make triple-quoted strings line up with the left
    edge of the display, while still presenting them in the source code
    in indented form.

    Note that tabs and spaces are both treated as whitespace, but they
    are not equal: the lines "  hello" and "\thello" are
    considered to have no common leading whitespace.  (This behaviour is
    new in Python 2.5; older versions of this module incorrectly
    expanded tabs before searching for common leading whitespace.)
    """
    # Look for the longest leading string of spaces and tabs common to
    # all lines.
    margin = None
    text = _whitespace_only_re.sub('', text)
    indents = _leading_whitespace_re.findall(text)
    for indent in indents:
        if margin is None:
            margin = indent

        # Current line more deeply indented than previous winner:
        # no change (previous winner is still on top).
        elif indent.startswith(margin):
            pass

        # Current line consistent with and no deeper than previous winner:
        # it's the new winner.
        elif margin.startswith(indent):
            margin = indent

        # Current line and previous winner have no common whitespace:
        # there is no margin.
        else:
            margin = ""
            break

    # sanity check (testing/debugging only)
    if 0 and margin:
        for line in text.split("\n"):
            assert not line or line.startswith(margin), \
                   "line = %r, margin = %r" % (line, margin)

    if margin:
        text = re.sub(r'(?m)^' + margin, '', text)
    return text