Skip to content

Commit

Permalink
Merge pull request #257 from Pachicobue/escaping-format
Browse files Browse the repository at this point in the history
#256 regexpやglobにエスケープ処理を追加
  • Loading branch information
kmyk authored Jan 23, 2019
2 parents 7beb244 + 498f6b8 commit 3b661b0
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions onlinejudge/implementation/format_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,50 @@
import sys
from typing import Dict, List, Match, Optional


def glob_with_format(directory: pathlib.Path, format: str) -> List[pathlib.Path]:
table = {}
table['s'] = '*'
table['e'] = '*'
pattern = str(directory / utils.percentformat(format, table))
pattern = (glob.escape(str(directory)) + '/' +
utils.percentformat(glob.escape(format).replace('\\%', '%'), table))
paths = list(map(pathlib.Path, glob.glob(pattern)))
for path in paths:
log.debug('testcase globbed: %s', path)
return paths


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)'
pattern = re.compile('^' + str(directory.resolve()) + '/' + utils.percentformat(format, table) + '$')
pattern = re.compile('^' + re.escape(str(directory.resolve())) +
'/' + utils.percentformat(re.escape(format).replace('\\%', '%'), table) + '$')
return pattern.match(str(path.resolve()))


def path_from_format(directory: pathlib.Path, format: str, name: str, ext: str) -> pathlib.Path:
table = {}
table['s'] = name
table['e'] = ext
return directory / utils.percentformat(format, table)


def is_backup_or_hidden_file(path: pathlib.Path) -> bool:
basename = path.stem
return basename.endswith('~') or (basename.startswith('#') and basename.endswith('#')) or basename.startswith('.')


def drop_backup_or_hidden_files(paths: List[pathlib.Path]) -> List[pathlib.Path]:
result = [] # type: List[pathlib.Path]
for path in paths:
if is_backup_or_hidden_file(path):
log.warning('ignore a backup file: %s', path)
else:
result += [ path ]
result += [path]
return result


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:
Expand All @@ -53,7 +61,7 @@ def construct_relationship_of_files(paths: List[pathlib.Path], directory: pathli
log.error('unrecognizable file found: %s', path)
sys.exit(1)
name = m.groupdict()['name']
ext = m.groupdict()['ext']
ext = m.groupdict()['ext']
assert ext not in tests[name]
tests[name][ext] = path
for name in tests:
Expand Down

0 comments on commit 3b661b0

Please sign in to comment.