|
| 1 | +class 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 | + """ |
| 11 | + def __init__(self, is_my_type, handler): |
| 12 | + self._is_my_type = is_my_type |
| 13 | + self.handler = handler |
| 14 | + |
| 15 | + 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 | + """ |
| 28 | + return self._is_my_type(json_type) |
| 29 | + |
| 30 | + def __call__(self, json_type): |
| 31 | + if self.is_my_type(json_type): |
| 32 | + return self.handler(json_type) |
| 33 | + return json_type |
| 34 | + |
| 35 | + |
| 36 | +handle_object = JsonTypeHandler( |
| 37 | + is_my_type=lambda json_type: json_type == "object", |
| 38 | + handler=lambda json_type: "dict", |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def _is_listof(json_type): |
| 43 | + try: |
| 44 | + return json_type["type"] == "array" |
| 45 | + except: # any exception should return false (mostly Key/Type Error) |
| 46 | + return False |
| 47 | + |
| 48 | + |
| 49 | +handle_listof = JsonTypeHandler( |
| 50 | + is_my_type=_is_listof, |
| 51 | + handler=lambda json_type: "list of " |
| 52 | + + json_type_to_string(json_type["items"]), |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +class 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 | + """ |
| 69 | + def __init__(self, type_name, def_string, link_to): |
| 70 | + self.type_name = type_name |
| 71 | + self.def_string = def_string |
| 72 | + self.link_to = link_to |
| 73 | + self.json_check = {"$ref": "#/definitions/" + def_string} |
| 74 | + super().__init__(is_my_type=self._reftype, handler=self._refhandler) |
| 75 | + |
| 76 | + def _reftype(self, json_type): |
| 77 | + return json_type == self.json_check |
| 78 | + |
| 79 | + def _refhandler(self, json_type): |
| 80 | + rst = f"{self.type_name}" |
| 81 | + if self.link_to: |
| 82 | + rst = f":ref:`{rst} <{self.link_to}>`" |
| 83 | + return rst |
| 84 | + |
| 85 | + |
| 86 | +class 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 | + """ |
| 98 | + def __init__(self, category): |
| 99 | + self.category = category |
| 100 | + def_string = f"{category}_type" |
| 101 | + link_to = f"compiling--{category}" |
| 102 | + super().__init__( |
| 103 | + type_name=category, def_string=def_string, link_to=link_to |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +class 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 | + """ |
| 124 | + def __init__(self, type_name, link_to=None): |
| 125 | + super().__init__( |
| 126 | + type_name=type_name, def_string=type_name, link_to=link_to |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +JSON_TYPE_HANDLERS = [ |
| 131 | + handle_object, |
| 132 | + handle_listof, |
| 133 | + CategoryHandler("engine"), |
| 134 | + CategoryHandler("cv"), |
| 135 | + CategoryHandler("volume"), |
| 136 | + EvalHandler("EvalInt"), |
| 137 | + EvalHandler("EvalFloat"), |
| 138 | +] |
| 139 | + |
| 140 | + |
| 141 | +def 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 | + """ |
| 157 | + for handler in JSON_TYPE_HANDLERS: |
| 158 | + handled = handler(json_type) |
| 159 | + if handled != json_type: |
| 160 | + return handled |
| 161 | + return json_type |
0 commit comments