Change some file extensions to those which are more likely to be text

    >>> pyc_to_py('vim.pyc') == 'vim.py'
    True
488461
def pyc_to_py(path_to_file):
    
    stem, ext = os.path.splitext(path_to_file)
    if ext == '.pyc':
        return '%s.py' % stem
    return path_to_file
751949
def clean_readme(fname):
    """"""
    with codecs.open(fname, 'r', 'utf-8') as f:
        return ''.join(
            re.sub(r':\w+:`([^`]+?)( <[^<>]+>)?`', r'``\1``', line)
            for line in f
            if not (line.startswith('.. currentmodule') or line.startswith('.. toctree'))
        )
3035
def is_python(text, filename='<string>'):
    ""
    try:
        compile(text, filename, 'exec')
    except (SyntaxError, TypeError):
        return False
    else:
        return True
906052
def TeXLaTeXStrFunction(target = None, source= None, env=None):
    """"""
    if env.GetOption("no_exec"):

        # find these paths for use in is_LaTeX to search for included files
        basedir = os.path.split(str(source[0]))[0]
        abspath = os.path.abspath(basedir)

        if is_LaTeX(source,env,abspath):
            result = env.subst('$LATEXCOM',0,target,source)+" ..."
        else:
            result = env.subst("$TEXCOM",0,target,source)+" ..."
    else:
        result = ''
    return result
1179158
def _get_pygments_extensions():
    """"""
    # NOTE: Leave this import here to keep startup process fast!
    import pygments.lexers as lexers

    extensions = []
    for lx in lexers.get_all_lexers():
        lexer_exts = lx[2]

        if lexer_exts:
            # Reference: This line was included for leaving untrimmed the
            # extensions not starting with `*`
            other_exts = [le for le in lexer_exts if not le.startswith('*')]
            # Reference: This commented line was replaced by the following one
            # to trim only extensions that start with '*'
            # lexer_exts = [le[1:] for le in lexer_exts]
            lexer_exts = [le[1:] for le in lexer_exts if le.startswith('*')]
            lexer_exts = [le for le in lexer_exts if not le.endswith('_*')]
            extensions = extensions + list(lexer_exts) + list(other_exts)

    return sorted(list(set(extensions)))
374191
def isPythonFile(filename):
    """"""
    if filename.endswith('.py'):
        return True

    # Avoid obvious Emacs backup files
    if filename.endswith("~"):
        return False

    max_bytes = 128

    try:
        with open(filename, 'rb') as f:
            text = f.read(max_bytes)
            if not text:
                return False
    except IOError:
        return False

    first_line = text.splitlines()[0]
    return PYTHON_SHEBANG_REGEX.match(first_line)
166623
def lower_extension(filename):
    '''
    
    '''
    if '.' in filename:
        main, ext = os.path.splitext(filename)
        return main + ext.lower()
    # For consistency with os.path.splitext,
    # do not treat a filename without an extension as an extension.
    # That is, do not return filename.lower().
    return filename
576472
def replace_ext(filename, ext):
    
    if ext.startswith('.'):
        ext = ext[1:]
    stem, _ = os.path.splitext(filename)
    return (stem + '.' + ext)
1112318
def punify_filename(filename):
    
    path, extension = splitext(filename)
    return path.encode('punycode').decode('utf8') + extension
1265809
def get_py_file_if_possible(pyc_name):
    """"""
    if pyc_name.endswith(('.py', '.so', '.pyd')):
        return pyc_name
    assert pyc_name.endswith('.pyc')
    non_compiled_file = pyc_name[:-1]
    if os.path.exists(non_compiled_file):
        return non_compiled_file
    return pyc_name
406194
def get_module_names(p):
    '''
    '''
    mods = list()
    mods = [f.split('.')[0] for f in listdir(p)
            if isfile(join(p, f)) and not f.endswith('.pyc') and not f.startswith('__')]
    print len(mods)
    return mods
663119
def __remove_pyc_pyo(fname):
    """"""
    if osp.splitext(fname)[1] == '.py':
        for ending in ('c', 'o'):
            if osp.exists(fname+ending):
                os.remove(fname+ending)
72549
def fix_hyphen_commands(raw_cli_arguments):
    """"""
    for i in ['gen-sample']:
        raw_cli_arguments[i.replace('-', '_')] = raw_cli_arguments[i]
        raw_cli_arguments.pop(i)
    return raw_cli_arguments
892900
def build_pyfile_path_from_docname(self, docfile):
        
        name, ext = os.path.splitext(docfile)
        expected_py_name = name.replace('.', '/') + '.py'
        return expected_py_name
1268577
def _expand_filename(self, line):
        """"""
        # expand .
        newline = line
        path = os.getcwd()
        if newline.startswith("."):
            newline = newline.replace(".", path, 1)
        # expand ~
        newline = os.path.expanduser(newline)
        return newline
893525
def replace_in_file(filename: str, text_from: str, text_to: str) -> None:
    
    log.info("Amending {}: {} -> {}",
             filename, repr(text_from), repr(text_to))
    with open(filename) as infile:
        contents = infile.read()
    contents = contents.replace(text_from, text_to)
    with open(filename, 'w') as outfile:
        outfile.write(contents)
770807
def extension(names):
    """"""
    for name in names:
        if not NAME_PATTERN.match(name):
            raise ValueError('invalid extension name: %s' % name)
    def decorator(f, names=names):
        return Extension(f, names=names)
    return decorator