-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added command to download tree images from iTOL
- Loading branch information
1 parent
7735080
commit 28edb1d
Showing
9 changed files
with
221 additions
and
50 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,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var dltreeid string | ||
var dlformat string | ||
var dloutput string | ||
|
||
// dlimageCmd represents the download command | ||
var dlimageCmd = &cobra.Command{ | ||
Use: "dlimage", | ||
Short: "Download a tree image from from a server", | ||
Long: `Download a tree image from a server`, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(dlimageCmd) | ||
dlimageCmd.PersistentFlags().StringVarP(&dltreeid, "tree-id", "i", "", "Tree id to download") | ||
dlimageCmd.PersistentFlags().StringVarP(&dlformat, "format", "f", "pdf", "Image format (png, pdf, eps, svg)") | ||
dlimageCmd.PersistentFlags().StringVarP(&dloutput, "output", "o", "", "Image output file") | ||
} |
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,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
|
||
"github.com/fredericlemoine/gotree/download" | ||
"github.com/fredericlemoine/gotree/io" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var dlconfig string | ||
|
||
// dlitolCmd represents the dlitol command | ||
var dlitolCmd = &cobra.Command{ | ||
Use: "itol", | ||
Short: "Download a tree image from iTOL", | ||
Long: `Download a tree image from iTOL | ||
Option -c allows to give a configuration file having tab separated key value pairs, | ||
as defined here: | ||
http://itol.embl.de/help.cgi#bExOpt | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if dloutput == "" { | ||
io.ExitWithMessage(errors.New("Output file must be specified")) | ||
} | ||
if dltreeid == "" { | ||
io.ExitWithMessage(errors.New("Tree id must be specified")) | ||
} | ||
format := download.Format(dlformat) | ||
if format == download.IMGFORMAT_UNKNOWN { | ||
io.ExitWithMessage(errors.New("Unkown format: " + dlformat)) | ||
} | ||
var config map[string]string | ||
if dlconfig != "" { | ||
var err error | ||
config, err = readMapFile(dlconfig, false) | ||
if err != nil { | ||
io.ExitWithMessage(err) | ||
} | ||
} else { | ||
config = make(map[string]string) | ||
} | ||
|
||
dl := download.NewItolImageDownloader(config) | ||
b, err := dl.Download(dltreeid, format) | ||
if err != nil { | ||
io.ExitWithMessage(err) | ||
} | ||
ioutil.WriteFile(dloutput, b, 0644) | ||
}, | ||
} | ||
|
||
func init() { | ||
dlimageCmd.AddCommand(dlitolCmd) | ||
dlitolCmd.PersistentFlags().StringVarP(&dlconfig, "config", "c", "", "Itol image config file") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package download | ||
|
||
type ImageDownloader interface { | ||
Download(id string) ([]byte, error) // Down a tree image from a server | ||
} |
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,39 @@ | ||
package download | ||
|
||
const ( | ||
IMGFORMAT_SVG = 0 | ||
IMGFORMAT_PNG = 1 | ||
IMGFORMAT_EPS = 2 | ||
IMGFORMAT_PDF = 3 | ||
IMGFORMAT_UNKNOWN = 4 | ||
) | ||
|
||
func Format(format string) int { | ||
switch format { | ||
case "svg": | ||
return IMGFORMAT_SVG | ||
case "png": | ||
return IMGFORMAT_PNG | ||
case "eps": | ||
return IMGFORMAT_EPS | ||
case "pdf": | ||
return IMGFORMAT_PDF | ||
default: | ||
return IMGFORMAT_UNKNOWN | ||
} | ||
} | ||
|
||
func StrFormat(format int) string { | ||
switch format { | ||
case IMGFORMAT_SVG: | ||
return "svg" | ||
case IMGFORMAT_PNG: | ||
return "png" | ||
case IMGFORMAT_EPS: | ||
return "eps" | ||
case IMGFORMAT_PDF: | ||
return "pdf" | ||
default: | ||
return "unknown" | ||
} | ||
} |
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,50 @@ | ||
package download | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type ItolImageDownloader struct { | ||
config map[string]string | ||
} | ||
|
||
func NewItolImageDownloader(config map[string]string) *ItolImageDownloader { | ||
return &ItolImageDownloader{config} | ||
} | ||
|
||
// Down a tree image from ITOL | ||
func (d *ItolImageDownloader) Download(id string, format int) ([]byte, error) { | ||
posturl := "http://itol.embl.de/batch_downloader.cgi" | ||
var err error | ||
var postresponse *http.Response | ||
var responsebody []byte | ||
|
||
form := url.Values{} | ||
form.Add("tree", id) | ||
|
||
strformat := StrFormat(format) | ||
if strformat == "unknown" { | ||
return nil, errors.New("Output image format unknown") | ||
} | ||
form.Add("format", strformat) | ||
|
||
for k, v := range d.config { | ||
if k != "" && v != "" { | ||
form.Add(k, v) | ||
} | ||
} | ||
postresponse, err = http.PostForm(posturl, form) | ||
|
||
defer postresponse.Body.Close() | ||
if responsebody, err = ioutil.ReadAll(postresponse.Body); err != nil { | ||
return nil, err | ||
} | ||
|
||
if postresponse.Header.Get("Content-Type") == "text/html" { | ||
return nil, errors.New(string(responsebody)) | ||
} | ||
return responsebody, nil | ||
} |
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