Skip to content

Commit

Permalink
#245: add clever confirmation using history to submit command
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Jan 20, 2019
1 parent d74709b commit d7526fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
33 changes: 24 additions & 9 deletions onlinejudge/implementation/command/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

def submit(args: 'argparse.Namespace') -> None:
# guess url
history = onlinejudge.implementation.download_history.DownloadHistory()
guessed_urls = history.get()
if args.url is None:
history = onlinejudge.implementation.download_history.DownloadHistory()
args.url = history.get()
if args.url is None:
if len(guessed_urls) == 1:
args.url = guessed_urls[0]
else:
log.error('failed to guess the URL to submit')
log.info('please manually specify URL as: $ oj submit URL FILE')
sys.exit(1)
Expand Down Expand Up @@ -96,16 +98,29 @@ def submit(args: 'argparse.Namespace') -> None:
sys.exit(1)

# confirm
guessed_unmatch = ([ problem.get_url() ] != guessed_urls)
if guessed_unmatch:
log.warning('the problem "%s" is specified to submit, but samples of "%s" were downloaded in this directory. this may be mis-operation', problem.get_url(), '", "'.join(guessed_urls))
if args.wait:
log.status('sleep(%.2f)', args.wait)
time.sleep(args.wait)
if not args.yes:
sys.stdout.write('Are you sure? [y/N] ')
sys.stdout.flush()
c = sys.stdin.read(1)
if c != 'y':
log.info('terminated.')
return
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:
log.info('terminated.')
return
else:
sys.stdout.write('Are you sure? [y/N] ')
sys.stdout.flush()
c = sys.stdin.read(1)
if c != 'y':
log.info('terminated.')
return

# submit
kwargs = {}
Expand Down
11 changes: 4 additions & 7 deletions onlinejudge/implementation/download_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def _flush(self) -> None:
fh.write(''.join(history_lines[: - len(history_lines) // 2]))
log.status('halve history at: %s', self.path)

def get(self, directory: pathlib.Path = pathlib.Path.cwd()) -> Optional[str]:
def get(self, directory: pathlib.Path = pathlib.Path.cwd()) -> List[str]:
if not self.path.exists():
return None
return []

log.status('read history from: %s', self.path)
found = set()
Expand All @@ -51,8 +51,5 @@ def get(self, directory: pathlib.Path = pathlib.Path.cwd()) -> Optional[str]:
continue
if pathlib.Path(data['directory']) == directory:
found.add(data['url'])
log.status('found urls in history: %s', ' '.join(found))
if len(found) == 1:
return next(iter(found))
else:
return None
log.status('found urls in history:\n%s', '\n'.join(found))
return list(found)

0 comments on commit d7526fa

Please sign in to comment.