Tools used by OpenStack Documentation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

152 lines
4.4 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Created on 2012-7-3
  5. @author: daisy
  6. '''
  7. import os, sys
  8. import tempfile
  9. import shutil
  10. import xml.dom.minidom
  11. from optparse import OptionParser
  12. import codecs
  13. from xml2po import Main
  14. default_mode = 'docbook'
  15. operation = 'merge'
  16. options = {
  17. 'mark_untranslated' : False,
  18. 'expand_entities' : True,
  19. 'expand_all_entities' : False,
  20. }
  21. ignore_folder = {"docbkx-example"}
  22. root = "./doc/src/docbkx"
  23. def mergeback (folder, language):
  24. if (folder==None) :
  25. path = root
  26. else :
  27. outputFiles = mergeSingleDocument(folder, language)
  28. if ((outputFiles != None) and (len(outputFiles)>0)) :
  29. for outXML in outputFiles :
  30. changeXMLLangSetting (outXML, language)
  31. return
  32. if (not os.path.isdir(path)) :
  33. return
  34. files = os.listdir(path)
  35. for aFile in files :
  36. if (not (aFile in ignore_folder)):
  37. outputFiles = mergeSingleDocument (aFile, language)
  38. if ((outputFiles != None) and (len(outputFiles)>0)) :
  39. for outXML in outputFiles :
  40. changeXMLLangSetting (outXML, language)
  41. def mergeSingleDocument(folder, language):
  42. xmlfiles = []
  43. outputfiles = []
  44. abspath = os.path.join(root, folder)
  45. if (os.path.isdir(abspath)) :
  46. os.path.walk(abspath, get_xml_list, xmlfiles)
  47. else:
  48. return None
  49. if len(xmlfiles)>0 :
  50. popath = os.path.join(abspath,"locale",language+".po")
  51. #generate MO file
  52. mofile_handler, mofile_tmppath = tempfile.mkstemp()
  53. os.close(mofile_handler)
  54. os.system("msgfmt -o %s %s" % (mofile_tmppath, popath))
  55. for aXML in xmlfiles :
  56. #(filename, ext) = os.path.splitext(os.path.basename(aXML))
  57. relpath = os.path.relpath(aXML, root)
  58. outputpath = os.path.join(os.path.curdir, "generated", language, relpath)
  59. try:
  60. xml2po_main = Main(default_mode, operation, outputpath, options)
  61. xml2po_main.merge(mofile_tmppath, aXML)
  62. outputfiles.append(outputpath)
  63. except IOError:
  64. print "Error: cannot open aFile %s for writing."
  65. sys.exit(5)
  66. except :
  67. print ("Exception happen")
  68. if mofile_tmppath :
  69. os.remove(mofile_tmppath)
  70. return outputfiles
  71. def changeXMLLangSetting(xmlFile, language):
  72. dom = xml.dom.minidom.parse(xmlFile)
  73. root = dom.documentElement
  74. root.setAttribute("xml:lang", language[:2])
  75. fileObj = codecs.open(xmlFile, "wb", encoding="utf-8")
  76. #add namespace to link
  77. nodelists = root.getElementsByTagName("link")
  78. for node in nodelists :
  79. if (node.hasAttribute("href")) :
  80. node.setAttribute("xlink:href", node.getAttribute("href"))
  81. if (node.hasAttribute("title")) :
  82. node.setAttribute("xlink:title", node.getAttribute("title"))
  83. dom.writexml(fileObj)
  84. def get_xml_list (sms, dr, flst):
  85. if ((flst == "target") or (flst == "wadls")) :
  86. return
  87. if (dr.find("target")>-1) :
  88. return
  89. if (dr.find("wadls")>-1) :
  90. return
  91. for f in flst:
  92. if ((f.endswith(".xml") and (f != "pom.xml"))) :
  93. sms.append(os.path.join(dr,f))
  94. def main(argv):
  95. usage = "usage: %prog [options] command [cmd_options]"
  96. description = "This is the tool to generate translated docbooks, which"\
  97. " will be stored in 'generated/[language]/"
  98. parser = OptionParser(
  99. usage=usage, version="0.6", description=description
  100. )
  101. parser.disable_interspersed_args()
  102. parser.add_option(
  103. "-l", "--language", dest="language", help=("specified language")
  104. )
  105. parser.add_option(
  106. "-b", "--book", dest="book", help=("specified docbook")
  107. )
  108. (options, args) = parser.parse_args()
  109. if (options.language == None) :
  110. print "must specify language"
  111. return
  112. #change working directory
  113. #copy folders
  114. folder = options.book
  115. language = options.language
  116. sourcepath = os.path.join(root, folder)
  117. destpath = os.path.join(os.path.curdir, "generated", language)
  118. if (not os.path.exists(destpath)) :
  119. os.makedirs(destpath)
  120. destfolder = os.path.join(destpath, folder)
  121. if (os.path.exists(destfolder)) :
  122. shutil.rmtree(destfolder)
  123. os.system("cp -r %s %s" % (sourcepath, destpath))
  124. mergeback(folder, language)
  125. if __name__ == '__main__':
  126. main(sys.argv[1:])