diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index f33c2944..a799e4f9 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -279,7 +279,6 @@ class Elasticsearch(object): return body # AUTO-GENERATED-API-DEFINITIONS # - @query_params() def ping(self, params=None): """ diff --git a/utils/generate_api.py b/utils/generate_api.py index 9ff4003d..31ee5c76 100644 --- a/utils/generate_api.py +++ b/utils/generate_api.py @@ -9,7 +9,7 @@ import black from click.testing import CliRunner from pathlib import Path -from jinja2 import Environment, DictLoader, ChoiceLoader, TemplateNotFound +from jinja2 import Environment, FileSystemLoader, TemplateNotFound # line to look for in the original source file @@ -40,135 +40,8 @@ XPACK_PATH = ( / "api" ) - -BASE_TEMPLATES = { - "url": """{% if api.url_parts.0 %}_make_path({{ api.url_parts.1|join(", ")}}){% else %}{{ api.url_parts.1|tojson }}{% endif %}""", - "required": """{% if api.required_parts.1 %} - for param in ({{ api.required_parts|join(", ")}}): - if param in SKIP_IN_PATH: - raise ValueError("Empty value passed for a required argument.") - - {% elif api.required_parts %} - if {{ api.required_parts.0 }} in SKIP_IN_PATH: - raise ValueError("Empty value passed for a required argument '{{ api.required_parts.0 }}'.") - - {% endif %}""", - "substitutions": """{% for p, info in api.params %} - {% if p in substitutions and p not in api.url_parts.1 %} - # {{ substitutions[p] }} is a reserved word so it cannot be used, use {{ p }} instead - if "{{ p }}" in params: - params["{{ substitutions[p] }}"] = params.pop("{{ p }}") - - {% endif %} - {% endfor %}""", - "base": ''' - @query_params({{ api.query_params|map("tojson")|join(", ")}}) - def {{ api.name }}(self, {{ api.func_params|join(", ") }}): - """ - {% if api.description %} - {{ api.description|replace("\n", " ")|wordwrap(wrapstring="\n ") }} - {% endif %} - {% if api.doc_url %} - `<{{ api.doc_url }}>`_ - {% endif %} - {% if api.params %} - - {% for p, info in api.params %} - {% filter wordwrap(72, wrapstring="\n ") %} - :arg {{ p }}: {{ info.description }} {% if info.options %}Valid choices: {{ info.options|join(", ") }}{% endif %} {% if info.default %}Default: {{ info.default }}{% endif %} - {% endfilter %} - - {% endfor %} - {% endif %} - """ - {% include "substitutions" %} - {% include "required" %} - {% if api.body.body.serialize == "bulk" %} - body = self._bulk_body(body) - {% endif %} - {% block request %} - return self.transport.perform_request("{{ api.method }}", {% include "url" %}, params=params{% if api.body.body %}, body=body{% endif %}) - {% endblock %} - ''', -} - -OVERRIDE_TEMPLATES = { - "cluster.stats": """ - {% extends "base" %} - {% block request %} - return self.transport.perform_request("{{ api.method }}", "/_cluster/stats" if node_id in SKIP_IN_PATH else _make_path("_cluster", "stats", "nodes", node_id), params=params) - {% endblock%} - """, - "__init__.ping": """ - {% extends "base" %} - {% block request %} - try: - {{ super()|trim }} - except TransportError: - return False - {% endblock %} - """, - "__init__.index": """ - {% extends "base" %} - {% block request %} - if doc_type is None: - doc_type = "_doc" - - - return self.transport.perform_request("POST" if id in SKIP_IN_PATH else "PUT", {% include "url" %}, params=params, body=body) - {% endblock %} - """, - "__init__.scroll": """ - {% extends "base" %} - {% block request %} - if scroll_id in SKIP_IN_PATH and body in SKIP_IN_PATH: - raise ValueError("You need to supply scroll_id or body.") - elif scroll_id and not body: - body = {"scroll_id": scroll_id} - elif scroll_id: - params["scroll_id"] = scroll_id - - return self.transport.perform_request("{{ api.method }}", "/_search/scroll", params=params, body=body) - {% endblock %} - """, - "__init__.clear_scroll": """ - {% extends "base" %} - {% block request %} - if scroll_id in SKIP_IN_PATH and body in SKIP_IN_PATH: - raise ValueError("You need to supply scroll_id or body.") - elif scroll_id and not body: - body = {"scroll_id": [scroll_id]} - elif scroll_id: - params["scroll_id"] = scroll_id - - return self.transport.perform_request("{{ api.method }}", "/_search/scroll", params=params, body=body) - {% endblock %} - """, - "indices.put_mapping": """ - {% extends "base" %} - {% block request %} - if doc_type not in SKIP_IN_PATH and index in SKIP_IN_PATH: - index = "_all" - - {{ super()|trim }} - {% endblock %} - """ - -} - -for op in ("get", "delete", "exists", "update", "create", "explain", "get_source", "termvectors"): - OVERRIDE_TEMPLATES[f"__init__.{op}"] = """ - {% extends "base" %} - {% block request %} - if doc_type in SKIP_IN_PATH: - doc_type = "_doc" - - {{ super()|trim }} - {% endblock %} - """ - jinja_env = Environment( - loader=ChoiceLoader((DictLoader(OVERRIDE_TEMPLATES), DictLoader(BASE_TEMPLATES))), + loader=FileSystemLoader([CODE_ROOT / "utils" / "templates"]), trim_blocks=True, lstrip_blocks=True, ) @@ -206,7 +79,7 @@ class Module: header_lines = [] for line in content.split("\n"): header_lines.append(line) - if line.startswith('class'): + if line.startswith("class"): break self.header = "\n".join(header_lines) defined_apis = re.findall( @@ -299,7 +172,11 @@ class API: @property def query_params(self): - return (k for k in sorted(self._def.get("params", {}).keys()) if k not in self._all_parts()) + return ( + k + for k in sorted(self._def.get("params", {}).keys()) + if k not in self._all_parts() + ) @property def path(self): @@ -353,7 +230,7 @@ class API: def to_python(self): try: - t = jinja_env.get_template(f"{self.namespace}.{self.name}") + t = jinja_env.get_template(f"overrides/{self.namespace}/{self.name}") except TemplateNotFound: t = jinja_env.get_template("base") return t.render( diff --git a/utils/templates/base b/utils/templates/base new file mode 100644 index 00000000..7d5ba694 --- /dev/null +++ b/utils/templates/base @@ -0,0 +1,29 @@ + + @query_params({{ api.query_params|map("tojson")|join(", ")}}) + def {{ api.name }}(self, {{ api.func_params|join(", ") }}): + """ + {% if api.description %} + {{ api.description|replace("\n", " ")|wordwrap(wrapstring="\n ") }} + {% endif %} + {% if api.doc_url %} + `<{{ api.doc_url }}>`_ + {% endif %} + {% if api.params %} + + {% for p, info in api.params %} + {% filter wordwrap(72, wrapstring="\n ") %} + :arg {{ p }}: {{ info.description }} {% if info.options %}Valid choices: {{ info.options|join(", ") }}{% endif %} {% if info.default %}Default: {{ info.default }}{% endif %} + {% endfilter %} + + {% endfor %} + {% endif %} + """ + {% include "substitutions" %} + {% include "required" %} + {% if api.body.body.serialize == "bulk" %} + body = self._bulk_body(body) + {% endif %} + {% block request %} + return self.transport.perform_request("{{ api.method }}", {% include "url" %}, params=params{% if api.body.body %}, body=body{% endif %}) + {% endblock %} + diff --git a/utils/templates/overrides/__init__/clear_scroll b/utils/templates/overrides/__init__/clear_scroll new file mode 100644 index 00000000..dde2170e --- /dev/null +++ b/utils/templates/overrides/__init__/clear_scroll @@ -0,0 +1,12 @@ +{% extends "base" %} +{% block request %} + if scroll_id in SKIP_IN_PATH and body in SKIP_IN_PATH: + raise ValueError("You need to supply scroll_id or body.") + elif scroll_id and not body: + body = {"scroll_id": [scroll_id]} + elif scroll_id: + params["scroll_id"] = scroll_id + + return self.transport.perform_request("{{ api.method }}", "/_search/scroll", params=params, body=body) +{% endblock %} + diff --git a/utils/templates/overrides/__init__/create b/utils/templates/overrides/__init__/create new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/create @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/delete b/utils/templates/overrides/__init__/delete new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/delete @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/exists b/utils/templates/overrides/__init__/exists new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/exists @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/explain b/utils/templates/overrides/__init__/explain new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/explain @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/get b/utils/templates/overrides/__init__/get new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/get @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/get_source b/utils/templates/overrides/__init__/get_source new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/get_source @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/index b/utils/templates/overrides/__init__/index new file mode 100644 index 00000000..9aae46a7 --- /dev/null +++ b/utils/templates/overrides/__init__/index @@ -0,0 +1,9 @@ +{% extends "base" %} +{% block request %} + if doc_type is None: + doc_type = "_doc" + + + return self.transport.perform_request("POST" if id in SKIP_IN_PATH else "PUT", {% include "url" %}, params=params, body=body) +{% endblock %} + diff --git a/utils/templates/overrides/__init__/ping b/utils/templates/overrides/__init__/ping new file mode 100644 index 00000000..a509a592 --- /dev/null +++ b/utils/templates/overrides/__init__/ping @@ -0,0 +1,8 @@ +{% extends "base" %} +{% block request %} + try: + {{ super()|trim }} + except TransportError: + return False +{% endblock %} + diff --git a/utils/templates/overrides/__init__/scroll b/utils/templates/overrides/__init__/scroll new file mode 100644 index 00000000..6ea4fbff --- /dev/null +++ b/utils/templates/overrides/__init__/scroll @@ -0,0 +1,12 @@ +{% extends "base" %} +{% block request %} + if scroll_id in SKIP_IN_PATH and body in SKIP_IN_PATH: + raise ValueError("You need to supply scroll_id or body.") + elif scroll_id and not body: + body = {"scroll_id": scroll_id} + elif scroll_id: + params["scroll_id"] = scroll_id + + return self.transport.perform_request("{{ api.method }}", "/_search/scroll", params=params, body=body) +{% endblock %} + diff --git a/utils/templates/overrides/__init__/single_doc_op b/utils/templates/overrides/__init__/single_doc_op new file mode 100644 index 00000000..8189fec7 --- /dev/null +++ b/utils/templates/overrides/__init__/single_doc_op @@ -0,0 +1,8 @@ +{% extends "base" %} +{% block request %} + if doc_type in SKIP_IN_PATH: + doc_type = "_doc" + + {{ super()|trim }} +{% endblock %} + diff --git a/utils/templates/overrides/__init__/termvectors b/utils/templates/overrides/__init__/termvectors new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/termvectors @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/__init__/update b/utils/templates/overrides/__init__/update new file mode 120000 index 00000000..7df7b009 --- /dev/null +++ b/utils/templates/overrides/__init__/update @@ -0,0 +1 @@ +single_doc_op \ No newline at end of file diff --git a/utils/templates/overrides/cluster/stats b/utils/templates/overrides/cluster/stats new file mode 100644 index 00000000..c5bd1c64 --- /dev/null +++ b/utils/templates/overrides/cluster/stats @@ -0,0 +1,5 @@ +{% extends "base" %} +{% block request %} + return self.transport.perform_request("{{ api.method }}", "/_cluster/stats" if node_id in SKIP_IN_PATH else _make_path("_cluster", "stats", "nodes", node_id), params=params) +{% endblock%} + diff --git a/utils/templates/overrides/indices/put_mapping b/utils/templates/overrides/indices/put_mapping new file mode 100644 index 00000000..50a1b1b9 --- /dev/null +++ b/utils/templates/overrides/indices/put_mapping @@ -0,0 +1,8 @@ +{% extends "base" %} +{% block request %} + if doc_type not in SKIP_IN_PATH and index in SKIP_IN_PATH: + index = "_all" + + {{ super()|trim }} +{% endblock %} + diff --git a/utils/templates/required b/utils/templates/required new file mode 100644 index 00000000..c1fb503c --- /dev/null +++ b/utils/templates/required @@ -0,0 +1,11 @@ + {% if api.required_parts.1 %} + for param in ({{ api.required_parts|join(", ")}}): + if param in SKIP_IN_PATH: + raise ValueError("Empty value passed for a required argument.") + + {% elif api.required_parts %} + if {{ api.required_parts.0 }} in SKIP_IN_PATH: + raise ValueError("Empty value passed for a required argument '{{ api.required_parts.0 }}'.") + + {% endif %} + diff --git a/utils/templates/substitutions b/utils/templates/substitutions new file mode 100644 index 00000000..d0158075 --- /dev/null +++ b/utils/templates/substitutions @@ -0,0 +1,9 @@ +{% for p, info in api.params %} + {% if p in substitutions and p not in api.url_parts.1 %} + # {{ substitutions[p] }} is a reserved word so it cannot be used, use {{ p }} instead + if "{{ p }}" in params: + params["{{ substitutions[p] }}"] = params.pop("{{ p }}") + + {% endif %} +{% endfor %} + diff --git a/utils/templates/url b/utils/templates/url new file mode 100644 index 00000000..e89df490 --- /dev/null +++ b/utils/templates/url @@ -0,0 +1 @@ +{% if api.url_parts.0 %}_make_path({{ api.url_parts.1|join(", ")}}){% else %}{{ api.url_parts.1|tojson }}{% endif %}