fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / pocommon.py
blob749982eee9412d13b15a37c822739d614e4abd5e
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2002-2007 Zuza Software Foundation
5 #
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from translate.storage import base
23 from translate.storage import poheader
25 class pounit(base.TranslationUnit):
27 def adderror(self, errorname, errortext):
28 """Adds an error message to this unit."""
29 text = u'(pofilter) %s: %s' % (errorname, errortext)
30 # Don't add the same error twice:
31 if text not in self.getnotes(origin='translator'):
32 self.addnote(text, origin="translator")
34 def geterrors(self):
35 """Get all error messages."""
36 notes = self.getnotes(origin="translator").split('\n')
37 errordict = {}
38 for note in notes:
39 if '(pofilter) ' in note:
40 error = note.replace('(pofilter) ', '')
41 errorname, errortext = error.split(': ')
42 errordict[errorname] = errortext
43 return errordict
45 def markreviewneeded(self, needsreview=True, explanation=None):
46 """Marks the unit to indicate whether it needs review. Adds an optional explanation as a note."""
47 if needsreview:
48 reviewnote = "(review)"
49 if explanation:
50 reviewnote += " " + explanation
51 self.addnote(reviewnote, origin="translator")
52 else:
53 # Strip (review) notes.
54 notestring = self.getnotes(origin="translator")
55 notes = notestring.split('\n')
56 newnotes = []
57 for note in notes:
58 if not '(review)' in note:
59 newnotes.append(note)
60 newnotes = '\n'.join(newnotes)
61 self.removenotes()
62 self.addnote(newnotes, origin="translator")
64 class pofile(base.TranslationStore, poheader.poheader):
66 def makeheader(self, **kwargs):
67 """create a header for the given filename. arguments are specially handled, kwargs added as key: value
68 pot_creation_date can be None (current date) or a value (datetime or string)
69 po_revision_date can be None (form), False (=pot_creation_date), True (=now), or a value (datetime or string)"""
71 headerpo = self.UnitClass(encoding=self._encoding)
72 headerpo.markfuzzy()
73 headerpo.source = ""
74 headeritems = self.makeheaderdict(**kwargs)
75 headervalue = ""
76 for (key, value) in headeritems.items():
77 headervalue += "%s: %s\n" % (key, value)
78 headerpo.target = headervalue
79 return headerpo