[lxc-devel] [pylxd/master] Deprecation warning

rockstar on Github lxc-bot at linuxcontainers.org
Mon May 30 03:39:57 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 729 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160530/cd7fca82/attachment.bin>
-------------- next part --------------
From 6da04d99260d5e9d58f67d7db4e3acc298a9bfe5 Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul at eventuallyanyway.com>
Date: Sun, 29 May 2016 21:27:30 -0600
Subject: [PATCH 1/3] Add a deprecated decorator

---
 pylxd/container.py   |  5 ++++-
 pylxd/deprecation.py | 24 ++++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 pylxd/deprecation.py

diff --git a/pylxd/container.py b/pylxd/container.py
index f330401..6ce64c1 100644
--- a/pylxd/container.py
+++ b/pylxd/container.py
@@ -15,6 +15,7 @@
 import six
 
 from pylxd import exceptions, mixin
+from pylxd.deprecation import deprecated
 from pylxd.operation import Operation
 
 
@@ -90,7 +91,9 @@ def fetch(self):
             setattr(self, key, value)
     # XXX: rockstar (28 Mar 2016) - This method was named improperly
     # originally. It's being kept here for backwards compatibility.
-    reload = fetch
+    reload = deprecated(
+        "Container.reload is deprecated. Please use Container.fetch")(
+        fetch)
 
     def update(self, wait=False):
         """Update the container in lxd from local changes."""
diff --git a/pylxd/deprecation.py b/pylxd/deprecation.py
new file mode 100644
index 0000000..6fdebb9
--- /dev/null
+++ b/pylxd/deprecation.py
@@ -0,0 +1,24 @@
+import warnings
+
+warnings.simplefilter('once', DeprecationWarning)
+
+
+class deprecated():
+    """A decorator for warning about deprecation warnings.
+
+    The decorator takes an optional message argument. This message can
+    be used to direct the user to a new API or specify when it will
+    be removed.
+    """
+
+    def __init__(self, message=None):
+        self.message = message
+
+    def __call__(self, f):
+        def wrapped(*args, **kwargs):
+            if self.message is None:
+                self.message = '{} is deprecated and will be removed soon.'.format(
+                    f.__name__)
+            warnings.warn(self.message, DeprecationWarning)
+            return f(*args, **kwargs)
+        return wrapped

From cff0599bbc891ec690f818cb85bffce3e78212df Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul at eventuallyanyway.com>
Date: Sun, 29 May 2016 21:30:50 -0600
Subject: [PATCH 2/3] Fix pep8

---
 pylxd/deprecation.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/pylxd/deprecation.py b/pylxd/deprecation.py
index 6fdebb9..3b1f22a 100644
--- a/pylxd/deprecation.py
+++ b/pylxd/deprecation.py
@@ -11,13 +11,15 @@ class deprecated():
     be removed.
     """
 
+    DEFAULT_MESSAGE = '{} is deprecated and will be removed soon.'
+
     def __init__(self, message=None):
         self.message = message
 
     def __call__(self, f):
         def wrapped(*args, **kwargs):
             if self.message is None:
-                self.message = '{} is deprecated and will be removed soon.'.format(
+                self.message = self.DEFAULT_MESSAGE.format(
                     f.__name__)
             warnings.warn(self.message, DeprecationWarning)
             return f(*args, **kwargs)

From 22fc058c727b24480ed0c1f237fb50ac2521b230 Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul at eventuallyanyway.com>
Date: Sun, 29 May 2016 21:34:57 -0600
Subject: [PATCH 3/3] Change all internal instances of `Container.reload` to
 `Container.fetch`

---
 pylxd/container.py            |  4 ++--
 pylxd/tests/test_container.py | 10 +++++-----
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/pylxd/container.py b/pylxd/container.py
index 6ce64c1..b394d04 100644
--- a/pylxd/container.py
+++ b/pylxd/container.py
@@ -54,7 +54,7 @@ def all(cls, client):
 
         Containers returned from this method will only have the name
         set, as that is the only property returned from LXD. If more
-        information is needed, `Container.reload` is the method call
+        information is needed, `Container.fetch` is the method call
         that should be used.
         """
         response = client.api.containers.get()
@@ -137,7 +137,7 @@ def _set_state(self, state, timeout=30, force=True, wait=False):
             })
         if wait:
             self.wait_for_operation(response.json()['operation'])
-            self.reload()
+            self.fetch()
 
     def state(self):
         response = self._client.api.containers[self.name].state.get()
diff --git a/pylxd/tests/test_container.py b/pylxd/tests/test_container.py
index b6620ba..41ea120 100644
--- a/pylxd/tests/test_container.py
+++ b/pylxd/tests/test_container.py
@@ -69,16 +69,16 @@ def create_fail(request, context):
             exceptions.CreateFailed,
             container.Container.create, self.client, config)
 
-    def test_reload(self):
-        """A reload updates the properties of a container."""
+    def test_fetch(self):
+        """A fetch updates the properties of a container."""
         an_container = container.Container(
             name='an-container', _client=self.client)
 
-        an_container.reload()
+        an_container.fetch()
 
         self.assertTrue(an_container.ephemeral)
 
-    def test_reload_not_found(self):
+    def test_fetch_not_found(self):
         """NameError is raised on a 404 for updating container."""
         def not_found(request, context):
             context.status_code = 404
@@ -95,7 +95,7 @@ def not_found(request, context):
         an_container = container.Container(
             name='an-missing-container', _client=self.client)
 
-        self.assertRaises(NameError, an_container.reload)
+        self.assertRaises(NameError, an_container.fetch)
 
     def test_update(self):
         """A container is updated."""


More information about the lxc-devel mailing list