Browse Source

Merge and rename generatedocbook/generatepot

The tool scripts used common functions, merge them to a single file
with different entry points.

Cleanup the merged file so that it passes the pep8 test.

Install the commands now as openstack-generate-docbook and
openstack-generate-pot. Leave a compatibility wrapper which should be
removed in version 0.8.

Change-Id: I7e4d651c4c62df58499e64e38528cb2cc4e9175b
changes/35/72135/7
Andreas Jaeger 7 years ago
parent
commit
ccbfdb592f
5 changed files with 263 additions and 266 deletions
  1. +14
    -3
      README.rst
  2. +0
    -167
      bin/generatedocbook
  3. +0
    -93
      bin/generatepot
  4. +244
    -0
      os_doc_tools/handle_pot.py
  5. +5
    -3
      setup.cfg

+ 14
- 3
README.rst View File

@ -68,9 +68,20 @@ Release notes
0.7
---
* Fix building of identity-api and image-api books.
* Add option --debug.
* Generate log file for each build.
* openstack-doc-test:
- Fix building of identity-api and image-api books.
- Add option --debug.
- Generate log file for each build.
- Do not install build-ha-guide.sh and markdown-docbook.sh in
/usr/bin, use special scripts dir instead.
* generatedocbook and generatepot have been merged into a single
file, the command has been renamed to
openstack-generate-docbook/openstack-generate-pot. For
compatibility, wrapper scripts are installed that will be removed
in version 0.8.
0.6
---


+ 0
- 167
bin/generatedocbook View File

