Package pybaz :: Module _logmessage
[frames] | no frames]

Source Code for Module pybaz._logmessage

  1  # arch-tag: 7eae5a80-d62f-49b4-b61e-b20f460f5fe8 
  2  # Copyright (C) 2003--2005 David Allouche <david@allouche.net> 
  3  # 
  4  #    This program is free software; you can redistribute it and/or modify 
  5  #    it under the terms of the GNU General Public License as published by 
  6  #    the Free Software Foundation; either version 2 of the License, or 
  7  #    (at your option) any later version. 
  8  # 
  9  #    This program is distributed in the hope that it will be useful, 
 10  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  #    GNU General Public License for more details. 
 13  # 
 14  #    You should have received a copy of the GNU General Public License 
 15  #    along with this program; if not, write to the Free Software 
 16  #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 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   
37 -class LogMessage(object):
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
55 - def __init__(self, name):
56 self.__name = name 57 self.__email = None 58 self.__dirty = False
59
60 - def get_name(self): return self.__name
61 name = property(get_name) 62
63 - def load(self):
64 """Read the log message from disk.""" 65 self.__email = email.Parser.Parser().parse(file(self.__name)) 66 self.__dirty = False
67
68 - def save(self):
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
75 - def clear(self):
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
85 - def __getitem__(self, header):
86 """Text of a patchlog header by name.""" 87 if self.__email is None: 88 self.load() 89 return self.__email[header]
90
91 - def __setitem__(self, header, text):
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
101 - def get_description(self):
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
108 - def set_description(self, s):
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