28
28
29
29
import glob
30
30
import imp
31
+ import importlib
31
32
import os .path
32
33
import sys
33
34
@@ -50,10 +51,9 @@ def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, m
50
51
ast .accept (gen )
51
52
if not target :
52
53
target = os .path .join (output_dir , os .path .basename (path ))
53
- for i in range (target .count ('/' )):
54
- subdir = os .path .dirname (target )
55
- if subdir and not os .path .isdir (subdir ):
56
- os .makedirs (subdir )
54
+ subdir = os .path .dirname (target )
55
+ if subdir and not os .path .isdir (subdir ):
56
+ os .makedirs (subdir )
57
57
with open (target , 'w' ) as file :
58
58
if add_header :
59
59
write_header (file , module )
@@ -62,26 +62,22 @@ def generate_stub(path, output_dir, _all_=None, target=None, add_header=False, m
62
62
63
63
def generate_stub_for_module (module , output_dir , quiet = False , add_header = False , sigs = {},
64
64
class_sigs = {}):
65
- mod = __import__ (module )
65
+ mod = importlib . import_module (module )
66
66
imp .reload (mod )
67
- components = module .split ('.' )
68
- for attr in components [1 :]:
69
- mod = getattr (mod , attr )
70
67
if is_c_module (mod ):
71
- target = '/' . join ( components [: - 1 ] + [ components [ - 1 ] + '.pyi' ])
68
+ target = module . replace ( '.' , '/' ) + '.pyi'
72
69
target = os .path .join (output_dir , target )
73
70
generate_stub_for_c_module (module_name = module ,
74
71
target = target ,
75
72
add_header = add_header ,
76
73
sigs = sigs ,
77
74
class_sigs = class_sigs )
78
75
else :
79
- target = '/' .join (module .split ('.' )[:- 1 ])
80
- modfnam = os .path .basename (mod .__file__ )
81
- if modfnam == '__init__.py' :
82
- target = os .path .join (target , module .split ('.' )[- 1 ], '__init__.pyi' )
76
+ target = module .replace ('.' , '/' )
77
+ if os .path .basename (mod .__file__ ) == '__init__.py' :
78
+ target += '/__init__.pyi'
83
79
else :
84
- target = os . path . join ( target , modfnam . replace ( '.py' , '. pyi'))
80
+ target += '. pyi'
85
81
target = os .path .join (output_dir , target )
86
82
generate_stub (mod .__file__ , output_dir , getattr (mod , '__all__' , None ),
87
83
target = target , add_header = add_header , module = module )
0 commit comments