in utils/dsl-generator.py [0:0]
def add_attribute(self, k, arg, for_types_py=False, for_response=False):
"""Add an attribute to the internal representation of a class.
This method adds the argument `arg` to the data structure for a class
stored in `k`. In particular, the argument is added to the `k["args"]`
list, making sure required arguments are first in the list. If the
argument is of a type that needs Python DSL specific typing details to
be stored in the DslBase._param_defs attribute, then this is added to
`k["params"]`.
When `for_types_py` is `True`, type hints are formatted in the most
convenient way for the types.py file. When possible, double quotes are
removed from types, and for types that are in the same file the quotes
are kept to prevent forward references, but the "types." namespace is
removed. When `for_types_py` is `False`, all non-native types use
quotes and are namespaced.
When `for_response` is `True`, type hints are not given the optional
dictionary representation, nor the `DefaultType` used for omitted
attributes.
"""
try:
type_, param = self.get_python_type(arg["type"], for_response=for_response)
except RuntimeError:
type_ = "Any"
param = None
if not for_response:
if type_ != "Any":
if (
'Sequence["types.' in type_
or 'Sequence["wrappers.AggregationRange' in type_
):
type_ = add_seq_dict_type(type_) # interfaces can be given as dicts
elif "types." in type_ or "wrappers.AggregationRange" in type_:
type_ = add_dict_type(type_) # interfaces can be given as dicts
type_ = add_not_set(type_)
if for_types_py:
type_ = type_for_types_py(type_)
required = "(required) " if arg["required"] else ""
server_default = (
f" Defaults to `{arg['serverDefault']}` if omitted."
if arg.get("serverDefault")
else ""
)
doc = wrapped_doc(
f":arg {arg['name']}: {required}{arg.get('description', '')}{server_default}",
subsequent_indent=" ",
)
arg = {
"name": PROP_REPLACEMENTS.get(arg["name"], arg["name"]),
"type": type_,
"doc": doc,
"required": arg["required"],
}
if param is not None:
param = {"name": arg["name"], "param": param}
if arg["required"]:
# insert in the right place so that all required arguments
# appear at the top of the argument list
i = 0
for i in range(len(k["args"]) + 1):
if i == len(k["args"]):
break
if k["args"][i].get("positional"):
continue
if k["args"][i]["required"] is False:
break
k["args"].insert(i, arg)
else:
k["args"].append(arg)
if param and "params" in k:
k["params"].append(param)