def generate_stub(path: str, output_dir: str, _all_: Optional[List[str]] = None, target: Optional[str] = None, add_header: bool = False, module: Optional[str] = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, include_private: bool = False ) -> None: with open(path, 'rb') as f: data = f.read() source = mypy.util.decode_python_encoding(data, pyversion) options = MypyOptions() options.python_version = pyversion try: ast = mypy.parse.parse(source, fnam=path, module=module, errors=None, options=options) except mypy.errors.CompileError as e: # Syntax error! for m in e.messages: sys.stderr.write('%s\n' % m) sys.exit(1) gen = StubGenerator(_all_, pyversion=pyversion, include_private=include_private) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module, pyversion=pyversion) file.write(''.join(gen.output()))
def generate_stub( path: str, output_dir: str, _all_: Optional[List[str]] = None, target: str = None, add_header: bool = False, module: str = None, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION) -> None: source = open(path, 'rb').read() try: ast = mypy.parse.parse(source, fnam=path, pyversion=pyversion) except mypy.errors.CompileError as e: # Syntax error! for m in e.messages: sys.stderr.write('%s\n' % m) exit(1) gen = StubGenerator(_all_, pyversion=pyversion) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module, pyversion=pyversion) file.write(''.join(gen.output()))
def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, module=None, pyversion=3): source = open(path, 'rb').read() try: ast = mypy.parse.parse(source, fnam=path, pyversion=pyversion) except mypy.errors.CompileError as e: # Syntax error! for m in e.messages: sys.stderr.write('%s\n' % m) exit(1) gen = StubGenerator(_all_, pyversion=pyversion) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module, pyversion=pyversion) file.write(''.join(gen.output()))
def generate_stub_from_ast(mod: StubSource, target: str, parse_only: bool = False, pyversion: Tuple[int, int] = defaults.PYTHON3_VERSION, include_private: bool = False, add_header: bool = True) -> None: """Use analysed (or just parsed) AST to generate type stub for single file. If directory for target doesn't exist it will created. Existing stub will be overwritten. """ gen = StubGenerator(mod.runtime_all, pyversion=pyversion, include_private=include_private, analyzed=not parse_only) assert mod.ast is not None, "This function must be used only with analyzed modules" mod.ast.accept(gen) # Write output to file. subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, mod.module, pyversion=pyversion) file.write(''.join(gen.output()))
def generate_stub_for_c_module( module_name: str, target: str, add_header: bool = True, sigs: Dict[str, str] = {}, class_sigs: Dict[str, str] = {}, ) -> None: module = importlib.import_module(module_name) assert is_c_module(module), '%s is not a C module' % module_name subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) functions = [] # type: List[str] done = set() items = sorted(module.__dict__.items(), key=lambda x: x[0]) for name, obj in items: if is_c_function(obj): generate_c_function_stub(module, name, obj, functions, sigs=sigs) done.add(name) types = [] # type: List[str] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if is_c_type(obj): generate_c_type_stub(module, name, obj, types, sigs=sigs, class_sigs=class_sigs) done.add(name) variables = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if name not in done: type_str = type(obj).__name__ if type_str not in ('int', 'str', 'bytes', 'float', 'bool'): type_str = 'Any' variables.append('%s: %s' % (name, type_str)) output = [] for line in variables: output.append(line) if output and functions: output.append('') for line in functions: output.append(line) for line in types: if line.startswith('class') and output and output[-1]: output.append('') output.append(line) output = add_typing_import(output) with open(target, 'w') as file: if add_header: write_header(file, module_name) for line in output: file.write('%s\n' % line)
def generate_stub_for_c_module(module_name, target, add_header=True, sigs={}, class_sigs={}): module = __import__(module_name) assert is_c_module(module), '%s is not a C module' % module_name functions = [] done = set() items = sorted(module.__dict__.items(), key=lambda x: x[0]) for name, obj in items: if is_c_function(obj): generate_c_function_stub(module, name, obj, functions, sigs=sigs) done.add(name) types = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if is_c_type(obj): generate_c_type_stub(module, name, obj, types, sigs=sigs, class_sigs=class_sigs) done.add(name) variables = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if name not in done: type_str = type(obj).__name__ if type_str not in ('int', 'str', 'bytes', 'float', 'bool'): type_str = 'Any' variables.append('%s = ... # type: %s' % (name, type_str)) output = [] for line in variables: output.append(line) if output and functions: output.append('') for line in functions: output.append(line) for line in types: if line.startswith('class') and output and output[-1]: output.append('') output.append(line) output = add_typing_import(output) with open(target, 'w') as file: if add_header: write_header(file, module_name) for line in output: file.write('%s\n' % line)
def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, module=None): source = open(path).read() ast = mypy.parse.parse(source) gen = StubGenerator(_all_) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module) file.write(''.join(gen.output()))
def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, module=None): source = open(path).read() ast = mypy.parse.parse(source) gen = StubGenerator(_all_) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module) file.write(''.join(gen.output()))
def generate_stub_for_c_module(module_name, target, add_header=True, sigs={}, class_sigs={}): module = __import__(module_name) assert is_c_module(module), '%s is not a C module' % module_name functions = [] done = set() items = sorted(module.__dict__.items(), key=lambda x: x[0]) for name, obj in items: if is_c_function(obj): generate_c_function_stub(module, name, obj, functions, sigs=sigs) done.add(name) types = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if is_c_type(obj): generate_c_type_stub(module, name, obj, types, sigs=sigs, class_sigs=class_sigs) done.add(name) variables = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if name not in done: type_str = type(obj).__name__ if type_str not in ('int', 'str', 'bytes', 'float', 'bool'): type_str = 'Any' variables.append('%s = Undefined(%s)' % (name, type_str)) output = [] for line in variables: output.append(line) if output and functions: output.append('') for line in functions: output.append(line) for line in types: if line.startswith('class') and output and output[-1]: output.append('') output.append(line) output = add_typing_import(output) with open(target, 'w') as file: if add_header: write_header(file, module_name) for line in output: file.write('%s\n' % line)
def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, module=None, pyversion=defaults.PYTHON3_VERSION): source = open(path, 'rb').read() try: ast = mypy.parse.parse(source, fnam=path, pyversion=pyversion) except mypy.errors.CompileError as e: # Syntax error! for m in e.messages: sys.stderr.write('%s\n' % m) exit(1) gen = StubGenerator(_all_, pyversion=pyversion) ast.accept(gen) if not target: target = os.path.join(output_dir, os.path.basename(path)) subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) with open(target, 'w') as file: if add_header: write_header(file, module, pyversion=pyversion) file.write(''.join(gen.output()))
def generate_stub_for_c_module( module_name: str, target: str, add_header: bool = True, sigs: Optional[Dict[str, str]] = None, class_sigs: Optional[Dict[str, str]] = None) -> None: """Generate stub for C module. This combines simple runtime introspection (looking for docstrings and attributes with simple builtin types) and signatures inferred from .rst documentation (if given). If directory for target doesn't exist it will be created. Existing stub will be overwritten. """ module = importlib.import_module(module_name) assert is_c_module(module), '%s is not a C module' % module_name subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) imports = [] # type: List[str] functions = [] # type: List[str] done = set() items = sorted(module.__dict__.items(), key=lambda x: x[0]) for name, obj in items: if is_c_function(obj): generate_c_function_stub(module, name, obj, functions, imports=imports, sigs=sigs) done.add(name) types = [] # type: List[str] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if is_c_type(obj): generate_c_type_stub(module, name, obj, types, imports=imports, sigs=sigs, class_sigs=class_sigs) done.add(name) variables = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if name not in done and not inspect.ismodule(obj): type_str = type(obj).__name__ if type_str not in ('int', 'str', 'bytes', 'float', 'bool'): type_str = 'Any' variables.append('%s: %s' % (name, type_str)) output = [] for line in sorted(set(imports)): output.append(line) for line in variables: output.append(line) if output and functions: output.append('') for line in functions: output.append(line) for line in types: if line.startswith('class') and output and output[-1]: output.append('') output.append(line) output = add_typing_import(output) with open(target, 'w') as file: if add_header: write_header(file, module_name) for line in output: file.write('%s\n' % line)
def generate_stub_for_c_module(module_name: str, target: str, add_header: bool = True, sigs: Optional[Dict[str, str]] = None, class_sigs: Optional[Dict[str, str]] = None) -> None: """Generate stub for C module. This combines simple runtime introspection (looking for docstrings and attributes with simple builtin types) and signatures inferred from .rst documentation (if given). If directory for target doesn't exist it will be created. Existing stub will be overwritten. """ module = importlib.import_module(module_name) assert is_c_module(module), '%s is not a C module' % module_name subdir = os.path.dirname(target) if subdir and not os.path.isdir(subdir): os.makedirs(subdir) imports = [] # type: List[str] functions = [] # type: List[str] done = set() items = sorted(module.__dict__.items(), key=lambda x: x[0]) for name, obj in items: if is_c_function(obj): generate_c_function_stub(module, name, obj, functions, imports=imports, sigs=sigs) done.add(name) types = [] # type: List[str] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if is_c_type(obj): generate_c_type_stub(module, name, obj, types, imports=imports, sigs=sigs, class_sigs=class_sigs) done.add(name) variables = [] for name, obj in items: if name.startswith('__') and name.endswith('__'): continue if name not in done and not inspect.ismodule(obj): type_str = type(obj).__name__ if type_str not in ('int', 'str', 'bytes', 'float', 'bool'): type_str = 'Any' variables.append('%s: %s' % (name, type_str)) output = [] for line in sorted(set(imports)): output.append(line) for line in variables: output.append(line) if output and functions: output.append('') for line in functions: output.append(line) for line in types: if line.startswith('class') and output and output[-1]: output.append('') output.append(line) output = add_typing_import(output) with open(target, 'w') as file: if add_header: write_header(file, module_name) for line in output: file.write('%s\n' % line)