@ -1,167 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012-7-3
@author: daisy
'''
import os, sys
import tempfile
import shutil
import xml.dom.minidom
from optparse import OptionParser
import codecs
from xml2po import Main
from xml2po.modes.docbook import docbookXmlMode
class myDocbookXmlMode(docbookXmlMode):
def __init__(self):
self.lists = ['itemizedlist', 'orderedlist', 'variablelist',
'segmentedlist', 'simplelist', 'calloutlist', 'varlistentry', 'userinput',
'computeroutput','prompt','command','screen']
self.objects = [ 'figure', 'textobject', 'imageobject', 'mediaobject',
'screenshot','literallayout', 'programlisting' ]
default_mode = 'docbook'
operation = 'merge'
options = {
'mark_untranslated' : False,
'expand_entities' : True,
'expand_all_entities' : False,
}
ignore_folder = {"docbkx-example"}
root = "./doc"
def mergeback (folder, language):
if (folder==None) :
path = root
else :
outputFiles = mergeSingleDocument(folder, language)
if ((outputFiles != None) and (len(outputFiles)>0)) :
for outXML in outputFiles :
changeXMLLangSetting (outXML, language)
return
if (not os.path.isdir(path)) :
return
files = os.listdir(path)
for aFile in files :
if (not (aFile in ignore_folder)):
outputFiles = mergeSingleDocument (aFile, language)
if ((outputFiles != None) and (len(outputFiles)>0)) :
for outXML in outputFiles :
changeXMLLangSetting (outXML, language)
def mergeSingleDocument(folder, language):
xmlfiles = []
outputfiles = []
abspath = os.path.join(root, folder)
if (os.path.isdir(abspath)) :
os.path.walk(abspath, get_xml_list, xmlfiles)
else:
return None
if len(xmlfiles)>0 :
popath = os.path.join(abspath,"locale",language+".po")
#generate MO file
mofile_handler, mofile_tmppath = tempfile.mkstemp()
os.close(mofile_handler)
os.system("msgfmt -o %s %s" % (mofile_tmppath, popath))
for aXML in xmlfiles :
#(filename, ext) = os.path.splitext(os.path.basename(aXML))
relpath = os.path.relpath(aXML, root)
outputpath = os.path.join(os.path.curdir, "generated", language, relpath)
try:
xml2po_main = Main(default_mode, operation, outputpath, options)
xml2po_main.current_mode = myDocbookXmlMode()
xml2po_main.merge(mofile_tmppath, aXML)
outputfiles.append(outputpath)
except IOError:
print "Error: cannot open aFile %s for writing."
sys.exit(5)
except :
print ("Exception happen")
if mofile_tmppath :
os.remove(mofile_tmppath)
return outputfiles
def changeXMLLangSetting(xmlFile, language):
dom = xml.dom.minidom.parse(xmlFile)
root = dom.documentElement
root.setAttribute("xml:lang", language[:2])
fileObj = codecs.open(xmlFile, "wb", encoding="utf-8")
#add namespace to link
nodelists = root.getElementsByTagName("link")
for node in nodelists :
if (node.hasAttribute("href")) :
node.setAttribute("xlink:href", node.getAttribute("href"))
if (node.hasAttribute("title")) :
node.setAttribute("xlink:title", node.getAttribute("title"))
dom.writexml(fileObj)
def get_xml_list (sms, dr, flst):
if ((flst == "target") or (flst == "wadls")) :
return
if (dr.find("target")>-1) :
return
if (dr.find("wadls")>-1) :
return
for f in flst:
if ((f.endswith(".xml") and (f != "pom.xml"))) :
sms.append(os.path.join(dr,f))
def get_default_book():
return os.listdir(root)[0]
def main(argv):
usage = "usage: %prog [options] command [cmd_options]"
description = "This is the tool to generate translated docbooks, which"\
" will be stored in 'generated/[language]/"
parser = OptionParser(
usage=usage, version="0.6", description=description
)
parser.disable_interspersed_args()
parser.add_option(
"-l", "--language", dest="language", help=("specified language")
)
parser.add_option(
"-b", "--book", dest="book", default=get_default_book(), help=("specified docbook")
)
(options, args) = parser.parse_args()
if (options.language == None) :
print "must specify language"
return
#change working directory
#copy folders
folder = options.book
language = options.language
sourcepath = os.path.join(root, folder)
destpath = os.path.join(os.path.curdir, "generated", language)
if (not os.path.exists(destpath)) :
os.makedirs(destpath)
destfolder = os.path.join(destpath, folder)
if (os.path.exists(destfolder)) :
shutil.rmtree(destfolder)
os.system("cp -r %s %s" % (sourcepath, destpath))
mergeback(folder, language)
if __name__ == '__main__':
main(sys.argv[1:])

+ 0
- 93
bin/generatepot View File

@ -1,93 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2012-7-3
@author: daisy
'''
import os, sys
from xml2po import Main
from xml2po.modes.docbook import docbookXmlMode
class myDocbookXmlMode(docbookXmlMode):
def __init__(self):
self.lists = ['itemizedlist', 'orderedlist', 'variablelist',
'segmentedlist', 'simplelist', 'calloutlist', 'varlistentry', 'userinput',
'computeroutput','prompt','command','screen']
self.objects = [ 'figure', 'textobject', 'imageobject', 'mediaobject',
'screenshot','literallayout', 'programlisting' ]
default_mode = 'docbook'
operation = 'pot'
options = {
'mark_untranslated' : False,
'expand_entities' : True,
'expand_all_entities' : False,
}
ignore_folder = {"docbkx-example", "training-guide"}
ignore_file = {"api-examples.xml"}
root = "./doc"
def generatePoT (folder):
if (folder==None) :
path = root
else :
generateSinglePoT(folder)
return
if (not os.path.isdir(path)) :
return
files = os.listdir(path)
for aFile in files :
if (not (aFile in ignore_folder)):
generateSinglePoT (aFile)
def generateSinglePoT(folder):
xmlfiles = []
abspath = os.path.join(root, folder)
if (os.path.isdir(abspath)) :
os.path.walk(abspath, get_all_xml, xmlfiles)
else:
return
if len(xmlfiles)>0 :
output = os.path.join(abspath,"locale")
if (not os.path.exists(output)) :
os.mkdir(output)
output = os.path.join(output, folder+".pot")
try:
xml2po_main = Main(default_mode, operation, output, options)
xml2po_main.current_mode = myDocbookXmlMode()
except IOError:
print "Error: cannot open aFile %s for writing." % (output)
sys.exit(5)
#print(xmlfiles)
#print(">>>outout: %s ", output)
xml2po_main.to_pot(xmlfiles)
def get_all_xml (sms, dr, flst):
if ((flst == "target") or (flst == "wadls")) :
return
if (dr.find("target")>-1) :
return
if (dr.find("wadls")>-1) :
return
for f in flst:
if (f.endswith(".xml") and (f != "pom.xml") and (not (f in ignore_file))) :
sms.append(os.path.join(dr,f))
def main():
try:
folder = sys.argv[1]
except:
folder = None
generatePoT(folder)
if __name__ == '__main__':
main()

