| | 91 | |
|---|
| | 92 | def is_tracback(self, comment): |
|---|
| | 93 | return comment.startswith(self.TRACBACK_MAGIC_NUMBER) |
|---|
| | 94 | |
|---|
| | 95 | def create_tracbacks(self, ticket, ticket_to_tracback, comment): |
|---|
| | 96 | tracback = self.TRACBACK_MAGIC_NUMBER + self.TRACBACK_PREFACE + str(ticket.id) + ":" |
|---|
| | 97 | |
|---|
| | 98 | # find all occurrences of ticket_to_tracback. This is error prone. |
|---|
| | 99 | # we'll weed the errors out later. |
|---|
| | 100 | string_representation = "#" + str(ticket_to_tracback.id) |
|---|
| | 101 | |
|---|
| | 102 | excerpts = [] |
|---|
| | 103 | |
|---|
| | 104 | index = -1 |
|---|
| | 105 | while comment.find(string_representation, index + 1) > -1: |
|---|
| | 106 | # Get two characters in context so we can make sure this is really |
|---|
| | 107 | # a reference to a ticket, and not anything else. |
|---|
| | 108 | index = comment.find(string_representation, index + 1) |
|---|
| | 109 | |
|---|
| | 110 | print str(index) + " ::: " |
|---|
| | 111 | |
|---|
| | 112 | if not self.is_weed(comment, index, index + len(string_representation)): |
|---|
| | 113 | start = index - self.EXCERPT_CHARACTERS |
|---|
| | 114 | end = index + len(string_representation) + self.EXCERPT_CHARACTERS |
|---|
| | 115 | |
|---|
| | 116 | # Make sure we don't go into the negative. |
|---|
| | 117 | if start < 0: |
|---|
| | 118 | start = 0 |
|---|
| | 119 | |
|---|
| | 120 | excerpt = comment[start:end] |
|---|
| | 121 | excerpt = excerpt.replace("\n", "") |
|---|
| | 122 | |
|---|
| | 123 | # There's probably a better way to say this in python, but Tim doesn't know |
|---|
| | 124 | # how to do it. (He's tried """ but something's foobar'ed.) |
|---|
| | 125 | excerpts.append("\n> ''...%s...''\n" % excerpt) |
|---|
| | 126 | |
|---|
| | 127 | tracback += ''.join(excerpts) |
|---|
| | 128 | return tracback |
|---|
| | 129 | |
|---|
| | 130 | def is_weed(self, comment, start, end): |
|---|
| | 131 | start -= self.WEED_BUFFER |
|---|
| | 132 | end += self.WEED_BUFFER |
|---|
| | 133 | |
|---|
| | 134 | # Make sure we don't have a negative starting value. |
|---|
| | 135 | if start < 0: |
|---|
| | 136 | start = 0 |
|---|
| | 137 | |
|---|
| | 138 | try: |
|---|
| | 139 | match = re.search(self.TICKET_REGEX, comment[start:end]) |
|---|
| | 140 | return False |
|---|
| | 141 | except: # Not a match. This must be a weed. |
|---|
| | 142 | return True |
|---|
| | 143 | |
|---|
| | 144 | |
|---|
| | 145 | |
|---|
| | 146 | |
|---|
| | 147 | # Doug, with some very very cool regular expression prowess, produced |
|---|
| | 148 | # the following regular expression that returns sentences with ticket |
|---|
| | 149 | # links in them. We could use this -- and almost should -- but I'm |
|---|
| | 150 | # going to use the easy method for now as it takes less expertise. |
|---|
| | 151 | # |
|---|
| | 152 | # sentence_pattern = r""" |
|---|
| | 153 | # (?: # This initial group isn't a matching group |
|---|
| | 154 | # (?<=\.) # End of previous sentence is a period |
|---|
| | 155 | # |(?<=\.\s) # or period with one space |
|---|
| | 156 | # |(?<=\.\s\s) # or period with two space |
|---|
| | 157 | # |(?<=\.\s\s\s) # or period with three spaces |
|---|
| | 158 | # |(?<=\.\s\s\s\s) # or period with four spaces |
|---|
| | 159 | # |^ # Or we match the beginning of the line |
|---|
| | 160 | # ) |
|---|
| | 161 | # ( # We match everything else and return it |
|---|
| | 162 | # # Because of this, we don't return any other |
|---|
| | 163 | # # matches |
|---|
| | 164 | # [^\s] # A sentence does not begin with a space |
|---|
| | 165 | # (?: # Match the beginning of the sentence |
|---|
| | 166 | # [^.] # A sentence does not contain a period |
|---|
| | 167 | # |\.[^\s] # unless it's part of a word, like a URL |
|---|
| | 168 | # )* # Match any length |
|---|
| | 169 | # |
|---|
| | 170 | # (?= # Starting here is a duplicate of the ticketlink |
|---|
| | 171 | # # above, but without returning any text |
|---|
| | 172 | # (?<=^\#) # Look for a TracLink Ticket at the beginning of the string |
|---|
| | 173 | # |(?<=[\s,.;:!]\#) # or on a whitespace boundary or some punctuation |
|---|
| | 174 | # ) |
|---|
| | 175 | # (?=\d+) # Any length ticket number (return the digits) |
|---|
| | 176 | # (?= # Don't return the end of the ticke tink |
|---|
| | 177 | # (?=\b) # Whether it's a word boundary |
|---|
| | 178 | # |$ # Or an end of string |
|---|
| | 179 | # ) |
|---|
| | 180 | # (?: # Here we match the end of the sentence |
|---|
| | 181 | # # It follows the same rules as the beginning |
|---|
| | 182 | # [^.] # Don't match a period |
|---|
| | 183 | # |\.[^\s] # unless it's inside a word |
|---|
| | 184 | # )* # Any length to the end of the sentence |
|---|
| | 185 | # (?: # Here we will match the end of the sentence |
|---|
| | 186 | # (?:\. # Which is a period (returned as part of the |
|---|
| | 187 | # # above expression |
|---|
| | 188 | # (?=\s+|$) # then followed by unmatched whitespace or the |
|---|
| | 189 | # # end of the line |
|---|
| | 190 | # ) |
|---|
| | 191 | # |$ # if there's no period, jus tthe end of the line |
|---|
| | 192 | # # We'll accept that too |
|---|
| | 193 | # ) |
|---|
| | 194 | # ) |
|---|
| | 195 | # """ |
|---|
| | 196 | # excerpt = re.compile(sentence_pattern, re.VERBOSE | re.MULTILINE) |
|---|