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.
glossary-push/pull for zanata-cli (#120)
* feat(glossary): Implement glossary command in zanta-cli https://zanata.atlassian.net/browse/ZNTA-44 * Update comments and docs
- Loading branch information
Alex Eng
authored
Jul 1, 2016
1 parent
064725e
commit e1e3064
Showing
15 changed files
with
327 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
To delete glossary entry in Zanata, the command-line client's `glossary-delete` command can be used. | ||
|
||
```bash | ||
zanata-cli glossary-delete --id 1005 | ||
``` | ||
|
||
This command will: | ||
|
||
1. Look up the server config from `zanata.xml`. | ||
2. Delete glossary entry with id `1005` | ||
|
||
To delete all glossary in Zanata | ||
|
||
```bash | ||
zanata-cli glossary-delete --all | ||
``` | ||
|
||
To see all options available for `glossary-delete` option: | ||
```bash | ||
zanata-cli help glossary-delete | ||
``` |
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,30 @@ | ||
To download glossary entries from Zanata, the command-line client's `glossary-pull` command can be used. | ||
|
||
```bash | ||
zanata-cli glossary-pull | ||
``` | ||
|
||
This command will: | ||
|
||
1. look up the server config from `zanata.xml`. | ||
2. Download all glossary entries in server | ||
3. The file will be in `.csv` format. (default) | ||
|
||
To download in different format, use the `--file-type` options (csv or po). | ||
For example: | ||
|
||
```bash | ||
zanata-cli glossary-pull --file-type po | ||
``` | ||
|
||
To download only specific locales for `--file-type po`, use the ` --trans-lang` options. | ||
For example to download `de` and `fr` locales only: | ||
|
||
```bash | ||
zanata-cli glossary-pull --file-type po --trans-lang de,fr | ||
``` | ||
|
||
To see all options available for `glossary-pull` option: | ||
```bash | ||
zanata-cli help glossary-pull | ||
``` |
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,23 @@ | ||
To push glossary entries to Zanata, the command-line client's `glossary-push` command can be used. | ||
The source language of the glossary file should be in `en-US` | ||
|
||
```bash | ||
zanata-cli glossary-push --file glossary.csv | ||
``` | ||
|
||
This command will: | ||
|
||
1. Look up the server config from `zanata.xml`. | ||
2. Push glossary.csv file to Zanata. | ||
|
||
To push a po file, `--trans-lang` option will be needed. | ||
For example: | ||
|
||
```bash | ||
zanata-cli glossary-push --file german-glossary.po --trans-lang de | ||
``` | ||
|
||
To see all options available for `glossary-push` option: | ||
```bash | ||
zanata-cli help glossary-push | ||
``` |
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
30 changes: 30 additions & 0 deletions
30
...nt-commands/src/main/java/org/zanata/client/commands/ConfigurableGlossaryOptionsImpl.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,30 @@ | ||
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 abstract class ConfigurableGlossaryOptionsImpl extends ConfigurableOptionsImpl | ||
implements ConfigurableGlossaryOptions { | ||
|
||
/** | ||
* Configuration file for Zanata client. | ||
*/ | ||
private File config = new File("zanata.xml"); | ||
|
||
@Override | ||
public File getConfig() { | ||
return config; | ||
} | ||
|
||
@Option(name = "--config", metaVar = "FILENAME", | ||
usage = "Configuration file, eg zanata.xml", | ||
required = false) | ||
public void setConfig(File config) { | ||
this.config = config; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...s/src/main/java/org/zanata/client/commands/glossary/delete/GlossaryDeleteOptionsImpl.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,49 @@ | ||
package org.zanata.client.commands.glossary.delete; | ||
|
||
import org.kohsuke.args4j.Option; | ||
import org.zanata.client.commands.ConfigurableGlossaryOptionsImpl; | ||
import org.zanata.client.commands.ZanataCommand; | ||
|
||
public class GlossaryDeleteOptionsImpl extends ConfigurableGlossaryOptionsImpl | ||
implements GlossaryDeleteOptions { | ||
|
||
private String id; | ||
private boolean allGlossary = false; | ||
|
||
@Override | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public boolean getAllGlossary() { | ||
return allGlossary; | ||
} | ||
|
||
@Option(name = "--id", metaVar = "ID", | ||
usage = "id of a glossary entry to delete.") | ||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
@Option(name = "--all", metaVar = "ALL", | ||
usage = "Delete entire glossaries from the server. Default: false") | ||
public void setAllGlossary(boolean allGlossary) { | ||
this.allGlossary = allGlossary; | ||
} | ||
|
||
@Override | ||
public ZanataCommand initCommand() { | ||
return new GlossaryDeleteCommand(this); | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return "glossary-delete"; | ||
} | ||
|
||
@Override | ||
public String getCommandDescription() { | ||
return "Delete glossary entries in Zanata"; | ||
} | ||
} |
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
1 change: 0 additions & 1 deletion
1
...-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
60 changes: 60 additions & 0 deletions
60
...mands/src/main/java/org/zanata/client/commands/glossary/pull/GlossaryPullOptionsImpl.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,60 @@ | ||
package org.zanata.client.commands.glossary.pull; | ||
|
||
|
||
import org.kohsuke.args4j.Option; | ||
import org.zanata.client.commands.ConfigurableGlossaryOptionsImpl; | ||
import org.zanata.client.commands.ZanataCommand; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
/** | ||
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a> | ||
*/ | ||
public class GlossaryPullOptionsImpl extends ConfigurableGlossaryOptionsImpl | ||
implements GlossaryPullOptions { | ||
|
||
private String fileType; | ||
private String[] transLang; | ||
|
||
@Option(name = "--file-type", metaVar = "(CSV or PO)", | ||
usage = "File type to be downloaded.\n" + | ||
"csv (default) - csv file format with comma separated\n" + | ||
"po - a zip file of po files on available locales") | ||
public void setFileType(String fileType) { | ||
this.fileType = fileType; | ||
} | ||
|
||
@Option(name = "--trans-lang", metaVar = "LOCALE1,LOCALE2", | ||
usage = "Translation languages to pull from Zanata.\nLeave empty for all available languages.") | ||
public void setTransLang(String transLang) { | ||
this.transLang = transLang.split(","); | ||
} | ||
|
||
@Override | ||
public String getFileType() { | ||
return fileType; | ||
} | ||
|
||
@Override | ||
public ImmutableList<String> getTransLang() { | ||
if (transLang != null) { | ||
return ImmutableList.copyOf(transLang); | ||
} | ||
return ImmutableList.of(); | ||
} | ||
|
||
@Override | ||
public ZanataCommand initCommand() { | ||
return new GlossaryPullCommand(this); | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return "glossary-pull"; | ||
} | ||
|
||
@Override | ||
public String getCommandDescription() { | ||
return "Pull glossary file from Zanata"; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -38,12 +38,13 @@ | |
import org.zanata.adapter.glossary.GlossaryPoReader; | ||
import org.zanata.client.commands.ConfigurableCommand; | ||
import org.zanata.client.commands.OptionsUtil; | ||
import org.zanata.client.config.LocaleMapping; | ||
import org.zanata.common.LocaleId; | ||
import org.zanata.rest.client.GlossaryClient; | ||
import org.zanata.rest.client.RestClientFactory; | ||
import org.zanata.rest.dto.GlossaryEntry; | ||
|
||
import static org.zanata.client.commands.glossary.push.GlossaryPushOptions.DEFAULT_SOURCE_LANG; | ||
|
||
/** | ||
* | ||
* @author Alex Eng <a href="mailto:[email protected]">[email protected]</a> | ||
|
@@ -54,8 +55,8 @@ public class GlossaryPushCommand extends | |
private static final Logger log = LoggerFactory | ||
.getLogger(GlossaryPushCommand.class); | ||
|
||
private static final Map<String, AbstractGlossaryPushReader> glossaryReaders = | ||
new HashMap<String, AbstractGlossaryPushReader>(); | ||
private static final Map<String, AbstractGlossaryPushReader> | ||
glossaryReaders = new HashMap<String, AbstractGlossaryPushReader>(); | ||
private final GlossaryClient client; | ||
|
||
public GlossaryPushCommand(GlossaryPushOptions opts, | ||
|
@@ -67,8 +68,10 @@ public GlossaryPushCommand(GlossaryPushOptions opts, | |
public GlossaryPushCommand(GlossaryPushOptions opts) { | ||
this(opts, OptionsUtil.createClientFactory(opts)); | ||
|
||
LocaleId srcLocaleId = new LocaleId(getOpts().getSourceLang()); | ||
LocaleId transLocaleId = new LocaleId(getOpts().getTransLang()); | ||
LocaleId srcLocaleId = new LocaleId(DEFAULT_SOURCE_LANG); | ||
String transLang = getOpts().getTransLang(); | ||
LocaleId transLocaleId = StringUtils.isNotBlank(transLang) | ||
? new LocaleId(transLang) : null; | ||
glossaryReaders.put("po", new GlossaryPoReader( | ||
srcLocaleId, transLocaleId, getOpts().getBatchSize())); | ||
glossaryReaders | ||
|
@@ -86,40 +89,39 @@ private AbstractGlossaryPushReader getReader(String fileExtension) { | |
|
||
private String validateFileExtensionWithTransLang() throws RuntimeException { | ||
String fileExtension = | ||
FilenameUtils.getExtension(getOpts().getGlossaryFile() | ||
.getName()); | ||
|
||
if (StringUtils.isEmpty(getOpts().getTransLang())) { | ||
if (fileExtension.equals("po")) { | ||
throw new RuntimeException( | ||
"Option 'zanata.transLang' is required for this file type."); | ||
} | ||
FilenameUtils.getExtension(getOpts().getFile().getName()); | ||
|
||
if (fileExtension.equals("po") | ||
&& StringUtils.isBlank(getOpts().getTransLang())) { | ||
throw new RuntimeException( | ||
"Option '--trans-lang' is required for this file type."); | ||
} | ||
return fileExtension; | ||
} | ||
|
||
@Override | ||
public void run() throws Exception { | ||
|
||
log.info("Server: {}", getOpts().getUrl()); | ||
log.info("Username: {}", getOpts().getUsername()); | ||
log.info("Source language: {}", getOpts().getSourceLang()); | ||
log.info("Source language: {}", DEFAULT_SOURCE_LANG); | ||
log.info("Translation language: {}", getOpts().getTransLang()); | ||
log.info("Glossary file: {}", getOpts().getGlossaryFile()); | ||
log.info("Glossary file: {}", getOpts().getFile()); | ||
log.info("Batch size: {}", getOpts().getBatchSize()); | ||
|
||
File glossaryFile = getOpts().getGlossaryFile(); | ||
File glossaryFile = getOpts().getFile(); | ||
|
||
if (glossaryFile == null) { | ||
throw new RuntimeException( | ||
"Option '--file' is required."); | ||
} | ||
if (!glossaryFile.exists()) { | ||
throw new RuntimeException("File '" + glossaryFile | ||
+ "' does not exist - check glossaryFile option"); | ||
} | ||
|
||
if (getOpts().getSourceLang() == null || getOpts().getSourceLang().length() < 0) { | ||
throw new RuntimeException("Need to specify source language."); | ||
+ "' does not exist. Check '--file' option"); | ||
} | ||
|
||
if (getOpts().getBatchSize() <= 0) { | ||
throw new RuntimeException("Batch size needs to be 1 or more."); | ||
throw new RuntimeException("Option '--batch-size' needs to be 1 or more."); | ||
} | ||
|
||
String fileExtension = validateFileExtensionWithTransLang(); | ||
|
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
Oops, something went wrong.