This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(glossary export): glossary pull (#117)
* 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
Showing
16 changed files
with
328 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...client-commands/src/main/java/org/zanata/client/commands/ConfigurableGlossaryOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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()); | ||
|
4 changes: 2 additions & 2 deletions
4
...mands/src/main/java/org/zanata/client/commands/glossary/delete/GlossaryDeleteOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...-commands/src/main/java/org/zanata/client/commands/glossary/pull/GlossaryPullCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...-commands/src/main/java/org/zanata/client/commands/glossary/pull/GlossaryPullOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -71,5 +70,4 @@ public GlossaryDeleteCommand initCommand() { | |
public String getCommandName() { | ||
return "glossary-delete"; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
zanata-maven-plugin/src/main/java/org/zanata/maven/GlossaryMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.