-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from online-judge-tools/feature/filter
Use external formatters
- Loading branch information
Showing
7 changed files
with
132 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import shlex | ||
import subprocess | ||
import sys | ||
import traceback | ||
from logging import getLogger | ||
from typing import * | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def _prepare_hook(*, data: Dict[str, Any]) -> None: | ||
data['hook'] = [] | ||
|
||
|
||
def register_filter_command(command: List[str], *, data: Dict[str, Any]) -> None: | ||
if not command: | ||
raise ValueError('command is empty') | ||
if data['hook']: | ||
raise RuntimeError('hook is already registered') | ||
data['hook'].extend(command) | ||
|
||
|
||
def _execute_hook(rendered: bytes, *, data: Dict[str, Any]) -> bytes: | ||
if not data['hook']: | ||
return rendered | ||
logger.info('execute filter command: $ %s', ' '.join(map(shlex.quote, data['hook']))) | ||
try: | ||
return subprocess.check_output(data['hook'], input=rendered, stderr=sys.stderr) | ||
except Exception as e: | ||
logger.exception(e) | ||
return b'\n'.join([ | ||
traceback.format_exc().encode(), | ||
b'', | ||
b'Generated code (before processed by the filter):', | ||
rendered, | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters