From 13979735876a1bdfba41886dfcdbe8625a9124db Mon Sep 17 00:00:00 2001 From: Xiaozhe Yao Date: Thu, 13 Sep 2018 02:00:42 +0800 Subject: [PATCH] code format --- Makefile | 3 +++ cli/config.go | 22 ++++++++--------- cli/daemon.go | 22 +++++++++-------- cli/handler.go | 60 +++++++++++++++++++++++------------------------ cli/main.go | 2 +- cli/query.go | 4 ++-- cli/repository.go | 50 +++++++++++++++++++-------------------- cli/service.go | 47 ++++++++++++++++++------------------- cli/template.go | 16 ++++++------- docs/README.md | 15 +++++++++++- 10 files changed, 129 insertions(+), 112 deletions(-) diff --git a/Makefile b/Makefile index 86756bf24..ead9e5e7b 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ format: docs: cd docs && npm run docs:build +docs-dev: + cd docs && npm run docs:dev + package: python setup.py sdist bdist_wheel diff --git a/cli/config.go b/cli/config.go index 63f0ca1d6..3cdb67e89 100644 --- a/cli/config.go +++ b/cli/config.go @@ -1,39 +1,39 @@ package main -import( - "log" - "io/ioutil" - "path/filepath" +import ( + "bytes" "github.com/BurntSushi/toml" "github.com/mitchellh/go-homedir" - "bytes" + "io/ioutil" + "log" + "path/filepath" ) type cvpmConfig struct { - Local local `toml:"local"` + Local local `toml:"local"` Repositories []Repository `toml:repository` } type local struct { LocalFolder string - Pip string - Python string + Pip string + Python string } var apiURL = "http://192.168.1.12:8080/" -func readConfig () cvpmConfig { +func readConfig() cvpmConfig { var config cvpmConfig homepath, _ := homedir.Dir() configFile := filepath.Join(homepath, "cvpm", "config.toml") - if _, err := toml.DecodeFile(configFile, &config); err!=nil { + if _, err := toml.DecodeFile(configFile, &config); err != nil { log.Fatal(err) return config } return config } -func writeConfig (config cvpmConfig) { +func writeConfig(config cvpmConfig) { buf := new(bytes.Buffer) if err := toml.NewEncoder(buf).Encode(config); err != nil { log.Fatal(err) diff --git a/cli/daemon.go b/cli/daemon.go index 2590c5982..20b53a323 100644 --- a/cli/daemon.go +++ b/cli/daemon.go @@ -1,20 +1,22 @@ package main import ( - "github.com/gin-gonic/gin" "github.com/fatih/color" + "github.com/gin-gonic/gin" "net/http" ) const DaemonPort = "10590" + var RunningRepos []Repository + type RunRepoRequest struct { - Name string `json:name` + Name string `json:name` Vendor string `json:vendor` Solver string `json:solver` } -func PostRepoHandler (c *gin.Context) { +func PostRepoHandler(c *gin.Context) { var runRepoRequest RunRepoRequest c.BindJSON(&runRepoRequest) go runRepo(runRepoRequest.Vendor, runRepoRequest.Name, runRepoRequest.Solver) @@ -23,18 +25,18 @@ func PostRepoHandler (c *gin.Context) { }) } -func GetReposHandler (c *gin.Context) { +func GetReposHandler(c *gin.Context) { } -func runServer (port string) { +func runServer(port string) { color.Red("Initiating") - r := gin.Default() - r.GET("/status", func (c *gin.Context) { + r := gin.Default() + r.GET("/status", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ - "daemon":"running", + "daemon": "running", }) - }) + }) r.POST("/repo", PostRepoHandler) - r.Run("127.0.0.1:" + port) + r.Run("127.0.0.1:" + port) } diff --git a/cli/handler.go b/cli/handler.go index 8924be186..82f9ace82 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -1,19 +1,19 @@ package main import ( + "github.com/fatih/color" + "github.com/olekukonko/tablewriter" + "github.com/urfave/cli" "os" "strconv" "strings" - "github.com/olekukonko/tablewriter" - "github.com/urfave/cli" - "github.com/fatih/color" ) -func InstallHandler (c *cli.Context) { +func InstallHandler(c *cli.Context) { config := readConfig() localFolder := config.Local.LocalFolder remoteURL := c.Args().Get(0) - if (remoteURL == "cvpm:test") { + if remoteURL == "cvpm:test" { color.Cyan("Installing... Please wait patiently") pip([]string{"install", "--index-url", "https://test.pypi.org/simple/", "cvpm", "--user"}) return @@ -34,10 +34,10 @@ func InstallHandler (c *cli.Context) { GeneratingRunners(repoFolder) color.Cyan("Adding to Local Configuration") config.Repositories = addRepo(config.Repositories, repo) - writeConfig(config) + writeConfig(config) } -func listRepos (c *cli.Context) { +func listRepos(c *cli.Context) { config := readConfig() table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"No.", "Name", "Vendor", "Path"}) @@ -47,35 +47,35 @@ func listRepos (c *cli.Context) { table.Render() } -func DaemonHandler (c *cli.Context) { +func DaemonHandler(c *cli.Context) { params := c.Args().Get(0) switch params { - case "install": - InstallService() - case "uninstall": - UninstallService() - case "run": - runServer(DaemonPort) - default: - color.Red("Unsupported command") + case "install": + InstallService() + case "uninstall": + UninstallService() + case "run": + runServer(DaemonPort) + default: + color.Red("Unsupported command") } } -func RepoHandler (c *cli.Context) { +func RepoHandler(c *cli.Context) { taskParams := c.Args().Get(0) switch taskParams { - case "run": - solverstring := c.Args().Get(1) - runParams := strings.Split(solverstring, "/") - color.Cyan("Running " + runParams[0] + "/" + runParams[1] + "/" + runParams[2] ) - requestParams := map[string]string{ - "vendor": runParams[0], - "name": runParams[1], - "solver": runParams[2], - } - ClientPost("repo", requestParams) - // runRepo(runParams[0], runParams[1], runParams[2]) - default: - color.Red("Command Not Supported!") + case "run": + solverstring := c.Args().Get(1) + runParams := strings.Split(solverstring, "/") + color.Cyan("Running " + runParams[0] + "/" + runParams[1] + "/" + runParams[2]) + requestParams := map[string]string{ + "vendor": runParams[0], + "name": runParams[1], + "solver": runParams[2], + } + ClientPost("repo", requestParams) + // runRepo(runParams[0], runParams[1], runParams[2]) + default: + color.Red("Command Not Supported!") } } diff --git a/cli/main.go b/cli/main.go index 557046bfd..1151225ee 100644 --- a/cli/main.go +++ b/cli/main.go @@ -15,7 +15,7 @@ func main() { // sessionToken := getCache("session-token") var currentUser User // if sessionToken != "" { - // currentUser = User{"", "", sessionToken} + // currentUser = User{"", "", sessionToken} // currentUser.become() // } cvpm := cli.NewApp() diff --git a/cli/query.go b/cli/query.go index f6fb7007f..36199440a 100644 --- a/cli/query.go +++ b/cli/query.go @@ -1,14 +1,14 @@ package main import ( - "log" "github.com/levigross/grequests" + "log" ) func ClientPost(endpoint string, params map[string]string) { url := "http://127.0.0.1:10590/" + endpoint resp, err := grequests.Post(url, &grequests.RequestOptions{ - JSON: params, + JSON: params, IsAjax: true, }) if err != nil { diff --git a/cli/repository.go b/cli/repository.go index 8d0b724c7..2c8700a33 100644 --- a/cli/repository.go +++ b/cli/repository.go @@ -1,25 +1,25 @@ package main import ( - "os" + "fmt" + "github.com/BurntSushi/toml" + "github.com/fatih/color" + "gopkg.in/src-d/go-git.v4" + "io/ioutil" "log" + "os" "path/filepath" - "fmt" "strings" - "io/ioutil" - "gopkg.in/src-d/go-git.v4" - "github.com/fatih/color" - "github.com/BurntSushi/toml" ) type Repository struct { - Name string + Name string LocalFolder string - Vendor string + Vendor string } type solver struct { - Name string + Name string Class string } @@ -27,16 +27,16 @@ type solvers struct { Solvers []solver } -func readRepos () []Repository { +func readRepos() []Repository { configs := readConfig() repos := configs.Repositories return repos } -func addRepo (repos []Repository, repo Repository) []Repository { +func addRepo(repos []Repository, repo Repository) []Repository { alreadyInstalled := false for _, existed_repo := range repos { - if (repo.Name == existed_repo.Name && repo.Vendor == existed_repo.Vendor) { + if repo.Name == existed_repo.Name && repo.Vendor == existed_repo.Vendor { alreadyInstalled = true } } @@ -47,23 +47,23 @@ func addRepo (repos []Repository, repo Repository) []Repository { return repos } -func delRepo (repos []Repository, Vendor string, Name string) []Repository { +func delRepo(repos []Repository, Vendor string, Name string) []Repository { for i, repo := range repos { - if (repo.Name == Name && repo.Vendor == Vendor) { + if repo.Name == Name && repo.Vendor == Vendor { repos = append(repos[:i], repos[i+1:]...) } } return repos } -func runRepo (Vendor string, Name string, Solver string) { +func runRepo(Vendor string, Name string, Solver string) { repos := readRepos() existed := false for _, existed_repo := range repos { if existed_repo.Name == Name && existed_repo.Vendor == Vendor { files, _ := ioutil.ReadDir(existed_repo.LocalFolder) for _, file := range files { - if (file.Name() == "runner_" + Solver + ".py") { + if file.Name() == "runner_"+Solver+".py" { existed = true runfileFullPath := filepath.Join(existed_repo.LocalFolder, file.Name()) python([]string{runfileFullPath}) @@ -76,31 +76,31 @@ func runRepo (Vendor string, Name string, Solver string) { } } -func CloneFromGit (remoteURL string, targetFolder string) Repository { +func CloneFromGit(remoteURL string, targetFolder string) Repository { localFolderName := strings.Split(remoteURL, "/") vendorName := localFolderName[len(localFolderName)-2] repoName := localFolderName[len(localFolderName)-1] - localFolder := filepath.Join(targetFolder, vendorName,repoName) - color.Cyan("Cloning " + remoteURL + " into " +localFolder) + localFolder := filepath.Join(targetFolder, vendorName, repoName) + color.Cyan("Cloning " + remoteURL + " into " + localFolder) _, err := git.PlainClone(localFolder, false, &git.CloneOptions{ - URL: remoteURL, + URL: remoteURL, Progress: os.Stdout, }) if err != nil { fmt.Println(err) } - repo := Repository{Name:repoName, Vendor: vendorName, LocalFolder: localFolder} + repo := Repository{Name: repoName, Vendor: vendorName, LocalFolder: localFolder} return repo } -func InstallDependencies (localFolder string) { +func InstallDependencies(localFolder string) { pip([]string{"install", "-r", filepath.Join(localFolder, "requirements.txt"), "--user"}) } -func GeneratingRunners (localFolder string) { - var mySolvers solvers +func GeneratingRunners(localFolder string) { + var mySolvers solvers cvpmFile := filepath.Join(localFolder, "cvpm.toml") - if _, err := toml.DecodeFile(cvpmFile, &mySolvers); err!=nil { + if _, err := toml.DecodeFile(cvpmFile, &mySolvers); err != nil { log.Fatal(err) } renderRunnerTpl(localFolder, mySolvers) diff --git a/cli/service.go b/cli/service.go index fb96a9813..c0ae0ae3a 100644 --- a/cli/service.go +++ b/cli/service.go @@ -1,63 +1,62 @@ package main import ( - "log" "github.com/kardianos/service" + "log" ) type sol struct { - } func (s *sol) Start(srv service.Service) error { go runServer(DaemonPort) - return nil + return nil } func (s *sol) Stop(srv service.Service) error { - return nil + return nil } -func getCVPMDConfig () *service.Config { - srvConf := &service.Config{ - Name: "cvpmd", +func getCVPMDConfig() *service.Config { + srvConf := &service.Config{ + Name: "cvpmd", DisplayName: "CVPM Daemon", Description: "Computer Vision Package Manager[Daemon]", - Arguments: []string{"daemon", "run"}, - } + Arguments: []string{"daemon", "run"}, + } return srvConf } -func InstallService () { +func InstallService() { srvConfig := getCVPMDConfig() dae := &sol{} - s, err := service.New(dae, srvConfig) - if err != nil { + s, err := service.New(dae, srvConfig) + if err != nil { log.Fatal(err) - } + } err = s.Install() - if err != nil { + if err != nil { log.Fatal(err) - } + } err = s.Start() if err != nil { log.Fatal(err) } } -func UninstallService () { +func UninstallService() { srvConfig := getCVPMDConfig() - dae := &sol{} + dae := &sol{} s, err := service.New(dae, srvConfig) - if err != nil { + if err != nil { log.Fatal(err) - } + } err = s.Stop() - if err != nil { + if err != nil { log.Fatal(err) - } - err = s.Uninstall() - if err != nil { + } + err = s.Uninstall() + if err != nil { log.Fatal(err) - } + } } diff --git a/cli/template.go b/cli/template.go index e1a7f2dcf..1905b7ac5 100644 --- a/cli/template.go +++ b/cli/template.go @@ -1,15 +1,15 @@ package main import ( - "log" + "github.com/flosch/pongo2" "io/ioutil" - "path/filepath" + "log" "net/http" + "path/filepath" "strings" - "github.com/flosch/pongo2" ) -func _getRunnerTpl () string { +func _getRunnerTpl() string { var runnerTpl = "https://tpl.cvtron.xyz/runners/runner.tpl" resp, err := http.Get(runnerTpl) body, err := ioutil.ReadAll(resp.Body) @@ -19,7 +19,7 @@ func _getRunnerTpl () string { return string(body) } -func renderRunnerTpl (localFolder string, mysolvers solvers) { +func renderRunnerTpl(localFolder string, mysolvers solvers) { tpl, err := pongo2.FromString(_getRunnerTpl()) if err != nil { log.Fatal(err) @@ -29,14 +29,14 @@ func renderRunnerTpl (localFolder string, mysolvers solvers) { } } -func writeRunner (tpl *pongo2.Template, localFolder string, Solver solver) { +func writeRunner(tpl *pongo2.Template, localFolder string, Solver solver) { filename := "runner_" + Solver.Name + ".py" fileFullPath := filepath.Join(localFolder, filename) tplContext := strings.Split(Solver.Class, "/") - out, err := tpl.Execute(pongo2.Context{"Package":tplContext[0],"Filename":tplContext[1], "Classname": tplContext[2]}) + out, err := tpl.Execute(pongo2.Context{"Package": tplContext[0], "Filename": tplContext[1], "Classname": tplContext[2]}) if err != nil { log.Fatal(err) - } + } err = ioutil.WriteFile(fileFullPath, []byte(out), 0644) if err != nil { log.Fatal(err) diff --git a/docs/README.md b/docs/README.md index ce523360e..912ca483a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1 +1,14 @@ -# CVPM +--- +home: true +heroImage: https://i.loli.net/2018/04/20/5ad9fa0eefc5a.png +actionText: Get Started → +actionLink: /guide/ +features: +- title: User Friendly + details: It is smaller and less complex than some other computer vision libraries. It provide an easy-to-use user interface to interacts with. +- title: Developer Friendly + details: Enjoy the dev experience of developing with CVTron. Adopting it to your own product is very soon and easy. Join our community will bring you more fun to develop with. +- title: Commercial Ready + details: Feel free to use it in your commercial product. Some companies have tested it in production environment already. We are also providing consulting service. +footer: Apache Licensed | Copyright © 2018-Present UnArxiv Foundation +--- \ No newline at end of file