# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Add a comment to a bug"""
-from libbe import cmdutil, bugdir, editor
+from libbe import cmdutil, bugdir, settings_object, editor
import os
__desc__ = __doc__
>>> execute(["a", "This is a comment about a"], test=True)
>>> bd._clear_bugs()
>>> bug = bd.bug_from_shortname("a")
- >>> bug.load_comments()
+ >>> bug.load_comments(load_full=False)
>>> comment = bug.comment_root[0]
>>> print comment.body
This is a comment about a
True
>>> comment.time <= int(time.time())
True
- >>> comment.in_reply_to is None
+ >>> comment.in_reply_to is settings_object.EMPTY
True
>>> if 'EDITOR' in os.environ:
>>> execute(["b"], test=True)
>>> bd._clear_bugs()
>>> bug = bd.bug_from_shortname("b")
- >>> bug.load_comments()
+ >>> bug.load_comments(load_full=False)
>>> comment = bug.comment_root[0]
>>> print comment.body
I like cheese
bd = bugdir.BugDir(from_disk=True, manipulate_encodings=not test)
bug = bd.bug_from_shortname(bugname)
- bug.load_comments()
+ bug.load_comments(load_full=False)
if is_reply:
parent = bug.comment_root.comment_from_shortname(shortname,
bug_shortname=bugname)
for bug in bugs:
shortname = bd.bug_shortname(bug)
ids.append(shortname)
- bug.load_comments()
+ bug.load_comments(load_full=False)
for id,comment in bug.comment_shortnames(shortname):
ids.append(id)
except bugdir.NoBugDir:
from properties import Property, doc_property, local_property, \
defaulting_property, checked_property, cached_property, \
primed_property, change_hook_property, settings_property
+import settings_object
import mapfile
from tree import Tree
import utility
assert comment.uuid != None
uuid_map[comment.uuid] = comment
for comm in comments:
- if comm.in_reply_to == None:
+ rep = comm.in_reply_to
+ if rep == None or rep == settings_object.EMPTY or rep == bug.uuid:
root_comments.append(comm)
else:
parentUUID = comm.in_reply_to
comment.save()
-# Define an invalid value for our properties, distinct from None,
-# which shows that a property has been initialized but has no value.
-EMPTY = -1
-
-class Comment(Tree):
+class Comment(Tree, settings_object.SavedSettingsObject):
"""
>>> c = Comment()
>>> c.uuid != None
>>> print c.content_type
text/plain
"""
-
- def _save_settings(self, old, new):
- if self.sync_with_disk==True:
- self.save_settings()
- def _load_settings(self):
- if self.sync_with_disk==True and self._settings_loaded==False:
- self.load_settings()
- else:
- for property in self.settings_properties:
- if property not in self.settings:
- self.settings[property] = EMPTY
settings_properties = []
- required_saved_properties = ['Content-type'] # to protect against future changes in default values
- def _setting_name_to_attr_name(self, name):
- "Helper for looking up default vals for required-saved-properties"
- return name.lower().replace('-', '_')
-
- def _versioned_property(name, doc, default=None, save=_save_settings, load=_load_settings, setprops=settings_properties, allowed=None):
- "Combine the common decorators in a single function"
- setprops.append(name)
- def decorator(funcs):
- if allowed != None:
- checked = checked_property(allowed=allowed)
- defaulting = defaulting_property(default=default, null=EMPTY)
- change_hook = change_hook_property(hook=save)
- primed = primed_property(primer=load)
- settings = settings_property(name=name)
- docp = doc_property(doc=doc)
- deco = defaulting(change_hook(primed(settings(docp(funcs)))))
- if allowed != None:
- deco = checked(deco)
- return Property(deco)
- return decorator
+ required_saved_properties = []
+ _prop_save_settings = settings_object.prop_save_settings
+ _prop_load_settings = settings_object.prop_load_settings
+ def _versioned_property(settings_properties=settings_properties,
+ required_saved_properties=required_saved_properties,
+ **kwargs):
+ if "settings_properties" not in kwargs:
+ kwargs["settings_properties"] = settings_properties
+ if "required_saved_properties" not in kwargs:
+ kwargs["required_saved_properties"]=required_saved_properties
+ return settings_object.versioned_property(**kwargs)
@_versioned_property(name="From",
doc="The author of the comment")
@_versioned_property(name="Content-type",
doc="Mime type for comment body",
- default="text/plain")
+ default="text/plain",
+ require_save=True)
def content_type(): return {}
@_versioned_property(name="Date",
in_reply_to should be the uuid string of the parent comment.
"""
Tree.__init__(self)
+ settings_object.SavedSettingsObject.__init__(self)
self.bug = bug
self.uuid = uuid
- self._settings_loaded = False
- self.settings = {}
if from_disk == True:
self.sync_with_disk = True
else:
continue
yield comment
+ def _setting_attr_string(self, setting):
+ value = getattr(self, setting)
+ if value == settings_object.EMPTY:
+ return ""
+ else:
+ return str(value)
+
def string(self, indent=0, shortname=None):
"""
>>> comm = Comment(bug=None, body="Some\\ninsightful\\nremarks\\n")
lines = []
lines.append("--------- Comment ---------")
lines.append("Name: %s" % shortname)
- lines.append("From: %s" % (self.From or ""))
+ lines.append("From: %s" % (self._setting_attr_string("From")))
lines.append("Date: %s" % self.time_string)
lines.append("")
#lines.append(textwrap.fill(self.body or "",
def load_settings(self):
self.settings = mapfile.map_load(self.rcs, self.get_path("values"))
- for property in self.settings_properties:
- if property not in self.settings:
- self.settings[property] = EMPTY
- elif self.settings[property] == None:
- self.settings[property] = EMPTY
- self._settings_loaded = True
+ self._setup_saved_settings()
def save_settings(self):
- map = {}
- for k,v in self.settings.items():
- if v != None and v != EMPTY:
- map[k] = v
- for k in self.required_saved_properties:
- map[k] = getattr(self, self._setting_name_to_attr_name(k))
-
self.rcs.mkdir(self.get_path())
path = self.get_path("values")
- mapfile.map_save(self.rcs, path, map)
+ mapfile.map_save(self.rcs, path, self._get_saved_settings())
def save(self):
assert self.body != None, "Can't save blank comment"