def main(): parser = _create_parser() args = parser.parse_args() if args.command in SUBCOMMANDS: try: SUBCOMMANDS[args.command](args) except KeyboardInterrupt: aborted = errors.CptoolError("command aborted.") return aborted.get_message() except errors.Error as e: return e.get_message() else: parser.print_help() return errors.CptoolError("command {} not found.".format( args.command)).get_message()
def _scrape(self): url = io.ask("Problem URL: ", default="") if "codechef.com" in url: scraper = CodeChefScraper(url) else: raise errors.CptoolError("website is not supported.") name = scraper.parse_problem_name() code = scraper.parse_problem_code() self._problem_yaml.set_basic_info(name, code, url) level = scraper.parse_metadata_level() tags = scraper.parse_metadata_tags() (contest_name, contest_code, contest_url) = scraper.parse_metadata_contest() self._problem_yaml.set_metadata(level, tags, contest_name, contest_url, contest_code) statement = scraper.parse_statement() input = scraper.parse_input() output = scraper.parse_output() constraints = scraper.parse_constraints() self._problem_yaml.set_statement(statement, input, output, constraints) example_input, example_output, example_explanation = scraper.parse_example( ) self._problem_yaml.add_examples(example_input, example_output, example_explanation)
def create_problem_yaml(self, problem_dir: Path): problem_dir.mkdir(parents=True, exist_ok=True) problem_name = problem_dir.name self._problem_yaml = ProblemYaml() if not self._interactive: self._problem_yaml.set_basic_info(problem_name) else: if io.confirm( "Would you like to automatically extract problem information from URL?\n" " Supporting sites: CodeChef.\n" " (yes/no) [yes] ", default=True, ): self._scrape() else: self._manual(problem_name) io.line("Generated file") io.line() print(self._problem_yaml) io.line() if not io.confirm("Do you confirm generation? (yes/no) [yes] ", default=True): raise errors.CptoolError("command aborted.") self._problem_yaml.save(problem_dir / "problem.yaml")
def _retrieve_webpage(self, url): req = Request( url, headers={ "Accept": "*/*", "User-Agent": "curl/7.58.0" }, ) data = urlopen(req).read() if not data: raise errors.CptoolError("fail to retrieve webpage.") return data
def handle(self, path: str, interactive: bool): problem_dir = Path.cwd() / Path(path) problem_name = problem_dir.name if problem_dir.exists() and problem_dir.is_dir(): raise errors.CptoolError( "destination `{}` already exists.".format(problem_dir)) creator = Template(interactive) creator.create(problem_dir) print("Successfully created problem `{}` in `{}`".format( problem_name, problem_dir))
def _prepare_code_files(self, code_regexp: str): code_files = list( get_lang(file) for file in self.cptool.codes_dir.glob(code_regexp) if file.is_file() ) if not code_files: raise errors.CptoolError("cannot find any code file in `codes` directory.") self.cptool.compiled_codes_dir.mkdir(parents=True, exist_ok=True) for code in code_files: code.compile(self.cptool.compiled_codes_dir) print(f" {code.code_file}") return code_files