Skip to content

Commit

Permalink
Improvementts
Browse files Browse the repository at this point in the history
  • Loading branch information
Jatana committed Jan 29, 2019
1 parent e9854b8 commit bfab43b
Show file tree
Hide file tree
Showing 16 changed files with 1,269 additions and 9 deletions.
66 changes: 66 additions & 0 deletions ContestHandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import sublime, sublime_plugin
import os
from os import path
from .ContestHandlers import codeforces
from .test_manager import TestManagerCommand

handlers = [codeforces]

class ContestHandlerCommand(sublime_plugin.TextCommand):
def create_path(self, base, _path):
for i in range(len(_path)):
cur = path.join(base, path.join(*_path[:i + 1]))
if not path.exists(cur):
os.mkdir(cur)
return path.join(base, path.join(*_path))

def init_problems(self, handler, contest_id, base, pid=0):
inputs, outputs = handler.try_load_tests(contest_id, pid)
if inputs:
file_name = path.join(base, chr(ord('A') + pid) + '.cpp')
if not path.exists(file_name):
file = open(file_name, 'w')
file.close()
tests = []
for i in range(len(inputs)):
tests.append({
'test': inputs[i],
'correct_answers': [outputs[i]]
})
file = open(file_name + TestManagerCommand.TESTS_FILE_SUFFIX, 'w')
file.write(sublime.encode_value(tests, True))
file.close()
def go(self=self, handler=handler, contest_id=contest_id, base=base, pid=pid + 1):
self.init_problems(handler, contest_id, base, pid=pid)
sublime.set_timeout_async(go)

def init_contest(self, handler, contest_id):
base = self.create_path(path.expanduser('~'), ['contest_base', handler.get_basename(), handler.get_contest_info(contest_id)['title']])
sublime.run_command('new_window')
sublime.active_window().set_project_data({'folders': [{'path': base}]})
self.init_problems(handler, contest_id, base)

def try_init_contest(self, url, on_init):
for handler in handlers:
if handler.is_valid_url(url):
self.init_contest(handler, handler.extract_contest_id(url))
return

def run(self, edit, action=None):
if action == 'setup_contest':
def on_done(url, self=self):
self.try_init_contest(url, None)

def on_change(url):
pass

def on_cancel():
pass

self.view.window().show_input_panel(
'URL',
'https://codeforces.com/contest/1056/problem/C',
on_done,
on_change,
on_cancel
)
57 changes: 57 additions & 0 deletions ContestHandlers/codeforces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import urllib
import urllib.request
import re


test_start_input = '<div class="input"><div class="title">Input</div><pre>'
test_start_output = '<div class="output"><div class="title">Output</div><pre>'
test_end = '</pre></div>'

def try_load_tests(contest_id, task_id):
url = 'https://codeforces.com/contest/{contest_id}/problem/{task_id}'.format(
contest_id=contest_id,
task_id=chr(ord('A') + task_id)
)
req = urllib.request.urlopen(url)
text = req.read().decode()
inputs = []
outputs = []
for i in range(len(text)):
if text[i:i + len(test_start_input)] == test_start_input:
for j in range(i, len(text)):
if text[j:j + len(test_end)] == test_end:
inputs.append(text[i + len(test_start_input):j].replace('<br />', '\n').strip())
break
if text[i:i + len(test_start_output)] == test_start_output:
for j in range(i, len(text)):
if text[j:j + len(test_end)] == test_end:
outputs.append(text[i + len(test_start_output):j].replace('<br />', '\n').strip())
break

if (len(inputs) != len(outputs)) or (not inputs): return None, None
return inputs, outputs


contest_name_start = '<div style="padding: 4px 0 0 6px;font-size:1.4rem;position:relative;">'
contest_name_end = '<div style="position:absolute;right:0.25em;top:0.35em;">'

def get_contest_info(contest_id):
url = 'https://codeforces.com/contests/' + str(contest_id)
req = urllib.request.urlopen(url)
text = req.read().decode()
title = text[text.find(contest_name_start) + len(contest_name_start):text.find(contest_name_end)].strip()
return {
'title': title
}

def get_basename():
return 'CodeForces'

def is_valid_url(url):
return url.find('codeforces.com') != -1

def extract_contest_id(url):
match = re.search(r'\d+', url)
return match.group(0)

print(extract_contest_id('https://codeforces.com/contest/1078/problem/A'))
5 changes: 5 additions & 0 deletions ContestHandlers/codeforces.py:tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"test": ""
}
]
3 changes: 3 additions & 0 deletions ContestHandlers/handler_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import codeforces

handlers = [codeforces]
5 changes: 5 additions & 0 deletions ContestHandlers/handler_info.py:tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"test": ""
}
]
Loading

0 comments on commit bfab43b

Please sign in to comment.