1- import re
1+ # -*- coding: utf-8 -*-
2+
3+ import hashlib
4+ import json
25import locale
6+ import re
37
48import trac .wiki .formatter
59from trac .mimeview .api import Context
610from time import strftime , localtime
711from code_comments import db
812from trac .util import Markup
913from trac .web .href import Href
10- from trac .test import EnvironmentStub , Mock , MockPerm
14+ from trac .test import Mock , MockPerm
1115
12- try :
13- import json
14- except ImportError :
15- import simplejson as json
1616
17- try :
18- import hashlib
19- md5_hexdigest = lambda s : hashlib .md5 (s ).hexdigest ()
20- except ImportError :
21- import md5
22- md5_hexdigest = lambda s : md5 .new (s ).hexdigest ()
17+ def md5_hexdigest (s ):
18+ return hashlib .md5 (s ).hexdigest ()
2319
2420
2521VERSION = 1
2622
23+
2724class Comment :
2825 columns = [column .name for column in db .schema ['code_comments' ].columns ]
2926
@@ -40,7 +37,7 @@ def __init__(self, req, env, data):
4037 self .req = req
4138 if self ._empty ('version' ):
4239 self .version = VERSION
43- if self ._empty ( 'path' ):
40+ if self ._empty ('path' ):
4441 self .path = ''
4542 self .html = format_to_html (self .req , self .env , self .text )
4643 email = self .email_map ().get (self .author , 'baba@baba.net' )
@@ -64,17 +61,25 @@ def email_map(self):
6461 return Comment ._email_map
6562
6663 def validate (self ):
67- missing = [column_name for column_name in self .required if self ._empty (column_name )]
64+ missing = [
65+ column_name
66+ for column_name in self .required if self ._empty (column_name )
67+ ]
6868 if missing :
69- raise ValueError ("Comment column(s) missing: %s" % ', ' .join (missing ))
69+ raise ValueError ("Comment column(s) missing: %s"
70+ % ', ' .join (missing ))
7071
7172 def href (self ):
7273 if self .is_comment_to_file :
73- href = self .req .href .browser (self .path , rev = self .revision , codecomment = self .id )
74+ href = self .req .href .browser (self .path , rev = self .revision ,
75+ codecomment = self .id )
7476 elif self .is_comment_to_changeset :
7577 href = self .req .href .changeset (self .revision , codecomment = self .id )
7678 elif self .is_comment_to_attachment :
77- href = self .req .href ('/attachment/ticket/%d/%s' % (self .attachment_ticket , self .attachment_filename ), codecomment = self .id )
79+ href = self .req .href ('/attachment/ticket/%d/%s'
80+ % (self .attachment_ticket ,
81+ self .attachment_filename ),
82+ codecomment = self .id )
7883 if self .line and not self .is_comment_to_changeset :
7984 href += '#L' + str (self .line )
8085 return href
@@ -96,7 +101,8 @@ def link_text(self):
96101
97102 def changeset_link_text (self ):
98103 if 0 != self .line :
99- return 'Changeset @%d#L%d (in %s)' % ( self .revision , self .line , self .path )
104+ return 'Changeset @%d#L%d (in %s)' % (self .revision , self .line ,
105+ self .path )
100106 else :
101107 return 'Changeset @%s' % self .revision
102108
@@ -109,8 +115,8 @@ def trac_link(self):
109115 return 'source:' + self .link_text ()
110116
111117 def attachment_info (self ):
112- info = { 'ticket' : None , 'filename' : None }
113- if not self .path .startswith ( 'attachment' ):
118+ info = {'ticket' : None , 'filename' : None }
119+ if not self .path .startswith ('attachment' ):
114120 return info
115121 match = re .match (r'attachment:/ticket/(\d+)/(.*)' , self .path )
116122 if not match :
@@ -124,17 +130,20 @@ def path_link_tag(self):
124130
125131 def formatted_date (self ):
126132 encoding = locale .getlocale ()[1 ] if locale .getlocale ()[1 ] else 'utf-8'
127- return strftime ('%d %b %Y, %H:%M' , localtime (self .time )).decode (encoding )
133+ return strftime ('%d %b %Y, %H:%M' ,
134+ localtime (self .time )).decode (encoding )
128135
129136 def get_ticket_relations (self ):
130- relations = set ()
131- db = self .env .get_db_cnx ()
132- cursor = db .cursor ()
133- query = """SELECT ticket FROM ticket_custom WHERE name = 'code_comment_relation' AND
134- (value LIKE '%(comment_id)d' OR
135- value LIKE '%(comment_id)d,%%' OR
136- value LIKE '%%,%(comment_id)d' OR value LIKE '%%,%(comment_id)d,%%')""" % {'comment_id' : self .id }
137+ query = """
138+ SELECT ticket FROM ticket_custom
139+ WHERE name = 'code_comment_relation' AND
140+ (VALUE LIKE '%(comment_id)d' OR
141+ VALUE LIKE '%(comment_id)d,%%' OR
142+ VALUE LIKE '%%,%(comment_id)d' OR
143+ VALUE LIKE '%%,%(comment_id)d,%%')
144+ """ % {'comment_id' : self .id }
137145 result = {}
146+
138147 @self .env .with_transaction ()
139148 def get_ticket_ids (db ):
140149 cursor = db .cursor ()
@@ -153,17 +162,24 @@ def delete_comment(db):
153162 cursor = db .cursor ()
154163 cursor .execute ("DELETE FROM code_comments WHERE id=%s" , [self .id ])
155164
165+
156166class CommentJSONEncoder (json .JSONEncoder ):
157167 def default (self , o ):
158168 if isinstance (o , Comment ):
159- for_json = dict ([(name , getattr (o , name )) for name in o .__dict__ if isinstance (getattr (o , name ), (basestring , int , list , dict ))])
169+ for_json = dict ([
170+ (name , getattr (o , name ))
171+ for name in o .__dict__
172+ if isinstance (getattr (o , name ), (basestring , int , list , dict ))
173+ ])
160174 for_json ['formatted_date' ] = o .formatted_date ()
161175 for_json ['permalink' ] = o .href ()
162176 return for_json
163177 else :
164178 return json .JSONEncoder .default (self , o )
165179
180+
166181def format_to_html (req , env , text ):
167- req = Mock (href = Href ('/' ), abs_href = Href ('http://www.example.com/' ), authname = 'anonymous' , perm = MockPerm (), args = {})
182+ req = Mock (href = Href ('/' ), abs_href = Href ('http://www.example.com/' ),
183+ authname = 'anonymous' , perm = MockPerm (), args = {})
168184 context = Context .from_request (req )
169185 return trac .wiki .formatter .format_to_html (env , context , text )
0 commit comments