Skip to content

Commit

Permalink
#rhbz736898, implement --push-trans-only option for publican project
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesni committed Apr 16, 2012
1 parent ff6dadf commit fca6688
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 58 deletions.
2 changes: 1 addition & 1 deletion zanataclient/command.py
@@ -1,4 +1,4 @@
#vim:set et sts=4 sw=4:
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
Expand Down
4 changes: 2 additions & 2 deletions zanataclient/optionsutil.py
@@ -1,5 +1,5 @@
#vim:set et sts=4 sw=4:
#
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
# Copyright (c) 2011 Jian Ni <jni@redhat.com>
Expand Down
2 changes: 1 addition & 1 deletion zanataclient/publicanutil.py
@@ -1,4 +1,4 @@
#vim:set et sts=4 sw=4:
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
Expand Down
27 changes: 23 additions & 4 deletions zanataclient/zanata.py
@@ -1,5 +1,5 @@
#vim:set et sts=4 sw=4:
#
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
# Copyright (c) 2011 Jian Ni <jni@redhat.com>
Expand Down Expand Up @@ -243,6 +243,12 @@
type='command',
long=['--noskeletons'],
),
],
'pushtransonly' : [
dict(
type='command',
long=['--push-trans-only'],
),
]
}

Expand Down Expand Up @@ -773,7 +779,7 @@ def create_version(command_options, args):
if args:
version_id = args[0]
else:
log.error("Please provide ITERATION_ID for creating iteration")
log.error("Please provide ITERATION_ID for creating version")
sys.exit(1)

if command_options.has_key('version_name'):
Expand Down Expand Up @@ -1149,7 +1155,20 @@ def push(command_options, args, project_type = None):
log.error("The project type is unknown")
sys.exit(1)

if tmlfolder == "":
if command_options.has_key('pushtransonly'):
transfolder = process_transdir(command_options, "")
merge = process_merge(command_options)
lang_list = get_lang_list(command_options, project_config)

if project_config.has_key('locale_map'):
locale_map = project_config['locale_map']
else:
locale_map = None

zanatacmd.push_trans_command(zanata, transfolder, project_id, iteration_id, lang_list, locale_map, command_type, merge)
sys.exit(0)

if tmlfolder == "":
tmlfolder = process_srcdir(command_options)

if not os.path.isdir(tmlfolder):
Expand Down
115 changes: 77 additions & 38 deletions zanataclient/zanatacmd.py
@@ -1,4 +1,4 @@
#vim:set et sts=4 sw=4:
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
Expand Down Expand Up @@ -57,21 +57,21 @@ def check_project(self, zanataclient, command_options, project_config):
else:
if project_config.has_key('project_id'):
project_id = project_config['project_id']

if command_options.has_key('project_version'):
iteration_id = command_options['project_version'][0]['value']
else:
if project_config.has_key('project_version'):
iteration_id = project_config['project_version']

if not project_id:
self.log.error("Please specify a valid project id in zanata.xml or with '--project-id' option")
sys.exit(1)

if not iteration_id:
self.log.error("Please specify a valid version id in zanata.xml or with '--project-version' option")
sys.exit(1)

self.log.info("Project: %s"%project_id)
self.log.info("Version: %s"%iteration_id)

