11class JsonTypeHandler :
2+ """Abstract class to obtain documentation type from JSON schema type.
3+
4+ Parameters
5+ ----------
6+ is_my_type : Callable[Any] -> bool
7+ return True if this instance should handle the given input type
8+ handler : Callable[Any] -> str
9+ convert the input type to a string suitable for the RST docs
10+ """
211 def __init__ (self , is_my_type , handler ):
312 self ._is_my_type = is_my_type
413 self .handler = handler
514
615 def is_my_type (self , json_type ):
16+ """Determine whether this instance should handle this type.
17+
18+ Parameters
19+ ----------
20+ json_type : Any
21+ input type from JSON schema
22+
23+ Returns
24+ -------
25+ bool :
26+ whether to handle this type with this instance
27+ """
728 return self ._is_my_type (json_type )
829
930 def __call__ (self , json_type ):
@@ -21,7 +42,7 @@ def __call__(self, json_type):
2142def _is_listof (json_type ):
2243 try :
2344 return json_type ["type" ] == "array"
24- except :
45+ except : # any exception should return false (mostly Key/Type Error)
2546 return False
2647
2748
@@ -33,6 +54,18 @@ def _is_listof(json_type):
3354
3455
3556class RefTypeHandler (JsonTypeHandler ):
57+ """Handle JSON types of the form {"$ref": "#/definitions/..."}
58+
59+ Parameters
60+ ----------
61+ type_name : str
62+ the name to use in the RST type
63+ def_string : str
64+ the string following "#/definitions/" in the JSON type definition
65+ link_to : str or None
66+ if not None, the RST type will be linked with a ``:ref:`` pointing
67+ to the anchor given by ``link_to``
68+ """
3669 def __init__ (self , type_name , def_string , link_to ):
3770 self .type_name = type_name
3871 self .def_string = def_string
@@ -51,6 +84,17 @@ def _refhandler(self, json_type):
5184
5285
5386class CategoryHandler (RefTypeHandler ):
87+ """Handle JSON types for OPS category definitions.
88+
89+ OPS category definitions show up with JSON references pointing to
90+ "#/definitions/{CATEGORY}_type". This provides a convenience class over
91+ the :class:RefTypeHandler to treat OPS categories.
92+
93+ Parameters
94+ ----------
95+ category : str
96+ name of the category
97+ """
5498 def __init__ (self , category ):
5599 self .category = category
56100 def_string = f"{ category } _type"
@@ -61,6 +105,22 @@ def __init__(self, category):
61105
62106
63107class EvalHandler (RefTypeHandler ):
108+ """Handle JSON types for OPS custom evaluation definitions.
109+
110+ Some parameters for the OPS compiler use the OPS custom evaluation
111+ mechanism, which evaluates certain Python-like string input. These are
112+ treated as special definition types in the JSON schema, and this object
113+ provides a convenience class over :class:`.RefTypeHandler` to treat
114+ custom evaluation types.
115+
116+ Parameters
117+ ----------
118+ type_name : str
119+ name of the custom evaluation type
120+ link_to : str or None
121+ if not None, the RST type will be linked with a ``:ref:`` pointing
122+ to the anchor given by ``link_to``
123+ """
64124 def __init__ (self , type_name , link_to = None ):
65125 super ().__init__ (
66126 type_name = type_name , def_string = type_name , link_to = link_to
@@ -79,6 +139,21 @@ def __init__(self, type_name, link_to=None):
79139
80140
81141def json_type_to_string (json_type ):
142+ """Convert JSON schema type to string for RST docs.
143+
144+ This is the primary public-facing method for dealing with JSON schema
145+ types in RST document generation.
146+
147+ Parameters
148+ ----------
149+ json_type : Any
150+ the type from the JSON schema
151+
152+ Returns
153+ -------
154+ str :
155+ the type string description to be used in the RST document
156+ """
82157 for handler in JSON_TYPE_HANDLERS :
83158 handled = handler (json_type )
84159 if handled != json_type :
0 commit comments