-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredmine.py
executable file
·79 lines (71 loc) · 2.02 KB
/
redmine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
#from email import *
#
# Class for Redmine-specific Email parsing
#
class RedmineMail:
#
# Instantiate with reference to existing Email instance
#
# @param text: Email as array of lines without \n line ending
#
def __init__(self, lines):
self.lines = lines
#
# Find the line, which separates the Redmine change summary from the attached Redmine ticket body
#
# @return: Line index of separator; counting starts with 0
#
def getSeparatorLineIndex(self):
# check all lines
for i in range(len(self.lines)):
# line begins with separator string
if self.lines[i].find('--------') == 0:
# found
return i
# not found
return -1
#
# Extract the first line of the Redmine ticket body
#
def getTicketBodyHeader(self):
# ticket body immediately starts after separator line
i = self.getSeparatorLineIndex()
if i == -1 or len(self.lines) < i:
return ""
return self.lines[i+1]
#
# Parse, to which Tracker the mail content is subject to
#
# @return: Tracker as string
#
def getTracker(self):
# the string before the '#'
s = self.getTicketBodyHeader()
if s.find('#') < 0:
return ""
return s.split('#')[0].strip()
#
# Parse, to which Ticket number the mail content is subject to
#
# @return: Ticket number as string
#
def getTicketNumber(self):
# the number after the '#'
s = self.getTicketBodyHeader()
if s.find('#') < 0 or s.find(':') < 0:
return ""
return s.split('#')[1].split(':')[0]
#
# Unit-Test
#
# Run by executing this file: ./redmine.py
#
if __name__ == '__main__':
lines = """Ziemlich aktualisiert
----------------------------------------
Poison Dart Frog #11293:
""".split('\n')
mail = RedmineMail(lines)
print "Tracker: "+mail.getTracker()
print "Ticket: #"+mail.getTicketNumber()