Replace requests with urllib

This commit is contained in:
ines 2018-03-28 12:46:07 +02:00
parent da1f200362
commit 7fbc9e5874
2 changed files with 14 additions and 11 deletions

View File

@ -2,13 +2,14 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import plac import plac
import requests
import os import os
import subprocess import subprocess
import sys import sys
import ujson
from .link import link from .link import link
from ..util import prints, get_package_path from ..util import prints, get_package_path
from ..compat import url_open, url_error
from .. import about from .. import about
@ -56,13 +57,14 @@ def download(model, direct=False):
def get_json(url, desc): def get_json(url, desc):
r = requests.get(url) try:
if r.status_code != 200: r = url_open(url)
except url_error as e:
msg = ("Couldn't fetch %s. Please find a model for your spaCy " msg = ("Couldn't fetch %s. Please find a model for your spaCy "
"installation (v%s), and download it manually.") "installation (v%s), and download it manually.")
prints(msg % (desc, about.__version__), about.__docs_models__, prints(msg % (desc, about.__version__), about.__docs_models__,
title="Server error (%d)" % r.status_code, exits=1) title="Server error (%d: %s)" % (e.code, e.reason), exits=1)
return r.json() return ujson.load(r)
def get_compatibility(): def get_compatibility():

View File

@ -1,12 +1,12 @@
# coding: utf8 # coding: utf8
from __future__ import unicode_literals, print_function from __future__ import unicode_literals, print_function
import requests
import pkg_resources import pkg_resources
from pathlib import Path from pathlib import Path
import sys import sys
import ujson
from ..compat import path2str, locale_escape from ..compat import path2str, locale_escape, url_open, url_error
from ..util import prints, get_data_path, read_json from ..util import prints, get_data_path, read_json
from .. import about from .. import about
@ -15,11 +15,12 @@ def validate():
"""Validate that the currently installed version of spaCy is compatible """Validate that the currently installed version of spaCy is compatible
with the installed models. Should be run after `pip install -U spacy`. with the installed models. Should be run after `pip install -U spacy`.
""" """
r = requests.get(about.__compatibility__) try:
if r.status_code != 200: r = url_open(about.__compatibility__)
except url_error as e:
prints("Couldn't fetch compatibility table.", prints("Couldn't fetch compatibility table.",
title="Server error (%d)" % r.status_code, exits=1) title="Server error (%d: %s)" % (e.code, e.reason), exits=1)
compat = r.json()['spacy'] compat = ujson.load(r)['spacy']
current_compat = compat.get(about.__version__) current_compat = compat.get(about.__version__)
if not current_compat: if not current_compat:
prints(about.__compatibility__, exits=1, prints(about.__compatibility__, exits=1,