Added Bug.comments(), BugDir.has_bug() & cleaned up diff.diff().
authorW. Trevor King <wking@drexel.edu>
Mon, 24 Nov 2008 12:47:06 +0000 (07:47 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 24 Nov 2008 12:47:06 +0000 (07:47 -0500)
+ some other minor fixes and cleanups.

libbe/bug.py
libbe/bugdir.py
libbe/diff.py
libbe/utility.py

index 09a2c1d0935cc027653ca3925959ad805a0dfa36..c75c968c29993677244a5b13bc26d69149d7794d 100644 (file)
@@ -206,6 +206,12 @@ class Bug(object):
         self.comment_root = comment.loadComments(self)
         self._comments_loaded = True
 
+    def comments(self):
+        if self._comments_loaded == False:
+            self.load_comments()
+        for comment in self.comment_root.traverse():
+            yield comment
+
     def _add_attr(self, map, name):
         value = getattr(self, name)
         if value is not None:
index d3b7e617a8ffcc73cad88ff0c0eb634b91f73cc3..7e4cf3ec0a82a75fcff29bfd806ecca4069a3c97 100644 (file)
@@ -105,7 +105,7 @@ class BugDir (list):
         if root == None:
             root = os.getcwd()
         if sink_to_existing_root == True:
-            self.root = self.find_root(root)
+            self.root = self._find_root(root)
         else:
             if not os.path.exists(root):
                 raise NoRootEntry(root)
@@ -117,11 +117,11 @@ class BugDir (list):
                 if os.path.exists(self.get_path()):
                     raise AlreadyInitialized, self.get_path()
             if rcs == None:
-                rcs = self.guess_rcs(allow_rcs_init)
+                rcs = self._guess_rcs(allow_rcs_init)
             self.rcs = rcs
             user_id = self.rcs.get_user_id()
 
-    def find_root(self, path):
+    def _find_root(self, path):
         """
         Search for an existing bug database dir and it's ancestors and
         return a BugDir rooted there.
@@ -214,7 +214,7 @@ that the Arch RCS backend *enforces* ids with this format.""")
         assert args[0] in ["version", "settings", "bugs"], str(args)
         return os.path.join(my_dir, *args)
 
-    def guess_rcs(self, allow_rcs_init=False):
+    def _guess_rcs(self, allow_rcs_init=False):
         deepdir = self.get_path()
         if not os.path.exists(deepdir):
             deepdir = os.path.dirname(deepdir)
@@ -319,7 +319,7 @@ that the Arch RCS backend *enforces* ids with this format.""")
         for uuid in self.list_uuids():
             if uuid not in map:
                 map[uuid] = None
-        self.bug_map = map
+        self._bug_map = map
 
     def list_uuids(self):
         uuids = []
@@ -366,7 +366,7 @@ that the Arch RCS backend *enforces* ids with this format.""")
         used for long term reference.
         """
         chars = 3
-        for uuid in self.bug_map.keys():
+        for uuid in self._bug_map.keys():
             if bug.uuid == uuid:
                 continue
             while (bug.uuid[:chars] == uuid[:chars]):
@@ -384,7 +384,7 @@ that the Arch RCS backend *enforces* ids with this format.""")
         """
         matches = []
         self._bug_map_gen()
-        for uuid in self.bug_map.keys():
+        for uuid in self._bug_map.keys():
             if uuid.startswith(shortname):
                 matches.append(uuid)
         if len(matches) > 1:
@@ -394,15 +394,20 @@ that the Arch RCS backend *enforces* ids with this format.""")
         raise KeyError("No bug matches %s" % shortname)
 
     def bug_from_uuid(self, uuid):
-        if uuid not in self.bug_map:
-            self._bug_map_gen()
-            if uuid not in self.bug_map:
-                raise KeyError("No bug matches %s\n  bug map: %s\n  root: %s" \
-                                   % (uuid, self.bug_map, self.root))
-        if self.bug_map[uuid] == None:
+        if not self.has_bug(uuid):
+            raise KeyError("No bug matches %s\n  bug map: %s\n  root: %s" \
+                               % (uuid, self._bug_map, self.root))
+        if self._bug_map[uuid] == None:
             self._load_bug(uuid)
-        return self.bug_map[uuid]
+        return self._bug_map[uuid]
 
+    def has_bug(self, bug_uuid):
+        if bug_uuid not in self._bug_map:
+            self._bug_map_gen()
+            if bug_uuid not in self._bug_map:
+                return False
+        return True
+        
 
 def simple_bug_dir():
     """
index 84b3bfe53d61901192e146bdc8a0147afec06cda..86a91ca4330fbdf9af421ac38b4cee437ac05b03 100644 (file)
@@ -33,7 +33,7 @@ def diff(old_bugdir, new_bugdir):
         except KeyError:
             removed.append(old_bug)
     for uuid in new_bugdir.list_uuids():
-        if not old_bugdir.bug_map.has_key(uuid):
+        if not old_bugdir.has_bug(uuid):
             new_bug = new_bugdir.bug_from_uuid(uuid)
             added.append(new_bug)
     return (removed, modified, added)
@@ -87,18 +87,17 @@ def bug_changes(old, new, bugs):
     change_list = change_lines(old, new, ("time", "creator", "severity",
     "target", "summary", "status", "assigned"))
 
-    old.load_comments()
-    old_comment_ids = [c.uuid for c in old.comment_root.traverse()]
-    new.load_comments()
-    new_comment_ids = [c.uuid for c in new.comment_root.traverse()]
+    old_comment_ids = [c.uuid for c in old.comments()]
+    new_comment_ids = [c.uuid for c in new.comments()]
     change_strings = ["%s: %s -> %s" % f for f in change_list]
     for comment_id in new_comment_ids:
         if comment_id not in old_comment_ids:
-            summary = comment_summary(new.comment_root.comment_from_uuid(comment_id), "new")
+            summary = comment_summary(new.comment_from_uuid(comment_id), "new")
             change_strings.append(summary)
     for comment_id in old_comment_ids:
         if comment_id not in new_comment_ids:
-            summary = comment_summary(new.comment.root.comment_from_uuid(comment_id), "removed")
+            summary = comment_summary(new.comment_from_uuid(comment_id),
+                                      "removed")
             change_strings.append(summary)
 
     if len(change_strings) == 0:
index 1168580e8c4a29d53098a5363277a60b4a82dd4e..2c77fcf66e5804c228c9b574d61ac0d635867e97 100644 (file)
@@ -104,10 +104,10 @@ def editor_string(comment=None):
     CantFindEditor: Can't find editor to get string from
     >>> os.environ["EDITOR"] = "echo bar > "
     >>> editor_string()
-    'bar\\n'
+    u'bar\\n'
     >>> os.environ["VISUAL"] = "echo baz > "
     >>> editor_string()
-    'baz\\n'
+    u'baz\\n'
     >>> del os.environ["EDITOR"]
     >>> del os.environ["VISUAL"]
     """