Skip to content

Commit

Permalink
define filters when pulling a language
Browse files Browse the repository at this point in the history
  • Loading branch information
lukin0110 committed Jul 9, 2015
1 parent 1d64328 commit 42a2cd3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
38 changes: 36 additions & 2 deletions src/main/java/be/lukin/poeditor/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Config {
private String type;
private String terms;
private Map<String, String> translations = new HashMap<String, String>();
private Map<String, String> filters = new HashMap<String, String>(); // Filters are stored per language
private String[] tagsAll;
private String[] tagsNew;
private String[] tagsObsolete;
Expand All @@ -28,12 +29,22 @@ public Config(String apiKey, String projectId, String type, String terms, Map<St
this.translations = translations;
}

public Config(String apiKey, String projectId, String type, String terms, Map<String, String> translations, String[] tagsAll, String[] tagsNew, String[] tagsObsolete, String[] tagsPull) {
public Config(String apiKey,
String projectId,
String type,
String terms,
Map<String, String> translations,
Map<String, String> filters,
String[] tagsAll,
String[] tagsNew,
String[] tagsObsolete,
String[] tagsPull) {
this.apiKey = apiKey;
this.projectId = projectId;
this.type = type;
this.terms = terms;
this.translations = translations;
this.filters = filters;
this.tagsAll = tagsAll;
this.tagsNew = tagsNew;
this.tagsObsolete = tagsObsolete;
Expand Down Expand Up @@ -68,6 +79,17 @@ public Set<String> getLanguageKeys(){
return this.translations.keySet();
}

public FilterByEnum[] getFilters(String language){
String f = this.filters.get(language);

if(f != null){
String[] array = f.split(",");
return FilterByEnum.toArray(array);
}

return null;
}

public String[] getTagsAll() {
return tagsAll;
}
Expand Down Expand Up @@ -116,13 +138,25 @@ public static Config load(Properties properties) {
config.type = properties.getProperty("poeditor.type");
Objects.requireNonNull(config.type, "'poeditor.type' is required");
config.terms = properties.getProperty("poeditor.terms");

String f = properties.getProperty("poeditor.filters");
if(f != null){
//config.filters = f.split(",");
}

for(String key : properties.stringPropertyNames()){

// Fetch translation files
if(key.startsWith("poeditor.trans.")){
config.translations.put(key.substring(15), properties.getProperty(key));
}

// Fetch filters
if(key.startsWith("poeditor.filters.")){
config.filters.put(key.substring(17), properties.getProperty(key));
}
}

if(config.translations.size() == 0){
throw new RuntimeException("No languages detected, you should provide at least on 'poeditor.trans.<lang> key'");
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/be/lukin/poeditor/FilterByEnum.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package be.lukin.poeditor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* https://poeditor.com/api_reference/#export
*/
Expand Down Expand Up @@ -28,4 +32,24 @@ public static String[] toStringArray(FilterByEnum[] filters){

return null;
}

public static FilterByEnum[] toArray(String[] filters){
if(filters != null){
ArrayList<FilterByEnum> list = new ArrayList<FilterByEnum>();

for(int i=0; i<filters.length; i++){
if(filters[i] != null){
try {
list.add(FilterByEnum.valueOf(filters[i].trim().toUpperCase()));
} catch(IllegalArgumentException iae) {
// silently pass
}
}
}

return list.toArray(new FilterByEnum[list.size()]);
}

return null;
}
}
21 changes: 12 additions & 9 deletions src/main/java/be/lukin/poeditor/POEditorClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
Expand Down Expand Up @@ -245,19 +246,21 @@ public File export(String projectId, String language, FileTypeEnum fte, FilterBy
FileExport fe = service.export(Action.EXPORT, apiKey, projectId, language, fte.name().toLowerCase(), FilterByEnum.toStringArray(filters), tagsStr);

try {
if(exportFile != null){
if (exportFile != null) {
exportFile.createNewFile();
} else {
exportFile = File.createTempFile("poeditor-export-file-", ".tmp");
}

URL website = new URL(fe.item);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());

FileOutputStream fos = new FileOutputStream(exportFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
return exportFile;


if(fe.item != null) {
URL website = new URL(fe.item);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());

FileOutputStream fos = new FileOutputStream(exportFile);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
return exportFile;
}

} catch (FileNotFoundException e) {
LOG.log(Level.SEVERE, e.toString(), e);
} catch (IOException e) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/be/lukin/poeditor/tasks/PullTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import be.lukin.poeditor.Config;
import be.lukin.poeditor.FileTypeEnum;
import be.lukin.poeditor.FilterByEnum;
import be.lukin.poeditor.models.Project;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class PullTask extends BaseTask {

Expand All @@ -24,8 +27,11 @@ public void handle() {
String path = config.getLanguage(languageKey);
File exportFile = new File(current.toAbsolutePath().toString(), path);
exportFile.getParentFile().mkdirs();
File f = client.export(config.getProjectId(), languageKey, fte, null, exportFile, config.getTagsPull());
FilterByEnum[] filters = config.getFilters(languageKey);

File f = client.export(config.getProjectId(), languageKey, fte, filters, exportFile, config.getTagsPull());
System.out.println(" - Trans " + languageKey + ": " + path);
System.out.println(" - Filters " + languageKey + ": " + Arrays.toString(filters) + "\n");
}
}
}

0 comments on commit 42a2cd3

Please sign in to comment.