Expand All @@ -95,7 +95,7 @@ def update_template(self, zanata, project_id, iteration_id, filename, body, copy
request_name = filename.replace('/', ',')
else:
request_name = filename

try:
result = zanata.documents.update_template(project_id, iteration_id, request_name, body, copytrans)
if result:
Expand All @@ -109,10 +109,10 @@ def commit_translation(self, zanata, project_id, iteration_id, request_name, pof
try:
result = zanata.documents.commit_translation(project_id, iteration_id, request_name, lang, body, merge)
if result:
self.log.warn(result)
self.log.warn(result)
self.log.info("Successfully pushed translation %s to the Zanata server"%pofile)
except UnAuthorizedException, e:
self.log.error(e.msg)
self.log.error(e.msg)
except BadRequestBodyException, e:
self.log.error(e.msg)
except UnexpectedStatusException, e:
Expand Down Expand Up @@ -169,13 +169,13 @@ def del_server_content(self, zanata, tmlfolder, project_id, iteration_id, push_f
except Exception, e:
self.log.error(str(e))
sys.exit(1)

def list_projects(self, zanata):
"""
List the information of all the project on the zanata server
"""
projects = zanata.projects.list()

if not projects:
self.log.error("There is no projects on the server or the server not working")
sys.exit(1)
Expand All @@ -186,7 +186,7 @@ def list_projects(self, zanata):
print ("Project Links: %s\n")%[{'href':link.href, 'type':link.type, 'rel':link.rel} for link in project.links]

return projects

def project_info(self, zanata, project_id):
"""
Retrieve the information of a project
Expand Down Expand Up @@ -258,8 +258,8 @@ def create_version(self, zanata, project_id, version_id, version_name=None, vers
self.log.error(e.msg)

def import_po(self, zanata, potfile, trans_folder, project_id, iteration_id, lang_list, locale_map, merge, project_type):
sub_dir = ""
publicanutil = PublicanUtility()
sub_dir = ""
publicanutil = PublicanUtility()
for item in lang_list:
if not locale_map:
lang = item
Expand All @@ -268,7 +268,7 @@ def import_po(self, zanata, potfile, trans_folder, project_id, iteration_id, lan
lang = locale_map[item]
else:
lang = item

if '/' in potfile:
request_name = potfile.replace('/', ',')
sub_dir = potfile[0:potfile.rfind('/')]
Expand All @@ -279,10 +279,10 @@ def import_po(self, zanata, potfile, trans_folder, project_id, iteration_id, lan

if project_type == "podir":
folder = os.path.join(trans_folder, item)

if not os.path.isdir(folder):
self.log.error("Can not find translation, please specify path of the translation folder")
continue
continue

pofile = os.path.join(folder, potfile+'.po')

Expand All @@ -292,20 +292,60 @@ def import_po(self, zanata, potfile, trans_folder, project_id, iteration_id, lan
path = os.path.join(trans_folder, sub_dir)
else:
path = trans_folder
pofile = os.path.join(path, filename)
pofile = os.path.join(path, filename)

if not os.path.isfile(pofile):
self.log.error("Can not find the %s translation for %s"%(item, potfile))
continue
body = publicanutil.pofile_to_json(pofile)

body = publicanutil.pofile_to_json(pofile)

if not body:
self.log.error("No content or all entries are obsolete in %s"%pofile)
sys.exit(1)

self.commit_translation(zanata, project_id, iteration_id, request_name, pofile, lang, body, merge)

def push_trans_command(self, zanata, transfolder, project_id, iteration_id, lang_list, locale_map, project_type, merge):
publicanutil = PublicanUtility()

for item in lang_list:
if not locale_map:
lang = item
else:
if item in locale_map:
lang = locale_map[item]
else:
lang = item

self.log.info("\nPushing %s translation for %s to server:"%(item, project_id))

if project_type == "podir":
folder = os.path.join(transfolder, item)

if not os.path.isdir(folder):
self.log.error("Can not find translation, please specify path of the translation folder")
continue

filelist = publicanutil.get_file_list(folder, ".po")

for filepath in filelist:
self.log.info("\nPushing the content of %s to server:"%filepath)
filename = publicanutil.strip_path(filepath, folder, '.po')

if '/' in filename:
request_name = filename.replace('/', ',')
else:
request_name = filename

body = publicanutil.pofile_to_json(filepath)

if not body:
self.log.error("No content or all entries are obsolete in %s"%filepath)
sys.exit(1)

self.commit_translation(zanata, project_id, iteration_id, request_name, filepath, lang, body, merge)

def push_command(self, zanata, file_list, srcfolder, project_id, iteration_id, copytrans, plural_support = False, import_param = None):
"""
Push the content of publican files to a Project version on Zanata server
Expand All @@ -318,16 +358,16 @@ def push_command(self, zanata, file_list, srcfolder, project_id, iteration_id, c
plural_exist = publicanutil.check_plural(filepath)
if plural_exist and not plural_support:
self.log.error("The plural is only supported in zanata server >= 1.6, this file will be ignored")
break
break
body, filename = publicanutil.potfile_to_json(filepath, srcfolder)

try:
result = zanata.documents.commit_template(project_id, iteration_id, body, copytrans)
if result:
self.log.info("Successfully pushed %s to the server"%filepath)
except UnAuthorizedException, e:
self.log.error(e.msg)
break
break
except BadRequestBodyException, e:
self.log.error(e.msg)
continue
Expand All @@ -336,14 +376,14 @@ def push_command(self, zanata, file_list, srcfolder, project_id, iteration_id, c
except UnexpectedStatusException, e:
self.log.error(e.msg)
continue

if import_param:
merge = import_param['merge']
lang_list = import_param['lang_list']
project_type = import_param['project_type']
transdir = import_param['transdir']
locale_map = import_param['locale_map']

self.import_po(zanata, filename, transdir, project_id, iteration_id, lang_list, locale_map, merge, project_type)

def pull_command(self, zanata, locale_map, project_id, iteration_id, filelist, lang_list, output, project_type, skeletons):
Expand Down Expand Up @@ -371,7 +411,7 @@ def pull_command(self, zanata, locale_map, project_id, iteration_id, filelist, l
self.log.info("\nFetching the content of %s from Zanata server: "%name)

try:
pot = zanata.documents.retrieve_template(project_id, iteration_id, request_name)
pot = zanata.documents.retrieve_template(project_id, iteration_id, request_name)
except UnAuthorizedException, e:
self.log.error(e.msg)
break
Expand All @@ -380,8 +420,8 @@ def pull_command(self, zanata, locale_map, project_id, iteration_id, filelist, l
break
except UnexpectedStatusException, e:
self.log.error(e.msg)
break
break

for item in lang_list:
if not locale_map:
lang = item
Expand All @@ -390,16 +430,16 @@ def pull_command(self, zanata, locale_map, project_id, iteration_id, filelist, l
lang = locale_map[item]
else:
lang = item

if project_type == "podir":
outpath = os.path.join(output, item)
if not os.path.isdir(outpath):
os.mkdir(outpath)
os.mkdir(outpath)
save_name = name
elif project_type == "gettext":
outpath = output
save_name = item.replace('-','_')

if folder:
subdirectory = os.path.join(outpath, folder)
if not os.path.isdir(subdirectory):
Expand All @@ -409,22 +449,21 @@ def pull_command(self, zanata, locale_map, project_id, iteration_id, filelist, l
pofile = os.path.join(outpath, save_name+'.po')

self.log.info("Retrieving %s translation from server:"%item)

try:
result = zanata.documents.retrieve_translation(lang, project_id, iteration_id, request_name, skeletons)
publicanutil.save_to_pofile(pofile, result, pot, skeletons, item, name)
except UnAuthorizedException, e:
self.log.error(e.msg)
self.log.error(e.msg)
break
except UnAvaliableResourceException, e:
self.log.info("There is no %s translation for %s"%(item, name))
except BadRequestBodyException, e:
self.log.error(e.msg)
continue
continue
except UnexpectedStatusException, e:
self.log.error(e.msg)


def poglossary_push(self, path, url, username, apikey, lang, sourcecomments):
publicanutil = PublicanUtility()
json = publicanutil.glossary_to_json(path, lang, sourcecomments)
Expand All @@ -447,7 +486,7 @@ def poglossary_push(self, path, url, username, apikey, lang, sourcecomments):

def csvglossary_push(self, path, url, username, apikey, locale_map, comments_header):
csvconverter = CSVConverter()
json = csvconverter.convert_to_json(path, locale_map, comments_header)
json = csvconverter.convert_to_json(path, locale_map, comments_header)
glossary = GlossaryService(url)

try:
Expand Down

0 comments on commit fca6688

Please sign in to comment.