From 29d418fe035602af137f15168fa01e440e0c838f Mon Sep 17 00:00:00 2001 From: Renjie Cai Date: Wed, 10 Apr 2019 16:28:57 +0800 Subject: [PATCH] add a base EtcdModel to all dynamic created model Signed-off-by: Renjie Cai --- etcd3/__init__.py | 11 +++++++++++ etcd3/models.py | 22 +++++++++++++--------- etcd3/stateful/lock.py | 4 +++- etcd3/swagger_helper.py | 8 ++++++-- scripts/extract_models.py | 11 ++++++++--- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/etcd3/__init__.py b/etcd3/__init__.py index bf4bb7c..dd1786d 100644 --- a/etcd3/__init__.py +++ b/etcd3/__init__.py @@ -9,6 +9,7 @@ from .baseclient import BaseClient from .client import Client + AioClient = None if six.PY3: # pragma: no cover from .aio_client import AioClient @@ -33,3 +34,13 @@ 'Lock', 'EventType' ]) + +try: + from .models import EtcdModel +except ImportError: # pragma: no cover + class EtcdModel(object): + pass + +__all__.extend([ + 'EtcdModel' +]) diff --git a/etcd3/models.py b/etcd3/models.py index c1d549c..f63bbca 100644 --- a/etcd3/models.py +++ b/etcd3/models.py @@ -3,7 +3,11 @@ import enum -class AlarmRequestAlarmAction(enum.Enum): +class EtcdModel(object): + pass + + +class AlarmRequestAlarmAction(EtcdModel, enum.Enum): """ ref: #/definitions/AlarmRequestAlarmAction @@ -14,7 +18,7 @@ class AlarmRequestAlarmAction(enum.Enum): DEACTIVATE = 'DEACTIVATE' -class CompareCompareResult(enum.Enum): +class CompareCompareResult(EtcdModel, enum.Enum): """ ref: #/definitions/CompareCompareResult @@ -26,7 +30,7 @@ class CompareCompareResult(enum.Enum): NOT_EQUAL = 'NOT_EQUAL' -class CompareCompareTarget(enum.Enum): +class CompareCompareTarget(EtcdModel, enum.Enum): """ ref: #/definitions/CompareCompareTarget @@ -38,7 +42,7 @@ class CompareCompareTarget(enum.Enum): VALUE = 'VALUE' -class EventEventType(enum.Enum): +class EventEventType(EtcdModel, enum.Enum): """ ref: #/definitions/EventEventType @@ -48,7 +52,7 @@ class EventEventType(enum.Enum): DELETE = 'DELETE' -class RangeRequestSortOrder(enum.Enum): +class RangeRequestSortOrder(EtcdModel, enum.Enum): """ ref: #/definitions/RangeRequestSortOrder @@ -59,7 +63,7 @@ class RangeRequestSortOrder(enum.Enum): DESCEND = 'DESCEND' -class RangeRequestSortTarget(enum.Enum): +class RangeRequestSortTarget(EtcdModel, enum.Enum): """ ref: #/definitions/RangeRequestSortTarget @@ -72,7 +76,7 @@ class RangeRequestSortTarget(enum.Enum): VALUE = 'VALUE' -class WatchCreateRequestFilterType(enum.Enum): +class WatchCreateRequestFilterType(EtcdModel, enum.Enum): """ ref: #/definitions/WatchCreateRequestFilterType @@ -82,7 +86,7 @@ class WatchCreateRequestFilterType(enum.Enum): NODELETE = 'NODELETE' -class authpbPermissionType(enum.Enum): +class authpbPermissionType(EtcdModel, enum.Enum): """ ref: #/definitions/authpbPermissionType @@ -93,7 +97,7 @@ class authpbPermissionType(enum.Enum): READWRITE = 'READWRITE' -class etcdserverpbAlarmType(enum.Enum): +class etcdserverpbAlarmType(EtcdModel, enum.Enum): """ ref: #/definitions/etcdserverpbAlarmType diff --git a/etcd3/stateful/lock.py b/etcd3/stateful/lock.py index 4846e9b..248b1e8 100644 --- a/etcd3/stateful/lock.py +++ b/etcd3/stateful/lock.py @@ -19,6 +19,7 @@ class EtcdLockAcquireTimeout(Exception): pass +# TODO: [critical] thread safety class Lock(object): # TODO: maybe we could improve the performance by reduce some HTTP requests """ Locking recipe for etcd, inspired by the kazoo recipe for zookeeper @@ -56,6 +57,7 @@ def __init__(self, client, lock_name, lock_ttl=DEFAULT_LOCK_TTL, reentrant=None, self.is_taken = False # if the lock is taken by someone self.lease = None self.__holders_lease = None + self._watcher = None log.debug("Initiating lock for %s with uuid %s", self.lock_key, self.uuid) def _get_uuid(self): @@ -242,7 +244,7 @@ def wait(self, locker=None, timeout=None): locker = locker or self._get_locker() if not locker: return - self.watcher = watcher = self.client.Watcher(key=locker.key, max_retries=0) + self._watcher = watcher = self.client.Watcher(key=locker.key, max_retries=0) return watcher.watch_once(lambda e: e.type == EventType.DELETE or e.value == self.uuid, timeout=timeout) def release(self): diff --git a/etcd3/swagger_helper.py b/etcd3/swagger_helper.py index f3af0a7..91af5c2 100644 --- a/etcd3/swagger_helper.py +++ b/etcd3/swagger_helper.py @@ -18,11 +18,15 @@ file_types = (io.IOBase,) try: - from .models import name_to_model + from .models import name_to_model, EtcdModel except ImportError: # pragma: no cover name_to_model = {} + class EtcdModel(object): + pass + + def swagger_escape(s): # pragma: no cover """ / and ~ are special characters in JSON Pointers, @@ -422,7 +426,7 @@ def init(this, data): rep = lambda self: '%s(%s)' % (name, ', '.join( ['%s=%s' % (k, repr(v)) for k, v in six.iteritems(self.__dict__) if k in self._data])) - return type(str(name), (), { + return type(str(name), (EtcdModel,), { '__init__': init, '__repr__': rep, '__iter__': ite, diff --git a/scripts/extract_models.py b/scripts/extract_models.py index 9263902..be1c108 100644 --- a/scripts/extract_models.py +++ b/scripts/extract_models.py @@ -3,8 +3,11 @@ # flake8: noqa import enum +class EtcdModel(object): + pass + {% for e in enums %} -class {{e._path | last}}(enum.Enum): +class {{e._path | last}}(EtcdModel, enum.Enum): """ ref: {{ e._ref }} @@ -25,6 +28,8 @@ class {{e._path | last}}(enum.Enum): ''' +DEFAULT_MODEL_VERSION = '3.3.0' + if __name__ == '__main__': import os import sys @@ -36,9 +41,9 @@ class {{e._path | last}}(enum.Enum): from isort import SortImports from etcd3.swagger_helper import SwaggerSpec + from etcd3.swaggerdefs import get_spec - rpc_swagger_json = os.path.join(os.path.dirname(__file__), '../etcd3/rpc.swagger.json') - swaggerSpec = SwaggerSpec(rpc_swagger_json) + swaggerSpec = SwaggerSpec(get_spec(DEFAULT_MODEL_VERSION)) enums = [i for i in swaggerSpec.definitions if i._is_enum] enum_tpl = jinja2.Template(ENUM_FILE_TPL)