Move sys_exit() function to util

This commit is contained in:
ines 2017-03-16 17:08:58 +01:00
parent ccd1a79988
commit 68c04fa897
3 changed files with 44 additions and 37 deletions

View File

@ -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,24 +29,27 @@ 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(
"your spaCy installation (v{v}), and download it manually:".format(v=version), "Couldn't fetch compatibility table. Please find the right model for "
"python -m spacy.download [full model name + version] --direct", "your spaCy installation (v{v}), and download it manually:".format(v=version),
title="Server error ({c})".format(c=r.status_code)) "python -m spacy.download [full model name + version] --direct",
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(
title="Compatibility error") "No compatible models found for v{v} of spaCy.".format(v=version),
title="Compatibility error")
else: else:
return comp[version] return comp[version]
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(
"{m} (spaCy v{v}).".format(m=model, v=about.__version__), "No compatible model found for "
title="Compatibility error") "{m} (spaCy v{v}).".format(m=model, v=about.__version__),
title="Compatibility error")
return comp[model][0] return comp[model][0]
@ -59,20 +61,17 @@ 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(
title="Missing model name or shortcut") "python -m spacy.download [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(
"download the models individually via spacy.download [model name] " "As of v1.7.0, the download all command is deprecated. Please "
"or pip install. For more info on this, see the " "download the models individually via spacy.download [model name] "
"documentation: {d}".format(d=about.__docs__), "or pip install. For more info on this, see the documentation: "
title="Deprecated command") "{d}".format(d=about.__docs__),
title="Deprecated command")
def exit(*messages, **kwargs):
util.print_msg(*messages, **kwargs)
sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -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,8 +31,9 @@ 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(
title="Can't locate model data") "The data should be located in {p}".format(p=model_path),
title="Can't locate model data")
data_path = str(util.get_data_path()) data_path = str(util.get_data_path())
link_path = os.path.join(os.path.abspath(__file__ + '/../../'), data_path, link_name) link_path = os.path.join(os.path.abspath(__file__ + '/../../'), data_path, link_name)
@ -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(
title="Link {l} already exists".format(l=link_name)) "To overwrite an existing link, use the --force flag.",
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(
"You can now load the model via spacy.load('{l}').".format(l=link_name), "{a} --> {b}".format(a=model_path, b=link_path),
title="Linking successful") "You can now load the model via spacy.load('{l}').".format(l=link_name),
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)

View File

@ -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)