mirror of
https://github.com/explosion/spaCy.git
synced 2025-07-10 16:22:29 +03:00
Move sys_exit() function to util
This commit is contained in:
parent
ccd1a79988
commit
68c04fa897
|
@ -1,7 +1,6 @@
|
||||||
# coding: utf8
|
# coding: utf8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import sys
|
|
||||||
import pip
|
import pip
|
||||||
import plac
|
import plac
|
||||||
import requests
|
import requests
|
||||||
|
@ -30,14 +29,16 @@ def get_compatibility():
|
||||||
version = about.__version__
|
version = about.__version__
|
||||||
r = requests.get(about.__compatibility__)
|
r = requests.get(about.__compatibility__)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
exit("Couldn't fetch compatibility table. Please find the right model for "
|
util.sys_exit(
|
||||||
|
"Couldn't fetch compatibility table. Please find the right model for "
|
||||||
"your spaCy installation (v{v}), and download it manually:".format(v=version),
|
"your spaCy installation (v{v}), and download it manually:".format(v=version),
|
||||||
"python -m spacy.download [full model name + version] --direct",
|
"python -m spacy.download [full model name + version] --direct",
|
||||||
title="Server error ({c})".format(c=r.status_code))
|
title="Server error ({c})".format(c=r.status_code))
|
||||||
|
|
||||||
comp = r.json()['spacy']
|
comp = r.json()['spacy']
|
||||||
if version not in comp:
|
if version not in comp:
|
||||||
exit("No compatible models found for v{v} of spaCy.".format(v=version),
|
util.sys_exit(
|
||||||
|
"No compatible models found for v{v} of spaCy.".format(v=version),
|
||||||
title="Compatibility error")
|
title="Compatibility error")
|
||||||
else:
|
else:
|
||||||
return comp[version]
|
return comp[version]
|
||||||
|
@ -45,7 +46,8 @@ def get_compatibility():
|
||||||
|
|
||||||
def get_version(model, comp):
|
def get_version(model, comp):
|
||||||
if model not in comp:
|
if model not in comp:
|
||||||
exit("No compatible model found for "
|
util.sys_exit(
|
||||||
|
"No compatible model found for "
|
||||||
"{m} (spaCy v{v}).".format(m=model, v=about.__version__),
|
"{m} (spaCy v{v}).".format(m=model, v=about.__version__),
|
||||||
title="Compatibility error")
|
title="Compatibility error")
|
||||||
return comp[model][0]
|
return comp[model][0]
|
||||||
|
@ -59,21 +61,18 @@ def download_model(filename):
|
||||||
|
|
||||||
def check_error_depr(model):
|
def check_error_depr(model):
|
||||||
if not model:
|
if not model:
|
||||||
exit("python -m spacy.download [name or shortcut]",
|
util.sys_exit(
|
||||||
|
"python -m spacy.download [name or shortcut]",
|
||||||
title="Missing model name or shortcut")
|
title="Missing model name or shortcut")
|
||||||
|
|
||||||
if model == 'all':
|
if model == 'all':
|
||||||
exit("As of v1.7.0, the download all command is deprecated. Please "
|
util.sys_exit(
|
||||||
|
"As of v1.7.0, the download all command is deprecated. Please "
|
||||||
"download the models individually via spacy.download [model name] "
|
"download the models individually via spacy.download [model name] "
|
||||||
"or pip install. For more info on this, see the "
|
"or pip install. For more info on this, see the documentation: "
|
||||||
"documentation: {d}".format(d=about.__docs__),
|
"{d}".format(d=about.__docs__),
|
||||||
title="Deprecated command")
|
title="Deprecated command")
|
||||||
|
|
||||||
|
|
||||||
def exit(*messages, **kwargs):
|
|
||||||
util.print_msg(*messages, **kwargs)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
plac.call(download)
|
plac.call(download)
|
||||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import pip
|
import pip
|
||||||
import site
|
import site
|
||||||
import plac
|
import plac
|
||||||
|
@ -32,7 +31,8 @@ def link(origin, link_name, force=False):
|
||||||
|
|
||||||
def symlink(model_path, link_name, force):
|
def symlink(model_path, link_name, force):
|
||||||
if not os.path.isdir(model_path):
|
if not os.path.isdir(model_path):
|
||||||
exit("The data should be located in {p}".format(p=model_path),
|
util.sys_exit(
|
||||||
|
"The data should be located in {p}".format(p=model_path),
|
||||||
title="Can't locate model data")
|
title="Can't locate model data")
|
||||||
|
|
||||||
data_path = str(util.get_data_path())
|
data_path = str(util.get_data_path())
|
||||||
|
@ -42,18 +42,21 @@ def symlink(model_path, link_name, force):
|
||||||
if force:
|
if force:
|
||||||
os.unlink(link_path)
|
os.unlink(link_path)
|
||||||
else:
|
else:
|
||||||
exit("To overwrite an existing link, use the --force flag.",
|
util.sys_exit(
|
||||||
|
"To overwrite an existing link, use the --force flag.",
|
||||||
title="Link {l} already exists".format(l=link_name))
|
title="Link {l} already exists".format(l=link_name))
|
||||||
|
|
||||||
os.symlink(model_path, link_path)
|
os.symlink(model_path, link_path)
|
||||||
util.print_msg("{a} --> {b}".format(a=model_path, b=link_path),
|
util.print_msg(
|
||||||
|
"{a} --> {b}".format(a=model_path, b=link_path),
|
||||||
"You can now load the model via spacy.load('{l}').".format(l=link_name),
|
"You can now load the model via spacy.load('{l}').".format(l=link_name),
|
||||||
title="Linking successful")
|
title="Linking successful")
|
||||||
|
|
||||||
|
|
||||||
def get_meta(package_path, package):
|
def get_meta(package_path, package):
|
||||||
meta = util.parse_package_meta(package_path, package)
|
meta = util.parse_package_meta(package_path, package)
|
||||||
if not meta:
|
if not meta:
|
||||||
exit()
|
util.sys_exit()
|
||||||
return meta
|
return meta
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,11 +68,5 @@ def is_package(origin):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def exit(*messages, **kwargs):
|
|
||||||
if messages:
|
|
||||||
util.print_msg(*messages, **kwargs)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
plac.call(link)
|
plac.call(link)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import json
|
||||||
import re
|
import re
|
||||||
import os.path
|
import os.path
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import sys
|
||||||
|
|
||||||
import six
|
import six
|
||||||
import textwrap
|
import textwrap
|
||||||
|
@ -173,3 +174,13 @@ def _wrap_text(text):
|
||||||
wrap_width = wrap_max - len(indent)
|
wrap_width = wrap_max - len(indent)
|
||||||
return textwrap.fill(text, width=wrap_width, initial_indent=indent,
|
return textwrap.fill(text, width=wrap_width, initial_indent=indent,
|
||||||
subsequent_indent=indent)
|
subsequent_indent=indent)
|
||||||
|
|
||||||
|
|
||||||
|
def sys_exit(*messages, **kwargs):
|
||||||
|
"""Performs SystemExit. For modules used from the command line, like
|
||||||
|
download and link. To print message, use the same arguments as for
|
||||||
|
print_msg()."""
|
||||||
|
|
||||||
|
if messages:
|
||||||
|
print_msg(*messages, **kwargs)
|
||||||
|
sys.exit(0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user