Skip to content

Instantly share code, notes, and snippets.

@Tattoo
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tattoo/8781378 to your computer and use it in GitHub Desktop.
Save Tattoo/8781378 to your computer and use it in GitHub Desktop.
"""translate_lib.py
CLI tool for creating Robot Framework resource file which holds translations for all keywords from a given library.
"""
import codecs
import sys
from robot.libdoc import libdoc
from StringIO import StringIO
def get_kws(lib):
old = sys.stdout
sys.stdout = StringIO()
kws = []
try:
libdoc(lib, 'list')
kws = sys.stdout.getvalue().split('\n')
finally:
sys.stdout = old
return [kw for kw in kws if kw]
def _tidy(string)
words = [unicode(word, 'utf-8').lower().title()
for word in translation.split()]
return ' '.join(words)
def get_translations(kws):
translations = []
i = 1
total = len(kws)
for kw in kws:
translation = raw_input('(%d/%d) provide translation for "%s":\n' % (
i, total, kw))
translations.append(_tidy(translation))
i += 1
def write(translations):
while True:
file_name = raw_input('Please provide file to write to: ')
if file_name:
break
print 'Invalid file "%s"' % file_name
with codecs.open(file_name, 'w', 'utf-8') as f:
f.write('*** Keywords ***\n\n')
for kw, translation in translations:
f.write('%s\n [Arguments] @{varargs}\n %s\n\n' % (translation, kw))
while True:
cmd_or_lib = raw_input('Please provide library name (quit by typing "quit"): ')
if cmd_or_lib.strip().lower() == 'quit':
break
kws = get_kws(cmd_or_lib)
translations = get_translations(kws)
write(zip(kws, translations))
print '\nThank for using'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment