From e520934ade4d7bc0eafd7befc667e7293e7acec6 Mon Sep 17 00:00:00 2001 From: cuiweizhi <560397@gree.com.cn> Date: Sat, 4 Nov 2023 14:48:57 +0800 Subject: [PATCH] Update project --- .../libs/flask_elasticsearch/elasticsearch.py | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/application/libs/flask_elasticsearch/elasticsearch.py b/application/libs/flask_elasticsearch/elasticsearch.py index 9888a32..56874fb 100644 --- a/application/libs/flask_elasticsearch/elasticsearch.py +++ b/application/libs/flask_elasticsearch/elasticsearch.py @@ -21,11 +21,20 @@ from application.common import ConfigHelper class FlaskElasticsearch: def __init__(self, app=None): + """ + Initialize a FlaskElasticsearch instance. + + :param app: Flask application instance for Elasticsearch initialization. + """ if app is not None: self.init_app(app) def init_app(self, app: Flask) -> None: - """Initialize the app""" + """ + Initialize the app with Elasticsearch connection. + + :param app: Flask application instance for Elasticsearch initialization. + """ if hasattr(app, 'teardown_appcontext'): app.teardown_appcontext(self.teardown) else: @@ -33,7 +42,11 @@ class FlaskElasticsearch: @staticmethod def teardown(exception): - """Clears the Elasticsearch connection after each request.""" + """ + Clear the Elasticsearch connection after each request. + + :param exception: Exception instance, if any. + """ ctx = current_app._get_current_object() if hasattr(ctx, 'elasticsearch'): ctx.elasticsearch = None @@ -41,7 +54,12 @@ class FlaskElasticsearch: raise RuntimeError(exception) def __getattr__(self, item: Any) -> Any: - """Lazy initialization of Elasticsearch connection on first use.""" + """ + Lazy initialization of Elasticsearch connection on first use. + + :param item: Attribute to get from the Elasticsearch connection. + :return: Value of the requested attribute. + """ ctx = current_app._get_current_object() if ctx is not None: if not hasattr(ctx, 'elasticsearch'): @@ -61,9 +79,6 @@ class FlaskElasticsearch: if current_app: config_helper = ConfigHelper(current_app) cfg = config_helper.Elasticsearch - if cfg is None: - raise KeyError('Key Elasticsearch is not defined') - host = cfg.Host or 'localhost' port = int(cfg.Port) or 9200 user = cfg.User or None -- GitLab