From e34069db9f1c17f3a78d45589e20ac76bcd5afc4 Mon Sep 17 00:00:00 2001 From: ines Date: Sun, 7 May 2017 23:24:51 +0200 Subject: [PATCH] Move is_package and get_model_package_path to util --- spacy/util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spacy/util.py b/spacy/util.py index 0c7136522..8044b4cdc 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals, print_function import ujson +import pip +import importlib import regex as re from pathlib import Path import sys @@ -106,6 +108,29 @@ def read_json(location): return ujson.load(f) +def is_package(origin): + """ + Check if string maps to a package installed via pip. + """ + packages = pip.get_installed_distributions() + for package in packages: + if package.project_name.replace('-', '_') == origin: + return True + return False + + + +def get_model_package_path(package_name): + # Here we're importing the module just to find it. This is worryingly + # indirect, but it's otherwise very difficult to find the package. + # Python's installation and import rules are very complicated. + pkg = importlib.import_module(package_name) + package_path = Path(pkg.__file__).parent.parent + meta = parse_package_meta(package_path, package_name) + model_name = '%s-%s' % (package_name, meta['version']) + return package_path / package_name / model_name + + def parse_package_meta(package_path, package, require=True): """ Check if a meta.json exists in a package and return its contents as a