Skip to content

Commit

Permalink
Fix pushing empty msgctxt to server, extract pull command to a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesni committed May 9, 2012
1 parent c828191 commit 56134ee
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 128 deletions.
5 changes: 4 additions & 1 deletion zanataclient/publicanutil.py
Expand Up @@ -78,7 +78,10 @@ def create_txtflow(self, pofile):
else:
content = entry.msgid

extensions=[{'object-type':'comment', 'value': extracted_comment, 'space': 'preserve'}, {"object-type": "pot-entry-header", "context": context, "references": reflist, "extractedComment": '', "flags": flags}]
if entry.msgctxt:
extensions=[{'object-type':'comment', 'value': extracted_comment, 'space': 'preserve'}, {"object-type": "pot-entry-header", "context": context, "references": reflist, "extractedComment": '', "flags": flags}]
else:
extensions=[{'object-type':'comment', 'value': extracted_comment, 'space': 'preserve'}, {"object-type": "pot-entry-header", "references": reflist, "extractedComment": '', "flags": flags}]

if entry.msgid_plural:
textflow = {'id': textflowId, 'lang':'en-US', 'contents': content, 'plural':'true', 'extensions':extensions}
Expand Down
127 changes: 127 additions & 0 deletions zanataclient/pullcmd.py
@@ -0,0 +1,127 @@
# vim: set et sts=4 sw=4:
#
# Zanata Python Client
#
# Copyright (c) 2011 Jian Ni <jni@redhat.com>
# Copyright (c) 2011 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

import os
import sys
import string

from zanatalib.logger import Logger
from pushcmd import Push

log = Logger()

class GenericPull(Push):
def process_transdir(self, command_options, src_folder):
trans_folder = ""

if command_options.has_key('transdir'):
trans_folder = command_options['transdir'][0]['value']
elif src_folder:
trans_folder = src_folder
else:
trans_folder = os.getcwd()

return trans_folder

def create_outpath(self, command_options, output_folder):
if command_options.has_key('transdir'):
output = command_options['transdir'][0]['value']
elif output_folder:
output = output_folder
else:
output = os.getcwd()

if not os.path.isdir(output):
os.mkdir(output)

return output

def run(self, command_options, args, project_type):
dir_option = False
skeletons = True
filelist = []
output_folder = None
project_config = self.read_project_config(command_options)

if not project_config:
log.info("Can not find zanata.xml, please specify the path of zanata.xml")

url = self.process_url(project_config, command_options)
username, apikey = self.read_user_config(url, command_options)
self.get_version(url, command_options)

zanatacmd = self.generate_zanatacmd(url, username, apikey)

if command_options.has_key('disablesslcert'):
zanatacmd.disable_ssl_cert_validation()

#if file not specified, push all the files in pot folder to zanata server
project_id, iteration_id = zanatacmd.check_project(command_options, project_config)
log.info("Username: %s" % username)

lang_list = self.get_lang_list(command_options, project_config)

#list the files in project
try:
filelist = zanatacmd.get_file_list(project_id, iteration_id)
except Exception, e:
log.error(str(e))
sys.exit(1)

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

if project_type:
command_type = project_type
dir_option = True
elif command_options.has_key('project_type'):
command_type = command_options['project_type'][0]['value']
elif project_config['project_type']:
command_type = project_config['project_type']
else:
log.error("The project type is unknown")
sys.exit(1)

if dir_option:
#Keep dir option for publican/po pull
if command_options.has_key('dir'):
output_folder = command_options['dir'][0]['value']

if command_options.has_key('dstdir'):
output_folder = command_options['dstdir'][0]['value']
else:
#Disable dir option for generic pull command
if command_options.has_key('dir'):
log.warn("dir option is disabled in pull command, please use --transdir, or specify value in zanata.xml")

if command_options.has_key('dstdir'):
log.warn("dstdir option is changed to transdir option for generic pull command")
output_folder = command_options['dstdir'][0]['value']

if command_options.has_key('noskeletons'):
skeletons = False

outpath = self.create_outpath(command_options, output_folder)

zanatacmd.pull_command(locale_map, project_id, iteration_id, filelist, lang_list, outpath, command_type, skeletons)
128 changes: 3 additions & 125 deletions zanataclient/zanata.py
Expand Up @@ -44,6 +44,7 @@
from pushcmd import PoPush
from pushcmd import PublicanPush
from pushcmd import GenericPush
from pullcmd import GenericPull

log = Logger()

Expand Down Expand Up @@ -451,62 +452,6 @@ def generate_zanatacmd(url, username, apikey):
log.error("Please specify username and apikey in zanata.ini or with '--username' and '--apikey' options")
sys.exit(1)

def get_lang_list(command_options, project_config):
lang_list = []
if command_options.has_key('lang'):
lang_list = command_options['lang'][0]['value'].split(',')
elif project_config.has_key('locale_map'):
lang_list = project_config['locale_map'].keys()
else:
log.error("Please specify the language with '--lang' option or in zanata.xml")
sys.exit(1)

return lang_list

#################################
#
# Process source, trans and output folder
#
#################################

def process_transdir(command_options, src_folder):
trans_folder = ""

if command_options.has_key('transdir'):
trans_folder = command_options['transdir'][0]['value']
elif src_folder:
trans_folder = src_folder
else:
trans_folder = os.getcwd()

return trans_folder

def create_outpath(command_options, output_folder):
if command_options.has_key('transdir'):
output = command_options['transdir'][0]['value']
elif output_folder:
output = output_folder
else:
output = os.getcwd()

if not os.path.isdir(output):
os.mkdir(output)

return output

def check_plural_support(server_version):
if server_version == None:
return False

version = str(server_version.split('-')[0])
main_ver = version[:3]
version_number = string.atof(main_ver)

if version_number >= 1.6:
return True
else:
return False

#################################
#
# Command Handler
Expand Down Expand Up @@ -860,75 +805,8 @@ def pull(command_options, args, project_type = None):
--noskeletons: omit po files when translations not found
--disable-ssl-cert disable ssl certificate validation in 0.7.x python-httplib2
"""
dir_option = False
skeletons = True
filelist = []
output_folder = None
project_config = read_project_config(command_options)

if not project_config:
log.info("Can not find zanata.xml, please specify the path of zanata.xml")

url = process_url(project_config, command_options)
username, apikey = read_user_config(url, command_options)
get_version(url, command_options)

zanatacmd = generate_zanatacmd(url, username, apikey)

if command_options.has_key('disablesslcert'):
zanatacmd.disable_ssl_cert_validation()

#if file not specified, push all the files in pot folder to zanata server
project_id, iteration_id = zanatacmd.check_project(command_options, project_config)
log.info("Username: %s" % username)

lang_list = get_lang_list(command_options, project_config)

#list the files in project
try:
filelist = zanatacmd.get_file_list(project_id, iteration_id)
except Exception, e:
log.error(str(e))
sys.exit(1)

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

if project_type:
command_type = project_type
dir_option = True
elif command_options.has_key('project_type'):
command_type = command_options['project_type'][0]['value']
elif project_config['project_type']:
command_type = project_config['project_type']
else:
log.error("The project type is unknown")
sys.exit(1)

if dir_option:
#Keep dir option for publican/po pull
if command_options.has_key('dir'):
output_folder = command_options['dir'][0]['value']

if command_options.has_key('dstdir'):
output_folder = command_options['dstdir'][0]['value']
else:
#Disable dir option for generic pull command
if command_options.has_key('dir'):
log.warn("dir option is disabled in pull command, please use --transdir, or specify value in zanata.xml")

if command_options.has_key('dstdir'):
log.warn("dstdir option is changed to transdir option for generic pull command")
output_folder = command_options['dstdir'][0]['value']

if command_options.has_key('noskeletons'):
skeletons = False

outpath = create_outpath(command_options, output_folder)

zanatacmd.pull_command(locale_map, project_id, iteration_id, filelist, lang_list, outpath, command_type, skeletons)
command = GenericPull()
command.run(command_options, args, project_type)

def glossary_push(command_options, args):
"""
Expand Down
4 changes: 2 additions & 2 deletions zanataclient/zanatalib/docservice.py
Expand Up @@ -95,9 +95,9 @@ def commit_template(self, projectid, iterationid, resources, copytrans):
headers['X-Auth-Token'] = self.projects.apikey

ext = "?ext=gettext&ext=comment&copyTrans=%s"%copytrans

res, content = self.projects.restclient.request_post('/seam/resource/restv1/projects/p/%s/iterations/i/%s/r'%(projectid,iterationid), args=resources, headers=headers, extension=ext)

if res['status'] == '201' or res['status'] == '301':
return True
elif res['status'] == '401':
Expand Down

1 comment on commit 56134ee

@seanf
Copy link
Contributor

@seanf seanf commented on 56134ee May 10, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi James, it's often better to do each change in its own commit, eg 1. fix empty msgctxt, 2. extract pull command. Usually makes it easier to read the changes. Thanks!

It appears that the pull command is still treating empty msgctxt as no msgctxt.

"Note that an empty context string and an absent msgctxt line do not mean the same thing." - http://www.gnu.org/software/gettext/manual/html_node/PO-Files.html

Please sign in to comment.