Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
feat(glossary export): glossary pull (#117)
Browse files Browse the repository at this point in the history
* WIP: glossary export

* feat(glossary export): glossary-pull to download file

https://zanata.atlassian.net/browse/ZNTA-153

* Update regex and test

* Fix findbugs issue
  • Loading branch information
Alex Eng authored Jun 27, 2016
1 parent 57438ba commit 064725e
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.zanata.rest.service;

import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
Expand Down Expand Up @@ -53,6 +54,12 @@ public Response getEntries(LocaleId srcLocale,
return MockResourceUtil.notUsedByClient();
}

@Override
public Response downloadFile(@DefaultValue("csv") String fileType,
String locales) {
return MockResourceUtil.notUsedByClient();
}

@Override
public Response post(List<GlossaryEntry> glossaryEntries) {
GenericEntity<List<GlossaryEntry>> genericEntity =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.zanata.client.commands;

import java.io.File;

import org.kohsuke.args4j.Option;

/**
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a>
*/
public interface ConfigurableGlossaryOptions extends ConfigurableOptions {

public File getConfig();

@Option(name = "--config", metaVar = "FILENAME",
usage = "Configuration file, eg zanata.xml",
required = false)
public void setConfig(File config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public static void applyConfigFiles(ConfigurableOptions opts)
projectConfigFile);
}
}
} else if (opts instanceof ConfigurableGlossaryOptions) {
ConfigurableGlossaryOptions glossaryOpts =
(ConfigurableGlossaryOptions) opts;
File configFile = glossaryOpts.getConfig();
if (configFile != null) {
JAXBContext jc = JAXBContext.newInstance(ZanataConfig.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
if (configFile.exists()) {
log.info("Loading config from {}", configFile);
ZanataConfig projectConfig = (ZanataConfig) unmarshaller
.unmarshal(configFile);
applyBasicConfig(glossaryOpts, projectConfig);
} else {
log.warn("Config file '{}' not found; ignoring.",
configFile);
}
}
}
if (opts.getUserConfig() != null) {
if (opts.getUserConfig().exists()) {
Expand Down Expand Up @@ -132,12 +149,10 @@ public LocaleMapping apply(LocaleDetails input) {
*/
private static void applyProjectConfig(ConfigurableProjectOptions opts,
ZanataConfig config) {
applyBasicConfig(opts, config);
if (opts.getProj() == null) {
opts.setProj(config.getProject());
}
if (opts.getUrl() == null) {
opts.setUrl(config.getUrl());
}
if (opts.getProjectVersion() == null) {
opts.setProjectVersion(config.getProjectVersion());
}
Expand All @@ -156,6 +171,13 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts,
checkPotentialMistakesInRules(opts, new ConsoleInteractorImpl(opts));
}

private static void applyBasicConfig(ConfigurableOptions opts,
ZanataConfig config) {
if (opts.getUrl() == null) {
opts.setUrl(config.getUrl());
}
}

/**
* Will check potential mistakes in file mapping rules. Missing locale in
* the rule is considered invalid. Extra "{" and/or "}" will incur warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.ConfigurableCommand;
import org.zanata.client.commands.ConsoleInteractor;
import org.zanata.client.commands.ConsoleInteractorImpl;
import org.zanata.client.commands.OptionsUtil;
import org.zanata.rest.client.GlossaryClient;
import org.zanata.rest.client.RestClientFactory;

import static org.zanata.client.commands.ConsoleInteractor.DisplayMode.Question;

/**
*
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a>
Expand All @@ -54,10 +58,16 @@ public GlossaryDeleteCommand(GlossaryDeleteOptions opts) {
public void run() throws Exception {
log.info("Server: {}", getOpts().getUrl());
log.info("Username: {}", getOpts().getUsername());
log.info("Entry id to delete: {}", getOpts().getId());
if (!StringUtils.isEmpty(getOpts().getId())) {
log.info("Entry id to delete: {}", getOpts().getId());
}
log.info("Delete entire glossary?: {}", getOpts().getAllGlossary());

if (getOpts().getAllGlossary()) {
if (getOpts().isInteractiveMode()) {
ConsoleInteractor console = new ConsoleInteractorImpl(getOpts());
console.printf(Question, "\nAre you sure (y/n)? ");
console.expectYes();
}
glossaryClient.deleteAll();
} else if (!StringUtils.isEmpty(getOpts().getId())) {
glossaryClient.delete(getOpts().getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.zanata.client.commands.glossary.delete;

import org.zanata.client.commands.ConfigurableProjectOptions;
import org.zanata.client.commands.ConfigurableGlossaryOptions;

public interface GlossaryDeleteOptions extends ConfigurableProjectOptions {
public interface GlossaryDeleteOptions extends ConfigurableGlossaryOptions {
public String getId();

public boolean getAllGlossary();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2011, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This 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.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.client.commands.glossary.pull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.ConfigurableCommand;
import org.zanata.client.commands.OptionsUtil;
import org.zanata.rest.client.ClientUtil;
import org.zanata.rest.client.GlossaryClient;
import org.zanata.rest.client.RestClientFactory;
import org.zanata.util.PathUtil;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.ClientResponse;

/**
*
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a>
*
**/
public class GlossaryPullCommand extends
ConfigurableCommand<GlossaryPullOptions> {
private static final Logger log =
LoggerFactory.getLogger(GlossaryPullCommand.class);

private final GlossaryClient client;

public GlossaryPullCommand(GlossaryPullOptions opts,
RestClientFactory clientFactory) {
super(opts, clientFactory);
client = getClientFactory().getGlossaryClient();
}

public GlossaryPullCommand(GlossaryPullOptions opts) {
this(opts, OptionsUtil.createClientFactory(opts));
}

@Override
public void run() throws Exception {
String fileType = StringUtils.isEmpty(getOpts().getFileType()) ? "csv"
: getOpts().getFileType();
if (!fileType.equalsIgnoreCase("po")
&& !fileType.equalsIgnoreCase("csv")) {
throw new RuntimeException(
"Option 'zanata.fileType' is not valid. Please use 'csv' or 'po'");
}

log.info("Server: {}", getOpts().getUrl());
log.info("Username: {}", getOpts().getUsername());
log.info("File type: {}", fileType);
ImmutableList<String> transLang = getOpts().getTransLang();
if (transLang != null && !transLang.isEmpty()) {
log.info("Translation language: {}", Joiner.on(",").join(transLang));
}

log.info("pulling glossary from server");
ClientResponse response =
client.downloadFile(fileType, transLang);

if (response
.getClientResponseStatus() == ClientResponse.Status.NOT_FOUND) {
log.info("No glossary file in server");
return;
}

ClientUtil.checkResult(response);
InputStream glossaryFile = response.getEntity(InputStream.class);
if (glossaryFile == null) {
log.info("No glossary file in server");
return;
}
String fileName =
ClientUtil.getFileNameFromHeader(response.getHeaders());
File file = new File(fileName);
PathUtil.makeDirs(file.getParentFile());
try (OutputStream out = new FileOutputStream(file)) {
int read;
byte[] buffer = new byte[1024];
while ((read = glossaryFile.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
} finally {
glossaryFile.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.zanata.client.commands.glossary.pull;

import org.zanata.client.commands.ConfigurableGlossaryOptions;
import org.zanata.client.commands.ConfigurableOptions;

import com.google.common.collect.ImmutableList;

public interface GlossaryPullOptions extends ConfigurableGlossaryOptions {
public String getFileType();

public ImmutableList<String> getTransLang();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,18 @@ public GlossaryPushCommand(GlossaryPushOptions opts,
client = getClientFactory().getGlossaryClient();
}


public GlossaryPushCommand(GlossaryPushOptions opts) {
this(opts, OptionsUtil.createClientFactory(opts));

LocaleId srcLocaleId = new LocaleId(getOpts().getSourceLang());
LocaleId transLocaleId = new LocaleId(getOpts().getTransLang());
glossaryReaders.put("po", new GlossaryPoReader(
getLocaleFromMap(getOpts().getSourceLang()),
getLocaleFromMap(getOpts().getTransLang()), getOpts()
.getBatchSize()));
srcLocaleId, transLocaleId, getOpts().getBatchSize()));
glossaryReaders
.put("csv", new GlossaryCSVReader(getLocaleFromMap(
getOpts().getSourceLang()),
.put("csv", new GlossaryCSVReader(srcLocaleId,
getOpts().getBatchSize()));
}

private LocaleId getLocaleFromMap(String localLocale) {
if (getOpts() != null && getOpts().getLocaleMapList() != null
&& !getOpts().getLocaleMapList().isEmpty()) {
for (LocaleMapping loc : getOpts().getLocaleMapList()) {
if (loc.getLocalLocale().equals(localLocale)) {
return new LocaleId(loc.getLocale());
}
}
}
return new LocaleId(localLocale);
}

private AbstractGlossaryPushReader getReader(String fileExtension) {
AbstractGlossaryPushReader reader = glossaryReaders.get(fileExtension);
if (reader == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import java.io.File;

import org.zanata.client.commands.ConfigurableProjectOptions;
import org.zanata.client.commands.ConfigurableGlossaryOptions;
import org.zanata.client.commands.ConfigurableOptions;

public interface GlossaryPushOptions extends ConfigurableProjectOptions {
public interface GlossaryPushOptions extends ConfigurableGlossaryOptions {
public File getGlossaryFile();

public String getSourceLang();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.zanata.rest.dto.stats.ContainerTranslationStatistics;
import org.zanata.rest.dto.stats.TranslationStatistics;

import org.apache.commons.csv.CSVPrinter;

import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a>
*
**/
public class GlossaryDeleteMojo extends
ConfigurableProjectMojo<GlossaryDeleteOptions> implements
GlossaryDeleteOptions {
public class GlossaryDeleteMojo extends GlossaryMojo<GlossaryDeleteOptions>
implements GlossaryDeleteOptions {

/**
* id of glossary to delete
Expand Down Expand Up @@ -71,5 +70,4 @@ public GlossaryDeleteCommand initCommand() {
public String getCommandName() {
return "glossary-delete";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.zanata.maven;

import java.io.File;

import org.zanata.client.commands.ConfigurableGlossaryOptions;
import org.zanata.client.commands.ConfigurableOptions;

/**
* Base mojo for glossary commands.
*
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a>
*/
public abstract class GlossaryMojo<O extends ConfigurableOptions>
extends ConfigurableMojo<O> implements ConfigurableGlossaryOptions {
/**
* Zanata configuration file.
*
* @parameter expression="${zanata.config}"
* default-value="${basedir}/zanata.xml"
*/
private File config;

@Override
public File getConfig() {
return config;
}

@Override
public void setConfig(File config) {
this.config = config;
}
}
Loading

0 comments on commit 064725e

Please sign in to comment.