1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """Class that manages .ini files for translation
23
24 @note: A simple summary of what is permissible follows.
25
26 # a comment
27 ; a comment
28
29 [Section]
30 a = a string
31 b : a string
32
33 """
34 from translate.storage import base
35 from translate.misc.ini import INIConfig
36 from StringIO import StringIO
37 import re
38
39
40 -class iniunit(base.TranslationUnit):
41 """A INI file entry"""
42 - def __init__(self, source=None, encoding="UTF-8"):
47
49 self.location = location
50
52 return [self.location]
53
54 -class inifile(base.TranslationStore):
55 """An INI file"""
56 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)
66
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 ""
77
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))
97