fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / ini.py
blob13232c668e71c1aa8e80b8e2368e8ab2d8f851ac
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 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 """Class that manages .ini files for translation
24 @note: A simple summary of what is permissible follows.
26 # a comment
27 ; a comment
29 [Section]
30 a = a string
31 b : a string
33 """
34 from translate.storage import base
35 from translate.misc.ini import INIConfig
36 from StringIO import StringIO
37 import re
40 class iniunit(base.TranslationUnit):
41 """A INI file entry"""
42 def __init__(self, source=None, encoding="UTF-8"):
43 self.location = ""
44 if source:
45 self.source = source
46 super(iniunit, self).__init__(source)
48 def addlocation(self, location):
49 self.location = location
51 def getlocations(self):
52 return [self.location]
54 class inifile(base.TranslationStore):
55 """An INI file"""
56 UnitClass = iniunit
57 def __init__(self, inputfile=None, unitclass=iniunit):
58 """construct an INI file, optionally reading in from inputfile."""
59 self.UnitClass = unitclass
60 base.TranslationStore.__init__(self, unitclass=unitclass)
61 self.units = []
62 self.filename = ''
63 self._inifile = None
64 if inputfile is not None:
65 self.parse(inputfile)
67 def __str__(self):
68 _outinifile = self._inifile
69 for unit in self.units:
70 for location in unit.getlocations():
71 match = re.match('\\[(?P<section>.+)\\](?P<entry>.+)', location)
72 _outinifile[match.groupdict()['section']][match.groupdict()['entry']] = unit.target
73 if _outinifile:
74 return str(_outinifile)
75 else:
76 return ""
78 def parse(self, input):
79 """parse the given file or file source string"""
80 if hasattr(input, 'name'):
81 self.filename = input.name
82 elif not getattr(self, 'filename', ''):
83 self.filename = ''
84 if hasattr(input, "read"):
85 inisrc = input.read()
86 input.close()
87 input = inisrc
88 if isinstance(input, str):
89 input = StringIO(input)
90 self._inifile = INIConfig(input, optionxformvalue=None)
91 else:
92 self._inifile = INIConfig(file(input), optionxformvalue=None)
93 for section in self._inifile:
94 for entry in self._inifile[section]:
95 newunit = self.addsourceunit(self._inifile[section][entry])
96 newunit.addlocation("[%s]%s" % (section, entry))