Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: wooda image mode #39

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}