def update_and_get_srms(self): provider = None try: provider = Provider(self.conf.database_path) provider.update_srm_list() finally: provider.close() return None
def _set_current_problem_solved(self, problem): if problem.solved: return provider = None try: provider = Provider(self._conf.database_path) provider.set_problem_solved(problem) finally: if provider: provider.close()
def init_srm_solution(search_term): folder_name = make_folder_name(search_term) search_term = replace_srm(search_term) print ('Initing environment for SRM "%s"...' % folder_name) provider = Provider(conf.database_path) srms = provider.find_srms(search_term) if len(srms) == 0: print ('Updating SRM list...') provider.update_srm_list() srms = provider.find_srms(search_term) if len(srms) == 0: print ('ERROR: Sorry, I couldn\'t find the SRM you\'re looking for.') return 1 srm = None if len(srms) == 1: srm = srms[0] else: while srm == None: for i in range(0, len(srms)): print (str(i) + ') "' + srms[i].title + '"') number = raw_input('More than one SRM was found. Which one were you looking for?') try: srm = srms[int(number)] except: pass srm.problems = provider.get_srm_problems(srm) if srm.problems == None or len(srm.problems) == 0: print ('Retreiving SRM data...') conn = create_conn_and_login() conn.fill_whole_srm(srm) conn.close() provider.insert_srm_data(srm) # create folder & associate folder os.makedirs(folder_name) provider.associate_folder(folder_name, srm) provider.close() # generate harness generator = HarnessGenerator(conf) generator.generate(os.path.join(folder_name, 'harness.cpp'), 'cpp', srm, 'solution.cpp') # create a solution.cpp file generator = SolutionGenerator(conf) generator.generate(os.path.join(folder_name, 'solution.cpp'), 'cpp', srm) # finito print ('Solution ready at %s/solution.cpp. Good luck.' % folder_name) return 0
def test_srm_solution(search_term, problem=None, run_all=False, data=None, build=True): if problem: print ('Testing problem "%s" of SRM "%s"...' % (problem, search_term)) else: print ('Testing SRM "%s"...' % search_term) if data == None and problem == None: print ('ERROR: Need to specify problem when using custom test data.') return 1 parsed_data = None if data: try: parsed_data = Executor.to_python_tuple(data) except: print ('ERROR: The custom data supplied could not be parsed.') raise return 1 folder_name = make_folder_name(search_term) provider = Provider(conf.database_path) srm = provider.srm_for_folder(folder_name) if srm == None: print ('ERROR: Couldn\'t figure out what SRM \'%s\' belongs to.' % folder_name) provider.close() return 1 if srm.problems == None or len(srm.problems) == 0: print ('Retreiving SRM data...') conn = create_conn_and_login() conn.fill_whole_srm(srm) conn.close() provider.insert_srm_data(srm) provider.close() problem_to_test = None if problem: for p in srm.problems: if p.type_name == problem: problem_to_test = p break if not problem_to_test: print ('ERROR: \'%s\' is not a valid problem name.' % problem) executor = Executor('cpp', srm, folder_name) if build or not executor.solution: print ('Building solution "%s":' % folder_name) if not executor.build(): print ('ERROR: Failed to build, aborting.') return 1 if data == None: num_failed = None if run_all: num_failed = executor.run_all_tests( \ problem=problem_to_test, onstart=display_testing_start, \ onfinish=display_test_results, onbeforerun=display_testcase_id) else: num_failed = executor.run_example_tests( \ problem=problem_to_test, onstart=display_testing_start, \ onfinish=display_test_results, onbeforerun=display_testcase_id) if num_failed == 0: print ('') print ('All tests pass') print ('') else: print ('') print ('%i tests failed' % num_failed) print ('') else: executor.run_test(problem_to_test, data, onstart=display_testing_start, onfinish=display_test_results) # finito return 0
def on_practice(self, action = None): # TODO: check for errors provider = None tcconn = None srm = None folder_name = None try: provider = Provider(self.conf.database_path) dlog = PickAnSRMDialog(self.win, provider) srm = dlog.run_and_return() if not srm: return srm.problems = provider.get_srm_problems(srm) provider.get_srm_test_cases(srm) if not srm.problems or len(srm.problems) == 0: tcconn = TCConnection() # TODO: username should be saved in configuration (username, pwd) = GetCredentialsDialog(self.win, tcconn).run_and_return() if not tcconn.logged_in: return try: # TODO: need to use future tcconn.fill_whole_srm(srm) except: display_error_msg(self.win, Errors.CANNOT_RETRIEVE) return provider.insert_srm_data(srm) # get folder name folder_name = provider.folder_for_srm(srm) finally: if tcconn: tcconn.close() if provider: provider.close() # make the srm folder folder_path = os.path.join(self.conf.srms_path, folder_name) if not os.path.exists(folder_path): os.makedirs(folder_path) # check if files exist, if they don't exist, generate them generator = SolutionGenerator(self.conf) paths = self._files_for_srm(srm, folder_path) for i in range(0, len(paths)): if not os.path.isfile(paths[i]): generator.generate(paths[i], 'cpp', srm, srm.problems[i]) # generate a harness harness_path = os.path.join(folder_path, 'harness.cpp') if not os.path.isfile(harness_path): generator = HarnessGenerator(self.conf) generator.generate(harness_path, 'cpp', srm) # create a new tab stumped_text = DEFAULT_STUMPED_TEXT if self.conf.stumped_text: stumped_text = self.conf.stumped_text ctx = PracticingContext(self._console, self.conf, 'cpp', srm, folder_path) tab = SrmTab(ctx, paths, stumped_text) tab.add_to_window(self.win, True)