Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Improve error messages related to dictionary setup (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorami Hisamoto authored Jun 2, 2020
1 parent 1a6649b commit 93a42ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 3 additions & 2 deletions sudachipy/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ def _command_link(args, print_usage):
try:
return set_default_dict_package(dict_package, output=output)
except ImportError:
print_usage()
print('{} not installed'.format(dict_package), file=sys.stderr)
print('Package `{0}` does not exist.\n'
'You may install it with a command `$ pip install {0}`'
.format(dict_package), file=sys.stderr)
exit(1)


Expand Down
21 changes: 11 additions & 10 deletions sudachipy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@
DEFAULT_SETTINGFILE = DEFAULT_SETTINGFILE.as_posix()


def unlink_default_dict_package(output):
def unlink_default_dict_package(output, verbose=True):
try:
dst_path = Path(import_module('sudachidict').__file__).parent
except ImportError:
print('sudachidict not exists', file=output)
if verbose:
print('Package `sudachidict` does not exist.', file=output)
return

if dst_path.is_symlink():
print('unlinking sudachidict', file=output)
dst_path.unlink()
print('sudachidict unlinked', file=output)
if verbose:
print('Removed the package symbolic link `sudachidict`.', file=output)
if dst_path.exists():
raise IOError('unlink failed (directory exists)')
raise IOError('Unlink failed (The `sudachidict` directory exists and it is not a symbolic link).')


def set_default_dict_package(dict_package, output):
unlink_default_dict_package(output)
unlink_default_dict_package(output, verbose=False)

src_path = Path(import_module(dict_package).__file__).parent
dst_path = src_path.parent / 'sudachidict'
dst_path.symlink_to(src_path)
print('default dict package = {}'.format(dict_package), file=output)
print('Set the default dictionary to `{}`.'.format(dict_package), file=output)

return dst_path

Expand All @@ -57,15 +58,15 @@ def create_default_link_for_sudachidict_core(output):
try:
import_module('sudachidict_core')
except ImportError:
raise KeyError('`systemDict` must be specified if `SudachiDict_core` not installed')
raise KeyError('You need to specify `systemDict` in the config when `sudachidict_core` is not installed.')
try:
import_module('sudachidict_full')
raise KeyError('Multiple packages of `SudachiDict_*` installed. Set default dict with link command.')
raise KeyError('Multiple dictionaries installed. Set the default with `link -t` command.')
except ImportError:
pass
try:
import_module('sudachidict_small')
raise KeyError('Multiple packages of `SudachiDict_*` installed. Set default dict with link command.')
raise KeyError('Multiple dictionaries installed. Set the default with `link -t` command.')
except ImportError:
pass
dict_path = set_default_dict_package('sudachidict_core', output=output)
Expand Down

0 comments on commit 93a42ec

Please sign in to comment.