Skip to content

Commit

Permalink
why does the order matter?
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Nov 30, 2024
1 parent 1b441b8 commit cb2ed24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
5 changes: 4 additions & 1 deletion manga_translator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from argparse import Namespace

from manga_translator import Config
from manga_translator.args import parser, reparse
from .manga_translator import (
set_main_logger, load_dictionary, apply_dictionary,
)
Expand Down Expand Up @@ -85,7 +86,9 @@ async def dispatch(args: Namespace):
args = None
init_logging()
try:
args = parser.parse_args()
args, unknown = parser.parse_known_args()
parser.add_argument_group()
args = Namespace(**{**vars(args), **vars(reparse(unknown))})
set_log_level(level=logging.DEBUG if args.verbose else logging.INFO)
logger = get_logger(args.mode)
set_main_logger(logger)
Expand Down
39 changes: 27 additions & 12 deletions manga_translator/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,37 @@ def _format_action_invocation(self, action: argparse.Action) -> str:
else:
return super()._format_action_invocation(action)

def general_parser(g_parser):
g_parser.add_argument('-v', '--verbose', action='store_true',
help='Print debug info and save intermediate images in result folder')
g_parser.add_argument('--attempts', default=0, type=int,
help='Retry attempts on encountered error. -1 means infinite times.')
g_parser.add_argument('--ignore-errors', action='store_true', help='Skip image on encountered error.')
g_parser.add_argument('--model-dir', default=None, type=dir_path,
help='Model directory (by default ./models in project root)')
g = g_parser.add_mutually_exclusive_group()
g.add_argument('--use-gpu', action='store_true', help='Turn on/off gpu (auto switch between mps and cuda)')
g.add_argument('--use-gpu-limited', action='store_true', help='Turn on/off gpu (excluding offline translator)')
g_parser.add_argument('--font-path', default='', type=file_path, help='Path to font file')
g_parser.add_argument('--pre-dict', default=None, type=file_path, help='Path to the pre-translation dictionary file')
g_parser.add_argument('--post-dict', default=None, type=file_path,
help='Path to the post-translation dictionary file')
g_parser.add_argument('--kernel-size', default=3, type=int,
help='Set the convolution kernel size of the text erasure area to completely clean up text residues')



def reparse(arr: list):
p = argparse.ArgumentParser(prog='manga_translator',
description='Seamlessly translate mangas into a chosen language',
formatter_class=HelpFormatter)
general_parser(p)
return p.parse_args(arr)

parser = argparse.ArgumentParser(prog='manga_translator', description='Seamlessly translate mangas into a chosen language', formatter_class=HelpFormatter)
general_parser(parser)
subparsers = parser.add_subparsers(dest='mode', required=True, help='Mode of operation')

parser.add_argument('-v', '--verbose', action='store_true', help='Print debug info and save intermediate images in result folder')
parser.add_argument('--attempts', default=0, type=int, help='Retry attempts on encountered error. -1 means infinite times.')
parser.add_argument('--ignore-errors', action='store_true', help='Skip image on encountered error.')
parser.add_argument('--model-dir', default=None, type=dir_path, help='Model directory (by default ./models in project root)')
g = parser.add_mutually_exclusive_group()
g.add_argument('--use-gpu', action='store_true', help='Turn on/off gpu (auto switch between mps and cuda)')
g.add_argument('--use-gpu-limited', action='store_true', help='Turn on/off gpu (excluding offline translator)')
parser.add_argument('--font-path', default='', type=file_path, help='Path to font file')
parser.add_argument('--pre-dict', default=None, type=file_path, help='Path to the pre-translation dictionary file')
parser.add_argument('--post-dict', default=None, type=file_path, help='Path to the post-translation dictionary file')
parser.add_argument('--kernel-size', default=3, type=int, help='Set the convolution kernel size of the text erasure area to completely clean up text residues')

# Batch mode
parser_batch = subparsers.add_parser('local', help='Run in batch translation mode')
parser_batch.add_argument('-i', '--input', required=True, type=path, nargs='+', help='Path to an image folder')
Expand Down

0 comments on commit cb2ed24

Please sign in to comment.