5
5
6
6
_default_skip_file_list = ["__init__.py" ]
7
7
_default_skip_dir_list = ["__pycache__" ]
8
+ _conflict_name_dict = {
9
+ "manager" : ["eviction" , "object_data" , "scalar_data" , "vector_data" ]
10
+ }
8
11
9
12
10
13
class DocGen :
11
- def __init__ (self , lib_name = "gptcache" , source_dir = "../gptcache" , output_dir = "references" , skip_list = []):
14
+ def __init__ (
15
+ self ,
16
+ lib_name = "gptcache" ,
17
+ source_dir = "../gptcache" ,
18
+ output_dir = "references" ,
19
+ skip_list = [],
20
+ ):
12
21
self .lib_name = lib_name
13
22
self .output_dir = os .path .abspath (output_dir )
14
23
self .source_dir = os .path .abspath (source_dir )
@@ -26,8 +35,13 @@ def section_bar(input_str):
26
35
def get_filename (input_str ):
27
36
if input_str == "gptcache" :
28
37
return input_str
29
- input_str = os .path .splitext (input_str )[1 ][1 :]
30
- return input_str
38
+ suffix = os .path .splitext (input_str )[1 ][1 :]
39
+ for conflict_dir , conflict_names in _conflict_name_dict .items ():
40
+ for conflict_name in conflict_names :
41
+ if f"{ conflict_dir } .{ conflict_name } " in input_str :
42
+ return f"{ conflict_name } .{ suffix } "
43
+
44
+ return suffix
31
45
32
46
@staticmethod
33
47
def cap (input_str ):
@@ -37,17 +51,25 @@ def cap(input_str):
37
51
return str .join (" " , [i .capitalize () for i in input_str .split ("_" )])
38
52
39
53
def model_name (self , input_str : str ):
40
- return self .lib_name + input_str [len (self .source_dir ):].replace ("/" , "." )
54
+ return self .lib_name + input_str [len (self .source_dir ) :].replace ("/" , "." )
41
55
42
56
def get_module_and_libs (self , module_dir , is_root ):
43
57
module = self .model_name (module_dir )
44
58
libs = []
45
59
for file in os .listdir (module_dir ):
46
- if os .path .isfile (os .path .join (module_dir , file )) and file not in _default_skip_file_list :
60
+ if (
61
+ os .path .isfile (os .path .join (module_dir , file ))
62
+ and file not in _default_skip_file_list
63
+ ):
47
64
libs .append (module + "." + os .path .splitext (file )[0 ])
48
65
if not is_root :
49
- if os .path .isdir (os .path .join (module_dir , file )) and file not in _default_skip_dir_list :
50
- _ , child_libs = self .get_module_and_libs (os .path .join (module_dir , file ), False )
66
+ if (
67
+ os .path .isdir (os .path .join (module_dir , file ))
68
+ and file not in _default_skip_dir_list
69
+ ):
70
+ _ , child_libs = self .get_module_and_libs (
71
+ os .path .join (module_dir , file ), False
72
+ )
51
73
libs .extend (child_libs )
52
74
if len (libs ) > 0 :
53
75
sorted (libs )
@@ -57,7 +79,8 @@ def get_module_and_libs(self, module_dir, is_root):
57
79
def generate (self ):
58
80
# Set the output directory
59
81
env = Environment (
60
- loader = FileSystemLoader (os .path .join (self .output_dir , "../_templates" )), autoescape = select_autoescape ()
82
+ loader = FileSystemLoader (os .path .join (self .output_dir , "../_templates" )),
83
+ autoescape = select_autoescape (),
61
84
)
62
85
63
86
# Add custom filters
@@ -89,18 +112,29 @@ def generate(self):
89
112
index_temp = env .get_template ("index.rst" )
90
113
91
114
with open (os .path .join (self .output_dir , "index.rst" ), "w" ) as f :
92
- t = index_temp .render ({"modules" : [DocGen .get_filename (module ) for module in modules ]})
115
+ t = index_temp .render (
116
+ {"modules" : [DocGen .get_filename (module ) for module in modules ]}
117
+ )
93
118
f .write (t )
94
119
95
120
# Render the function templates and write rendered output to files
96
121
func_temp = env .get_template ("function.rst" )
97
122
98
123
for index , module in enumerate (modules ):
99
- with open (os .path .join (self .output_dir , f"{ DocGen .get_filename (module )} .rst" ), "w" ) as f :
100
- t = func_temp .render ({"module_name" : module , "funcs" : [(DocGen .get_filename (lib ), lib ) for lib in libs [index ]]})
124
+ with open (
125
+ os .path .join (self .output_dir , f"{ DocGen .get_filename (module )} .rst" ), "w"
126
+ ) as f :
127
+ t = func_temp .render (
128
+ {
129
+ "module_name" : module ,
130
+ "funcs" : [
131
+ (DocGen .get_filename (lib ), lib ) for lib in libs [index ]
132
+ ],
133
+ }
134
+ )
101
135
f .write (t )
102
136
103
137
104
138
# if __name__ == "__main__":
105
139
# gen = DocGen(source_dir="/Users/derek/fubang/gptcache/gptcache", output_dir="/Users/derek/fubang/gptcache/docs/references")
106
- # gen.generate()
140
+ # gen.generate()
0 commit comments