1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Internal module providing log-message handling.
19
20 This module implements some of public interface for the
21 pybaz_ package. But for convenience reasons the author prefers
22 to store this code in a file separate from ``__init__.py``.
23
24 .. _pybaz: pybaz-module.html
25
26 This module is strictly internal and should never be used.
27 """
28
29 import email
30 import email.Parser
31 import email.Generator
32
33 __all__ = [
34 'LogMessage',
35 ]
36
38
39 """Log message for use with commit, import or tag operations.
40
41 This is the write-enabled counterpart of Patchlog. When creating a new
42 revision with import, commit or tag, a log message file can be used to
43 specify a long description and custom headers.
44
45 Commit and import can use the default log file of the source tree, with a
46 special name. You can create the LogMessage object associated to the
47 default log file with the WorkingTree.log_message method.
48
49 For integration with external tools, it is useful to be able to parse an
50 existing log file and write the parsed object back idempotently. We are
51 lucky since this idempotence is provided by the standard email.Parser and
52 email.Generator.
53 """
54
56 self.__name = name
57 self.__email = None
58 self.__dirty = False
59
61 name = property(get_name)
62
64 """Read the log message from disk."""
65 self.__email = email.Parser.Parser().parse(file(self.__name))
66 self.__dirty = False
67
69 """Write the log message to disk."""
70 if self.__dirty:
71 f = file(self.__name, 'w')
72 email.Generator.Generator(f).flatten(self.__email)
73 self.__dirty = False
74
76 """Clear the in-memory log message.
77
78 When creating a new log message file, this method must be used
79 first before setting the message parts. That should help early
80 detection of erroneous log file names.
81 """
82 self.__email = email.Message.Message()
83 self.__dirty = True
84
86 """Text of a patchlog header by name."""
87 if self.__email is None:
88 self.load()
89 return self.__email[header]
90
92 """Set a patchlog header."""
93 if self.__email is None:
94 self.load()
95 try:
96 self.__email.replace_header(header, text)
97 except KeyError:
98 self.__email.add_header(header, text)
99 self.__dirty = True
100
102 """Body of the log message."""
103 if self.__email is None:
104 self.load()
105 assert not self.__email.is_multipart()
106 return self.__email.get_payload()
107
109 """Set the body of the log message."""
110 if self.__email is None:
111 self.load()
112 self.__email.set_payload(s)
113 self.__dirty = True
114
115 description = property(get_description, set_description)
116