(args: argparse.Namespace)
return True
:returns: whether the submission is succeeded or not.
:returns: whether the submission is succeeded or not.
: returns : whether the submission is succeeded or not .
def run(args: argparse.Namespace) -> bool:
    """
    :returns: whether the submission is succeeded or not.
    """

    # guess url
    history = onlinejudge_command.download_history.DownloadHistory()
    if args.file.parent.resolve() == pathlib.Path.cwd():
        guessed_urls = history.get(directory=pathlib.Path.cwd())
    else:
        logger.warning('cannot guess URL since the given file is not in the current directory')
        guessed_urls = []
    if args.url is None:
        if len(guessed_urls) == 1:
            args.url = guessed_urls[0]
            logger.info('guessed problem: %s', args.url)
        else:
            logger.error('failed to guess the URL to submit')
            logger.info('please manually specify URL as: $ oj submit URL FILE')
            return False

    # parse url
    problem = dispatch.problem_from_url(args.url)
    if problem is None:
        return False

    # read code
    with args.file.open('rb') as fh:
        code: bytes = fh.read()

    # report code
    logger.info('code (%d byte):', len(code))
    logger.info(utils.NO_HEADER + '%s', pretty_printers.make_pretty_large_file_content(code, limit=30, head=10, tail=10))

    with utils.new_session_with_our_user_agent(path=args.cookie) as sess:
        # check the login status
        try:
            is_logged_in = problem.get_service().is_logged_in(session=sess)
        except Exception as e:
            logger.exception('failed to check the login status: %s', e)
            return False
        else:
            if is_logged_in:
                logger.info('You are logged in.')
            else:
                logger.error('You are not logged in. Please run $ oj login %s', problem.get_url())
                return False

        # guess or select language ids
        language_dict: Dict[LanguageId, str] = {language.id: language.name for language in problem.get_available_languages(session=sess)}
        matched_lang_ids: Optional[List[str]] = None
        if args.language in language_dict:
            matched_lang_ids = [args.language]
        else:
            if args.guess:
                kwargs = {
                    'language_dict': language_dict,
                    'cxx_latest': args.guess_cxx_latest,
                    'cxx_compiler': args.guess_cxx_compiler,
                    'python_version': args.guess_python_version,
                    'python_interpreter': args.guess_python_interpreter,
                }
                matched_lang_ids = guess_lang_ids_of_file(args.file, code, **kwargs)
                if not matched_lang_ids:
                    logger.info('failed to guess languages from the file name')
                    matched_lang_ids = list(language_dict.keys())
                if args.language is not None:
                    logger.info('you can use `--no-guess` option if you want to do an unusual submission')
                    matched_lang_ids = select_ids_of_matched_languages(args.language.split(), matched_lang_ids, language_dict=language_dict)
            else:
                if args.language is None:
                    matched_lang_ids = None
                else:
                    matched_lang_ids = select_ids_of_matched_languages(args.language.split(), list(language_dict.keys()), language_dict=language_dict)

        # report selected language ids
        if matched_lang_ids is not None and len(matched_lang_ids) == 1:
            args.language = matched_lang_ids[0]
            logger.info('chosen language: %s (%s)', args.language, language_dict[LanguageId(args.language)])
        else:
            if matched_lang_ids is None:
                logger.error('language is unknown')
                logger.info('supported languages are:')
            elif len(matched_lang_ids) == 0:
                logger.error('no languages are matched')
                logger.info('supported languages are:')
            else:
                logger.error('Matched languages were not narrowed down to one.')
                logger.info('You have to choose:')
            for lang_id in sorted(matched_lang_ids or language_dict.keys()):
                logger.info(utils.NO_HEADER + '%s (%s)', lang_id, language_dict[LanguageId(lang_id)])
            return False

        # confirm
        guessed_unmatch = ([problem.get_url()] != guessed_urls)
        if guessed_unmatch:
            samples_text = ('samples of "{}'.format('", "'.join(guessed_urls)) if guessed_urls else 'no samples')
            logger.warning('the problem "%s" is specified to submit, but %s were downloaded in this directory. this may be mis-operation', problem.get_url(), samples_text)
        if args.wait:
            time.sleep(args.wait / 2)
        if not args.yes:
            if guessed_unmatch:
                problem_id = problem.get_url().rstrip('/').split('/')[-1].split('?')[-1]  # this is too ad-hoc
                key = problem_id[:3] + (problem_id[-1] if len(problem_id) >= 4 else '')
                sys.stdout.write('Are you sure? Please type "{}" '.format(key))
                sys.stdout.flush()
                c = sys.stdin.readline().rstrip()
                if c != key:
                    logger.info('terminated.')
                    return False
            else:
                sys.stdout.write('Are you sure? [y/N] ')
                sys.stdout.flush()
                c = sys.stdin.read(1)
                if c.lower() != 'y':
                    logger.info('terminated.')
                    return False
        if args.wait:
            time.sleep(args.wait / 2)

        # submit
        try:
            submission = problem.submit_code(code, language_id=LanguageId(args.language), session=sess)
        except NotLoggedInError:
            logger.info(utils.FAILURE + 'login required')
            return False
        except SubmissionError:
            logger.info(utils.FAILURE + 'submission failed')
            return False

        # show result
        if args.open:
            utils.webbrowser_register_explorer_exe()
            try:
                browser = webbrowser.get()
            except webbrowser.Error as e:
                logger.error('%s', e)
                logger.info('please set the $BROWSER envvar')
            else:
                logger.info('open the submission page with browser: %s', browser)
                browser.open_new_tab(submission.get_url())

    return True
