22d5187cb76e

Big API changes.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 04 Oct 2009 20:19:32 -0400
parents 0441f8a9096a
children b65adf15ec72
branches/tags (none)
files review/api.py review/messages.py review/templates.py

Changes

--- a/review/api.py	Sun Oct 04 20:01:58 2009 -0400
+++ b/review/api.py	Sun Oct 04 20:19:32 2009 -0400
@@ -4,21 +4,13 @@
 '''
 
 import os
+import messages, templates
 from datetime import datetime
 from mercurial import cmdutil, hg
 from mercurial.node import hex
 from mercurial.util import sha1
 
 
-COMMENT_FILE_TEMPLATE = '''\
-author:%s
-datetime:%s
-node:%s
-filename:%s
-lines:%s
-
-%s'''
-
 class PreexistingDatastore(Exception):
     """Raised when trying to initialize a datastore when one seems to exist."""
     def __init__(self, committed):
@@ -48,7 +40,7 @@
 def _match(start):
     return lambda fn: fn.startswith(start)
 
-def _parse_comment_data(data):
+def _parse_data(data):
     meta, _, message = data.partition('\n\n')
     
     data = {}
@@ -58,13 +50,9 @@
     data['message'] = message
     return data
 
-def _parse_signoffdata(data):
-    return None
-
 
 class ReviewDatastore(object):
     '''The data store for all the reviews so far.'''
-    
     def __init__(self, ui, repo, lpath=None, rpath=None, create=False):
         self.ui = ui
         
@@ -97,10 +85,8 @@
         return ReviewChangeset(self.ui, self.repo, node)
     
 
-
 class ReviewChangeset(object):
     '''The review data about one changeset in the target repository.'''
-    
     def __init__(self, ui, repo, node):
         self.repo = repo
         self.ui = ui
@@ -112,11 +98,13 @@
             signofffns = filter(_match('%s/signoffs' % node), relevant)
             
             self.comments = [ 
-                ReviewComment(**_parse_comment_data(self.repo['tip'][fn].data()))
+                ReviewComment(**_parse_data(self.repo['tip'][fn].data()))
                               for fn in commentfns
             ]
-            self.signoffs = [ _parse_signoff_data(self.repo['tip'][fn].data())
-                              for fn in signofffns ]
+            self.signoffs = [ 
+                ReviewSignoff(**_parse_data(self.repo['tip'][fn].data()))
+                              for fn in signofffns
+            ]
         else:
             self.comments = []
             self.signoffs = []
@@ -142,10 +130,37 @@
         comment.commit(self.ui, self.repo)
     
 
-class ReviewComment(object):
+class _ReviewObject(object):
+    '''Some kind of object.'''
+    def __init__(self, container, commit_message):
+        self.container = container
+        self.commit_message = commit_message
+    
+    def commit(self, ui, repo):
+        '''Write and commit this object to the given repo.'''
+        
+        path = os.path.join(repo.root, self.node, self.container)
+        if not os.path.exists(path):
+            os.mkdir(path)
+        
+        data = self.render_data()
+        filename = sha1(data).hexdigest()
+        objectpath = os.path.join(path, filename)
+        
+        with open(objectpath, 'w') as objectfile:
+            objectfile.write(data)
+        
+        cmdutil.commit(ui, repo, _commitfunc,
+            [objectpath],
+            { 'message': self.commit_message % self.node, 'addremove': True, })
+    
+
+class ReviewComment(_ReviewObject):
     '''A single review comment.'''
-    
     def __init__(self, author, datetime, node, filename, lines, message, **extra):
+        super(ReviewComment, self).__init__(
+            container='comments', commit_message=messages.COMMIT_COMMENT,
+        )
         self.author = author
         self.datetime = datetime
         self.node = node
@@ -156,26 +171,24 @@
     def render_data(self):
         datetime = str(self.datetime)
         lines = ','.join(self.lines)
-        return COMMENT_FILE_TEMPLATE % ( self.author, datetime,
+        return templates.COMMENT_FILE_TEMPLATE % ( self.author, datetime,
             self.node, self.filename, lines, self.message )
     
-    def commit(self, ui, repo):
-        '''Write and commit this comment to the given repo.'''
-        
-        path = os.path.join(repo.root, self.node, 'comments')
-        if not os.path.exists(path):
-            os.mkdir(path)
-        
-        data = self.render_data()
-        filename = sha1(data).hexdigest()
-        commentpath = os.path.join(path, filename)
-        
-        with open(commentpath, 'w') as commentfile:
-            commentfile.write(data)
-        
-        cmdutil.commit(ui, repo, _commitfunc,
-            [commentpath],
-            { 'message': 'Add a comment on changeset %s' % self.node,
-              'addremove': True, })
+
+class ReviewSignoff(_ReviewObject):
+    '''A single review signoff.'''
+    def __init__(self, author, datetime, node, opinion, message, **extra):
+        super(ReviewComment, self).__init__(
+            container='signoffs', commit_message=messages.COMMIT_SIGNOFF,
+        )
+        self.author = author
+        self.datetime = datetime
+        self.node = node
+        self.opinion = opinion
+        self.message = message
     
-
+    def render_data(self):
+        datetime = str(self.datetime)
+        return templates.SIGNOFF_FILE_TEMPLATE % ( self.author, datetime,
+            self.node, self.opinion, self.message )
+    
--- a/review/messages.py	Sun Oct 04 20:01:58 2009 -0400
+++ b/review/messages.py	Sun Oct 04 20:19:32 2009 -0400
@@ -43,3 +43,7 @@
 comments:  %d comments from %d authors
 
 '''
+
+
+COMMIT_COMMENT = '''Add a comment on changeset %s'''
+COMMIT_SIGNOFF = '''Sign off on changeset %s'''
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/review/templates.py	Sun Oct 04 20:19:32 2009 -0400
@@ -0,0 +1,16 @@
+COMMENT_FILE_TEMPLATE = '''\
+author:%s
+datetime:%s
+node:%s
+filename:%s
+lines:%s
+
+%s'''
+
+SIGNOFF_FILE_TEMPLATE = '''\
+author:%s
+datetime:%s
+node:%s
+opinion:%s
+
+%s'''
\ No newline at end of file