Ejemplo n.º 1
0
def _gen_stub2_filename(stub_file, base_module):
    if os.path.isfile(stub_file):
        bn = os.path.basename(stub_file).rpartition(module_filename_delim)[0]
        if pytypes.stub_gen_dir is None:
            checksum = util._md5(stub_file)
            return tempfile.gettempdir()+os.sep+bn+'__'+checksum+'.pyi2'
        else:
            pck = '' if base_module.__package__ is None else \
                    base_module.__package__.replace('.', os.sep)+os.sep
            return os.path.abspath(pytypes.stub_gen_dir)+os.sep+pck+bn+'.pyi2'
    else:
        # If there is no original file, no generated file(name) can be created:
        return None
Ejemplo n.º 2
0
def _check_py2_stubmodule(pyi_file, pyi2_module):
    if pyi2_module.__doc__ is None:
        # File was hand-crafted.
        return True
    lines = pyi2_module.__doc__.split('\n')
    if len(lines) < 5 or lines[4] != 'This file was generated by pytypes. Do not edit directly.':
        # File was hand-crafted.
        return True
    if (not pyi_file is None) and os.path.normpath(pyi_file) != os.path.normpath(lines[2]):
        # File wasn't generated from the source we thought it was.
        return False
    in_file = lines[2] if pyi_file is None else pyi_file
    if os.path.isfile(in_file):
        return lines[3].endswith(util._md5(in_file))
    else:
        return False
Ejemplo n.º 3
0
def convert(in_file, out_file=None):
    _print('in_file: ' + in_file)
    assert (os.path.isfile(in_file))
    checksum = util._md5(in_file)
    if out_file is None:
        out_file = in_file + '2'
    _print('out_file: ' + out_file)

    with open(in_file, stub_open_mode) as module_file:
        module_name = os.path.basename(in_file)
        stub_module = imp.load_module(module_name, module_file, in_file,
                                      stub_descr)

    funcs = [
        func[1] for func in inspect.getmembers(stub_module, inspect.isfunction)
    ]
    cls = [cl[1] for cl in inspect.getmembers(stub_module, inspect.isclass)]

    directory = os.path.dirname(out_file)
    if not os.path.exists(directory):
        os.makedirs(directory)

    with open(out_file, 'w') as out_file_handle:
        lines = [
            "'''", 'Python 2.7-compliant stubfile of ', in_file,
            'with MD5-Checksum: ' + checksum,
            'This file was generated by pytypes. Do not edit directly.', "'''",
            '', 'import typing',
            'from typing import Any, Tuple, List, Union, Generic, Optional, \\',
            2 * indent + 'TypeVar, Set, FrozenSet, Dict, Generator',
            'import numbers'
        ]
        for func in funcs:
            lines.append('')
            _write_func(func, lines)

        for cl in cls:
            if not (hasattr(numbers, cl.__name__)
                    or hasattr(typing, cl.__name__)):
                lines.append('\n')
                _write_class(cl, lines)

        for i in range(len(lines)):
            _print(lines[i])
            lines[i] = lines[i] + '\n'
        lines.append('\n')
        out_file_handle.writelines(lines)
Ejemplo n.º 4
0
def convert(in_file, out_file=None):
    global _implicit_globals
    _print('in_file: ' + in_file)
    assert (os.path.isfile(in_file))
    checksum = util._md5(in_file)
    if out_file is None:
        if in_file.endswith('py'):
            if py3:
                out_file = in_file + 'i'
            else:
                out_file = in_file + 'i2'
        elif in_file.endswith('pyi'):
            if py3:
                out_file = in_file
            else:
                out_file = in_file + '2'
        else:
            out_file = in_file + '2'
        if os.path.exists(out_file) and not force:
            _print("File already exists: " + str(out_file))
            _print("Run with --force to force overwriting.")
            sys.exit(os.EX_CANTCREAT)
    _print('out_file: ' + out_file)

    with open(in_file, stub_open_mode) as module_file:
        module_name = os.path.basename(in_file)
        stub_module = imp.load_module(module_name, module_file, in_file,
                                      stub_descr)
        if module_name in sys.modules:
            _implicit_globals.add(sys.modules[module_name])
        module_basename = module_name.rsplit('.')[0]
        if module_basename in sys.modules:
            _implicit_globals.add(sys.modules[module_basename])

    funcs = [
        func[1] for func in inspect.getmembers(stub_module, inspect.isfunction)
    ]
    cls = [cl[1] for cl in inspect.getmembers(stub_module, inspect.isclass)]
    tpvs = set()
    i = 0
    if _tpvar_is_class:
        while i < len(cls):
            if isinstance(cls[i], TypeVar):
                tpvs.add(cls[i])
                del cls[i]
            else:
                i += 1
    else:
        # Python 3.6
        for tpv in inspect.getmembers(stub_module,
                                      lambda t: isinstance(t, TypeVar)):
            tpvs.add(tpv[1])

    funcs.sort(key=_func_get_line)
    cls.sort(key=_class_get_line)

    directory = os.path.dirname(out_file)
    if directory == '':
        directory = '.'
    if not os.path.exists(directory):
        os.makedirs(directory)
    assumed_glbls = set()
    assumed_typevars = set()
    nw = datetime.datetime.now()
    with open(out_file, 'w') as out_file_handle:
        try:
            version = pkg_resources.get_distribution('pytypes').version
        except:
            version = pytypes._version
        lines = [
            '"""', 'Python 2.7-compliant stubfile of ', in_file,
            'MD5-Checksum: ' + checksum,
            '\nThis file was generated by pytypes.stubfile_2_converter v' +
            version, 'at ' + nw.isoformat(),
            'running on ' + util._python_version_string() + '.\n', 'WARNING:',
            'If you edit this file, be aware that it was automatically generated.',
            'Save your customized version to a distinct place;',
            'this file might be overwritten without notice.', '"""', '', ''
        ]  # import section later goes here; don't forget to track in imp_index
        imp_index = 13
        any_tpv = False
        for tpv in tpvs:
            if util.search_class_module(tpv) is stub_module:
                if not any_tpv:
                    lines.append('')
                _write_TypeVar(tpv, lines)
                any_tpv = True

        for func in funcs:
            lines.append('')
            _write_func(func, lines, assumed_globals=assumed_glbls)

        for cl in cls:
            if sys.modules[cl.__module__] in _implicit_globals:
                lines.append('\n')
                _write_class(cl,
                             lines,
                             assumed_globals=assumed_glbls,
                             implicit_globals=_implicit_globals,
                             assumed_typevars=assumed_typevars)

        for el in tpvs:
            assumed_typevars.add(el)

        imp_lines = typelogger._make_import_section(assumed_glbls,
                                                    assumed_typevars,
                                                    _implicit_globals)
        lines.insert(imp_index, '\n'.join(imp_lines))
        for i in range(len(lines)):
            _print(lines[i])
            lines[i] = lines[i] + '\n'
        lines.append('\n')
        out_file_handle.writelines(lines)