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

Commit

Permalink
glossary-push/pull for zanata-cli (#120)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 45 deletions.
21 changes: 21 additions & 0 deletions docs/commands/glossary-delete.md
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
```
30 changes: 30 additions & 0 deletions docs/commands/glossary-pull.md
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
```
23 changes: 23 additions & 0 deletions docs/commands/glossary-push.md
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
```
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pages:
- init: 'commands/init.md'
- push: 'commands/push.md'
- pull: 'commands/pull.md'
- glossary-push: 'commands/glossary-push.md'
- glossary-pull: 'commands/glossary-pull.md'
- glossary-delete: 'commands/glossary-delete.md'
- Maven Plugin:
- Installation: 'maven-plugin/installation.md'
- Configuration: 'maven-plugin/configuration.md'
Expand Down
9 changes: 8 additions & 1 deletion zanata-cli/src/main/java/org/zanata/client/ZanataClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.zanata.client.commands.PutVersionOptionsImpl;
import org.zanata.client.commands.SystemExitStrategy;
import org.zanata.client.commands.ZanataCommand;
import org.zanata.client.commands.glossary.delete.GlossaryDeleteOptionsImpl;
import org.zanata.client.commands.glossary.pull.GlossaryPullOptionsImpl;
import org.zanata.client.commands.glossary.push.GlossaryPushOptionsImpl;
import org.zanata.client.commands.init.InitOptionsImpl;
import org.zanata.client.commands.pull.PullOptionsImpl;
import org.zanata.client.commands.push.PushOptionsImpl;
Expand Down Expand Up @@ -64,7 +67,11 @@ public class ZanataClient extends BasicOptionsImpl {
@SubCommand(name = "put-user", impl = PutUserOptionsImpl.class),
@SubCommand(name = "put-version",
impl = PutVersionOptionsImpl.class),
@SubCommand(name = "stats", impl = GetStatisticsOptionsImpl.class) })
@SubCommand(name = "stats", impl = GetStatisticsOptionsImpl.class),
@SubCommand(name = "glossary-delete", impl = GlossaryDeleteOptionsImpl.class),
@SubCommand(name = "glossary-push", impl = GlossaryPushOptionsImpl.class),
@SubCommand(name = "glossary-pull", impl = GlossaryPullOptionsImpl.class)})

// if this field name changes, change COMMAND_FIELD too
private Object command;
private static final String COMMAND_FIELD = "command";
Expand Down
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;
}

}
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;

Expand Down
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import org.zanata.client.commands.ConfigurableOptions;

public interface GlossaryPushOptions extends ConfigurableGlossaryOptions {
public File getGlossaryFile();
public String DEFAULT_SOURCE_LANG = "en-US";
public int DEFAULT_BATCH_SIZE = 50;

public String getSourceLang();
public File getFile();

public String getTransLang();

Expand Down
Loading

0 comments on commit e1e3064

Please sign in to comment.