6
6
7
7
import logging
8
8
from collections import OrderedDict
9
- from andes .core .documenter import Documenter as adDocumenter
10
9
from andes .utils .tab import make_doc_table , math_wrap
11
10
12
11
logger = logging .getLogger (__name__ )
13
12
14
13
15
- def disable_method (func ):
16
- def wrapper (* args , ** kwargs ):
17
- msg = f"Method `{ func .__name__ } ` is included in ANDES Documenter but not supported in AMS Documenter."
18
- logger .warning (msg )
19
- return None
20
- return wrapper
21
-
22
-
23
- def disable_methods (methods ):
24
- for method in methods :
25
- setattr (Documenter , method , disable_method (getattr (Documenter , method )))
26
-
27
-
28
- class Documenter (adDocumenter ):
14
+ class Documenter :
29
15
"""
30
16
Helper class for documenting models.
31
17
@@ -37,16 +23,6 @@ class Documenter(adDocumenter):
37
23
38
24
def __init__ (self , parent ):
39
25
self .parent = parent
40
- self .system = parent .system
41
- self .class_name = parent .class_name
42
- self .config = parent .config
43
- self .params = parent .params
44
- self .algebs = parent .algebs
45
- self .services = parent .services
46
-
47
- func_to_disable = ['_init_doc' , '_eq_doc' ,
48
- '_discrete_doc' , '_block_doc' ]
49
- disable_methods (func_to_disable )
50
26
51
27
def get (self , max_width = 78 , export = 'plain' ):
52
28
"""
@@ -68,14 +44,15 @@ def get(self, max_width=78, export='plain'):
68
44
if export == 'rest' :
69
45
max_width = 0
70
46
model_header = '-' * 80 + '\n '
71
- out += f'.. _{ self .class_name } :\n \n '
47
+ out += f'.. _{ self .parent . class_name } :\n \n '
72
48
else :
73
49
model_header = ''
74
50
75
51
if export == 'rest' :
76
- out += model_header + f'{ self .class_name } \n ' + model_header
52
+ out += model_header + f'{ self .parent . class_name } \n ' + model_header
77
53
else :
78
- out += model_header + f'Model <{ self .class_name } > in Group <{ self .parent .group } >\n ' + model_header
54
+ out += model_header + \
55
+ f'Model <{ self .parent .class_name } > in Group <{ self .parent .group } >\n ' + model_header
79
56
80
57
if self .parent .__doc__ is not None :
81
58
out += inspect .cleandoc (self .parent .__doc__ )
@@ -86,20 +63,35 @@ def get(self, max_width=78, export='plain'):
86
63
out += self ._var_doc (max_width = max_width , export = export )
87
64
out += self ._service_doc (max_width = max_width , export = export )
88
65
# TODO: fix and add the config doc later on
89
- # out += self.config.doc(max_width=max_width, export=export)
66
+ # out += self.parent. config.doc(max_width=max_width, export=export)
90
67
91
68
return out
92
69
93
70
def _var_doc (self , max_width = 78 , export = 'plain' ):
71
+ """
72
+ Export formatted model variable documentation as a string.
73
+
74
+ Parameters
75
+ ----------
76
+ max_width : int, optional = 80
77
+ Maximum table width. If export format is ``rest`` it will be unlimited.
78
+ export : str, optional = 'plain'
79
+ Export format, 'plain' for plain text, 'rest' for restructuredText.
80
+
81
+ Returns
82
+ -------
83
+ str
84
+ Tabulated output in a string
85
+ """
94
86
# variable documentation
95
- if len (self .algebs ) == 0 :
87
+ if len (self .parent . algebs ) == 0 :
96
88
return ''
97
89
98
90
names , symbols , units = list (), list (), list ()
99
91
info = list ()
100
92
units_rest , ty = list (), list ()
101
93
102
- for p in self .algebs .values ():
94
+ for p in self .parent . algebs .values ():
103
95
names .append (p .name )
104
96
ty .append (p .class_name )
105
97
info .append (p .info if p .info else '' )
@@ -110,7 +102,7 @@ def _var_doc(self, max_width=78, export='plain'):
110
102
111
103
# replace with latex math expressions if export is ``rest``
112
104
if export == 'rest' :
113
- symbols = [item .tex_name for item in self .algebs .values ()]
105
+ symbols = [item .tex_name for item in self .parent . algebs .values ()]
114
106
symbols = math_wrap (symbols , export = export )
115
107
title = 'Variables\n ---------'
116
108
@@ -132,14 +124,29 @@ def _var_doc(self, max_width=78, export='plain'):
132
124
rest_dict = rest_dict )
133
125
134
126
def _service_doc (self , max_width = 78 , export = 'plain' ):
135
- if len (self .services ) == 0 :
127
+ """
128
+ Export formatted model service documentation as a string.
129
+
130
+ Parameters
131
+ ----------
132
+ max_width : int, optional = 80
133
+ Maximum table width. If export format is ``rest`` it will be unlimited.
134
+ export : str, optional = 'plain'
135
+ Export format, 'plain' for plain text, 'rest' for restructuredText.
136
+
137
+ Returns
138
+ -------
139
+ str
140
+ Tabulated output in a string
141
+ """
142
+ if len (self .parent .services ) == 0 :
136
143
return ''
137
144
138
145
names , symbols = list (), list ()
139
146
info = list ()
140
147
class_names = list ()
141
148
142
- for p in self .services .values ():
149
+ for p in self .parent . services .values ():
143
150
names .append (p .name )
144
151
class_names .append (p .class_name )
145
152
info .append (p .info if p .info else '' )
@@ -165,6 +172,74 @@ def _service_doc(self, max_width=78, export='plain'):
165
172
plain_dict = plain_dict ,
166
173
rest_dict = rest_dict )
167
174
175
+ def _param_doc (self , max_width = 78 , export = 'plain' ):
176
+ """
177
+ Export formatted model parameter documentation as a string.
178
+
179
+ Parameters
180
+ ----------
181
+ max_width : int, optional = 80
182
+ Maximum table width. If export format is ``rest`` it will be unlimited.
183
+
184
+ export : str, optional = 'plain'
185
+ Export format, 'plain' for plain text, 'rest' for restructuredText.
186
+
187
+ Returns
188
+ -------
189
+ str
190
+ Tabulated output in a string
191
+ """
192
+ if len (self .parent .params ) == 0 :
193
+ return ''
194
+
195
+ # prepare temporary lists
196
+ names , units , class_names = list (), list (), list ()
197
+ info , defaults , properties = list (), list (), list ()
198
+ units_rest = list ()
199
+
200
+ for p in self .parent .params .values ():
201
+ names .append (p .name )
202
+ class_names .append (p .class_name )
203
+ info .append (p .info if p .info else '' )
204
+ defaults .append (p .default if p .default is not None else '' )
205
+ units .append (f'{ p .unit } ' if p .unit else '' )
206
+ units_rest .append (f'*{ p .unit } *' if p .unit else '' )
207
+
208
+ plist = []
209
+ for key , val in p .property .items ():
210
+ if val is True :
211
+ plist .append (key )
212
+ properties .append (',' .join (plist ))
213
+
214
+ # symbols based on output format
215
+ if export == 'rest' :
216
+ symbols = [item .tex_name for item in self .parent .params .values ()]
217
+ symbols = math_wrap (symbols , export = export )
218
+ title = 'Parameters\n ----------'
219
+ else :
220
+ symbols = [item .name for item in self .parent .params .values ()]
221
+ title = 'Parameters'
222
+
223
+ plain_dict = OrderedDict ([('Name' , names ),
224
+ ('Description' , info ),
225
+ ('Default' , defaults ),
226
+ ('Unit' , units ),
227
+ ('Properties' , properties )])
228
+
229
+ rest_dict = OrderedDict ([('Name' , names ),
230
+ ('Symbol' , symbols ),
231
+ ('Description' , info ),
232
+ ('Default' , defaults ),
233
+ ('Unit' , units_rest ),
234
+ ('Properties' , properties )])
235
+
236
+ # convert to rows and export as table
237
+ return make_doc_table (title = title ,
238
+ max_width = max_width ,
239
+ export = export ,
240
+ plain_dict = plain_dict ,
241
+ rest_dict = rest_dict )
242
+
168
243
169
244
class RDocumenter :
170
245
"""
@@ -178,14 +253,6 @@ class RDocumenter:
178
253
179
254
def __init__ (self , parent ):
180
255
self .parent = parent
181
- self .system = parent .system
182
- self .class_name = parent .class_name
183
- self .config = parent .config
184
- self .services = parent .services
185
- self .rparams = parent .rparams
186
- self .vars = parent .vars
187
- self .constrs = parent .constrs
188
- self .obj = parent .obj
189
256
190
257
def get (self , max_width = 78 , export = 'plain' ):
191
258
"""
@@ -207,14 +274,15 @@ def get(self, max_width=78, export='plain'):
207
274
if export == 'rest' :
208
275
max_width = 0
209
276
model_header = '-' * 80 + '\n '
210
- out += f'.. _{ self .class_name } :\n \n '
277
+ out += f'.. _{ self .parent . class_name } :\n \n '
211
278
else :
212
279
model_header = ''
213
280
214
281
if export == 'rest' :
215
- out += model_header + f'{ self .class_name } \n ' + model_header
282
+ out += model_header + f'{ self .parent . class_name } \n ' + model_header
216
283
else :
217
- out += model_header + f'Routine <{ self .class_name } > in Type <{ self .parent .type } >\n ' + model_header
284
+ out += model_header + \
285
+ f'Routine <{ self .parent .class_name } > in Type <{ self .parent .type } >\n ' + model_header
218
286
219
287
if self .parent .__doc__ is not None :
220
288
out += inspect .cleandoc (self .parent .__doc__ )
@@ -229,20 +297,20 @@ def get(self, max_width=78, export='plain'):
229
297
out += self ._exprc_doc (max_width = max_width , export = export )
230
298
out += self ._service_doc (max_width = max_width , export = export )
231
299
out += self ._param_doc (max_width = max_width , export = export )
232
- out += self .config .doc (max_width = max_width , export = export )
300
+ out += self .parent . config .doc (max_width = max_width , export = export )
233
301
234
302
return out
235
303
236
304
def _service_doc (self , max_width = 78 , export = 'plain' ):
237
305
# routine service documentation
238
- if len (self .services ) == 0 :
306
+ if len (self .parent . services ) == 0 :
239
307
return ''
240
308
241
309
names , symbols = list (), list ()
242
310
info = list ()
243
311
class_names = list ()
244
312
245
- for p in self .services .values ():
313
+ for p in self .parent . services .values ():
246
314
names .append (p .name )
247
315
info .append (p .info if p .info else '' )
248
316
class_names .append (p .class_name )
@@ -251,7 +319,7 @@ def _service_doc(self, max_width=78, export='plain'):
251
319
252
320
# replace with latex math expressions if export is ``rest``
253
321
if export == 'rest' :
254
- symbols = [item .tex_name for item in self .services .values ()]
322
+ symbols = [item .tex_name for item in self .parent . services .values ()]
255
323
symbols = math_wrap (symbols , export = export )
256
324
title = 'Services\n ---------'
257
325
@@ -272,21 +340,21 @@ def _service_doc(self, max_width=78, export='plain'):
272
340
273
341
def _constr_doc (self , max_width = 78 , export = 'plain' ):
274
342
# constraint documentation
275
- if len (self .constrs ) == 0 :
343
+ if len (self .parent . constrs ) == 0 :
276
344
return ''
277
345
278
346
# prepare temporary lists
279
347
names , class_names , info = list (), list (), list ()
280
348
281
- for p in self .constrs .values ():
349
+ for p in self .parent . constrs .values ():
282
350
names .append (p .name )
283
351
class_names .append (p .class_name )
284
352
info .append (p .info if p .info else '' )
285
353
286
354
# expressions based on output format
287
355
expressions = []
288
356
if export == 'rest' :
289
- for p in self .constrs .values ():
357
+ for p in self .parent . constrs .values ():
290
358
expr = _tex_pre (self , p , self .parent .syms .tex_map )
291
359
if p .is_eq :
292
360
expr = f'{ expr } = 0'
@@ -460,7 +528,7 @@ def _var_doc(self, max_width=78, export='plain'):
460
528
# NOTE: this is for the optimization variables
461
529
# not the _var_doc for ANDES parameters
462
530
# variable documentation
463
- if len (self .vars ) == 0 :
531
+ if len (self .parent . vars ) == 0 :
464
532
return ''
465
533
466
534
# prepare temporary lists
@@ -469,7 +537,7 @@ def _var_doc(self, max_width=78, export='plain'):
469
537
sources = list ()
470
538
units_rest = list ()
471
539
472
- for p in self .vars .values ():
540
+ for p in self .parent . vars .values ():
473
541
names .append (p .name )
474
542
class_names .append (p .class_name )
475
543
info .append (p .info if p .info else '' )
@@ -493,11 +561,11 @@ def _var_doc(self, max_width=78, export='plain'):
493
561
494
562
# symbols based on output format
495
563
if export == 'rest' :
496
- symbols = [item .tex_name for item in self .vars .values ()]
564
+ symbols = [item .tex_name for item in self .parent . vars .values ()]
497
565
symbols = math_wrap (symbols , export = export )
498
566
title = 'Vars\n ----------------------------------'
499
567
else :
500
- symbols = [item .name for item in self .vars .values ()]
568
+ symbols = [item .name for item in self .parent . vars .values ()]
501
569
title = 'Vars'
502
570
503
571
plain_dict = OrderedDict ([('Name' , names ),
@@ -536,7 +604,7 @@ def _param_doc(self, max_width=78, export='plain'):
536
604
str
537
605
Tabulated output in a string
538
606
"""
539
- if len (self .rparams ) == 0 :
607
+ if len (self .parent . rparams ) == 0 :
540
608
return ''
541
609
542
610
# prepare temporary lists
@@ -545,7 +613,7 @@ def _param_doc(self, max_width=78, export='plain'):
545
613
sources = list ()
546
614
units_rest = list ()
547
615
548
- for p in self .rparams .values ():
616
+ for p in self .parent . rparams .values ():
549
617
names .append (p .name )
550
618
class_names .append (p .class_name )
551
619
info .append (p .info if p .info else '' )
@@ -562,11 +630,11 @@ def _param_doc(self, max_width=78, export='plain'):
562
630
563
631
# symbols based on output format
564
632
if export == 'rest' :
565
- symbols = [item .tex_name for item in self .rparams .values ()]
633
+ symbols = [item .tex_name for item in self .parent . rparams .values ()]
566
634
symbols = math_wrap (symbols , export = export )
567
635
title = 'Parameters\n ----------------------------------'
568
636
else :
569
- symbols = [item .name for item in self .rparams .values ()]
637
+ symbols = [item .name for item in self .parent . rparams .values ()]
570
638
title = 'Parameters'
571
639
572
640
plain_dict = OrderedDict ([('Name' , names ),
0 commit comments