def get_languages_list(user, step_id): if step_id is None: exit_util('Set current step.') step = get_step(user, step_id) block = step['steps'][0]['block'] if block['name'] != 'code': exit_util('Type step is not code.') languages = block['options']['code_templates'] return [lang for lang in languages]
def request(request_type, link, **kwargs): resp = None try: resp = requests.__dict__[request_type](link, **kwargs) except Exception as e: exit_util(e.args[0]) if resp.status_code >= 400: exit_util("Something went wrong.") return resp
def main(): """ Submitter 0.3 Tools for submitting solutions to stepik.org """ file_manager = FileManager() try: file_manager.create_dir(APP_FOLDER) except OSError: exit_util("Can't do anything. Not enough rights to edit folders.")
def text(): """ Display current step as text """ user = User() step_id = attempt_cache.get_step_id() if step_id is None: exit_util("Set current step") step = stepikclient.get_step(user, step_id) html = step['steps'][0]['block']['text'] click.secho(html2text.html2text(html))
def current(): """ Display the current step link """ lesson_id = attempt_cache.get_lesson_id() if lesson_id is None: exit_util("Set current step") step_position = attempt_cache.get_current_position() click.secho(STEPIK_HOST + "lesson/{}/step/{}".format(lesson_id, step_position), bold=True, fg="green")
def set_step(user, step_url): lesson_id = get_lesson_id(step_url) step_id = get_step_id(step_url) if lesson_id is None or not step_id: exit_util("The link is incorrect.") lesson = get_lesson(user, lesson_id) steps = None try: steps = lesson['lessons'][0]['steps'] except Exception: exit_util("Didn't receive such lesson.") if len(steps) < step_id or step_id < 1: exit_util("Too few steps in the lesson.") data = attempt_cache.get_data() data['steps'] = steps data['current_position'] = step_id attempt_cache.set_data(data) try: attempt_cache.set_lesson_id(lesson_id) except PermissionError: exit_util("You do not have permission to perform this action.") click.secho("Setting current step completed!", fg="green", bold=True)
def auth_user_password(user, password): try: data = { 'grant_type': user.grand_type, 'client_id': user.client_id, 'secret_id': user.secret, 'username': user.username, 'password': password } resp = requests.post('https://stepik.org/oauth2/token/', data) assert resp.status_code < 300 user.access_token = (resp.json())['access_token'] user.refresh_token = (resp.json())['refresh_token'] user.save() except AssertionError: exit_util("Check your authentication.")
def auth(): """ Authentication using username and password """ click.echo("Before using, register on https://stepik.org/") try: user = User() user.grand_type = GRAND_TYPE_PASSWORD user.client_id = CLIENT_ID user.secret = CLIENT_SECRET user.username = click.prompt(text="Enter your username") password = click.prompt(text="Enter your password", hide_input=True) auth_user_password(user, password) user.save() except Exception as e: exit_util("Enter right username and password" + str(e)) click.secho("Authentication was successfully!", fg="green", bold=True)
def submit_code(user, filename, lang=None, step_id=None): file_manager = FileManager() if not file_manager.is_local_file(filename): exit_util("File {} not found".format(filename)) code = "".join(open(filename).readlines()) current_time = time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime()) attempt_id = None if step_id is None: step_id = attempt_cache.get_step_id() try: attempt_id = get_attempt_id(user, step_id) except Exception: exit_util("Please, set the step link!") available_languages = get_languages_list(user, step_id) if lang in available_languages: language = lang else: language = LanguageManager().programming_language.get(os.path.splitext(filename)[1]) if language is None: exit_util("Doesn't correct extension for programme.") if language not in available_languages: exit_util("This language not available for current step.") submission = { "submission": { "time": current_time, "reply": { "code": code, "language": language }, "attempt": attempt_id } } submit = post_submit(user, json.dumps(submission)) evaluate(user, submit['submissions'][0]['id'])