From 68c04fa897b0b95f3617e4f54084e3146fc0de30 Mon Sep 17 00:00:00 2001 From: ines Date: Thu, 16 Mar 2017 17:08:58 +0100 Subject: [PATCH] Move sys_exit() function to util --- spacy/download.py | 43 +++++++++++++++++++++---------------------- spacy/link.py | 27 ++++++++++++--------------- spacy/util.py | 11 +++++++++++ 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/spacy/download.py b/spacy/download.py index 4aa94f128..f102072a9 100644 --- a/spacy/download.py +++ b/spacy/download.py @@ -1,7 +1,6 @@ # coding: utf8 from __future__ import unicode_literals -import sys import pip import plac import requests @@ -30,24 +29,27 @@ def get_compatibility(): version = about.__version__ r = requests.get(about.__compatibility__) if r.status_code != 200: - exit("Couldn't fetch compatibility table. Please find the right model for " - "your spaCy installation (v{v}), and download it manually:".format(v=version), - "python -m spacy.download [full model name + version] --direct", - title="Server error ({c})".format(c=r.status_code)) + 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), + "python -m spacy.download [full model name + version] --direct", + title="Server error ({c})".format(c=r.status_code)) comp = r.json()['spacy'] if version not in comp: - exit("No compatible models found for v{v} of spaCy.".format(v=version), - title="Compatibility error") + util.sys_exit( + "No compatible models found for v{v} of spaCy.".format(v=version), + title="Compatibility error") else: return comp[version] def get_version(model, comp): if model not in comp: - exit("No compatible model found for " - "{m} (spaCy v{v}).".format(m=model, v=about.__version__), - title="Compatibility error") + util.sys_exit( + "No compatible model found for " + "{m} (spaCy v{v}).".format(m=model, v=about.__version__), + title="Compatibility error") return comp[model][0] @@ -59,20 +61,17 @@ def download_model(filename): def check_error_depr(model): if not model: - exit("python -m spacy.download [name or shortcut]", - title="Missing model name or shortcut") + util.sys_exit( + "python -m spacy.download [name or shortcut]", + title="Missing model name or shortcut") if model == 'all': - exit("As of v1.7.0, the download all command is deprecated. Please " - "download the models individually via spacy.download [model name] " - "or pip install. For more info on this, see the " - "documentation: {d}".format(d=about.__docs__), - title="Deprecated command") - - -def exit(*messages, **kwargs): - util.print_msg(*messages, **kwargs) - sys.exit(0) + util.sys_exit( + "As of v1.7.0, the download all command is deprecated. Please " + "download the models individually via spacy.download [model name] " + "or pip install. For more info on this, see the documentation: " + "{d}".format(d=about.__docs__), + title="Deprecated command") if __name__ == '__main__': diff --git a/spacy/link.py b/spacy/link.py index 686e69197..2626861fd 100644 --- a/spacy/link.py +++ b/spacy/link.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import io import os -import sys import pip import site import plac @@ -32,8 +31,9 @@ def link(origin, link_name, force=False): def symlink(model_path, link_name, force): if not os.path.isdir(model_path): - exit("The data should be located in {p}".format(p=model_path), - title="Can't locate model data") + util.sys_exit( + "The data should be located in {p}".format(p=model_path), + title="Can't locate model data") data_path = str(util.get_data_path()) 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: os.unlink(link_path) else: - exit("To overwrite an existing link, use the --force flag.", - title="Link {l} already exists".format(l=link_name)) + util.sys_exit( + "To overwrite an existing link, use the --force flag.", + title="Link {l} already exists".format(l=link_name)) os.symlink(model_path, 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), - title="Linking successful") + 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), + title="Linking successful") + def get_meta(package_path, package): meta = util.parse_package_meta(package_path, package) if not meta: - exit() + util.sys_exit() return meta @@ -65,11 +68,5 @@ def is_package(origin): return False -def exit(*messages, **kwargs): - if messages: - util.print_msg(*messages, **kwargs) - sys.exit(0) - - if __name__ == '__main__': plac.call(link) diff --git a/spacy/util.py b/spacy/util.py index b5dbf882c..265b40d90 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -6,6 +6,7 @@ import json import re import os.path import pathlib +import sys import six import textwrap @@ -173,3 +174,13 @@ def _wrap_text(text): wrap_width = wrap_max - len(indent) return textwrap.fill(text, width=wrap_width, initial_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)