Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a base EtcdModel to all dynamic created model #79

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions etcd3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,3 +34,13 @@
'Lock',
'EventType'
])

try:
from .models import EtcdModel
except ImportError: # pragma: no cover
class EtcdModel(object):
pass

__all__.extend([
'EtcdModel'
])
22 changes: 13 additions & 9 deletions etcd3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import enum


class AlarmRequestAlarmAction(enum.Enum):
class EtcdModel(object):
pass


class AlarmRequestAlarmAction(EtcdModel, enum.Enum):
"""
ref: #/definitions/AlarmRequestAlarmAction

Expand All @@ -14,7 +18,7 @@ class AlarmRequestAlarmAction(enum.Enum):
DEACTIVATE = 'DEACTIVATE'


class CompareCompareResult(enum.Enum):
class CompareCompareResult(EtcdModel, enum.Enum):
"""
ref: #/definitions/CompareCompareResult

Expand All @@ -26,7 +30,7 @@ class CompareCompareResult(enum.Enum):
NOT_EQUAL = 'NOT_EQUAL'


class CompareCompareTarget(enum.Enum):
class CompareCompareTarget(EtcdModel, enum.Enum):
"""
ref: #/definitions/CompareCompareTarget

Expand All @@ -38,7 +42,7 @@ class CompareCompareTarget(enum.Enum):
VALUE = 'VALUE'


class EventEventType(enum.Enum):
class EventEventType(EtcdModel, enum.Enum):
"""
ref: #/definitions/EventEventType

Expand All @@ -48,7 +52,7 @@ class EventEventType(enum.Enum):
DELETE = 'DELETE'


class RangeRequestSortOrder(enum.Enum):
class RangeRequestSortOrder(EtcdModel, enum.Enum):
"""
ref: #/definitions/RangeRequestSortOrder

Expand All @@ -59,7 +63,7 @@ class RangeRequestSortOrder(enum.Enum):
DESCEND = 'DESCEND'


class RangeRequestSortTarget(enum.Enum):
class RangeRequestSortTarget(EtcdModel, enum.Enum):
"""
ref: #/definitions/RangeRequestSortTarget

Expand All @@ -72,7 +76,7 @@ class RangeRequestSortTarget(enum.Enum):
VALUE = 'VALUE'


class WatchCreateRequestFilterType(enum.Enum):
class WatchCreateRequestFilterType(EtcdModel, enum.Enum):
"""
ref: #/definitions/WatchCreateRequestFilterType

Expand All @@ -82,7 +86,7 @@ class WatchCreateRequestFilterType(enum.Enum):
NODELETE = 'NODELETE'


class authpbPermissionType(enum.Enum):
class authpbPermissionType(EtcdModel, enum.Enum):
"""
ref: #/definitions/authpbPermissionType

Expand All @@ -93,7 +97,7 @@ class authpbPermissionType(enum.Enum):
READWRITE = 'READWRITE'


class etcdserverpbAlarmType(enum.Enum):
class etcdserverpbAlarmType(EtcdModel, enum.Enum):
"""
ref: #/definitions/etcdserverpbAlarmType

Expand Down
4 changes: 3 additions & 1 deletion etcd3/stateful/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions etcd3/swagger_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions scripts/extract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand All @@ -25,6 +28,8 @@ class {{e._path | last}}(enum.Enum):

'''

DEFAULT_MODEL_VERSION = '3.3.0'

if __name__ == '__main__':
import os
import sys
Expand All @@ -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)
Expand Down