Skip to content

Commit

Permalink
#233: simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Jan 4, 2019
1 parent a2e5f36 commit fb83c14
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion onlinejudge/implementation/command/generate_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def generate_output(args: 'argparse.Namespace') -> None:
args.test = cutils.glob_with_format(args.directory, args.format) # by default
if args.ignore_backup:
args.test = cutils.drop_backup_or_hidden_files(args.test)
tests = cutils.construct_relationship_of_files(args.test, args.format)
tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format)
for name, it in sorted(tests.items()):
log.emit('')
log.info('%s', name)
Expand Down
2 changes: 1 addition & 1 deletion onlinejudge/implementation/command/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test(args: 'argparse.Namespace') -> None:
args.test = cutils.glob_with_format(args.directory, args.format) # by default
if args.ignore_backup:
args.test = cutils.drop_backup_or_hidden_files(args.test)
tests = cutils.construct_relationship_of_files(args.test, args.format)
tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format)
if args.error: # float mode
match = lambda a, b: compare_as_floats(a, b, args.error)
else:
Expand Down
18 changes: 4 additions & 14 deletions onlinejudge/implementation/command/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@ def glob_with_format(directory: pathlib.Path, format: str) -> List[pathlib.Path]
log.debug('testcase globbed: %s', path)
return paths

def counts_included_dirname(format: str) -> int:
count = 0 # type: int
for dirname in format.split('/'):
if dirname:
count += 1
return count

def match_with_format(format: str, path: pathlib.Path) -> Optional[Match[str]]:
def match_with_format(directory: pathlib.Path, format: str, path: pathlib.Path) -> Optional[Match[str]]:
table = {}
table['s'] = '(?P<name>.+)'
table['e'] = '(?P<ext>in|out)'
format_root_path = path # type: pathlib.Path
for i in range(counts_included_dirname(format)):
format_root_path = format_root_path.parent
pattern = re.compile('^' + str(format_root_path) + '/' + utils.parcentformat(format, table) + '$')
pattern = re.compile('^' + str(directory.resolve()) + '/' + utils.parcentformat(format, table) + '$')
return pattern.match(str(path.resolve()))

def path_from_format(directory: pathlib.Path, format: str, name: str, ext: str) -> pathlib.Path:
Expand All @@ -55,10 +45,10 @@ def drop_backup_or_hidden_files(paths: List[pathlib.Path]) -> List[pathlib.Path]
result += [ path ]
return result

def construct_relationship_of_files(paths: List[pathlib.Path], format: str) -> Dict[str, Dict[str, pathlib.Path]]:
def construct_relationship_of_files(paths: List[pathlib.Path], directory: pathlib.Path, format: str) -> Dict[str, Dict[str, pathlib.Path]]:
tests = collections.defaultdict(dict) # type: Dict[str, Dict[str, pathlib.Path]]
for path in paths:
m = match_with_format(format, path.resolve())
m = match_with_format(directory, format, path.resolve())
if not m:
log.error('unrecognizable file found: %s', path)
sys.exit(1)
Expand Down

0 comments on commit fb83c14

Please sign in to comment.