8
8
from sphinx .ext .napoleon .docstring import GoogleDocstring
9
9
10
10
11
+ def classref (value , short = True ):
12
+ tilde = '~' if short else ''
13
+ return ':class:`%sgitlab.objects.%s`' % (tilde , value .__name__ )
14
+
15
+
11
16
def setup (app ):
12
17
app .connect ('autodoc-process-docstring' , _process_docstring )
13
18
app .connect ('autodoc-skip-member' , napoleon ._skip_member )
@@ -28,58 +33,34 @@ def _process_docstring(app, what, name, obj, options, lines):
28
33
29
34
30
35
class GitlabDocstring (GoogleDocstring ):
31
- def _build_doc (self ):
32
- cls = self ._obj .obj_cls
33
- opt_get_list = cls .optionalGetAttrs
34
- opt_list_list = cls .optionalListAttrs
35
- md_create_list = list (itertools .chain (cls .requiredUrlAttrs ,
36
- cls .requiredCreateAttrs ))
37
- opt_create_list = cls .optionalCreateAttrs
38
-
39
- opt_get_keys = "None"
40
- if opt_get_list :
41
- opt_get_keys = ", " .join (['``%s``' % i for i in opt_get_list ])
42
-
43
- opt_list_keys = "None"
44
- if opt_list_list :
45
- opt_list_keys = ", " .join (['``%s``' % i for i in opt_list_list ])
46
-
47
- md_create_keys = opt_create_keys = "None"
48
- if md_create_list :
49
- md_create_keys = ", " .join (['``%s``' % i for i in md_create_list ])
50
- if opt_create_list :
51
- opt_create_keys = ", " .join (['``%s``' % i for i in
52
- opt_create_list ])
53
-
54
- md_update_list = list (itertools .chain (cls .requiredUrlAttrs ,
55
- cls .requiredUpdateAttrs ))
56
- opt_update_list = cls .optionalUpdateAttrs
57
-
58
- md_update_keys = opt_update_keys = "None"
59
- if md_update_list :
60
- md_update_keys = ", " .join (['``%s``' % i for i in md_update_list ])
61
- if opt_update_list :
62
- opt_update_keys = ", " .join (['``%s``' % i for i in
63
- opt_update_list ])
64
-
65
- tmpl_file = os .path .join (os .path .dirname (__file__ ), 'template.j2' )
66
- with open (tmpl_file ) as fd :
67
- template = jinja2 .Template (fd .read (), trim_blocks = False )
68
- output = template .render (filename = tmpl_file ,
69
- cls = cls ,
70
- md_create_keys = md_create_keys ,
71
- opt_create_keys = opt_create_keys ,
72
- md_update_keys = md_update_keys ,
73
- opt_update_keys = opt_update_keys ,
74
- opt_get_keys = opt_get_keys ,
75
- opt_list_keys = opt_list_keys )
36
+ _j2_env = None
37
+
38
+ def _build_j2_env (self ):
39
+ if self ._j2_env is None :
40
+ self ._j2_env = jinja2 .Environment (loader = jinja2 .FileSystemLoader (
41
+ os .path .dirname (__file__ )), trim_blocks = False )
42
+ self ._j2_env .filters ['classref' ] = classref
43
+
44
+ return self ._j2_env
45
+
46
+ def _build_manager_doc (self ):
47
+ env = self ._build_j2_env ()
48
+ template = env .get_template ('manager_tmpl.j2' )
49
+ output = template .render (cls = self ._obj .obj_cls )
50
+
51
+ return output .split ('\n ' )
52
+
53
+ def _build_object_doc (self ):
54
+ env = self ._build_j2_env ()
55
+ template = env .get_template ('object_tmpl.j2' )
56
+ output = template .render (obj = self ._obj )
76
57
77
58
return output .split ('\n ' )
78
59
79
60
def __init__ (self , * args , ** kwargs ):
80
61
super (GitlabDocstring , self ).__init__ (* args , ** kwargs )
81
62
82
- if not hasattr (self ._obj , 'obj_cls' ) or self ._obj .obj_cls is None :
83
- return
84
-
85
- self ._parsed_lines = self ._build_doc ()
63
+ if hasattr (self ._obj , 'obj_cls' ) and self ._obj .obj_cls is not None :
64
+ self . _parsed_lines = self . _build_manager_doc ()
65
+ elif hasattr ( self . _obj , 'canUpdate' ) and self . _obj . canUpdate :
66
+ self ._parsed_lines = self ._build_object_doc ()
0 commit comments