示例#1
0
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
示例#2
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)