forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ISO_639_3_LanguagesConverter.py
executable file
·400 lines (337 loc) · 19.5 KB
/
ISO_639_3_LanguagesConverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# ISO_639_3_LanguagesConverter.py
#
# Module handling ISO_639_3.xml to produce C and Python data tables
#
# Copyright (C) 2010-2016 Robert Hunt
# Author: Robert Hunt <Freely.Given.org@gmail.com>
# License: See gpl-3.0.txt
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Module handling ISO_639_3_Languages.xml and to export to JSON, C, and Python data tables.
"""
from gettext import gettext as _
LastModifiedDate = '2016-04-23' # by RJH
ShortProgName = "ISOLanguagesConverter"
ProgName = "ISO 639_3_Languages handler"
ProgVersion = "0.84"
ProgNameVersion = "{} v{}".format( ProgName, ProgVersion )
import logging, os.path
from datetime import datetime
from collections import OrderedDict
from xml.etree.ElementTree import ElementTree
from singleton import singleton
import BibleOrgSysGlobals
@singleton # Can only ever have one instance
class ISO_639_3_LanguagesConverter:
"""
Class for handling and converting ISO 639-3 language codes.
"""
def __init__( self ):
"""
Constructor: expects the filepath of the source XML file.
Loads (and crudely validates the XML file) into an element tree.
"""
self._filenameBase = "iso_639_3"
# These fields are used for parsing the XML
self._treeTag = "iso_639_3_entries"
self._mainElementTag = "iso_639_3_entry"
# These fields are used for automatically checking/validating the XML
self._compulsoryAttributes = ( "id", "status", "scope", "type", "reference_name", "name" )
self._optionalAttributes = ( "part1_code", "part2_code", "inverted_name", "common_name" )
self._uniqueAttributes = ( "id", "reference_name", "part1_code", "part2_code", )
self._compulsoryElements = ()
self._optionalElements = ()
self._uniqueElements = self._compulsoryElements + self._optionalElements
self.title = "ISO 639-3 language codes"
# These are fields that we will fill later
self._XMLtree, self.__DataDicts = None, None
# end of __init__
def loadAndValidate( self, XMLFilepath=None ):
"""
Loads (and crudely validates the XML file) into an element tree.
Allows the filepath of the source XML file to be specified, otherwise uses the default.
"""
if self._XMLtree is None: # We mustn't have already have loaded the data
if XMLFilepath is None:
XMLFilepath = os.path.join( os.path.dirname(__file__), "DataFiles", self._filenameBase + ".xml" ) # Relative to module, not cwd
self._load( XMLFilepath )
if BibleOrgSysGlobals.strictCheckingFlag:
self._validate()
else: # The data must have been already loaded
if XMLFilepath is not None and XMLFilepath!=self.__XMLFilepath: logging.error( _("ISO 639-3 language codes are already loaded -- your different filepath of {!r} was ignored").format( XMLFilepath ) )
return self
# end of loadAndValidate
def _load( self, XMLFilepath ):
"""
Load the source XML file and remove the header from the tree.
Also, extracts some useful Attributes from the header element.
"""
assert XMLFilepath
self.__XMLFilepath = XMLFilepath
assert self._XMLtree is None or len(self._XMLtree)==0 # Make sure we're not doing this twice
if BibleOrgSysGlobals.verbosityLevel > 2: print( "Loading ISO 639-3 languages XML file from {!r}…".format( XMLFilepath ) )
self._XMLtree = ElementTree().parse( XMLFilepath )
assert self._XMLtree # Fail here if we didn't load anything at all
if self._XMLtree.tag != self._treeTag:
logging.error( "Expected to load {!r} but got {!r}".format( self._treeTag, self._XMLtree.tag ) )
# end of _load
def _validate( self ):
"""
Check/validate the loaded data.
"""
assert self._XMLtree
uniqueDict = {}
#for elementName in self._uniqueElements: uniqueDict["Element_"+elementName] = []
for attributeName in self._uniqueAttributes: uniqueDict["Attribute_"+attributeName] = []
for j,element in enumerate(self._XMLtree):
if element.tag == self._mainElementTag:
BibleOrgSysGlobals.checkXMLNoText( element, element.tag )
BibleOrgSysGlobals.checkXMLNoTail( element, element.tag )
BibleOrgSysGlobals.checkXMLNoSubelements( element, element.tag )
# Check compulsory attributes on this main element
for attributeName in self._compulsoryAttributes:
attributeValue = element.get( attributeName )
if attributeValue is None:
logging.error( "Compulsory {!r} attribute is missing from {} element in record {}".format( attributeName, element.tag, j ) )
if not attributeValue and attributeName!="type":
logging.warning( "Compulsory {!r} attribute is blank on {} element in record {}".format( attributeName, element.tag, j ) )
# Check optional attributes on this main element
for attributeName in self._optionalAttributes:
attributeValue = element.get( attributeName )
if attributeValue is not None:
if not attributeValue:
logging.warning( "Optional {!r} attribute is blank on {} element in record {}".format( attributeName, element.tag, j ) )
# Check for unexpected additional attributes on this main element
for attributeName in element.keys():
attributeValue = element.get( attributeName )
if attributeName not in self._compulsoryAttributes and attributeName not in self._optionalAttributes:
logging.warning( "Additional {!r} attribute ({!r}) found on {} element in record {}".format( attributeName, attributeValue, element.tag, j ) )
# Check the attributes that must contain unique information (in that particular field -- doesn't check across different attributes)
for attributeName in self._uniqueAttributes:
attributeValue = element.get( attributeName )
if attributeValue is not None and attributeName!="reference_name":
if attributeValue in uniqueDict["Attribute_"+attributeName]:
logging.error( "Found {!r} data repeated in {!r} field on {} element in record {}".format( attributeValue, attributeName, element.tag, j ) )
uniqueDict["Attribute_"+attributeName].append( attributeValue )
else:
logging.warning( "Unexpected element: {} in record {}".format( element.tag, j ) )
# end of _validate
def __str__( self ):
"""
This method returns the string representation of a Bible book code.
@return: the name of a Bible object formatted as a string
@rtype: string
"""
result = "ISO_639_3_Languages_Converter object"
if self.title: result += ('\n' if result else '') + self.title
result += ('\n' if result else '') + " Number of entries = " + str(len(self._XMLtree))
return result
# end of __str__
def __len__( self ):
""" Returns the number of languages loaded. """
return len( self._XMLtree )
# end of __len__
def importDataToPython( self ):
"""
Loads (and pivots) the data into suitable Python containers to use in a Python program.
(Of course, you can just use the elementTree in self._XMLtree if you prefer.)
"""
assert self._XMLtree
if self.__DataDicts: # We've already done an import/restructuring -- no need to repeat it
return self.__DataDicts
# We'll create a number of dictionaries with different Attributes as the key
myIDDict, myNameDict = OrderedDict(), OrderedDict()
for element in self._XMLtree:
# Get the required information out of the tree for this element
# Start with the compulsory attributes
ID = element.get("id")
Name = element.get("name")
Scope = element.get("scope")
Type = element.get("type")
# The optional attributes are set to None if they don't exist
Part1Code = element.get("part1_code")
Part2Code = element.get("part2_code")
# Now put it into my dictionaries for easy access
# This part should be customized or added to for however you need to process the data
# Add .upper() if you require the abbreviations to be uppercase (or .lower() for lower case)
if "id" in self._compulsoryAttributes or ID:
if "id" in self._uniqueElements: assert ID not in myIDDict # Shouldn't be any duplicates
myIDDict[ID] = ( Name, Scope, Type, Part1Code, Part2Code, )
if "name" in self._compulsoryAttributes or Name:
if "name" in self._uniqueElements: assert Name not in myNameDict # Shouldn't be any duplicates
myNameDict[Name.upper()] = ID # Save it as UPPERCASE
self.__DataDicts = myIDDict, myNameDict
return self.__DataDicts
# end of importDataToPython
def pickle( self, filepath=None ):
"""
Writes the information tables to a .pickle file that can be easily loaded into a Python3 program.
"""
import pickle
assert self._XMLtree
self.importDataToPython()
assert self.__DataDicts
if not filepath:
folder = os.path.join( os.path.split(self.__XMLFilepath)[0], "DerivedFiles/" )
if not os.path.exists( folder ): os.mkdir( folder )
filepath = os.path.join( folder, self._filenameBase + "_Languages_Tables.pickle" )
if BibleOrgSysGlobals.verbosityLevel > 1: print( _("Exporting to {}…").format( filepath ) )
with open( filepath, 'wb' ) as myFile:
pickle.dump( self.__DataDicts, myFile )
# end of pickle
def exportDataToPython( self, filepath=None ):
"""
Writes the information tables to a .py file that can be cut and pasted into a Python program.
"""
def exportPythonDict( theFile, theDict, dictName, keyComment, fieldsComment ):
"""Exports theDict to theFile."""
theFile.write( "{} = {{\n # Key is {}\n # Fields are: {}\n".format( dictName, keyComment, fieldsComment ) )
for dictKey in sorted(theDict.keys()):
theFile.write( " {}: {},\n".format( repr(dictKey), repr(theDict[dictKey]) ) )
theFile.write( "}}\n# end of {}\n\n".format( dictName ) )
# end of exportPythonDict
assert self._XMLtree
self.importDataToPython()
assert self.__DataDicts
if not filepath:
folder = os.path.join( os.path.split(self.__XMLFilepath)[0], "DerivedFiles/" )
if not os.path.exists( folder ): os.mkdir( folder )
filepath = os.path.join( folder, self._filenameBase + "_Languages_Tables.py" )
if BibleOrgSysGlobals.verbosityLevel > 1: print( "Exporting to {}…".format( filepath ) )
IDDict, NameDict = self.__DataDicts
with open( filepath, 'wt', encoding='utf-8' ) as myFile:
myFile.write( "# {}\n#\n".format( filepath ) )
myFile.write( "# This UTF-8 file was automatically generated by ISO_639_3_Languages_Converter.py V{} on {}\n#\n".format( ProgVersion, datetime.now() ) )
if self.title: myFile.write( "# {}\n".format( self.title ) )
myFile.write( "# {} {} loaded from the original XML file.\n#\n\n".format( len(self._XMLtree), self._treeTag ) )
exportPythonDict( myFile, IDDict, "ISO639_3_Languages_IDDict", "id", "Name, Type, Scope, Part1Code, Part2Code" )
exportPythonDict( myFile, NameDict, "ISO639_3_Languages_NameDict", "name", "ID" )
myFile.write( "# end of {}".format( os.path.basename(filepath) ) )
# end of exportDataToPython
def exportDataToJSON( self, filepath=None ):
"""
Writes the information tables to a .json file that can be easily loaded into a Java program.
See http://en.wikipedia.org/wiki/JSON.
"""
import json
assert self._XMLtree
self.importDataToPython()
assert self.__DataDicts
if not filepath:
folder = os.path.join( os.path.split(self.__XMLFilepath)[0], "DerivedFiles/" )
if not os.path.exists( folder ): os.mkdir( folder )
filepath = os.path.join( folder, self._filenameBase + "_Languages_Tables.json" )
if BibleOrgSysGlobals.verbosityLevel > 1: print( "Exporting to {}…".format( filepath ) )
with open( filepath, 'wt', encoding='utf-8' ) as myFile:
json.dump( self.__DataDicts, myFile, indent=2 )
# end of exportDataToJSON
def exportDataToC( self, filepath=None ):
"""
Writes the information tables to a .h and .c files that can be included in c and c++ programs.
NOTE: The (optional) filepath should not have the file extension specified -- this is added automatically.
"""
def exportPythonDict( hFile, cFile, theDict, dictName, sortedBy, structure ):
""" Exports theDict to the .h and .c files. """
def convertEntry( entry ):
""" Convert special characters in an entry… """
result = ""
if isinstance( entry, tuple ):
for j, field in enumerate(entry):
if result: result += ", " # Separate the fields
if field is None: result += '""'
elif isinstance( field, str):
if j>0 and len(field)==1: result += "'" + field + "'" # Catch the character fields
else: result += '"' + str(field).replace('"','\\"') + '"' # String fields
else: logging.error( "Cannot convert unknown field type {!r} in entry {!r}".format( field, entry ) )
elif isinstance( entry, str):
result += '"' + str(entry).replace('"','\\"') + '"' # String fields
else:
logging.error( "Can't handle this type of entry yet: {}".format( repr(entry) ) )
return result
# end of convertEntry
for dictKey in theDict.keys(): # Have to iterate this :(
fieldsCount = len( theDict[dictKey] ) + 1 # Add one since we include the key in the count
break # We only check the first (random) entry we get
#hFile.write( "typedef struct {}EntryStruct { {} } {}Entry;\n\n".format( dictName, structure, dictName ) )
hFile.write( "typedef struct {}EntryStruct {{\n".format( dictName ) )
for declaration in structure.split(';'):
adjDeclaration = declaration.strip()
if adjDeclaration: hFile.write( " {};\n".format( adjDeclaration ) )
hFile.write( "}} {}Entry;\n\n".format( dictName ) )
cFile.write( "const static {}Entry\n {}[{}] = {{\n // Fields ({}) are {}\n // Sorted by {}\n".format( dictName, dictName, len(theDict), fieldsCount, structure, sortedBy ) )
for dictKey in sorted(theDict.keys()):
if isinstance( dictKey, str ):
cFile.write( " {{\"{}\", {}}},\n".format( dictKey, convertEntry(theDict[dictKey]) ) )
elif isinstance( dictKey, int ):
cFile.write( " {{{}, {}}},\n".format( dictKey, convertEntry(theDict[dictKey]) ) )
else:
logging.error( "Can't handle this type of key data yet: {}".format( dictKey ) )
cFile.write( "}}; // {} ({} entries)\n\n".format( dictName, len(theDict) ) )
# end of exportPythonDict
assert self._XMLtree
self.importDataToPython()
assert self.__DataDicts
if not filepath:
folder = os.path.join( os.path.split(self.__XMLFilepath)[0], "DerivedFiles/" )
if not os.path.exists( folder ): os.mkdir( folder )
filepath = os.path.join( folder, self._filenameBase + "_Languages_Tables" )
hFilepath = filepath + '.h'
cFilepath = filepath + '.c'
if BibleOrgSysGlobals.verbosityLevel > 1: print( "Exporting to {}…".format( cFilepath ) ) # Don't bother telling them about the .h file
ifdefName = self._filenameBase.upper() + "_Tables_h"
IDDict, NameDict = self.__DataDicts
with open( hFilepath, 'wt', encoding='utf-8' ) as myHFile, open( cFilepath, 'wt', encoding='utf-8' ) as myCFile:
myHFile.write( "// {}\n//\n".format( hFilepath ) )
myCFile.write( "// {}\n//\n".format( cFilepath ) )
lines = "// This UTF-8 file was automatically generated by ISO_639_3_Languages.py V{} on {}\n//\n".format( ProgVersion, datetime.now() )
myHFile.write( lines ); myCFile.write( lines )
myCFile.write( "// {} {} loaded from the original XML file.\n//\n\n".format( len(self._XMLtree), self._treeTag ) )
myHFile.write( "\n#ifndef {}\n#define {}\n\n".format( ifdefName, ifdefName ) )
myCFile.write( '#include "{}"\n\n'.format( os.path.basename(hFilepath) ) )
CHAR = "const unsigned char"
BYTE = "const int"
exportPythonDict( myHFile, myCFile, IDDict, "IDDict", "3-character lower-case ID field", "{}[3+1] ID; {}* Name; {} Type; {} Scope; {}[2+1] Part1Code; {}[3+1] Part2Code;".format(CHAR,CHAR,CHAR,CHAR,CHAR,CHAR) )
exportPythonDict( myHFile, myCFile, NameDict, "NameDict", "language name (alphabetical)", "{}* Name; {}[3+1] ID;".format(CHAR,CHAR) )
myHFile.write( "#endif // {}\n\n".format( ifdefName ) )
myHFile.write( "// end of {}".format( os.path.basename(hFilepath) ) )
myCFile.write( "// end of {}".format( os.path.basename(cFilepath) ) )
# end of exportDataToC
# end of ISO_639_3_LanguagesConverter class
def demo():
"""
Main program to handle command line parameters and then run what they want.
"""
if BibleOrgSysGlobals.verbosityLevel > 1: print( ProgNameVersion )
if BibleOrgSysGlobals.commandLineArguments.export:
lgC = ISO_639_3_LanguagesConverter().loadAndValidate() # Load the XML
lgC.pickle() # Produce a pickle output file
lgC.exportDataToPython() # Produce the .py tables
lgC.exportDataToJSON() # Produce a json output file
lgC.exportDataToC() # Produce the .h and .c tables
else: # Must be demo mode
# Demo the converter object
lgC = ISO_639_3_LanguagesConverter().loadAndValidate() # Load the XML
print( lgC ) # Just print a summary
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = BibleOrgSysGlobals.setup( ProgName, ProgVersion )
BibleOrgSysGlobals.addStandardOptionsAndProcess( parser, exportAvailable=True )
demo()
BibleOrgSysGlobals.closedown( ProgName, ProgVersion )
# end of ISO_639_3_LanguagesConverter.py