From d2c807e4c2bb353343485eb14a143e19f828c653 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Mon, 5 Nov 2007 00:42:50 +0000
Subject: [PATCH] Create OS from an InvalidOS error

Add a new FromInvalidOS static function to objects.OS that makes it easy to
create an object representing a broken OS starting from the relevant exception.

Reviewed-By: iustinp
---
 lib/objects.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/lib/objects.py b/lib/objects.py
index 2f27a270e..c5e7f4a07 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -597,6 +597,20 @@ class OS(ConfigObject):
     "rename_script",
     ]
 
+  @classmethod
+  def FromInvalidOS(cls, err):
+    """Create an OS from an InvalidOS error.
+
+    This routine knows how to convert an InvalidOS error to an OS
+    object representing the broken OS with a meaningful error message.
+
+    """
+    if not isinstance(err, errors.InvalidOS):
+      raise errors.ProgrammerError("Trying to initialize an OS from an"
+                                   " invalid object of type %s" % type(err))
+
+    return cls(name=err.args[0], path=err.args[1], status=err.args[2])
+
   def __nonzero__(self):
     return self.status == constants.OS_VALID_STATUS
 
-- 
GitLab