def run ( args : argparse . Namespace ) -> bool : # guess url history = onlinejudge_command . download_history . DownloadHistory ( ) if args . file . parent . resolve ( ) == pathlib . Path . cwd ( ) : guessed_urls = history . get ( directory = pathlib . Path . cwd ( ) ) else : logger . warning ( 'cannot guess URL since the given file is not in the current directory' ) guessed_urls = [ ] if args . url is None : if len ( guessed_urls ) == 1 : args . url = guessed_urls [ 0 ] logger . info ( 'guessed problem: %s' , args . url ) else : logger . error ( 'failed to guess the URL to submit' ) logger . info ( 'please manually specify URL as: $ oj submit URL FILE' ) return False # parse url problem = dispatch . problem_from_url ( args . url ) if problem is None : return False # read code with args . file . open ( 'rb' ) as fh : code : bytes = fh . read ( ) # report code logger . info ( 'code (%d byte):' , len ( code ) ) logger . info ( utils . NO_HEADER + '%s' , pretty_printers . make_pretty_large_file_content ( code , limit = 30 , head = 10 , tail = 10 ) ) with utils . new_session_with_our_user_agent ( path = args . cookie ) as sess : # check the login status try : is_logged_in = problem . get_service ( ) . is_logged_in ( session = sess ) except Exception as e : logger . exception ( 'failed to check the login status: %s' , e ) return False else : if is_logged_in : logger . info ( 'You are logged in.' ) else : logger . error ( 'You are not logged in. Please run $ oj login %s' , problem . get_url ( ) ) return False # guess or select language ids language_dict : Dict [ LanguageId , str ] = { language . id : language . name for language in problem . get_available_languages ( session = sess ) } matched_lang_ids : Optional [ List [ str ] ] = None if args . language in language_dict : matched_lang_ids = [ args . language ] else : if args . guess : kwargs = { 'language_dict' : language_dict , 'cxx_latest' : args . guess_cxx_latest , 'cxx_compiler' : args . guess_cxx_compiler , 'python_version' : args . guess_python_version , 'python_interpreter' : args . guess_python_interpreter , } matched_lang_ids = guess_lang_ids_of_file ( args . file , code , * * kwargs ) if not matched_lang_ids : logger . info ( 'failed to guess languages from the file name' ) matched_lang_ids = list ( language_dict . keys ( ) ) if args . language is not None : logger . info ( 'you can use `--no-guess` option if you want to do an unusual submission' ) matched_lang_ids = select_ids_of_matched_languages ( args . language . split ( ) , matched_lang_ids , language_dict = language_dict ) else : if args . language is None : matched_lang_ids = None else : matched_lang_ids = select_ids_of_matched_languages ( args . language . split ( ) , list ( language_dict . keys ( ) ) , language_dict = language_dict ) # report selected language ids if matched_lang_ids is not None and len ( matched_lang_ids ) == 1 : args . language = matched_lang_ids [ 0 ] logger . info ( 'chosen language: %s (%s)' , args . language , language_dict [ LanguageId ( args . language ) ] ) else : if matched_lang_ids is None : logger . error ( 'language is unknown' ) logger . info ( 'supported languages are:' ) elif len ( matched_lang_ids ) == 0 : logger . error ( 'no languages are matched' ) logger . info ( 'supported languages are:' ) else : logger . error ( 'Matched languages were not narrowed down to one.' ) logger . info ( 'You have to choose:' ) for lang_id in sorted ( matched_lang_ids or language_dict . keys ( ) ) : logger . info ( utils . NO_HEADER + '%s (%s)' , lang_id , language_dict [ LanguageId ( lang_id ) ] ) return False # confirm guessed_unmatch = ( [ problem . get_url ( ) ] != guessed_urls ) if guessed_unmatch : samples_text = ( 'samples of "{}' . format ( '", "' . join ( guessed_urls ) ) if guessed_urls else 'no samples' ) logger . warning ( 'the problem "%s" is specified to submit, but %s were downloaded in this directory. this may be mis-operation' , problem . get_url ( ) , samples_text ) if args . wait : time . sleep ( args . wait / 2 ) if not args . yes : if guessed_unmatch : problem_id = problem . get_url ( ) . rstrip ( '/' ) . split ( '/' ) [ - 1 ] . split ( '?' ) [ - 1 ] # this is too ad-hoc key = problem_id [ : 3 ] + ( problem_id [ - 1 ] if len ( problem_id ) >= 4 else '' ) sys . stdout . write ( 'Are you sure? Please type "{}" ' . format ( key ) ) sys . stdout . flush ( ) c = sys . stdin . readline ( ) . rstrip ( ) if c != key : logger . info ( 'terminated.' ) return False else : sys . stdout . write ( 'Are you sure? [y/N] ' ) sys . stdout . flush ( ) c = sys . stdin . read ( 1 ) if c . lower ( ) != 'y' : logger . info ( 'terminated.' ) return False if args . wait : time . sleep ( args . wait / 2 ) # submit try : submission = problem . submit_code ( code , language_id = LanguageId ( args . language ) , session = sess ) except NotLoggedInError : logger . info ( utils . FAILURE + 'login required' ) return False except SubmissionError : logger . info ( utils . FAILURE + 'submission failed' ) return False # show result if args . open : utils . webbrowser_register_explorer_exe ( ) try : browser = webbrowser . get ( ) except webbrowser . Error as e : logger . error ( '%s' , e ) logger . info ( 'please set the $BROWSER envvar' ) else : logger . info ( 'open the submission page with browser: %s' , browser ) browser . open_new_tab ( submission . get_url ( ) ) return True