Skip to content

Commit

Permalink
pep8 coding style conformance for scripts/unocommands.py
Browse files Browse the repository at this point in the history
Change-Id: Ic395a0692a808bc8519a05baf9cdd669d94a0fc4
  • Loading branch information
MucahidAydin authored and mrkara committed Oct 15, 2020
1 parent d977d13 commit e9ea83e
Showing 1 changed file with 79 additions and 32 deletions.
111 changes: 79 additions & 32 deletions scripts/unocommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#

import os
import polib
import re
import sys
import polib
from lxml import etree


def usageAndExit():
message = """usage: {program} [--check|--update|--translate] online_dir [...]
Expand All @@ -34,11 +35,12 @@ def usageAndExit():
{program} --translate /path/to/online /path/to/translations
"""
print(message.format(program = sys.argv[0]))
print(message.format(program=sys.argv[0]))
exit(1)

# Extract uno commands name from lines like " 'Command1', 'Command2',"

def commandsFromLine(line):
"""Extract uno commands name from lines like " 'Command1', 'Command2',"""
commands = []

inCommand = False
Expand All @@ -48,25 +50,27 @@ def commandsFromLine(line):
inCommand = not inCommand
# command ended, collect it
if not inCommand and command != '':
commands += [ command ]
commands += [command]
command = ''
elif inCommand:
command += c

return commands


# Extract uno commands name from lines like " {uno: '.uno:Command3',"
def commandFromMenuLine(line):
m = re.search(r"\buno: *'\.uno:([^']*)'", line)
if m:
return [ m.group(1) ]
return [m.group(1)]

m = re.search(r"\b_UNO\('.uno:([^']*)'", line)
if m:
return [ m.group(1) ]
return [m.group(1)]

return []


# Extract all the uno commands we are using in the Online menu
def extractMenuCommands(path):
commands = []
Expand All @@ -82,6 +86,7 @@ def extractMenuCommands(path):
# may the list unique
return set(commands)


# Extract all the uno commands we are using in the Online context menu
def extractContextCommands(path):
commandsToIgnore = ["FontDialogForParagraph"]
Expand Down Expand Up @@ -113,10 +118,12 @@ def extractContextCommands(path):
if line.find("_UNO(") >= 0:
commands += commandFromMenuLine(line)

commands = [command for command in commands if command not in commandsToIgnore]
commands = [command for command in commands
if command not in commandsToIgnore]
# may the list unique
return set(commands)


# Extract all the uno commands we are using in the Online toolbar
def extractToolbarCommands(path):
commands = []
Expand All @@ -137,7 +144,8 @@ def extractToolbarCommands(path):
if line.find("_UNO(") >= 0:
commands += commandFromMenuLine(line)

f = open(path + '/loleaflet/src/control/Control.NotebookbarBuilder.js', 'r')
f = open(path +
'/loleaflet/src/control/Control.NotebookbarBuilder.js', 'r')
for line in f:
if line.find("_UNO(") >= 0:
commands += commandFromMenuLine(line)
Expand All @@ -157,7 +165,8 @@ def extractToolbarCommands(path):
if line.find("_UNO(") >= 0:
commands += commandFromMenuLine(line)

f = open(path + '/loleaflet/src/control/Control.NotebookbarImpress.js', 'r')
f = open(path +
'/loleaflet/src/control/Control.NotebookbarImpress.js', 'r')
for line in f:
if line.find("_UNO(") >= 0:
commands += commandFromMenuLine(line)
Expand Down Expand Up @@ -185,10 +194,11 @@ def extractToolbarCommands(path):
# may the list unique
return set(commands)


# Create mapping between the commands and appropriate strings
def collectCommandsFromXCU(xcu, descriptions, commands, label, type):
root = etree.parse(xcu)
nodes = root.xpath("/oor:component-data/node/node/node", namespaces = {
nodes = root.xpath("/oor:component-data/node/node/node", namespaces={
'oor': 'http://openoffice.org/2001/registry',
})
for node in nodes:
Expand All @@ -198,10 +208,10 @@ def collectCommandsFromXCU(xcu, descriptions, commands, label, type):

if unoCommand in commands:
# normal labels
textElement = node.xpath('prop[@oor:name="' + label + '"]/value', namespaces = {
'oor': 'http://openoffice.org/2001/registry',
})

textElement = node.xpath('prop[@oor:name="' + label + '"]/value',
namespaces={'oor':
'http://open' +
'office.org/2001/registry', })
if len(textElement) == 1:
# extract the uno command's English text
text = ''.join(textElement[0].itertext())
Expand All @@ -225,13 +235,16 @@ def collectCommandsFromXCU(xcu, descriptions, commands, label, type):

return descriptions


# Print commands from all the XCU files, and collect them too
def writeUnocommandsJS(onlineDir, lofficeDir, menuCommands, contextCommands, toolbarCommands):
def writeUnocommandsJS(
onlineDir, lofficeDir, menuCommands, contextCommands, toolbarCommands):

descriptions = {}
dir = lofficeDir + '/officecfg/registry/data/org/openoffice/Office/UI'
for file in os.listdir(dir):
if file.endswith('.xcu'):
type = 'global';
type = 'global'
if file.startswith('Writer'):
type = 'text'
elif file.startswith('Calc'):
Expand All @@ -240,23 +253,46 @@ def writeUnocommandsJS(onlineDir, lofficeDir, menuCommands, contextCommands, too
type = 'presentation'

# main menu
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, menuCommands, 'ContextLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, contextCommands, 'ContextLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions, menuCommands,
'ContextLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
contextCommands,
'ContextLabel', type)

# right-click menu
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, contextCommands, 'PopupLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
contextCommands,
'PopupLabel', type)

# toolbar
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, toolbarCommands, 'PopupLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, toolbarCommands, 'TooltipLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
toolbarCommands,
'PopupLabel', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
toolbarCommands,
'TooltipLabel', type)

