Skip to content

Commit

Permalink
chore: wooda image mode (#39)
Browse files Browse the repository at this point in the history
* chore: wooda image mode

* fix: replace WriteField with CreateFormFile

* docs: minor
  • Loading branch information
Gornak40 authored Aug 10, 2024
1 parent 93f8f14 commit b5fc79a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,15 @@ This tool uses `problem.xml` for uploading all package content.

Useful for migration between `polygon.lksh.ru` and `polygon.codeforces.com`.

Designed to replace ~~legacy~~ [polygon-cli](https://github.com/kunyavskiy/polygon-cli) tool.
Designed designed as an alternative to [polygon-cli](https://github.com/kunyavskiy/polygon-cli).

**Ensure that the problem you are uploading the package into is empty.**

### Known issues

- If problem has testsets other than `tests`, you should create them manually, [issue](https://github.com/Codeforces/polygon-issue-tracking/issues/549);
- If problem is interactive, set `Is problem interactive` checkbox manually;
- If problem has statements resources, upload them manually;
- If problem has statement resources, upload them manually;
- If problem has custom input/output, set it manually;
- If problem has [FreeMaker](https://freemarker.apache.org) generator, it will expand;
- If problem has stresses, unpload them manually;
Expand Down Expand Up @@ -517,6 +517,7 @@ Matching uses natural order (`test.1.in`, `test.2.in`, ..., `test.10.in`, ...).
- `ok` - correct solution
- `incor` - incorrect solution
- `sample` - sample (append, not replace)
- `image` - statement resource (likely image)

### Flags
- `-i` - problem id (required)
Expand Down
1 change: 1 addition & 0 deletions cmd/wooda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
wooda.ModeSolutionCorrect,
wooda.ModeSolutionIncorrect,
wooda.ModeSample,
wooda.ModeImage,
}

parser := argparse.NewParser("wooda", "Upload problem files filtered by glob to Polygon.")
Expand Down
16 changes: 11 additions & 5 deletions polygon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ func buildRequest(method, link string, params url.Values) (*http.Request, error)
writer := multipart.NewWriter(buf)
for k, vals := range params {
for _, v := range vals {
if err := writer.WriteField(k, v); err != nil {
wr, err := writer.CreateFormFile(k, k)
if err != nil {
return nil, err
}
if _, err := wr.Write([]byte(v)); err != nil {
return nil, err
}
}
}
writer.Close()
if err := writer.Close(); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(context.TODO(), method, link, buf)
if err != nil {
return nil, err
Expand Down Expand Up @@ -438,11 +444,11 @@ func (p *Polygon) SaveTestGroup(tgr TestGroupRequest) error {
return err
}

func (p *Polygon) SaveStatementResource(pID int, name, file string) error {
link, params := p.buildURL("problem.saveScript", url.Values{
func (p *Polygon) SaveStatementResource(pID int, name, data string) error {
link, params := p.buildURL("problem.saveStatementResource", url.Values{
"problemId": []string{strconv.Itoa(pID)},
"name": []string{name},
"file": []string{file},
"file": []string{data},
})
_, err := p.makeQuery(http.MethodPost, link, params)

Expand Down
7 changes: 7 additions & 0 deletions polygon/wooda/wooda.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ModeSolutionCorrect = "ok"
ModeSolutionIncorrect = "incor"
ModeSample = "sample"
ModeImage = "image" // statement resource
)

var (
Expand Down Expand Up @@ -71,6 +72,8 @@ func (w *Wooda) Resolve(path string) error {
return w.resolveSolution(path, file, polygon.TagIncorrect)
case ModeSample:
return w.resolveTest(path, file, true)
case ModeImage:
return w.resolveImage(path, file)
default:
return fmt.Errorf("%w: %s", ErrUnknownMode, w.mode)
}
Expand Down Expand Up @@ -164,3 +167,7 @@ func (w *Wooda) resolveSolution(path, data string, tag polygon.SolutionTag) erro

return w.client.SaveSolution(sr)
}

func (w *Wooda) resolveImage(path, data string) error {
return w.client.SaveStatementResource(w.pID, filepath.Base(path), data)
}

0 comments on commit b5fc79a

Please sign in to comment.