2 # -*- coding: utf-8 -*-
4 # Copyright 2007 Zuza Software Foundation
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.
34 from translate
.storage
import base
35 from translate
.misc
.ini
import INIConfig
36 from StringIO
import StringIO
40 class iniunit(base
.TranslationUnit
):
41 """A INI file entry"""
42 def __init__(self
, source
=None, encoding
="UTF-8"):
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
):
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
)
64 if inputfile
is not None:
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
74 return str(_outinifile
)
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', ''):
84 if hasattr(input, "read"):
88 if isinstance(input, str):
89 input = StringIO(input)
90 self
._inifile
= INIConfig(input, optionxformvalue
=None)
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
))