# fallbacks
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, menuCommands, 'Label', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, contextCommands, 'Label', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file), descriptions, toolbarCommands, 'Label', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions, menuCommands,
'Label', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
contextCommands,
'Label', type)
descriptions = collectCommandsFromXCU(os.path.join(dir, file),
descriptions,
toolbarCommands,
'Label', type)

# output the unocommands.js
f = open(onlineDir + '/loleaflet/src/unocommands.js', 'w', encoding='utf-8')
f = open(onlineDir + '/loleaflet/src/unocommands.js', 'w',
encoding='utf-8')
f.write('''// Don't modify, generated using unocommands.py
var unoCommandsArray = {\n''')
Expand All @@ -266,7 +302,8 @@ def writeUnocommandsJS(onlineDir, lofficeDir, menuCommands, contextCommands, too
for type in sorted(descriptions[key].keys()):
f.write(type + ':{')
for menuType in sorted(descriptions[key][type].keys()):
f.write(menuType + ":_('" + descriptions[key][type][menuType] + "'),")
f.write(menuType + ":_('" + descriptions[key][type][menuType]
+ "'),")
f.write('},')
f.write('},\n')

Expand Down Expand Up @@ -312,11 +349,13 @@ def writeUnocommandsJS(onlineDir, lofficeDir, menuCommands, contextCommands, too

return descriptions


# Read the uno commands present in the unocommands.js for checking
def parseUnocommandsJS(onlineDir):
strings = {}

f = open(onlineDir + '/loleaflet/src/unocommands.js', 'r', encoding='utf-8')
f = open(onlineDir + '/loleaflet/src/unocommands.js', 'r',
encoding='utf-8')
readingCommands = False
for line in f:
m = re.match(r"\t([^:]*):.*", line)
Expand All @@ -329,19 +368,22 @@ def parseUnocommandsJS(onlineDir):

return strings


# Generate translation JSONs for the .uno: commands
def writeTranslations(onlineDir, translationsDir, strings):
keys = set(strings.keys())

dir = translationsDir + '/source/'
for lang in os.listdir(dir):
poFile = dir + lang + '/officecfg/registry/data/org/openoffice/Office/UI.po'
poFile = dir + lang
+ '/officecfg/registry/data/org/openoffice/Office/UI.po'
if not os.path.isfile(poFile):
continue

sys.stderr.write('Generating ' + lang + '...\n')

po = polib.pofile(poFile, autodetect_encoding=False, encoding="utf-8", wrapwidth=-1)
po = polib.pofile(poFile, autodetect_encoding=False,
encoding="utf-8", wrapwidth=-1)

translations = {}
for entry in po.translated_entries():
Expand All @@ -353,7 +395,8 @@ def writeTranslations(onlineDir, translationsDir, strings):
if text == entry.msgid:
translations[entry.msgid] = entry.msgstr

f = open(onlineDir + '/loleaflet/l10n/uno/' + lang + '.json', 'w', encoding='utf-8')
f = open(onlineDir + '/loleaflet/l10n/uno/' +
lang + '.json', 'w', encoding='utf-8')
f.write('{\n')

writeComma = False
Expand All @@ -362,10 +405,12 @@ def writeTranslations(onlineDir, translationsDir, strings):
f.write(',\n')
else:
writeComma = True
f.write('"' + key.replace('"','\\\"') + '":"' + translations[key].replace('"','\\\"') + '"')
f.write('"' + key.replace('"', '\\\"') + '":"' +
translations[key].replace('"', '\\\"') + '"')

f.write('\n}\n')


if __name__ == "__main__":
if len(sys.argv) < 2:
usageAndExit()
Expand Down Expand Up @@ -407,11 +452,13 @@ def writeTranslations(onlineDir, translationsDir, strings):
parsed = parseUnocommandsJS(onlineDir)
processedCommands = set(parsed.keys())
else:
written = writeUnocommandsJS(onlineDir, lofficeDir, menuCommands, contextCommands, toolbarCommands)
written = writeUnocommandsJS(onlineDir, lofficeDir, menuCommands,
contextCommands, toolbarCommands)
processedCommands = set(written.keys())

# check that we have translations for everything
dif = (menuCommands | contextCommands | toolbarCommands) - processedCommands

if len(dif) > 0:
sys.stderr.write("ERROR: The following commands are not covered in unocommands.js, run scripts/unocommands.py --update:\n\n.uno:" + '\n.uno:'.join(dif) + "\n\n")
exit(1)
Expand Down

0 comments on commit e9ea83e

Please sign in to comment.