1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Class that manages .ini files for translation
22
23 @note: A simple summary of what is permissible follows.
24
25 # a comment
26 ; a comment
27
28 [Section]
29 a = a string
30 b : a string
31 """
32
33 import re
34 from StringIO import StringIO
35
36 from translate.misc.ini import INIConfig
37 from translate.storage import base
38
39 _dialects = {}
40
41
45
46
48 """Base class for differentiating dialect options and functions"""
49 pass
50
51
59 register_dialect("default", DialectDefault)
60
61
69 register_dialect("inno", DialectInno)
70
71
72 -class iniunit(base.TranslationUnit):
73 """A INI file entry"""
74
75 - def __init__(self, source=None, encoding="UTF-8"):
80
82 self.location = location
83
85 return [self.location]
86
87
88 -class inifile(base.TranslationStore):
89 """An INI file"""
90 UnitClass = iniunit
91
93 """construct an INI file, optionally reading in from inputfile."""
94 self.UnitClass = unitclass
95 self._dialect = _dialects.get(dialect, DialectDefault)()
96 base.TranslationStore.__init__(self, unitclass=unitclass)
97 self.units = []
98 self.filename = ''
99 self._inifile = None
100 if inputfile is not None:
101 self.parse(inputfile)
102
104 _outinifile = self._inifile
105 for unit in self.units:
106 for location in unit.getlocations():
107 match = re.match('\\[(?P<section>.+)\\](?P<entry>.+)', location)
108 _outinifile[match.groupdict()['section']][match.groupdict()['entry']] = self._dialect.escape(unit.target)
109 if _outinifile:
110 return str(_outinifile)
111 else:
112 return ""
113
115 """parse the given file or file source string"""
116 if hasattr(input, 'name'):
117 self.filename = input.name
118 elif not getattr(self, 'filename', ''):
119 self.filename = ''
120 if hasattr(input, "read"):
121 inisrc = input.read()
122 input.close()
123 input = inisrc
124 if isinstance(input, str):
125 input = StringIO(input)
126 self._inifile = INIConfig(input, optionxformvalue=None)
127 else:
128 self._inifile = INIConfig(file(input), optionxformvalue=None)
129 for section in self._inifile:
130 for entry in self._inifile[section]:
131 newunit = self.addsourceunit(self._dialect.unescape(self._inifile[section][entry]))
132 newunit.addlocation("[%s]%s" % (section, entry))
133