+ 244
- 0
os_doc_tools/handle_pot.py View File

@ -0,0 +1,244 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
'''
Created on 2012-7-3
@author: daisy
'''
import codecs
import optparse
import os
import shutil
import sys
import tempfile
import xml.dom.minidom
from xml2po import Main
from xml2po.modes.docbook import docbookXmlMode
class myDocbookXmlMode(docbookXmlMode):
def __init__(self):
self.lists = ['itemizedlist', 'orderedlist', 'variablelist',
'segmentedlist', 'simplelist', 'calloutlist',
'varlistentry', 'userinput', 'computeroutput',
'prompt', 'command', 'screen']
self.objects = ['figure', 'textobject', 'imageobject', 'mediaobject',
'screenshot', 'literallayout', 'programlisting']
default_mode = 'docbook'
operation = 'merge'
options = {
'mark_untranslated': False,
'expand_entities': True,
'expand_all_entities': False,
}
IGNORE_FOLDER = []
IGNORE_FILE = []
root = "./doc"
def mergeback(folder, language):
if folder is None:
path = root
else:
outputFiles = mergeSingleDocument(folder, language)
if (outputFiles is not None) and (len(outputFiles) > 0):
for outXML in outputFiles:
changeXMLLangSetting(outXML, language)
return
if not os.path.isdir(path):
return
files = os.listdir(path)
for aFile in files:
if not (aFile in IGNORE_FOLDER):
outputFiles = mergeSingleDocument(aFile, language)
if (outputFiles is not None) and (len(outputFiles) > 0):
for outXML in outputFiles:
changeXMLLangSetting(outXML, language)
def mergeSingleDocument(folder, language):
xmlfiles = []
outputfiles = []
abspath = os.path.join(root, folder)
if os.path.isdir(abspath):
os.path.walk(abspath, get_xml_list, xmlfiles)
else:
return None
if len(xmlfiles) > 0:
popath = os.path.join(abspath, "locale", language + ".po")
#generate MO file
mofile_handler, mofile_tmppath = tempfile.mkstemp()
os.close(mofile_handler)
os.system("msgfmt -o %s %s" % (mofile_tmppath, popath))
for aXML in xmlfiles:
#(filename, ext) = os.path.splitext(os.path.basename(aXML))
relpath = os.path.relpath(aXML, root)
outputpath = os.path.join(os.path.curdir, "generated", language,
relpath)
try:
xml2po_main = Main(default_mode, "merge", outputpath,
options)
xml2po_main.current_mode = myDocbookXmlMode()
xml2po_main.merge(mofile_tmppath, aXML)
outputfiles.append(outputpath)
except IOError:
print("Error: cannot open aFile %s for writing.")
sys.exit(5)
except Exception:
print("Exception happen")
if mofile_tmppath:
os.remove(mofile_tmppath)
return outputfiles
def changeXMLLangSetting(xmlFile, language):
dom = xml.dom.minidom.parse(xmlFile)
root = dom.documentElement
root.setAttribute("xml:lang", language[:2])
fileObj = codecs.open(xmlFile, "wb", encoding="utf-8")
#add namespace to link
nodelists = root.getElementsByTagName("link")
for node in nodelists:
if node.hasAttribute("href"):
node.setAttribute("xlink:href", node.getAttribute("href"))
if node.hasAttribute("title"):
node.setAttribute("xlink:title", node.getAttribute("title"))
dom.writexml(fileObj)
def get_xml_list(sms, dr, flst):
if (flst == "target") or (flst == "wadls"):
return
if dr.find("target") > -1:
return
if dr.find("wadls") > -1:
return
for f in flst:
if (f.endswith(".xml") and (f != "pom.xml") and
not (f in IGNORE_FILE)):
sms.append(os.path.join(dr, f))
def get_default_book():
return os.listdir(root)[0]
def generatedocbook():
global IGNORE_FOLDER, IGNORE_FILE
usage = "usage: %prog [options] command [cmd_options]"
description = "This is the tool to generate translated docbooks, which"
" will be stored in 'generated/[language]/"
IGNORE_FOLDER = ["docbkx-example"]
IGNORE_FILE = []
parser = optparse.OptionParser(
usage=usage, version="0.6", description=description
)
parser.disable_interspersed_args()
parser.add_option(
"-l", "--language", dest="language", help=("specified language")
)
parser.add_option(
"-b", "--book", dest="book", default=get_default_book(),
help=("specified docbook")
)
(options, args) = parser.parse_args()
if options.language is None:
print("must specify language")
return
#change working directory
#copy folders
folder = options.book
language = options.language
sourcepath = os.path.join(root, folder)
destpath = os.path.join(os.path.curdir, "generated", language)
if not os.path.exists(destpath):
os.makedirs(destpath)
destfolder = os.path.join(destpath, folder)
if os.path.exists(destfolder):
shutil.rmtree(destfolder)
os.system("cp -r %s %s" % (sourcepath, destpath))
mergeback(folder, language)
def generatePoT(folder):
if folder is None:
path = root
else:
generateSinglePoT(folder)
return
if not os.path.isdir(path):
return
files = os.listdir(path)
for aFile in files:
if not (aFile in IGNORE_FOLDER):
generateSinglePoT(aFile)
def generateSinglePoT(folder):
xmlfiles = []
abspath = os.path.join(root, folder)
if os.path.isdir(abspath):
os.path.walk(abspath, get_xml_list, xmlfiles)
else:
return
if len(xmlfiles) > 0:
output = os.path.join(abspath, "locale")
if not os.path.exists(output):
os.mkdir(output)
output = os.path.join(output, folder + ".pot")
try:
xml2po_main = Main(default_mode, "pot", output, options)
xml2po_main.current_mode = myDocbookXmlMode()
except IOError:
print("Error: cannot open aFile %s for writing." % (output))
sys.exit(5)
#print(xmlfiles)
#print(">>>outout: %s ", output)
xml2po_main.to_pot(xmlfiles)
def generatepot():
global IGNORE_FOLDER, IGNORE_FILE
IGNORE_FOLDER = ["docbkx-example", "training-guide"]
IGNORE_FILE = ["api-examples.xml"]
try:
folder = sys.argv[1]
except Exception:
folder = None
generatePoT(folder)

+ 5
- 3
setup.cfg View File

@ -21,9 +21,6 @@ classifier =
packages =
os_doc_tools
autogenerate_config_docs
scripts =
bin/generatedocbook
bin/generatepot
data_files =
share/openstack-doc-tools/sitemap = sitemap/*
share/openstack-doc-tools/cleanup = cleanup/*
@ -37,6 +34,11 @@ console_scripts =
openstack-doc-test = os_doc_tools.doctest:main
openstack-autohelp = autogenerate_config_docs.autohelp:main
openstack-auto-commands = os_doc_tools.commands:main
openstack-generate-docbook = os_doc_tools.handle_pot:generatedocbook
openstack-generate-pot = os_doc_tools.handle_pot:generatepot
# Compatibility names, will be removed in 0.8
generatedocbook = os_doc_tools.handle_pot:generatedocbook
generatepot = os_doc_tools.handle_pot:generatepot
[build_sphinx]
source-dir = doc/source


Loading…
Cancel
Save