mirror of https://github.com/explosion/spaCy.git
Move is_package and get_model_package_path to util
This commit is contained in:
parent
957ba676b4
commit
e34069db9f
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue